Detailed explanation of creating a data table in MySQL and establishing primary and foreign key relationships

Detailed explanation of creating a data table in MySQL and establishing primary and foreign key relationships

Preface

When creating a primary and foreign key for a MySQL table, you need to pay attention to the following points:

  • The storage engine of the two tables that need to establish a primary and foreign key relationship must be InnoDB.
  • The foreign key column and the referencing column must have similar data types, that is, data types that can be implicitly converted.
  • Foreign key columns and reference columns must have indexes created. If an index does not exist for a foreign key column, MySQL will automatically create an index.

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.

  • cascade: If the main table deletes or updates the corresponding data row, the sub-table will also delete or update the rows that match the main table, i.e. cascade deletion and update.
  • set null: If the main table deletes or updates the corresponding data, the sub-table will also set the foreign key columns of the rows matching the main table to null. Has no effect when the foreign key column is set to not null.
  • no action: The database refuses to delete or update the primary table.
  • restrict: The database refuses to delete or update the main table. If the on delete or on update action is not specified, the default action of on delete or on update is restrict.

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:
  • MySQL and Oracle data type correspondence (tabular form)
  • How to create an index on a join table in MySQL
  • Summary of MySQL foreign key constraints and table relationships
  • Python saves dict dictionary type data to Mysql and automatically creates tables and columns
  • MySQL and PHP basics and application topics: creating database tables
  • How to quickly create a test data table with 8 million entries in MySQL
  • MySQL creates three relationship tables in practice

<<:  The implementation of Youda's new petite-vue

>>:  Zabbix configures DingTalk's alarm function with pictures

Recommend

Implementation of Nginx configuration of local image server

Table of contents 1. Introduction to Nginx 2. Ima...

How to monitor Linux server status

We deal with Linux servers every day, especially ...

Vue+swiper realizes timeline effect

This article shares the specific code of vue+swip...

Complete steps to reset the root user password in mysql8

Preface Recently, many new colleagues have asked ...

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...

Cross-origin image resource permissions (CORS enabled image)

The HTML specification document introduces the cr...

Complete steps for uninstalling MySQL database

The process of completely uninstalling the MySQL ...

Linux remote control windows system program (three methods)

Sometimes we need to remotely run programs on the...

How to use javascript to do simple algorithms

Table of contents 1 Question 2 Methods 3 Experime...

Getting Started with CSS3 Animation in 10 Minutes

Introduction Animation allows you to easily imple...

On Visual Design and Interaction Design

<br />In the entire product design process, ...

HTML weight loss Streamline HTML tags to create web pages

HTML 4 HTML (not XHTML), MIME type is text/html, ...

How to use Xtrabackup to back up and restore MySQL

Table of contents 1. Backup 1.1 Fully prepared 1....

Vue opens a new window and implements a graphic example of parameter transfer

The function I want to achieve is to open a new w...

Share 20 excellent web form design cases

Sophie Hardach Clyde Quay Wharf 37 East Soapbox Rx...