Adding foreign keys to Mysql

Here is my code

CREATE TABLE `student` (
 `id` INT(3) NOT NULL AUTO_INCREMENT COMMENT 'user ID',
  `name` VARCHAR(5) NOT NULL DEFAULT 'anonymous' COMMENT 'User name', 
  `pwd` VARCHAR(20) NOT NULL DEFAULT '123456' COMMENT 'User password', 
  `birthday` DATETIME DEFAULT NULL COMMENT 'User birthday',
   `address` VARCHAR(15) DEFAULT NULL COMMENT 'User address',
   `email` VARCHAR(15) NOT NULL DEFAULT '@qq.com' COMMENT 'User mailbox',
    `sex` VARCHAR(2) NOT NULL DEFAULT 'male' COMMENT 'Gender', 
    PRIMARY KEY (`id`)
    /*Add foreign key statement*/
   KEY `FK_name`(`name`) 
   CONSTRAINT `FK_name` FOREIGN KEY(`name`)  REFERENCES `t`(`name`)
   /*End of statement to add foreign key*/ 
  ) 
   ENGINE=INNODB DEFAULT CHARSET=utf8; 

My thinking process at that time
The statement for constructing a table will certainly not make mistakes (because the statement is derived from show create table student)
At that time, I thought the problem must be in the statement adding foreign keys
Then analyze the problem of adding foreign key statements
Statement declaring key
KEY FK_name(name) declares a key about name
Add corresponding restrictions to the key
CONSTRAINT FK_name FOREIGN KEY name REFERENCES t(name)
It seems that there is nothing wrong with the statement

Therefore, the problem may not appear in the statement itself, but in the setting of foreign keys
Therefore, it is possible to query CSDN
The following is the information found

Original link: https://blog.csdn.net/wangpeng047/article/details/19624351

  1. The types or sizes of the two fields do not strictly match. For example, if one is int(10), the foreign key must also be set to int(10), not int(11), and cannot be tinyint. In addition, you must also determine whether one of the two fields is signed and the other is unsigned (i.e. unsigned). The two fields must be strictly matched. For more information about signed and unsigned, see: http://www.verysimple.com/blog/?p=57

From the above information, the type and sign of the two fields must be consistent
After inspection, it is found that there is indeed a problem of inconsistent types
This problem is solved by modifying the data type and expense negative attribute of the second field
After solving this problem, we still can't continue to study other possible properties

  1. The field trying to set the foreign key has no index or is not a primary key. If one of the fields is not a primary key, you must first create an index for it.

To build a foreign key is actually a field that references the primary key of another table
I also made mistakes here
After setting the references object as the primary key of the corresponding table, the problem is roughly solved

  1. One or two of these tables are the tables of the MyISAM engine. If you want to use foreign key constraints, the table must be an InnoDB engine (in fact, if both tables are MyISAM engines, this error will not occur at all, but foreign keys will not be generated, and only indexes will be created). You need to check the engine type of the table.

Only InnoDB can use foreign keys
Here I choose the default option, which is InnoDB by default
But to be on the safe side, I chose to modify the engine to InnoDB

  1. Foreign key names cannot be repeated. You should check your database to ensure that the external key name is unique, or you should add a few random characters after the key name to test whether this is the reason.

After reading all the possible errors mentioned above and still having no effect, I thought that the two field names must be the same, so I tried to realize the foreign key

  1. You may have set ON DELETE SET NULL, but the related key field is set to NOTS NULL. You may solve this problem by modifying the attribute value of cascade or setting the field attribute to allow null.

  2. Please make sure that your Charset and collide options are consistent at the table level and field level.

  3. You may have set a default value for foreign keys, such as default=0.

  4. Syntax error in ALTER declaration

This is the field setting of the referenced table

This is the field setting of the reference table

This is the corresponding statement to create a foreign key

CREATE TABLE `student` (
  `id` INT(3) UNSIGNED NOT NULL COMMENT 'Student id',
  `name` VARCHAR(5) NOT NULL COMMENT 'Student's name',
  `gradeid` INT(5) UNSIGNED NOT NULL COMMENT 'Student achievement id',
  PRIMARY KEY (`id`,`gradeid`),
--Create a key  
  KEY `kf_gradeid` (`gradeid`),
--Add restrictions to this key  
  CONSTRAINT `kf_gradeid` FOREIGN KEY (`gradeid`) REFERENCES `grade` (`gradeid`)

) ENGINE=INNODB DEFAULT CHARSET=utf8

Added by mvd7793 on Sun, 26 Dec 2021 03:04:49 +0200