Creation, constraints and deletion of foreign keys in MySQL

Creation, constraints and deletion of foreign keys in MySQL

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 keys

Syntax 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

insert image description here

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

insert image description here

insert image description here

2. About the Four Constraint Methods

When performing update/delete operations on the parent table, the operation type of the child table

  1. CASCADE The subtable will delete all data that contains references to the deleted key value.
  2. SET NULL When the parent table is deleted or updated, the child table will set the column where the foreign key field of the associated record is located to null
  3. RESTRICT Rejects deletion requests for fields with relationships (this is the default and safest setting)
  4. NO ACTION is similar to RESTRICT

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

insert image description here

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

insert image description here

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

insert image description here

And as the main table is updated, the foreign key fields in the child table are also updated

insert image description here

3. How to delete foreign keys

alter 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.

insert image description here

Summarize

This 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:
  • MySQL not null constraint case explanation
  • MySQL foreign key constraint (FOREIGN KEY) case explanation
  • Summary of MySQL foreign key constraints and table relationships
  • MySQL integrity constraints definition and example tutorial
  • MySQL 8.0 New Features - Introduction to Check Constraints
  • Example statements for indexes and constraints in MySQL
  • Example explanation of MySQL foreign key constraints
  • Detailed explanation of MySQL foreign key constraints
  • Detailed explanation of mysql integrity constraints example
  • MySQL Constraints Super Detailed Explanation

<<:  HTML table markup tutorial (43): VALIGN attribute of the table header

>>:  Steps to build a file server using Apache under Linux

Recommend

How to add Lua module to Nginx

Install lua wget http://luajit.org/download/LuaJI...

CSS3 gradient background compatibility issues

When we make a gradient background color, we will...

Tomcat common exceptions and solution code examples

The company project was developed in Java and the...

Example of using Dockerfile to build an nginx image

Introduction to Dockerfile Docker can automatical...

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

Detailed explanation of the configuration method of Vue request interceptor

Follow the steps below 1. request.js content: htt...

How to install Jenkins using Docker

Table of contents 1. Pull the image 2. Create a l...

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

Detailed steps to upgrade mysql8.0.11 to mysql8.0.17 under win2008

Upgrade background: In order to solve the vulnera...

Native JS to implement the aircraft war game

This article example shares the specific code of ...

Using JS to implement a small game of aircraft war

This article example shares the specific code of ...

vue+springboot realizes login function

This article example shares the specific code of ...

Detailed explanation of JavaScript stack and copy

Table of contents 1. Definition of stack 2. JS st...

CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)

1. Install Apache # yum install -y httpd httpd-de...