xxxxxxxxxx
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=INNODB;
xxxxxxxxxx
CREATE TBALE parent_table_p (
id INT PRIMARY KEY
)
CREATE_TABLE child_table_c(
id INT PRIMARY KEY,
parent_id INT,
FOREIGN KEY (parent_id) REFERENCES parent_table_p(id) ON DELETE CASCADE
)
xxxxxxxxxx
/*
It's true that if your primary key is just an identity value auto incremented,
you would have no real use for ON UPDATE CASCADE.
However, let's say that your primary key is a 10 digit UPC bar code and because of
expansion, you need to change it to a 13-digit UPC bar code. In that case,
ON UPDATE CASCADE would allow you to change the primary key value and any tables
that have foreign key references to the value will be changed accordingly.
xxxxxxxxxx
ALTER TABLE TABLENAME
drop CONSTRAINT FK_CONSTRAINTNAME;
ALTER TABLE TABLENAME
ADD CONSTRAINT FK_CONSTRAINTNAME
FOREIGN KEY (FId)
REFERENCES OTHERTABLE
(Id)
ON DELETE CASCADE ON UPDATE NO ACTION;
xxxxxxxxxx
The ON DELETE CASCADE clause specifies that if a row in the
table A is deleted, any rows in the table B that reference the deleted
row will also be deleted.
xxxxxxxxxx
CREATE TABLE child_table
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
CONSTRAINT fk_name
FOREIGN KEY (child_col1, child_col2, child_col_n)
REFERENCES parent_table (parent_col1, parent_col2, parent_col_n)
ON DELETE CASCADE
[ ON UPDATE { NO ACTION | CASCADE | SET NULL | SET DEFAULT } ]
);
xxxxxxxxxx
CREATE TABLE child (
id INT,
parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id)
REFERENCES parent(id)
ON UPDATE CASCADE
) ENGINE=INNODB;
xxxxxxxxxx
CREATE TABLE Enroll (
sno INT,
cno INT,
jdate date,
PRIMARY KEY(sno,cno),
FOREIGN KEY(sno)
REFERENCES Student(sno)
ON DELETE CASCADE
FOREIGN KEY(cno)
REFERENCES Course(cno)
ON DELETE CASCADE
);