Documente Academic
Documente Profesional
Documente Cultură
With so many different types of keys (foreign, primary, unique, natural, super, etc), it can
really get quite confusing to have a solid understanding of keys in SQL. And to make it even
more confusing, many people think that relational database theory (which deals with terms
like tuples, attributes, and relations), SQL (which deals with terms like tables rows and
columns, and is our main concern here), and file systems (which deal with terms like records
and fields) are all the same concept when in fact they are all completely different.
With that in mind, we want to give the proper definition of keys in SQL so that the
foundation of your understanding can be solid.
www.bbam.blogspot.com
there are also some major differences that exist between primary, unique, and foreign keys.
We will go over those differences in this article. But first, we want to give a thorough
explanation of why foreign keys are necessary in some situations.
www.bbam.blogspot.com
www.bbam.blogspot.com
Natural keys are often also called business keys so both terms mean exactly the same thing.
Example of a superkey
Suppose we have a table that holds all the managers in a company, and that table is called
Managers. The table has columns called ManagerID, Name, Title, and DepartmentID. Every
manager has his/her own ManagerID, so that value is always unique in each and every row.
This means that if we combine the ManagerID column value for any given row with any
other column value, then we will have a unique set of values. So, for the combinations of
(ManagerID, Name), (ManagerID, Title), (ManagerID, DepartmentID), (ManagerID, Name,
DepartmentID), etc there will be no two rows in the table that share the exact same
combination of values, because the ManagerID will always be unique and different for each
www.bbam.blogspot.com
row. This means that pairing the Manager ID with any other column(s) will ensure that the
combination will also be unique across all rows in the table.
And that is exactly what defines a superkey its any combination of column(s) for which
that combination of values will be unique across all rows in a table. So, all of those
combinations of columns in the Manager table that we gave earlier would be considered to be
superkeys. Even the ManagerID column is considered to be a superkey, although a special
type of superkey as you can read more about below.
www.bbam.blogspot.com
www.bbam.blogspot.com
And if there are other tables in which Company X uses that employee then he would have to
be deleted from those tables as well an even bigger pain.
By enforcing referential integrity, we can solve that problem, so that we wouldnt have to
manually delete him from the Employee Salary table (or any others). Heres how: first we
would define the employee ID column in the Employee table to be our primary key. Then, we
would define the employee ID column in the Employee Salary table to be a foreign key that
points to a primary key that is the employee ID column in the Employee table. Once we
define our foreign to primary key relationship, we would need to add whats called a
constraint to the Employee Salary table. The constraint that we would add in particular is
called a cascading delete this would mean that any time an employee is removed from the
Employee table, any entries that employee has in the Employee Salary table would also
automatically be removed from the Employee Salary table.
Note in the example given above that referential integrity is something that must be enforced,
and that we enforced only one rule of referential integrity (the cascading delete). There are
actually 3 rules that referential integrity enforces:
www.bbam.blogspot.com
there are only two unique values that could possibly appear in that column Male and
Female.
www.bbam.blogspot.com