“TYPES OF KEYS IN
DATABASE”
PRESENTED BY:
DARSHAN HINGU
OBJECTIVES
 WHAT IS KEY?
 Super Key
 Primary Key
 Candidate Key
 Alternate Key
 Foreign Key
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.
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
 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.
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.
 CREATE TABLE CUSTOMER
( CustomerNO int NOT NULL PRIMARY KEY,
FirstName varchar(20),
LastName varchar(20)
);
CustomerNo FirstName LastName
1 Sally Thompson
2 Sally Henderson
3 Harry Henderson
4 Sandra Wellington
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.
 A primary key is being selected from the group of candidate keys.
 The value of Candidate Key is non-null for every tuple.
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.
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.
 --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.
RELATIONSHIP BETWEEN KEYS
THAN
KYOU

Types of keys in dbms

  • 1.
    “TYPES OF KEYSIN DATABASE” PRESENTED BY: DARSHAN HINGU
  • 2.
    OBJECTIVES  WHAT ISKEY?  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  Asuper 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 certainset 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  Aprimary 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.
  • 7.
     CREATE TABLECUSTOMER ( CustomerNO int NOT NULL PRIMARY KEY, FirstName varchar(20), LastName varchar(20) ); CustomerNo FirstName LastName 1 Sally Thompson 2 Sally Henderson 3 Harry Henderson 4 Sandra Wellington
  • 8.
    CANDIDATE KEY  Therecan 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 primarykey is being selected from the group of candidate keys.  The value of Candidate Key is non-null for every tuple.
  • 10.
    ALTERNATE KEY  Alternatekeys 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  Foreignkey 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 CREATETABLE 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.
  • 14.
  • 16.