0% found this document useful (0 votes)
2 views

SQL_3 (1)

The document outlines various types of keys in MS SQL Server, including candidate keys, primary keys, alternate keys, composite keys, and foreign keys. It explains the definitions and characteristics of each key type, emphasizing their roles in uniquely identifying records and maintaining referential integrity in database tables. Examples are provided to illustrate the concepts clearly.

Uploaded by

Nidhi Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL_3 (1)

The document outlines various types of keys in MS SQL Server, including candidate keys, primary keys, alternate keys, composite keys, and foreign keys. It explains the definitions and characteristics of each key type, emphasizing their roles in uniquely identifying records and maintaining referential integrity in database tables. Examples are provided to illustrate the concepts clearly.

Uploaded by

Nidhi Tiwari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MS SQL SERVER

Session #3 :
Identifying Keys in
Tables
Focus Points :
 Identify candidate keys.
 Identify primary keys.
 Identify alternate keys.
 Identify composite keys.
 Identify foreign keys.
Contents

 Candidate Key
 Primary Key
 Alternate Key
 Composite Key
 Referential Integrity
 Foreign Key
Candidate Key

 A candidate key is a column, or set of columns, in a


table that can uniquely identify any database record
without referring to any other data.
 Each table may have one or more candidate keys, but
one candidate key is special, and it is called the
primary key.
 Example :
 cFax, cPhone, cContractRecruiterCode are candidate keys
Candidate Key
Primary Key

 Primary key is a set of one or more fields/columns of


a table that uniquely identify a record in database
table. It can not accept null, duplicate values. Only
one Candidate Key can be Primary Key.
Primary Key

cEmpCode and cDepartmentCode


Alternate Key

 A Alternate key is a key that can be work as a


primary key. Basically it is a candidate key that
currently is not primary key.
 Example:
 In below cFax, cPhone becomes Alternate Keys when we define
cContractRecruiterCode as Primary Key.
Alternate Key
Composite Key

 Composite Key is a combination of more than one


fields/columns of a table. It can be a Candidate key,
Primary key.
 PRIMARY KEY (attribute1,attribute2)
 CREATE TABLE SAMPLE_TABLE
(COL1 integer,
COL2 nvarchar(30),
COL3 nvarchar(50),
PRIMARY KEY (COL1, COL2));
 Example :
 cEmployeeCode and cCandidateCode
Composite Key
Referential Integrity

 One table is the referenced table and the other is


the referencing table; values in the referencing
table(with foreign key) must match values in
referenced table(with primary key).
 SQL Server can automatically enforce referential
integrity through foreign key constraints that you
define in the referenced table.
Foreign Key

 Foreign Key is a field in database table that is


Primary key in another table. It can accept multiple
null, duplicate values.
 Example : We can have a DeptID column in the
Employee table which is pointing to DeptID column
in a department table where it a primary key.

Foreign Key
Complete Example


Summary

 Candidate Key
 Primary Key
 Alternate Key
 Composite Key
 Referential Key
 Foreign Key

You might also like