简体   繁体   中英

Cannot set foreign key in the table

I have 2 tables.

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);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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