DBMS KEYs
DBMS KEYs
Key plays an important role in relational database; it is used for identifying unique rows
from table. It also establishes relationship among tables.
Allows you to establish a relationship between and identify the relation between tables
The attribute(s) that is marked as primary key is not allowed to have null values.
Primary keys are not necessarily to be a single attribute (column). It can be a set of more
than one attributes (columns). For example {Stu_Id, Stu_Name} collectively can identify the
tuple in the above table, but we do not choose it as primary key because Stu_Id alone is
enough to uniquely identifies rows in a table and we always go for minimal set. Having
that said, we should choose more than one columns as primary key only when there is no
single column that can uniquely identify the tuple in table.
Eg-:
1-
2-
3-
ALTER TABLE ORDER
Super Key – A super key is a set of one of more columns (attributes) to uniquely
identify rows in a table. A Super key may have additional attributes that are not needed
for unique identification.
Example:
Candidate Key – A super key with no redundant attribute is known as candidate key
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate
Key is a super key with no repeated attributes. The Primary key should be selected
from the candidate keys. Every table must have at least a single candidate key. A table
can have multiple candidate keys but only a single primary key.
Alternate Key – Out of all candidate keys, only one gets selected as primary key,
remaining keys are known as alternate or secondary keys.
Composite Key – A key that consists of more than one attribute to uniquely identify rows
(also known as records & tuples) in a table is called composite key.
Foreign Key – Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
FOREIGN KEY is a column that creates a relationship between two tables. The purpose
of Foreign keys is to maintain data integrity and allow navigation between two
different instances of an entity. It acts as a cross-reference between two tables as it
references the primary key of another table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
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.