Keys in DBMS
What are Keys in DBMS?
Keys are an essential component of a relational database; they are used to establish and discover relationships between tables, as well as to uniquely identify any file or row of information within a table.
In this article , we will learn about Keys in DBMS.
Keys in DBMS?
Keys Identify any given tuple or record uniquely and also provide necessary functionality between various tables, in sql there are various types of keys, some of which are –
- Primary Key
- Foreign Key
- Candidate Key
- Super Key
- Alternate Key
- Composite Key
We also use keys as they surely help us in enforcing identity & integrity in the relationship.
Let’s have a look further to understand how each of them work –
How Keys in DBMS work
For an explanation of how each of the keys is working, we will take the help of the following tables –
Student Table
Student_ID | NAME | PHONE | AGE |
---|---|---|---|
1 | Arya | 9111122222 | 21 |
2 | Bran | 9000012121 | 19 |
3 | Jon | 1234567890 | 24 |
4 | Cersie | 9876543210 | 24 |
5 | Jamie | 9988776655 | 24 |
Course Table
Student_ID | COURSE_ID | NAME |
---|---|---|
1 | ECE101 | Basic Electronics |
2 | CSE101 | C++ |
3 | CSE101 | C++ |
4 | EEE101 | Basic Electrical |
5 | CSE201 | DBMS |
To uniquely identify each tuple or row in a data table. We use primary key as identifying attribute.
For example –
We all are given a unique rollnumber or registration number at the time of admission in a school or college. That is primary key for any student table.
In the example mentioned above STUDENT_ID
works as primary Key.
The minimal (minimum) number of attributes that can uniquely identify a record for a data table is/are candidate key(s).
For example –
- STUDENT_ID can identify a unique record in a datable
- For course table we need both STUDENT_ID and COURSE_ID to uniquely identify a record i.e. (STUDENT_ID, COURSE_ID) become candidate key, which is composite in nature.
- Candidate keys must have non null elements in each record/tuple
- Only Unique values are allowed
- Candidate key may have multiple attributes
- It should contain minimum fields to ensure uniqueness
- Should be able to uniquely identify each record in a table
Set of attributes that can help us uniquely identify a record or tuple in the database is called super key.
Now, super key may sound the same as candidate key. Yes, it does !!! There is only slight different.
- Candidate key says – Minimal
- Super key – Doesn’t say any number
Thus, for course table STUDENT_ID
, COURSE_ID
, NAME
can be a super key of 3 composite attributes in nature, as they will uniquely identify the record.
But, it is not a candidate key as just by using STUDENT_ID
and COURSE_ID
we can uniquely identify rows. Thus, minimum number is 2.
Note –
- Every Candidate key is a super key
- The vice versa is not true however
- If we just add one or more attributes to a candidate key then, it becomes a super key.
There can be multiple Candidate keys right? Out of which we may select one as a primary keys.
The other set of candidate keys than primary key itself are known as candidate keys.
Keys which more than one attribute that can uniquely identify a record in a table is a composite key.
Note – Some people may get confused between candidate key and composite key –
Candidate Key: A nominee for primary key field is known as candidate key.
Composite Key: Creating more than one primary key is jointly known as composite key.
A candidate key is a unique key that can be used as a primary key. Composite key is a key of two or more attributes that uniquely identifies the row. A key is a set of columns that can be used to uniquely identify each row within a table.
To define a relationship with another table of a database. We use foreign key. A foreign key is a column of a table that points towards the primary key of another table.
For example –
Department Table
Department_ID | Department_Name |
001 | CSE |
002 | IT |
005 | Mechnical |
Professor Table
Professor_ID | Fname | Lname |
P01 | Walter | White |
P02 | Jesse | Pinkman |
P03 | Skyler | White |
In the above tables Department_ID is the primary key for Department Table and Professor_ID is the primary key of Professor Table.
Now, if we add DeptarmentID in table 2 in that case we can create a relationship between table 1 and table 2. Understanding that which teacher belongs to which department. Thus adding Department_ID, which is foreign key in New table and the primary key Department table.
New Table
Professor_ID ID | Department_ID | Fname | Lname |
B002 | 002 | Walter | White |
B017 | 002 | Jesse | Pinkman |
B009 | 001 | Skyler | White |
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment