简体   繁体   English

无法在表格中设置外键

[英]Cannot set foreign key in the table

I have 2 tables. 我有2张桌子。

login (login_id primary key 
Email varchar(254) not null
Password varchar(20) not null)

and

registration(
id PK
Email
password
name
... etc

)

I m trying to set email of login as foreign key and registration(email) as references. 我试图将登录电子邮件设置为外键,并将注册(电子邮件)设置为参考。 but I m not able to do it ALTER TABLE login ADD FOREIGN KEY(email) REFERENCES registraion(email); 但是我不能这样做ALTER TABLE login ADD FOREIGN KEY(email)参考registraion(email);

I m using it for a project so that i cud match the login and registraion email while logging in as user. 我正在将其用于项目,以便在以用户身份登录时可以匹配登录名和注册电子邮件。 can someone plz help 有人可以帮忙吗

The column definitions in the 2 tables have to match for an FK to be created. 2个表中的列定义必须匹配才能创建FK。

registration( id PK, Email VARCHAR(254) NOT NULL, password name

The datatype and length of the columns in both table must match. 两个表中列的数据类型和长度必须匹配。

Foreign Key can be created by referencing a Primary Key on another table or a Unique Key with certain restriction such as column type and size. 可以通过引用另一个表上的主键或具有某些限制(例如列类型和大小)的唯一键来创建外键。 The type and size of both the columns must match. 两列的类型和大小必须匹配。

For a more detailed discussion on the facts you may reference this SO post mysql-non-primary-foreign-key and this MySql reference manual create-table-foreign-keys 有关事实的更详细讨论,您可以参考mysql-non-primary-foreign-key和MySql参考手册create-table-foreign-keys

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM