Database Systems DBMS KEYS 7
Database Systems DBMS KEYS 7
RDBMS KEYS
Computer Science
Why we need Keys?
❑ Keys help you 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.
❑ Allows you to identify the relation between tables
❑ To establish a relationship between tables
❑ Help you to enforce identity and integrity in the
relationship.
Computer Science
What are Keys?
A RDBMS key is an attribute or set of an attribute which helps you to identify a
row(tuple) in a relation(table).
They allow you to find the relation between two tables.
RDBMS has flowing types of Keys each have their different functionality:
1. Super Key
2. Candidate Key
3. Primary Key
4. Alternate Key
5. Foreign Key
6. Compound Key
7. Surrogate Key
Computer Science
1-Super Key
A super key is a single or group of multiple
attributes which uniquely identifies row in a
table. A Super key may have additional attributes
that are not needed for unique identification.
Table Student
rollno name address phone age
Computer Science
3-Primary Key
A selected attribute or group of attribute in a table which helps us to uniquely
identifies every row in that table is called a primary key. This DBMS can't be a
duplicate. The same value can't appear more than once in 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 Table Student
Computer Science
4- Alternate Key
All the minimal super keys which are not primary key are
called an alternate key. It is a candidate key which is
currently not the primary key.
Example: In this table.
StudID, RollNo, Email are qualified to become a primary key. But since StudID
is the primary key, RollNo, Email becomes the alternative key.
Table Student
StudID RollNo FirstName LastNa Email
me
Computer Science
Examples
Computer Science
5- Foreign Key
A foreign key is a column which is added to
create a relationship with another table. It is
a primary key in other table. Foreign keys
help us to maintain data integrity and also
allows navigation between two different
instances of an entity.
Computer Science
Foreign Key Example
Table Student
DEPT_CO
ROLLNO NAME ADDRESS PHONE AGE DE
Table DEPT
DEPT_CODE DEPT_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
CV CIVIL ENGINEERING
Computer Science
6- Compound Key
Compound key has many fields which allow you to uniquely
recognize a specific record. It is possible that each column
may be not unique by itself within the database. However,
when combined with the other column or columns the
combination of composite keys become unique.
Computer Science
7- Surrogate Key Example
Fname Lastname Start Time End Time
Computer Science
Keys
A Super Key in DBMS can identify every attribute in a database in
Super Key
a unique way. Hence, it is effectively just a key.
Alternate The remaining keys are known as Alternate Keys when a key is
Key selected from the collection of candidate keys.
Primary The primary key is selected as one candidate key from collecting all
Key potential candidate keys.
Computer Science
Difference b/w Primary & Foreign Keys
Primary Key Foreign Key
Helps you to uniquely identify a record in It is a field in the table that is the primary key
the table. of another table.
Primary Key never accept null values. A foreign key may accept multiple null
values.
Primary key is a clustered index and data A foreign key cannot automatically create an
in the DBMS table are physically index, clustered or non-clustered. However,
organized in the sequence of the clustered you can manually create an index on the
index. foreign key.
You can have the single Primary key in a You can have multiple foreign keys in a
table. table.
Computer Science