You are on page 1of 8

15- SQL

(Primary
key &
Foreign
key)
List of Constraints in MySQL:
 NOT NULL
 UNIQUE
 DEAFULT
 CHECK
 PRIMARY KEY
 FOREIGN KEY

Constrains are restrictions on the column(s) of a table that dictates what data can be added in the
column and what cannot be added in the column. So similarly, Primary key and Foreign key also impose
restrictions on the columns of a table.

What is Primary Key Constraint?


1. Primary key always has unique data.
 Whenever we apply Primary key constraint to a column, it will always contain unique data.
Meaning that the column will not allow any duplicate data. You might be thinking that we can
also do this with our UNIQUE constraint. Well, yes you can do this using UNIQUE constraint, but
there is a difference between PRIMARY KEY and UNIQUE constraint. With UNIQUE constraint,
we can also store NULL values but a column with PRIMARY KEY constraint cannot have a NULL
value.
2. A primary key cannot have null value.
3. A table can contain only one primary key constraint.
 When we set primary key constraint in a table, we can apply it to only one column. Meaning
only one column in a table can be set with primary key constraint. We cannot do it like with
UNIQUE constraint, where we can apply it to multiple columns of a table.

For an already existing table, PRIMARY KEY constraint can be set to any one column that has
unique data and doesn’t contain null value.
Now, let’s see how to set a column in a table with primary key constraint.

Auto increment automatically increments and adds a value to the column (starting from 1)
when a record is added to the table. There can be only one auto column in a table and it must
be defined as a primary key.

Following is the syntax to set a column of an already existing table with primary
key constraint.

We use the ALTER command to modify a table.


Following is the Syntax to DROP the PRIMARY KEY Constraint from a table.

To drop a primary key from a table, use an ALTER TABLE clause with the name of the table (in
our example, mahnoor_family) followed by the clause DROP PRIMARY KEY. Since a table can
have only one primary key, you don't need to specify the primary key column.

Note: We didn’t actually create Primary Key constraint in the mahnoor_family table. So the
above DROP query is just a demo.
What is Foreign Key Constraint?
1. A FOREIGN KEY is a key used to link two tables together.
 The purpose of setting a Foreign Key in a table is to link the table with another table.
2. A FOREIGN KEY in one table points to the PRIMARY KEY in another table (to which it is linked).
 A column set with FOREIGN KEY constraint in a table will contain only the PRIMARY KEY values
of another table to which it is linked.
 We do this to connect the two tables together. In other words, we do this to create a link
between two tables, or simply, to link two tables together.
3. Foreign Key constraint can be applied to any column of a table (other than the one set with Primary
Key constraint). Foreign Key constraint can also be applied to multiple columns of a table,
with each foreign key column referring to the primary key column of a different table – this
in turn means that the Parent table will be linked with multiple Child tables.

Example:

We use Foreign Key only when we want to link two tables together.

Here “Student” is the Parent Table and “City” is the Child Table (to which the parent table is
linked via Foreign Key). This is a normalized form of the Student table. Normalization is the
process to eliminate data redundancy, data duplication and enhance data integrity in the table.
The table will contain less data, which increases the speed of SQL operations on the table.

Now, let’s see how to set a column in a table with foreign key constraint.

Foreign Key can also be defined with a constraint name:

 SQL Statement  CONSTRAINT fk_city FOREIGN KEY (city) REFERENCES City (cid)
 fk_city is the constraint name. A foreign key constraint name starts with “fk_” (not case
sensitive).

Both the referencing and referenced columns must be of the same type (and same in this case
includes the signed/unsigned attribute). Otherwise, the Foreign Key constraint will not be
formed.

The keyword REFERENCES is used to tell that which table’s Primary Key the Foreign Key is
referring to.

Here “Student” is the Parent Table and “City” is the Child Table (to which the parent table is
linked via Foreign Key).

Note: It is absolutely not necessary to first set the PRIMARY KEY constraint in the table, in order
to apply FOREIGN KEY constraint(s) to the column(s) of the table. In fact, you don’t need to set
primary key constraint in the table at all if you don’t want to and you would still be able to
apply Foreign Key constraint(s) to the column(s) of the table. Foreign Keys are simply used for
referencing data from another table.

Following is the syntax to set any column of an already existing table with
foreign key constraint.

Foreign Key can also be added with a constraint name:

 SQL Statement  ADD CONSTRAINT fk_city FOREIGN KEY (city) REFERENCES City (cid)

Following is the Syntax to DROP the FOREIGN KEY Constraint(s) from a table.

If you defined a CONSTRAINT name when you created the FOREIGN KEY constraint, you can
refer to that name to drop the foreign key constraint. Otherwise, a constraint name was
generated internally, and you must use that value to drop the foreign key constraint. To
determine the internally generated foreign key constraint name, use SHOW CREATE TABLE
table_name; statement.
SQL PRACTICAL:

A Foreign Key with ON DELETE CASCADE means that if a record in the parent table is deleted,
then the corresponding records in the child table will automatically be deleted. This is called a
cascade delete in SQL.

We use the ON DELETE CASCADE option to specify that we want the rows deleted in a child
table when corresponding rows are deleted in the parent table. If we do not specify cascading
deletes, the default behavior of the database server prevents us from deleting data in a table if
other tables reference it.

Now, we have got primary key values in the parents column. But what if we want the names to
be shown instead of primary key values, when we run some sort of SQL query on the
mahnoor_family_kids table. For that we use JOIN Clause. We will see in next modules that how
we use JOIN Clause to bring the data of two or more tables together.

A foreign key column will not contain any values other than the primary key values it is
referencing.

You might also like