create trigger to alter multiple tables - mysql

i have n tables,
and each table share a common column 'COLX'. And each table's COLX column can have independent values in that column, but when someone alters value in Main table T's COLX, each table's corresponding COLX value must be updated with new value in T's COLX.
I can only write trigger for one table only, how to write this for n tables?

COLXCascade your triggers through the tables,
table1 will trigger updates to table2,
table2 can then run triggers to affect table3
CREATE TABLE table1 (COLX INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE table2 (id INT, COLX INT,
INDEX par_ind (parent_id),
FOREIGN KEY (COLX) REFERENCES table1(COLX)
ON UPDATE CASCADE
)

Related

MYSQL - on update one table automatically delete rows in second table

I have two tables in relation 1:n
create table A (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
data VARCHAR(32),
data2 INTEGER,
INDEX (id)
);
CREATE TABLE B (
A_id BINGINT,
some_data DOUBLE,
INDEX (A_id),
FOREIGN KEY (A_id) REFERENCES A(id) ON DELETE CASCADE
);
If i update a row in table A then i need to delete all records from table B with same id from table A. What is the best way to solve it?
i dont know if i should use a trigger or an event.
You need a trigger on table A:
DELIMITER //
CREATE TRIGGER t BEFORE UPDATE ON A
FOR EACH ROW
BEGIN
DELETE FROM B WHERE a_id = NEW.id;
END
//
DELIMITER ;

How can I change the assignment of a foreign key in SQL to point to a different table?

I know the impact of my question, but my case may be unique.
I am trying to create a delta script to apply to other test databases that change a foreign key's child table. For instance:
Table A has some_id with a foreign key pointing to a column in table B
I want to change some_id to point to a column in table C
I know this could harm and potentially ruin a system if there was already data there, but in my case there is no data in the parent table. I will simply be switching the foreign key to point to a different column of a different table.
Preferably, I would like a script to do this.
Since you said, you already know the implications of doing this...
You just need to drop the Foreign Key constraint on your old table.
Then create a new Foreign Key Constraint joining with your new table
Here is an example:
-- create test tables
create table A (column1 bigint primary key, column2 varchar(255))
create table B (column1 bigint primary key, column2 varchar(255), column3 bigint)
create table C (column1 bigint primary key, column2 varchar(255), column3 bigint)
alter table B add constraint fk_B_A foreign key (column3) references table1(column1)
-- change the constraints like this
alter table B drop constraint fk_B_A
alter table C add constraint fk_C_A foreign key (column3) references table1(column1)
-- drop test table
drop table A
drop table B
drop table C

How to make a relation between two tables without foreign key

I'm a total newbie and I'm trying to do a mysql database, using xampp for linux 1.8.1.
I want to make a relation between two tables A and B. For what I know foreign keys create a bijection, or a one-to-one relation. I must not have such a strict relation, so I only created a column inside table A that stores the id of table B.
Is this correct? There's not a way to enforce it? I mean, this way you could delete a row of table B that is referenced in table A. you could store a value inside A that doesn't correspond to an id of any row of B. How to prevent this?
The main problem for me is to prevent deletion of a row of table B if the row id is referenced by a row of table A.
create table table_b (
b_id integer primary key
);
create table table_a (
b_id integer primary key references table_b (b_id)
);
insert into table_b values (1);
insert into table_a values (1);
The following statement will fail.
delete from table_b where b_id = 1;
If you'd built that with PostgreSQL, the error message would say
ERROR: update or delete on table "table_b" violates foreign key constraint "table_a_b_id_fkey" on table "table_a" Detail: Key (b_id)=(1) is still referenced from table "table_a".
That structure gives you a "1 to 0 or 1" relationship between the two tables. For "1 to 0 or many", add one or more columns to table_a's primary key.
create table table_b (
b_id integer primary key
);
create table table_a (
b_id integer references table_b (b_id),
checkout_date date not null default current_date,
primary key (b_id, checkout_date)
);
That structure will let table_a store multiple rows for one value of b_id, but each of those rows must have a different checkout_date.
I think you allways need an primarykey for this, or you write a trigger who checks the consistence of ID from B when a change occurs.
I dont know that it would be possible without a trigger or a constraint ...

how do you delete a row, if this has foreign keys? sql

for example if you have this rows
create table table1
(
column1 int primary key,
column2 varchar(50),
column3 varchar(50),
column4 varchar(50),
)
create table tabla2
(
col1 int primary key,
col2 int,
col3 varchar(50),
foreign key(col2) references table1(column1)
)
and for example i have a row
insert into table1(column1,column2,column3,column4) values (1,'a','b','c');
insert into table2(col1,col2,col3) values (1,1,'xxx');
and i want to delete all these rows (just them)
delete from table1 where colum1=1;
doesn't work,
i know i can first delete the another and after it, but i have a database with a lot of tables
and they have a foreign key since another table, and i want to delete the row, for all rows are relationed with this, delete too
foreign key(col2) references table1(column1) on delete cascade
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
Add ON DELETE CASCADE to your foreign key constraint.

Mysql database - primary key problem

I have 2 tables: let say table 1 (userid, username, password) and table 2 (userid, a, b, c).
table 1 has a userid field (primary key), table 2 also has userid (references table 1).
If I do an insert statement, to add a row to table 2, how does table 2 generate the userid field?
Edit:
I want to insert things into table 2 where the userid in table 2 MUST match userid in table 1 - what constraints do I need to set up
The database doesn't understand the relationship between table1 and table2 unless you explicitly specify it. You can use SQL foreign keys if you are using a database engine the supports them (InnoDB).
CREATE TABLE table1 (
userid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL);
CREATE TABLE table2 (
rowid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INTEGER NOT NULL
REFERENCES table1(userid)
ON DELETE CASCADE ON UPDATE CASCADE
-- other columns here
);
The REFERENCES option will tell the database engine that table2.userid requires that a row exist in table1 such that table1.userid = table2.userid. The ON DELETE and ON UPDATE clauses will cause the deletion of a row in table1 to automatically delete/update rows in table2.
You cannot create a row in table2 until you have created a row for the user in table1. The row in table1 will auto-generate the userid. When you insert into table2, you will explicitly include the user id from table1 to establish the linkage between the rows.
If the userid field on table2 is not autoincrement then you have to provide the value or the insert will fail.
If you have defined userID in table2 as foriegn key constraint, then you will need to have the corresponding userID in the table1 as reference when inserting rows to table2, else if you have not defined any such constraints, then it will not matter.
Keep in mind, if the constraint is defined, then table2 will not generate ID by itself. It would require the ID to be present in the table1 column which is referenced.
If username and password is not null then don't insert in tabel 1 and give error