Keys in DBMS
Keys in DBMS
3.1. Keys: Super Key, Candidate Key, Primary Key, Alternate Key, Foreign Key
We have following types of keys in SQL which are used to fetch records from tables
and to make relationship among tables or views.
1. Super Key
Super key is a set of one or more than one keys that can be used to identify a
record uniquely in a table. Example : Primary key, Unique key, Alternate key
are subset of Super Keys.
2. Candidate Key
3. Primary Key
4. Alternate key
6. Unique Key
Foreign Key is a field in database table that is Primary key in another table. It can
accept multiple null, duplicate values.
--Department Table
CREATE TABLE Department
(
DeptID int PRIMARY KEY, --primary key
Name varchar (50) NOT NULL,
Address varchar (200) NOT NULL
)
--Student Table
CREATE TABLE Student
(
ID int PRIMARY KEY, --primary key
RollNo varchar(10) NOT NULL,
Name varchar(50) NOT NULL,
EnrollNo varchar(50) UNIQUE, --unique key
Address varchar(200) NOT NULL,
DeptID int FOREIGN KEY REFERENCES Department(DeptID) --foreign key
)
Practically in database, we have only three types of keys Primary Key, Unique Key
and Foreign Key. Other types of keys are only concepts of DBMS which you should
know.
3.2. Constraints
Integrity Constraints
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures
the accuracy and reliability of the data in the table. If there is any violation between
the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
Example
If the table has already been created, you can add a NOT NULL constraint to a
column with the ALTER TABLE statement.
The UNIQUE constraint ensures that all values in a column are different.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness
for a column or set of columns.
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only one primary key, which may consist of single or multiple
fields.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
"Persons" table:
"Orders" table:
Notice that the "PersonID" column in the "Orders" table points to the "PersonID"
column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons"
table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders"
table.
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables.
The FOREIGN KEY constraint also prevents invalid data from being inserted into the
foreign key column, because it has to be one of the values contained in the table it
points to.
The following SQL creates a FOREIGN KEY on the "PersonID" column when the
"Orders" table is created:
To create a FOREIGN KEY constraint on the "PersonID" column when the "Orders"
table is already created, use the following SQL:
There are the domain integrity, the entity integrity, the referential integrity and the
foreign key integrity constraints.
Domain Integrity
Domain integrity means the definition of a valid set of values for an attribute. You
define
- data type,
- length or size
- is null value allowed
- is the value unique or not
for an attribute.
You may also define the default value, the range (values in between) and/or specific
values for the attribute. Some DBMS allow you to define the output format and/or
input mask for the attribute.
These definitions ensure that a specific attribute will have a right and proper value
in the database.
This is because the primary key value is used to identify individual rows in a table. If
there were null values for primary keys, it would mean that we could not identify
those rows.
On the other hand, there can be null values other than primary key fields. Null value
means that one doesn't know the value for that field. Null value is different from
zero value or space.
In the Car Rental database in the Car table each car must have a proper and unique
Reg_No. There might be a car whose rate is unknown - maybe the car is broken or it
is brand new - i.e. the Rate field has a null value. See the picture below.
The entity integrity constraints assure that a specific row in a table can be
identified.
Picture. Car and CarType tables in the Rent database
Examples
Rule 1. You can't delete any of the rows in the CarType table that are visible in the
picture since all the car types are in use in the Car table.
Rule 2. You can't change any of the model_ids in the CarType table since all the car
types are in use in the Car table.
Rule 3. The values that you can enter in the model_id field in the Car table must be in
the model_id field in the CarType table.
Rule 4. The model_id field in the Car table can have a null value which means that
the car type of that car in not known