Set on the primary foreign key difference between SQL and cascading update cascade on delete cascade delete - Delete the main table at the same time removed from the table - and delete tables with primary foreign key relationships

And the difference on update cascade on delete cascade of

This is a foreign key definition database option, when used to set the primary key table is the reference row data changes, the foreign key in the table in response to the conversion rule field.
update is to be a value to update the reference field in the primary key table, delete means to delete a record in the primary key table:
the words on the back update and on delete can now has four
NO Action, SET null, SET default, Cascade
NO Action represents do nothing,
sET null set indicates the corresponding field in the table is a foreign key null
sET default set to default values represented
cascade represents a cascade operation, that is, if the reference field is updated, the foreign key in the table is also updated primary key table, record in the primary key table is deleted, the foreign key table diverted accordingly deleted.
When cascading updates, based on the data before matching, after the value of the master table updates associated foreign key column, the system automatically updates the value from the corresponding field in the table is a foreign key, but not the other are not provided based foreign key field is not a foreign key primary field is not affected.

 

SQL cascade delete - delete the main table at the same time removed from the table - and delete tables with primary foreign key relationships

 

Table A Create
(
ID VARCHAR (20 is) Primary Key,
password VARCHAR (20 is) Not null
)

Create Table B
(
ID int Identity (1,1) Primary Key,
name VARCHAR (50) Not null,
the userId VARCHAR (20 is),
Foreign Key (userId) References a (ID) cascade ON delete
)
table B creates the main code ID corresponding to the outer code userId a, declarations cascading deletes
test data:
INSERT a values ( '. 11', 'AAA')
INSERT values a ( '23 is', 'AAA')
INSERT B values ( 'DA', '. 11')
INSERT B values ( 'DAS', '. 11')
INSERT B values ( 'WW', '23 is')
remove the A table id data '11', is found userId "11" is also deleted automatically within the database table B, which is cascade delete
delete a where id = '11 '

 

Table A Create
(
ID VARCHAR (20 is) Primary Key,
password VARCHAR (20 is) Not null
)

Create Table B
(
ID int Identity (1,1) Primary Key,
name VARCHAR (50) Not null,
the userId VARCHAR (20 is),
Foreign Key (userId) References a (ID) cascade ON delete
)
table B creates the main code ID corresponding to the outer code userId a, declarations cascading deletes
test data:
INSERT a values ( '. 11', 'AAA')
INSERT values a ( '23 is', 'AAA')
INSERT B values ( 'DA', '. 11')
INSERT B values ( 'DAS', '. 11')
INSERT B values ( 'WW', '23 is')
remove the A table id data '11', is found userId "11" is also deleted automatically within the database table B, which is cascade delete
delete a where id = '11 '

Table A Create
(
ID VARCHAR (20 is) Primary Key,
password VARCHAR (20 is) Not null
)

Create Table B
(
ID int Identity (1,1) Primary Key,
name VARCHAR (50) Not null,
the userId VARCHAR (20 is),
Foreign Key (userId) References a (ID) cascade ON delete
)
table B creates the main code ID corresponding to the outer code userId a, declarations cascading deletes
test data:
INSERT a values ( '. 11', 'AAA')
INSERT values a ( '23 is', 'AAA')
INSERT B values ( 'DA', '. 11')
INSERT B values ( 'DAS', '. 11')
INSERT B values ( 'WW', '23 is')
remove the A table id data '11', is found userId "11" is also deleted automatically within the database table B, which is cascade delete
delete a where id = '11 '

Guess you like

Origin www.cnblogs.com/itjeff/p/11578721.html