我正在尝试在两个表之间创建关系。我的表名是employees
和salaries
。我尝试了以下代码:
ALTER TABLE `salaries`
ADD FOREIGN KEY (`eid`)
REFERENCES `employees`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
但它显示错误:
在eid上创建外键时出错(检查数据类型)
员工表结构:
+---------------------------------------+
| id | int(11) | primary |
|--------------+--------------+---------|
| name | varchar(255) | unique |
|--------------+--------------+---------|
| basic | int(11) | |
|--------------+--------------+---------|
| ot_rate | int(11) | |
|--------------+--------------+---------|
| joining_date | date | |
+---------------------------------------+
工资表结构:
+---------------------------------------+
| id | int(11) | primary |
|--------------+--------------+---------|
| eid | int(11) | index | <-- I want to create foreign key on this column with employees->id.
|--------------+--------------+---------|
| month | varchar(255) | |
|--------------+--------------+---------|
| date | date | |
|--------------+--------------+---------|
| working_day | int(11) | |
+--------------+--------------+---------+
| ot_hour | int(11) | |
|--------------+--------------+---------|
| ot_amount | int(11) | |
|--------------+--------------+---------|
| basic_amount | int(11) | |
|--------------+--------------+---------|
| tatal_salary | inf(11) | |
|--------------+--------------+---------|
| comment | varchar(255) | |
+---------------------------------------+
我尝试将eid
的数据类型更改为varchar
,tinyint
,text
等。所有内容都有相同的错误消息。
更新
使用后:
ALTER TABLE `salaries`
ADD CONSTRAINT `salary` FOREIGN KEY (`eid`)
REFERENCES `employees`(`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
我收到此错误消息:
#1005 - Can't create table `zamzam`.`#sql-14b4_1a5` (errno: 150 "Foreign key constraint is incorrectly formed")
答案 0 :(得分:0)
您是否使用表中的关系视图选项来创建外键约束。 这个链接会告诉你的方式 - http://www.binarytides.com/create-foreign-key-phpmyadmin/ 谢谢