
Read about the database relationships.
Join_data
Employee_ID | Join_Date | Room_no |
3941 | 20.04.18 | 204 |
3940 | 11.04.18 | 205 |
3943 | 28.05.18 | 206 |
Here in the element of the first column second row, we saw data 3941. We failed to understand what is meaning by this data. But if we see the header of this column there is a data name Employee_ID.
From this heading data, we can understand what the meaning of the data is in the whole column. This is Key.
Database keys types
Below we have discussed the Database key types with simple examples.Key is basically 3 types-
- Primary key
- Composite key
- Foreign key
Primary key
Employee_Info
Employee _Name | Employee _ID | Interview _date | Join _Date | Room _no |
Rahman | 3941 | 10.04.18 | 20.04.18 | 202 |
Khan | 3940 | 01.04.18 | 11.04.18 | 203 |
Rahman | 3943 | 18.05.18 | 28.05.18 | 203 |
Jordan | 3942 | 30.04.18 | 9.05.18 | 202 |
Stuart | 3944 | 19.03.18 | 29.03.18 | 202 |
Here each person cannot be uniquely identified by Employee_Name, Interview_date, Join_Date, and Room_no.
But it can be easily identified by Employee_ID. It works as a unique identifier.
Composite Key
Sometime we will fail to identify data by using only a single field. In those conditions, we will use two or more fields for identifying data. This is called a composite key.
Employee _Name | Employee _ID | Section | Interview _date | Join _Date | Room _no |
Rahman | 3941 | Building | 10.04.18 | 20.04.18 | 202 |
Khan | 3940 | Building | 01.04.18 | 11.04.18 | 203 |
Rahman | 3941 | Updating | 10.04.18 | 20.04.18 | 202 |
Khan | 3940 | Updating | 01.04.18 | 11.04.18 | 203 |
In this example, we are seeing that we failed to identify uniquely by Employee_ID. In this case, we have to use another key for identity. We can do one thing. There is a key in the table “Section”. Here we will make a composite key by using two keys “Employee_ID” and “Section”. After that, we can identify easily.
Foreign key
If we use a primary key of a table in another table as a normal key then it will be called a foreign key.
Join_data
Employee_ID | Join_Date | Room_no |
3941 | 20.04.18 | 204 |
3940 | 11.04.18 | 205 |
3943 | 28.05.18 | 206 |
Interview_data
Interview_date | Employee_ID |
10.04.18 | 3941 |
01.04.10 | 3940 |
18.05.18 | 3943 |
Employee_ID key is the primary key of the Join_data table but in the Interview_data table is a normal key.
What is the difference between primary key and foreign key?
Primary Key | Foreign Key |
By using which field each of the records can be identified individually that is called the primary key. | If the primary key of one table is used as a normal key of another table is called a foreign key. |
Used for data identification. | Used for creating a relationship between two tables. |
It is the parent. | It is a child. |
In a table, there will be a single primary key only. | In a table, multiple foreign keys can be saved. |
0 Comments