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
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!