SQL Keys

SQL Keys

1.PRIMARY KEY
2.FORIEN KEY

keys

 

Let us learn one by one.

PRIMARY KEY:

  • It allows only unique values.
  • It does not allow Null values and duplicate values in a table.
  • It is used inside other SQL statements such as Create, Alter…and so..on.

FOREIGN KEY:

  • A FOREIGN KEY is a PRIMARY KEY OF another table .
  • We are linking apple table with Banana Table using FruitID as FOREIGN KEY.
    Here Banana table has FruitID as PRIMARY KEY and we are referencing this to apple Table FruitID.

 


Can we drop Foreign Key table before dropping the Primary Key table?

  • apple table has FOREIGN KEY and it has Reference to Banana table which has Primary key.
  • Now let us try to drop Primary key table called Banana table,
    when we try to drop it throws an error stating that
    “It can not delete or update a parent row: a foreign key constraints fails.”
  • In this case if we want to drop the table with Foreign Key then we must delete at first the table with primary key and the table with foreign key.