Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Database keys explained
1. ASSIGNMENT # 2
SUBMITTED TO:
MISS AYESHA RASHID
SUBMITTED BY:
RABIA ZAFAR
17581556-045
BS IT (5TH
SEMESTER)
SECTION ‘A’
TOPIC:
KEYS IN DATABASE
2. Keys:
A key is a field, or combination of fields, in a database table used to retrieve and sort
rows in the table based on certain requirements. Keys are defined to speed up access to
data and, in many cases, to create links between different tables.
There are different types of keys in database:
1- Candidate key
2- Primary key
3- Super key
4- Composite key
5- Secondary key
6- Foreign key
Candidate Key:
The candidate keys in a table are defined as the set of keys that is minimal and
can uniquely identify any data row in the table.
3. Properties of Candidate key:
It must contain unique values
Candidate key may have multiple attributes
Must not contain null values
It should contain minimum fields to ensure uniqueness
Uniquely identify each record in a table
Primary Key:
The primary key is selected from one of the candidate keys and becomes the
identifying key of a table. It can uniquely identify any data row of the table.
Rules for defining Primary key:
Two rows can't have the same primary key value
It must for every row to have a primary key value.
The primary key field cannot be null.
The value in a primary key column can never be modified or updated if any
foreign key refers to that primary key.
Super Key:
Super Key is the superset of primary key. The super key contains a set of
attributes, including the primary key, which can uniquely identify any data row in
the table.
Alternate key:
Primary key is selected from candidate key and the other entities are alternate
keys.
Composite Key:
If any single attribute of a table is not capable of being the key. It cannot identify
a row uniquely, then we combine two or more attributes to form a key. This is
known as a composite key.
Secondary Key:
Only one of the candidate keys is selected as the primary key. The rest of them
are known as secondary keys.
4. Foreign Key:
A foreign key is an attribute value in a table that acts as the primary key in
another. Hence, the foreign key is useful in linking together two tables. Data
should be entered in the foreign key column with great care, as wrongly entered
data can invalidate the relationship between the two tables.
An example to explain the different keys is: