Home > Database > Mysql Tutorial > body text

What are the requirements for mysql foreign key constraints?

王林
Release: 2023-05-28 22:52:51
forward
1844 people have browsed it

1. To create a foreign key on the table, the main table should exist first.

2. A table can establish multiple foreign key constraints.

3. The foreign key column of the slave table must point to the primary key column of the master table.

4. The foreign key column of the slave table can have different names from the column referenced by the master table, but the data type must be the same.

Example

mysql> show create table students\G
*************************** 1. row ***************************
       Table: students
Create Table: CREATE TABLE `students` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `name` varchar(6) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `uid` (`uid`),
  CONSTRAINT `students_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `class` (`xuehao`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Copy after login

The above is the detailed content of What are the requirements for mysql foreign key constraints?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template