2. OBJECTIVES
WHAT IS KEY?
Super Key
Primary Key
Candidate Key
Alternate Key
Foreign Key
3. WHAT IS KEY?
Keys are very important part of Relational database model.
To maintain data integrity (that is data should be correct and in well
formed) we use concept of keys.
It is used for identifying unique rows from table.
They are used to establish and identify relationships between tables.
4. SUPER KEY
A super key is any combination of columns within a table that uniquely identifies
each record within that table.
As an example, a table used to store customer master details could contain
columns such as:
Customer name
Customer ID
Social Security number (SSN)
Address
Date of birth
5. A certain set of columns may be extracted and guaranteed unique to each
customer. Examples of super keys are as follows:
Name+SSN+DOB
ID+Name+SSN
All keys are Super key but not vice versa.
6. PRIMARY KEY
A primary key is a column in a table that uniquely identifies the rows in that
table.
The data values placed in the primary key column must be unique to each
row; no duplicates can be used.
Nulls are not allowed in primary key columns.
For example, in the table below, CustomerNo, which displays the ID number
assigned to different customers, is the primary key.
8. CANDIDATE KEY
There can be more than one candidate keys for a table.
A candidate key of a relation is a minimal super key for that relation.
A candidate key can be a combination of more than one columns.
A Candidate Key is a set of one or more columns that can identify a
record uniquely in a table. There can be multiple Candidate Keys in one
table.
Candidate keys are selected from the set of super keys, the only thing we
take care while selecting candidate key is: It should not have any
redundant attributes.
9. A primary key is being selected from the group of candidate keys.
The value of Candidate Key is non-null for every tuple.
10. ALTERNATE KEY
Alternate keys are candidate keys that are not selected as primary key.
Alternate key is also called “Secondary Key”.
If any table have more than one candidate key, then after choosing
primary key from those candidate key, rest of candidate keys are known
as an alternate key of that table.
11. FOREIGN KEY
Foreign key are those keys which is used to define relationship between
two tables.
Foreign key are columns that point to primary key columns in other
database tables.
It can accept multiple null, duplicate values.
It is also known as referential integrity.
12. --Course Table
CREATE TABLE course
(
courseid NUMBER(10) NOT NULL PRIMARY KEY, --primary key
CourseName varchar2(10) NOT NULL,
) ;
--Student Table
CREATE TABLE Student
(
studentId NUMBER(10) NOT NULL PRIMARY KEY, --primary key
firstName varchar2(10) NOT NULL,
lastName varchar2(10) NOT NULL,
courseid NUMBER(10) REFERENCES course(courseid) --foreign key
) ;
Example : We can have a courseid column in the Student table which is pointing to courseid
column in a course table where it a primary key.