Preface After MySQL version 3.23.44, InnoDB engine type tables support foreign key constraints. Conditions for using foreign keys: 1. Both tables must be InnoDB tables. MyISAM tables do not support foreign keys for the time being (it is said that future versions may support it, but at least not currently); 2. The foreign key column must have an index. MySQL 4.1.2 and later versions will automatically create an index when creating a foreign key, but if you use an earlier version, you need to explicitly create an index. 3. The columns of the two tables in the foreign key relationship must have similar data types, that is, columns that can be converted to each other, such as int and tinyint, but not int and char; Benefits of foreign keys: they can associate two tables, ensure data consistency, and implement some cascading operations; 1. Creation of foreign keysSyntax 1: Subsequent addition method alter table table name add constraint constraint name foreign key (the field constrained in the current table) references main table name (the field name to be constrained); alter table student add constraint fk_class_student foreign key(cls_id) class(cls_id) on update cascade on delete no action; Syntax 2: How to create a table CREATE TABLE student( sid int PRIMARY KEY, cls_id int not null, sname varchar(10) not null, constraint fk_class_student foreign key(cls_id) references class(cls_id) on update cascade on delete no action )ENGINE=InnoDB DEFAULT CHARSET=utf8; An additional point: SHOW CREATE TABLE class You can query the table creation information CREATE TABLE `class` ( `cls_id` int NOT NULL, `cls_name` varchar(15) NOT NULL, PRIMARY KEY (`cls_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 In the following demonstration, we will use the student table and class table we just created. The content is as follows 2. About the Four Constraint MethodsWhen performing update/delete operations on the parent table, the operation type of the child table
Take the foreign key we just set as an example (on update cascade on delete no action) When we try to delete delete from class WHERE cls_id=1 The results are as follows It can be seen that due to the existence of on delete no action in the foreign key constraint, deletion operations on the main table are not allowed. But the subtable can delete from student WHERE cls_id=1 When we update the associated key of the parent table, it can be updated normally due to the existence of on update cascade UPDATE class set cls_id=4 where cls_id=1 And as the main table is updated, the foreign key fields in the child table are also updated 3. How to delete foreign keysalter table subtable name drop foreign key foreign key constraint name alter table student drop foreign key fk_class_student After we delete the foreign key, the operation of the parent table becomes normal. SummarizeThis is the end of this article about foreign key creation, constraints, and deletion in MySQL. For more information about MySQL foreign key creation, constraints, and deletion, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: HTML table markup tutorial (43): VALIGN attribute of the table header
>>: Steps to build a file server using Apache under Linux
Install lua wget http://luajit.org/download/LuaJI...
When we make a gradient background color, we will...
The company project was developed in Java and the...
Introduction to Dockerfile Docker can automatical...
Preface We need to retrieve certain data that mee...
Follow the steps below 1. request.js content: htt...
Table of contents 1. Pull the image 2. Create a l...
Preface MySQL is a relational database with stron...
Upgrade background: In order to solve the vulnera...
This article example shares the specific code of ...
This article example shares the specific code of ...
This article example shares the specific code of ...
Table of contents 1. Definition of stack 2. JS st...
Preface When mysql modified the default database ...
1. Install Apache # yum install -y httpd httpd-de...