[英]Foreign Key not adding in while doing alter table?
Here is my users.sql file : 这是我的users.sql文件:
CREATE TABLE users (
'id' bigint(20) NOT NULL auto_increment,
'md5_id' varchar(200) collate latin1_general_ci NOT NULL default '',
'user_name' varchar(200) collate latin1_general_ci NOT NULL default '',
'user_email' varchar(220) collate latin1_general_ci NOT NULL default '',
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
AUTO_INCREMENT=55 ;
Here id is the primary key. id是主键。
Now this is the second table notifications.sql: 现在,这是第二个表notifications.sql:
CREATE TABLE 'notifications' (
'notificationid' int(11) NOT NULL,
'creation_date_time' varchar(30) NOT NULL,
'view_date_time' varchar(30) NOT NULL,
'user_id bigint(big) NOT NULL,
'notification_text' varchar(255) NOT NULL,
'is_viewed' varchar(3) NOT NULL
) )
Now when i try to add id in notifications table as foreign key it gives 1215 error. 现在,当我尝试在通知表中添加id作为外键时,会出现1215错误。 I don't know where i am going wrong. 我不知道我要去哪里错了。
This is my alter table code: 这是我的更改表代码:
ALTER TABLE 'notifications'
ADD FOREIGN KEY (id) REFERENCES users(id)
ON DELETE CASCADE ON UPDATE CASCADE
You have to add the engine,charste and collate information to the second table too: 您还必须添加引擎,charste并整理信息到第二个表中:
CREATE TABLE 'notifications' (
'notificationid' int(11) NOT NULL,
'creation_date_time' varchar(30) NOT NULL,
'view_date_time' varchar(30) NOT NULL,
'user_id bigint(big) NOT NULL,
'notification_text' varchar(255) NOT NULL,
'is_viewed' varchar(3) NOT NULL
)
ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci
Also your table notifications
ha no column id
. 另外,您的表notifications
没有列id
。 You have to add this first and Name it user_id: 您必须首先添加此名称并将其命名为user_id:
ALTER TABLE 'notifications'
ADD Column user_id int(11);
ALTER TABLE 'notifications'
ADD FOREIGN KEY (user_id) REFERENCES users(id)
ON DELETE CASCADE ON UPDATE CASCADE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.