Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
How to create the foreign key for column in SQL
2404 Views
Syntax
alter table tableName add constraint constraintVariable foreign key(columnName_for_foeign_key)
references primaryTableName(columnName_primaryKey);
Example
alter table empolyee add constraint fk foreign key(dept_no)
references Department(Dept_no);
Hints 
If you refer the column as foreign key it means column does not allow the values which is not present in related primary key column. When create foreign key you have to refer the primary key column.

Primary key is like a id(unique).if you insert the same values in the primary key column it wont allow the values inside the column.While run the query it shows some error.Different types of keys available for different purpose.Some of the keys names are foreign key,super key,unique key etc.,

NOTE: if you want to make foreign key for a column it should be a primary key column in another table.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details