Keys in databases help uniquely identify rows in tables. The main types of keys are:
1. Primary keys uniquely identify each row and cannot contain duplicate values.
2. Foreign keys link rows between tables and must match the primary key of another table.
3. Composite/compound keys combine two or more columns to uniquely identify rows, since the columns individually may not be unique.
Surrogate keys are artificial primary keys used when the table lacks a natural candidate key to uniquely identify rows. Other keys include unique keys, alternate keys, candidate keys, and super keys. Keys are important for maintaining data integrity and relationships between tables.
Key
“KEYS in DBMSis an attribute or set of
attributes which helps you to identify a
row(tuple) in a relation(table)”
4.
Why we needa Key?
1. Keys help us to identify any row of data in a table.
In a real-world application, a table could contain
thousands of records.
Moreover, the records could be duplicated.
Keys ensure that you can uniquely identify a
table record despite these challenges.
5.
Why we needa Key?
2. Allows you to establish a relationship
between and identify the relation between
tables
3. Help you to enforce identity and
integrity in the relationship.
6.
Types of Keys
1.Super Key
2. Primary Key
3. Unique Key
4. Candidate Key
5. Alternate Key
There are mainly different types of Keys
6. Foreign Key
7. Compound Key
8. Composite Key
9. Surrogate Key
7.
Super key
A superkey is a group of single or multiple keys which
identifies rows in a table. A Super key may have additional
attributes that are not needed for unique identification.
In the above-given example, EmpSSN and EmpNum name are superkeys.
8.
EID Name MobileNo AC
1 Komal
Sharma
9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Super Key
9.
PRIMARY KEY
PRIMARY KEYis a column or group of columns in a table
that uniquely identify every row in that table. The Primary
Key can't be a duplicate meaning the same value can't appear
more than once in the table. A table cannot have more than
one primary key.
10.
Rules for definingPrimary key
1. Two rows can't have the same primary key value
2. It must for every row to have a primary key value.
3. The primary key field cannot be null.
4. The value in a primary key column can never be
modified or updated if any foreign key refers to that
primary key.
11.
EID Name MobileNo AC
1 Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Primary Key
In the following example, EID is a Primary Key.
12.
Unique KEY
Unique KEYis a column or group of columns in a table that
uniquely identify every row in that table. The Unique Key
can't be a duplicate meaning the same value can't appear
more than once in the table. A table can have more than one
Unique key.
13.
Rules for definingUnique key
1. Two rows can have the same Unique key value
2. The Unique key field can be null.
14.
EID Name MobileNo AC
1 Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Unique Key
In the following example, EID and ACis a Unique Key.
15.
ALTERNATE KEYS
ALTERNATE KEYSis a column or group of columns in a
table that uniquely identify every row in that table. A table can
have multiple choices for a primary key but only one can be set
as the primary key.
All the keys which are not primary key are called an Alternate
Key.
16.
Example:
In this table,StudID, Roll No, Email are qualified to
become a primary key. But since StudID is the primary key,
Roll No, Email becomes the alternative key.
17.
EID Name MobileNo AC
1 Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Alternate Key
18.
CANDIDATE KEY
CANDIDATE KEYis a set of attributes that uniquely
identify tuples in a table. Candidate Key is a super key with
no repeated attributes. The Primary key should be selected
from the candidate keys. Every table must have at least a
single candidate key. A table can have multiple candidate keys
but only a single primary key.
19.
Properties of Candidatekey
1. It must contain unique values
2. Candidate key may have multiple attributes
3. Must not contain null values
4. It should contain minimum fields to ensure uniqueness
5. Uniquely identify each record in a table
20.
EID Name Mobile
No
AC
1Komal
Sharma
9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Candidate Key
22.
FOREIGN KEY
FOREIGN KEYis a column that creates a relationship
between two tables.
The purpose of Foreign keys is to maintain data integrity and
allow navigation between two different instances of an
entity. It acts as a cross-reference between two tables as it
references the primary key of another table.
COMPOUND KEY
COMPOUND KEYhas two or more attributes that allow you to
uniquely recognize a specific record. It is possible that each column
may not be unique by itself within the database.
However, when combined with the other column or columns the
combination of composite keys become unique. The purpose of the
compound key in database is to uniquely identify each record in the
table.
25.
EID Name MobileNo AC
1 Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
Compound Key
In this example, Mobile No can't be a primary key as it does not uniquely identify
a record. However, a compound key of Mobile No could be used as it
uniquely identified each record.
26.
COMPOSITE KEY
COMPOSITE KEYis a combination of two or
more columns that uniquely identify rows in a table.
The combination of columns guarantees uniqueness,
though individually uniqueness is not guaranteed.
Hence, they are combined to uniquely identify
records in a table.
27.
EID Name MobileNo AC
1 Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
COMPOSITE Key
28.
SURROGATE KEYS
SURROGATE KEYsis An artificial key which aims to uniquely
identify each record is called a surrogate key.
This kind of partial key in dbms is unique because it is created
when you don't have any natural primary key. They do not lend
any meaning to the data in the table.
Surrogate key is usually an integer.
29.
Example, shown shifttimings of the different employee. In
this example, a surrogate key is needed to uniquely
identify each employee.
Surrogate keys in sql are allowed when No property has
the parameter of the primary key.
30.
EID Name Mobile
No
AC
1Komal Sharma 9876543210 12
2 Rahul 8789654896 34
3 Prince Gupta 9854756985 56
4 Shalu Gupta 5896325878 78
5 Ritik Joshi 7896547896 19
AC Salary Driving
License
12 5000 DL123456
34 4500 DL123786
75 1850 DL123987
78 5520 DL123258
20 4896 DL123147
Foreign
Key
Primary Key
Primary Key
Super Key
Candidate Key
Alternate Key
Compound Key