What is Foreign key? How do you define a Foreign key in your table?

The common field which is Primary key in another related table is called Foriegn key. Foriegn key is a constraint which ensures that relationship between records in related tables are valid and user can not delete or update related data accidently.
We can define Foreign key in two ways-

1) Using CREATE TABLE-
FOREIGN KEY <column name to be set as foreign key> REFERENCES <Primary Table> (primary key column name)

2) Using ALTER TABLE-
ALTER TABLE <table name>
ADD FOREIGN KEY <column name to be made foreign key> REFERENCES <master table name >(primary key column name);

error: Content is protected !!