Sunteți pe pagina 1din 32

Relational Database Management System

(Keys)
A relational Model is concerned with 3 components:
Data Structure
Data Integrity
Data Manipulation
Relational Data Structure
Relation:
Is a table with columns and rows.
All data and relationships are represented in a two
dimensional Table called a relation.
A relation consists of number of records or row wise
information and column wise information.
It represents the relation between the rows and columns
of a two dimensional table.
Attribute/ field:
An attribute is a named column of a relation.
A relation is represented as a two dimensional table in
which the rows of the table correspond to individual
records and the table columns correspond to attributes.
Attributes can appear in any order and the relation will
be the same relation and therefore covey the same
meaning.
Domain
Is the set of allowed values for one or more
attributes.
Defines the kind of data represented by the attribute.
Example: If a college has 5000 students then the
domain of Roll number may be from 1001 to 6000.
Domain
Each attribute in the model should be assigned
domain information that includes:
Data type
Length
Date Format
Range
Constraints
Null Support
Default value
Relational Keys
There should not be any duplicate tuple within a
relation.
Therefore we should identify one or more
attributes(called relational keys) that uniquely
identify each tuple in a relation.
Types of Keys
Super Key
Candidate Key
Primary Key
Alternate Key
Artificial Key
There is one more key, which is very imp. But it does
not hold the property of uniqueness and it is known
as Foreign Key.
Super Key
Are those attributes of a relation, which have the
properties of uniqueness.
Let us consider a relation R, by definition ,the set of
all attributes of R has the uniqueness property ,
meaning that, at any given time, no two tuples of
relation R are duplicates of one another.
Let K be a set of attributes of relation R.
Then K is a super key for R if and only if it posses
uniqueness.
Example
R=Student(Roll no., Name, Class)
Suppose Roll no. is unique.

Identify the superkeys?


Superkeys:

1. Roll No.
2. Roll No.,Name
3. Roll No.,Class
4. Roll No.,Class,Name
Example
R=Student(Roll no., Name, Class)
Suppose Roll no. And Name are unique.

Identify the superkeys?


Superkeys:

1. Roll No.
2. Roll No.,Name
3. Roll No.,Class
4. Roll No.,Class,Name
5. Name
6. Name,Class
Candidate Key
Are those attributes of a relation, which have the
properties of uniqueness and irreducibility.
Let K be a set of attributes of Relation R. Then K is a
candidate key for R if and only if it possesses both
properties:
Uniqueness
Irreducibility: No proper subset of K has the uniqueness
property.

There can be any number of candidate keys in a relation


and no component of candidate key is allowed to be null.
Every relation has atleast one candidate key.
Example (Name,Class) is unique then it can be
identified as the candidate key if and only if Name
and class individually are not unique.

IS (Roll No, Class) is a candidate key?


No
Suppose we have following superkeys

Identify the candidate keys:

1. Roll No.
2. Roll No.,Name
3. Roll No.,Class
4. Roll No.,Class,Name

Answer is Roll No.


Student (Roll number, Name, class, mobile number)

Identify candidate keys.

Answer is Roll number, mobile number


Primary Key
It has three properties:
Uniqueness
Irreducibility
Not Null
A relation has only one primary key. It may be a
single attribute or a combination of attributes.
Properties
Stable: The value of Primary Key must not change or
should not be null throughout the life of an entity.
Example: Age
Minimal: Should be composed of the minimum
number of fields that ensures the occurrences are
unique.
Definitive: A value must exist for every record at
creation time.
Accessible: anyone who wants to create , read and
delete a record must be able to see the primary key
value.
Example
Student (Roll NO.,Name,Class,Mobile No.)

Identify candidate keys and a primary key.


Identify the Primary Key
Emp ID Project ID Hours_worked
01 01 200
01 02 120
02 01 50
02 03 120
03 03 100
03 04 200
Alternate Keys
Candidate keys which are not chosen or promoted as
the primary key by the database designer, are known
as Alternate Keys.
Student(Roll No., Name, Class, Mobile No.).
Identify candidate , primary and alternate keys.
Artificial Keys
An artificial key is the one that has no meaning to the
business or organization and performs the function
of primary key in a relation.
Artificial Keys are permitted when
No attributes has the primary key properties or the
primary key is large and complex.
Example of artificial key
Name age city
A 23 X
A 24 X
B 24 X
A 23 X

In the above table, neither a single column nor combination of columns


is unique, so we can insert one new column for uniqueness.
Name age city
1 A 23 X
2 A 24 X
3 B 24 X
4 A 23 X

Artificial Key
Foreign Keys
Are the attributes of a table, which refers to the
primary key of some other table.
Foreign Keys permit only those values, which appear
in the primary key of the table to which it refers and
may be null.
The foreign key is a reference to the tuple of a table
from which it is taken ,this tuple being called the
Referenced table .
Foreign Key
EMP
Emp No. Ename jOB Sal Dept NO
1 A Clerk 4000 10
2 A Clerk 4000 20

3 B Mgr 8000 20
4 C Peon 2000 40
5 D Clerk 4000 10

6 E Mgr 8000 50

Dept No. Dname Loc


Target 10 A asr
Attribute 20 B Jal Dept
or Or
30 C Qadian Targ
Primary
Key 40 D et
Table
Integrity Constraints
1. Referential Integrity
2. Entity Integrity
3. Enterprise Constraints
Referential Integrity
Referential Integrity is the mechanism the system
provides to maintain foreign keys.
foreign key must specify the table whose primary key
is being referenced. Referential Integrity ensures only
one property for foreign keys:
A valid foreign key value must always reference an
null
existing primary key or contain a 
A foreign key may contain a null ; it does not need to
reference an existing primary key (actually, it can't
reference any primary key since primary keys cannot
nulls ).
contain 
Entity Integrity

Entity Integrity is the mechanism the system provides to


maintain primary keys.
The primary key serves as a unique identifier for rows in the
table.
Entity Integrity ensures two properties for primary keys:
1. The primary key for a row is unique; it does not match the
primary key of any other row in the table.
2. The primary key is not null , no component of the primary key
may be set to null .
The uniqueness property ensures that the primary key of each
row uniquely identifies it; there are no duplicates. The second
property ensures that the primary key has meaning, has a value;
no component of the key is missing.
The system enforces Entity Integrity by not allowing operations
(INSERT, UPDATE) to produce an invalid primary key. Any
operation that creates a duplicate primary key or one
nulls  is rejected.
containing 
Enterprise Constraints
These are business rule that may affect the database
and the data in it
for example, if a manager is only permitted to
manage 10 employees then it would violate an
enterprise constraint to manage more
NOTE
FOR PRACTICAL IMPLEMENTATION OF KEYS, REFER
CHAPTER 10 of Oracle 9i slides.

S-ar putea să vă placă și