The most important thing, the primary key should not be set to unsigned type.

In addition, the foreign key can be set correctly under the following conditions:

No.1, the most important thing, the primary key should not be set to unsigned type.
No.2, the engines for both tables must be Innodb
No.3, the foreign key and the data type of the primary key is the same or similar
No.4, the foreign key must be set to index
No.5, the character set also must be the same and sorted by the rules

Foreign key definition:

[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
    REFERENCES tbl_name (index_col_name, ...)
    [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]
    [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT}]

Default value is RESTRICT.

The primary key table is the primary table, the foreign key table is the sub table.
The triggering of the foreign key constraint is updated by the primary table change, causing the child table to trigger the corresponding constraint event.



Explanation:

First.RESTRICT is a modification of the restrictions, when the primary key in the primary table in the sub-table is used, can not be deleted, and can not be modified. But the child table can be deleted, and the main table is not affected, when modified, the foreign key must be in the main table.
Second. Cascade is a cascading relationship, when the main table is modified or deleted, the corresponding sub-table is modified or deleted; but the child table can be deleted, and the main table is not affected, when modified, the foreign key must be in the main table.
Constraint events are triggered, and the object is the constraint on the primary table on the child table.