"This is an excepted question in an SQL Developer interview."
Introduction:
Key play an important role in data integrity and an efficient access to the records in the relational databases. It is used to prevent the data from being duplicated. It maintains the reliable relationship between the tables. There are several types of keys which are below listed:
Primary Key
Candidate Key
Super Key
Foreign Key
Unique Key
Alternate Key
Primary Key:
It is applied on the columns of the table which helps in identifying each row or tuple uniquely. It enforces integrity constraint on the table. Only one primary key is applied in a table. It does not accept any null and duplicate values. It does not accept null and duplicate values. It's value is rarely changed in a table. Primary key in one table can acts as foreign key in another table.
Let us understand the concept with the help of student table example.
create table student (roll_no int not null,sname varchar2(15),dept_name varchar2(20),mobile_no varchar2(10),uid varchar2(20), primary key(roll_no));
It can be made from one or more table fields. It contains not null constraints.
Unique Key:
Unique key constraints are used to identify an individual tuple or row uniquely in the relation or table.Table can have more than one primary key. It can accept one null value in the table. Unique key constraint is referenced by the foreign key of another table. It can be enforced when the group of columns is not having primary key.
create table student (roll_no int not null,sname varchar2(15),dept_name varchar2(20),mobile_no varchar2(10),uid varchar2(20) unique, primary key(roll_no));
Every citizen of India must have unique id. If he/she is migrated to another country permanently, he or she will not have uid in that country.So,unique key will replace null value for that fields.
Difference:
PRIMARY KEY |
UNIQUE KEY |
It supports auto
increment value. |
It does not
support auto increment value. |
It creates
the clustered index. |
It does not
create the clustered index. |
One table can
have only one primary key. |
One table can
have more than one unique key. |
Primary key
values cannot be changed or deleted. |
Unique key
can change |
Null value
cannot be accepted. |
Null value
can be accepted. |
Primary key
can be unique value. |
Unique key cannot
be primary key. |
Comments
Post a Comment