1. Question raising
Create two tables:
Product: sealer: supplier list
The corresponding SQL is as follows:
product:
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL COMMENT 'product name',
`price` float(10,3) NOT NULL,
`description` varchar(20) DEFAULT NULL,
`count` int(11) NOT NULL DEFAULT '0',
`sid` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_index` (`id`) USING HASH,
UNIQUE KEY `sid_index` (`sid`) USING HASH
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `sealer`;
CREATE TABLE `sealer` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`city` varchar(255) DEFAULT NULL,
`created_time` datetime DEFAULT NULL,
`updated_time` datetime DEFAULT NULL,
`level` int(11) NOT NULL DEFAULT '0',
`description` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_index_1` (`id`) USING HASH
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
Next, we need to associate product.SID to sealer. Id for the parent-child table primary foreign key association.
2. Encountering a mistake
The SQL and error message used to create the foreign key is as follows :
alter table `product' add CONSTRAINT `sid_ref` FOREIGN KEY (`sid`) REFERENCES `sealer` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
The error message encountered is as follows:
Unable to insert foreign key constraint correctly.
3. Problem analysis
The primary foreign key is more about the primary key of a table being associated with a column of a child table, which requires the same data type and attributes. Will this be a problem?
Requirements: Same data types and constraints
Unsigned. The character length of the number is not consistent.
4. Solutions
Modify the data type in the product.sid, add unsigned and the length of the field, and set it to the same.
5. To summarize
The problem of 1215 is due to the inconsistency of data types between the primary and foreign keys. Similar problems in the future can be handled accordingly.
Read More:
- SQL Server Deletes a table foreign key constraint Error [Solved]
- MYSQL Index Key Length 1071 – Specified key was too long; max key length is 3072 bytes
- [Solved] EOS7.6 Error: Init DB failed [Specified key was too long; max key length is 767 bytes…
- [305]MYSQL 1062 error: duplicate entry ‘…’ for key ‘primary
- Error 1406 (22001) in MySQL: data too long for column (Fixed)
- How to Fix MySQL error 1005: can’t create table (errno: 150)
- How to Solve Error: Rsa Public Key not Find
- Hive install initialization error: Error: Duplicate key name ‘PCS_STATS_IDX‘ (state=42000,code=1061)
- [Solved] public key is not available client side (option serverRsaPublicKeyFile not set)
- MySQL Build table error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL ser
- Mybatis Add Datas error: ERROR: Field * doesn‘t have a default value
- MYSQL: CURRENT_TIMESTAMP & ON UPDATE CURRENT_TIMESTAMP
- [MySQL] mysql 5.5 and 5.6 timestamp default default value CURRENT_TIMESTAMP problem
- [Solved]ERROR 1067 (42000): Invalid default value for ‘end_time‘ Mysql
- Mysql Flashback Warning: C:\Program Files\MySQL\MySQL Server 8.0\bin\mysql.exe
- MySQL Error: [ERROR] [FATAL] InnoDB: Table flags are 0 in the data dictionary but the flags in file
- MySQL: Got error 139 from storage engine [How to Solve]
- [Solved] MYSQL Command Execute Error: Can ‘t connect to local MySQL server through socket ‘/tmp/mysql.sock ‘(2) “
- [Solved] ERROR 2002 (HY000): Can’t connect to local MySQL server through socket’/var/lib/mysql/mysql.sock’ (2)
- MYSQL Enter password:ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:3306‘