Preface When creating a primary and foreign key for a MySQL table, you need to pay attention to the following points:
1. SQL statement to create a data table and set the primary and foreign key relationship create table demo.ChineseCharInfo ( ID int not null auto_increment, Hanzi varchar(10) not null, primary key (ID) ) engine=innodb auto_increment=1 default charset=utf8 collate=utf8_general_ci; create table demo.ChinesePinyinInfo ( ID int not null auto_increment, CharID int null, Pinyin varchar(10) null, Tone tinyint unsigned null, primary key (ID), -- Method 1: Do not specify a foreign key name, the database automatically generates a foreign key (CharID) references ChineseCharInfo(ID) on delete cascade on update cascade -- Method 2: Specify the foreign key name (FK_Name) -- constraint FK_Name foreign key (CharID) references ChineseCharInfo(ID) on delete cascade on update cascade ) engine=innodb auto_increment=1 default charset=utf8 collate=utf8_general_ci; 2. When the data table already exists, use the following method to establish the primary and foreign key relationship -- Add a foreign key to the field (CharID) in the table (demo.ChinesePinyinInfo) and specify the foreign key name as (FK_Name) alter table demo.ChinesePinyinInfo add constraint FK_Name foreign key (CharID) references ChineseCharInfo(ID); -- Add a foreign key to the field (CharID) in the table (demo.ChinesePinyinInfo). Do not specify the foreign key name. The database will automatically generate the foreign key name. alter table demo.ChinesePinyinInfo add foreign key (CharID) references ChineseCharInfo(ID); 3. Delete primary and foreign key constraints -- Delete the auto-increment by modifying the column properties. The first (ID) is the original column name, and the second (ID) is the new column name. alter table demo.ChinesePinyinInfo change ID ID int not null; -- Delete the primary key constraint in the table (demo.ChinesePinyinInfo). If the primary key column is an auto-increment column, you need to delete the auto-increment of the column first. alter table demo.ChinesePinyinInfo drop primary key; -- Delete the foreign key named (FK_Name) in the table (demo.ChinesePinyinInfo) alter table demo.ChinesePinyinInfo drop foreign key FK_Name; 4. Constraints of primary and foreign key relationships If the child table attempts to create a foreign key value that does not exist in the primary table, the database will reject any insert or update operation. If the primary table attempts to update or delete any existing or matching foreign key values in the child table, the final action depends on the on delete and on update options in the foreign key constraint definition. Both on delete and on update have the following four actions.
The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: The implementation of Youda's new petite-vue
>>: Zabbix configures DingTalk's alarm function with pictures
Today I will talk to you about clearing floats. B...
The one above shows the system time, and the one ...
introduction When I was learning more about datab...
Table of contents Written in front What exactly i...
Before we use JSX to build a component system, le...
How to create a Linux virtual machine in VMware a...
When using MySQL 5.7, you will find that garbled ...
Table of contents App Update Process Rough flow c...
Install vsftpd $ sudo apt-get install vsftpd -y S...
The effect is very simple, just copy the following...
Phenomenon The system could compile the Linux sys...
I. Introduction 1: SSL Certificate My domain name...
What is JSX JSX is a syntax extension of Javascri...
Description: Limit the number of lines of text di...
1. Basic steps 1: Install yarn add vue-i18n Creat...