时间:2021-07-01 10:21:17 帮助过:15人阅读
Let’s examine the syntax in greater detail:
CONSTRAINT
clause allows you to define constraint name for the foreign key constraint. If you omit it, MySQL will generate a name automatically.FOREIGN KEY
clause specifies the columns in the child table that refers to primary key columns in the parent table. You can put a foreign key name after FOREIGN KEY
clause or leave it to let MySQL create a name for you. Notice that MySQL automatically creates an index with the foreign_key_name
name.REFERENCES
clause specifies the parent table and its columns to which the columns in the child table refer. The number of columns in the child table and parent table specified in the FOREIGN KEY
and REFERENCES
must be the same.ON DELETE
clause allows you to define what happens to the records in the child table when the records in the parent table are deleted. If you omit the ON DELETE
clause and delete a record in the parent table that has records in the child table refer to, MySQL will reject the deletion. In addition, MySQL also provides you with actions so that you can have other options such as ON DELETE CASCADE that ask MySQL to delete records in the child table that refers to a record in the parent table when the record in the parent table is deleted. If you don’t want the related records in the child table to be deleted, you use the ON DELETE SET NULL
action instead. MySQL will set the foreign key column values in the child table to NULL
when the record in the parent table is deleted, with a condition that the foreign key column in the child table must accept NULL
values. Notice that if you use ON DELETE NO ACTION
or ON DELETE RESTRICT
action, MySQL will reject the deletion.ON UPDATE
clause enables you to specify what happens to the rows in the child table when rows in the parent table are updated. You can omit the ON UPDATE
clause to let MySQL reject any updates to the rows in the child table when the rows in the parent table are updated. The ON UPDATE CASCADE
action allows you to perform a cross-table update, and the ON UPDATE SET NULL
action resets the values in the rows in the child table to NULL
values when the rows in the parent table are updated. The ON UPDATE NO ACTION
or UPDATE RESTRICT
actions reject any updates.The following example creates a dbdemo
database and two tables: categories
and products.
Each category has one or more products and each product belongs to only one category. The cat_id
field in the products
table is defined as a foreign key with UPDATE ON CASCADE
and DELETE ON RESTRICT
actions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | CREATE DATABASE IF NOT EXISTS dbdemo; USE dbdemo; CREATE TABLE categories( cat_id int not null auto_increment primary key, cat_name varchar(255) not null, cat_description text ) ENGINE=InnoDB; CREATE TABLE products( prd_id int not null auto_increment primary key, prd_name varchar(355) not null, prd_price decimal, cat_id int not null, FOREIGN KEY fk_cat(cat_id) REFERENCES categories(cat_id) ON UPDATE CASCADE ON DELETE RESTRICT )ENGINE=InnoDB; |
To add a foreign key to an existing table, you use the ALTER TABLE statement with the foreign key definition syntax above:
1 2 3 4 5 6 | ALTER table_name ADD CONSTRAINT constraint_name FOREIGN KEY foreign_key_name(columns) REFERENCES parent_table(columns) ON DELETE action ON UPDATE action; |
Now, let’s add a new table named vendors
and change the products
table to include the vendor id field:
1 2 3 4 5 6 7 8 9 | USE dbdemo; CREATE TABLE vendors( vdr_id int not null auto_increment primary key, vdr_name varchar(255) )ENGINE=InnoDB; ALTER TABLE products ADD COLUMN vdr_id int not null AFTER cat_id; |
To add a foreign key to the products
table, you use the following statement:
1 2 3 4 5 | ALTER TABLE products ADD FOREIGN KEY fk_vendor(vdr_id) REFERENCES vendors(vdr_id) ON DELETE NO ACTION ON UPDATE CASCADE; |
Now, the products
table has two foreign keys, one refers to the categories
table and another refers to the vendors
table.
You also use the ALTER TABLE
statement to drop foreign key as the following statement:
1 2 | ALTER TABLE table_name DROP FOREIGN KEY constraint_name; |
In the statement above:
DROP FOREIGN KEY
clause.Notice that constraint_name
is the name of the constraint specified when you created or added the foreign key to the table. If you omit it, MySQL generates a constraint name for you.
To obtain the generated constraint name of a table, you use the SHOW CREATE TABLE
statement as follows:
1 | SHOW CREATE TABLE table_name; |
For example, to see the foreign keys of the products
table, you use the following statement:
1 | SHOW CREATE TABLE products; |
The following is the output of the statement:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | CREATE TABLE products ( prd_id int(11) NOT NULL AUTO_INCREMENT, prd_name varchar(355) NOT NULL, prd_price decimal(10,0) DEFAULT NULL, cat_id int(11) NOT NULL, vdr_id int(11) NOT NULL, PRIMARY KEY (prd_id), KEY fk_cat (cat_id), KEY fk_vendor(vdr_id), CONSTRAINT products_ibfk_2 FOREIGN KEY (vdr_id) REFERENCES vendors (vdr_id) ON DELETE NO ACTION ON UPDATE CASCADE, CONSTRAINT products_ibfk_1 FOREIGN KEY (cat_id) REFERENCES categories (cat_id) ON UPDATE CASCADE ) ENGINE=InnoDB; |
The products
table has two foreign key constraints: products_ibfk_1
and products_ibfk_2
You can drop the foreign keys of the products
table by using the following statement:
1 2 3 4 5 | ALTER TABLE products DROP FOREIGN KEY products_ibfk_1; ALTER TABLE products DROP FOREIGN KEY products_ibfk_2; |
Sometimes, it is very useful to disable foreign key checks e.g., when you import data from a CSV file into a table. If you don’t disable foreign key checks, you have to load data into a proper order i.e., you have to load data into parent tables first and then child tables, which can be tedious. However, if you disable the foreign key checks, you can load data in any orders.
Another example is that, unless you disable the foreign key checks, you cannot drop a table that is referenced by a foreign key constraint. When you drop a table, any constraints that you defined for the table are also removed.
To disable foreign key checks, you use the following statement:
1 | SET foreign_key_checks = 0; |
And of course, you can enable it using the statement below:
1 | SET foreign_key_checks = 1; |
In this tutorial, we have covered a lot about MySQL foreign key. We also introduced you to some very handy statements that allow you to manage foreign keys effectively in MySQL.
http://www.mysqltutorial.org/mysql-foreign-key/
MySQL Foreign Key
标签:cond class file default creat city about ram form