What are Database Keys?
- Database Keys are attribute that helps you to identify a row(tuple) in a relation(table).
- They allow you to find the relationship between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table.
- The database key is also helpful for finding a unique record or row from the table. Examples of database keys include the following: Primary Key, Unique Key, Foreign Key e.t.c
- The database relationship is a set of joint statements that creates associations between one or more tables. It is used to retrieve data from a database quickly.
- Relationships can be divided into three categories mainly; One-to-One, One-to-Many, and Many-to-Many .
What Is Primary Key?
- A primary key generally focuses on the uniqueness of the table. It is a column or a set of columns that uniquely distinguishes every row in the database. It means it should not have any duplicate value. It is a column of a table that uniquely identifies each tuple (row) in that table.
- The primary key enforces integrity constraints to the table. Only one primary key is allowed to use in a table.
- The primary key does not accept any duplicate and NULL values. The primary key value in a table changes very rarely so it is chosen with care where the changes can occur in a seldom manner.
- A primary key of one table can be referenced by a foreign key of another table.
What Is Foreign Key?
- A foreign key is generally used to build a relationship between the two tables.
- The major purpose of the foreign key is to sustain data integrity between two separate instances of an entity. It is a column that creates a relationship between two tables.
- It acts as a cross-reference between two tables as it references the primary key of another table.
- Every relationship in the database should be supported by a foreign key
What Is a Unique key
- Unique key constraints identify an individual tuple uniquely in relation or table.
- A table can have more than one unique key, unlike a primary key.
- Unique key constraints can accept only one NULL value for a column.
- Unique constraints are also referenced by the foreign key of another table.
- It can be used when someone wants to enforce unique constraints on a column and a group of columns that is not a primary key.