Search code snippets, questions, articles...

Add foreign key in existing table Mysql

ALTER TABLE 
ContactDetails 
ADD 
EmployeeId INT UNSIGNED NOT NULL;

ALTER TABLE 
ContactDetails 
ADD 
CONSTRAINT fk_EMP_CONTACT FOREIGN KEY (EmployeeId) REFERENCES Employee(Id);

You can use the above queries to add foreign key constraints to an existing table. In the code snippet, we are assigning 'EmployeeId'  as a foreign key in 'ContactDetails' table.

Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet