Database Constraints: What Are Keys?
Database Constraints: What Are Keys?
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This
ensures the accuracy and reliability of the data in the table. If there is any
violation between the constraint and the data action, the action is aborted.
Super Key
Primary Key
Candidate Key
Alternate Key
Foreign Key
Compound Key
Composite Key
What is the 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.
Example:
Example:
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.
Example: In the given table Stud ID, Roll No, and email are candidate keys which
help us to uniquely identify the student record in the table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
In this example, we have two table, teach and department in a school. However,
there is no way to see which search work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.
Example:
In this example, OrderNo and ProductID can't be a primary key as it does not
uniquely identify a record. However, a compound key of Order ID and Product ID
could be used as it uniquely identified each record.
The difference between compound and the composite key is that any part of the
compound key can be a foreign key, but the composite key may or maybe not a
part of the foreign key.
Above, given example, shown shift timings of the different employee. In this
example, a surrogate key is needed to uniquely identify each employee.
Helps you to uniquely identify a record in the It is a field in the table that is the primary k
table. table.
Primary Key never accept null values. A foreign key may accept multiple null va
Primary key is a clustered index and data in the A foreign key cannot automatically create
DBMS table are physically organized in the clustered or non-clustered. However, you c
sequence of the clustered index. create an index on the foreign key.
You can have the single Primary key in a table. You can have multiple foreign keys in a ta
Summary
A DBMS key is an attribute or set of an attribute which helps you to identify
a row(tuple) in a relation(table)
DBMS keys allow you to establish a relationship between and identify the
relation between tables
Seven Types of DBMS keys are Super, Primary, Candidate, Alternate,
Foreign, Compound, Composite, and Surrogate Key.
A super key is a group of single or multiple keys which identifies rows in a
table.
A column or group of columns in a table which helps us to uniquely
identifies every row in that table is called a primary key
All the keys which are not primary key are called an alternate key
A super key with no repeated attribute is called candidate key
A compound key is a key which has many fields which allow you to
uniquely recognize a specific record
A key which has multiple attributes to uniquely identify rows in a table is
called a composite key
An artificial key which aims to uniquely identify each record is called a
surrogate key
Primary Key never accept null values while a foreign key may accept
multiple null values.