Sunteți pe pagina 1din 59

Basic Relational Data Model

Keys & Constraints

Faryal Saud
Lecturer
Department of Computer Science
FAST-NU, CFD Campus

CS203 Database Systems 1


Objectives

• Terminology
• Characteristics of Relations
• Relational Data Model Notations
• Key constraints
• Others Constraints

CS203 Database Systems 2


Terminology
• A relation is a table with columns and rows.
• Attribute is a named column of a relation.
• Domain is a set of allowable values for one or more attributes.
• Tuple is a row of a relation.
• Degree is a number of attributes in a relation.
• Cardinality is a number of tuples in a relation.
• Relationship is a link or dependency between relations.
• Relational Database is a collection of relations.

CS203 Database Systems 3


Terminology
A relation is a table with columns and rows.

A table can be written as


STUDENT(ID, Name, Nat_ID, DOB, DeptCode)
Relation Columns or Attributes
name

STUDENT
ID NAME NAT_ID DOB DEPTCODE Cardinality

201111 KHALED 1111111111 16-05-1984 ICS

Rows 202222 ADEL 2222222222 23-09-1984 COE


or 203333 AHMED 3333333333 11-08-1984 COE
Tuples
211111 HASSAN 4444444444 23-10-1985 SWE

204444 MUSTAFA 55555555555 16-11-1984 ICS

Degree
CS203 Database Systems 4
Characteristics of Relations

• Each relation in the same relational database schema has a


distinct name
• Each value in a tuple is atomic
• Each attribute in a relation has a distinct name.
• Values of an attribute are all from the same domain.
• Each tuple is distinct.
• Order of attributes has no significance.
• Order of tuples has no significance, theoretically.

CS203 Database Systems 5


Relational Data Model
Notations
The letters Q, R, S denote the abstract relation names.

R(A1, A2, A3, …., An) denotes a relation schema R of degree n.


Example: STUDENT(RollNo, Name, Phone, Address, Mobile, DOB)
COURSE(CourseID, Title, CreditHours)

Both t[Ai] and t.Ai refers to the value vi in t for attribute Ai


Example: in second tuple, both t[name] and t.name refers to “Adel”

Dom(DEPTCODE) = {‘COE’,’ICS’,’SWE’}

CS203 Database Systems 6


Relation’s Definition
Relation: A relation is a subset of Cartesian product of all domains
R(r)  Dom(A1) x dom(A2) x …… x Dom(An)
Where A1, A2, …., An represent attributes of a relation r.
Evaluation:
Given
A1 = {1, 2}, A2 = {a}, A3 = {x, y}
Then relation
X = {(1, a, y), (2, a, x), (2, a, y)}; 3 tuples of a relation
is a subset of
Y = Dom(A1) x dom(A2) x Dom(A3)
= {(1, a, x), (1, a, y), (2, a, x), (2, a, y)}
Note: Degree of a relation is the number of attributes, in above case degree of relation is
3.
Alternate Definition of Relation
For each tuple ti is a mapping from R to D
Where D = Dom(A1)  Dom(A2) …  Dom(An)
And t[Ai]  Dom(Ai)
Then a tuple can be considered as attribute and value.
For example: The following representation shows a tuple definition
t = <(id, 21587), (Name, Muhammad,), (Hphone, Null), (Wphone, 866-1141)>

CS203 Database Systems 7


Data Models
• Object-Based Data Models
• Entity-Relationship
• Semantic
• Functional
• Object-Oriented
• Record-Based Data Models
• Relational Data Model
• Network Data Model
• Hierarchical Data Model
• Physical Data-Models

CS203 Database Systems 8


Keys

A key is a combination of one or more columns that


is used to identify rows in a relation
A composite key is a key that consists of two or
more columns

CS203 Database Systems 9


Super Key
A super key is a combination of columns that
uniquely identifies any row within a relational
database management system (RDBMS) table.

{Roll#}, {Roll#, NIC#}, {NIC#}


{Roll#, FirstName, LastName, Address, City, NIC#, Deptno}

CS203 Database Systems 10


Example-1

Roll Number First Name Last Name


CSU0001 Shabbir Bhimani
CSU0002 Jain Rao
CSU0003 Nitin Uday

Now we have the following as super keys

Roll Number First Name

Roll Number First Name Last Name

CS203 Database Systems 11


Example-2
Following table consists of four columns
EmployeeID, Name, Job, DeptID
Examples of superkeys in this table would be {employeeID, Name},
{employeeID, Name, job}, and
{employeeID, Name, job, departmentID}
In a real database we don't need values for all of those
columns to identify a row
We only need, per our example, the set {EmployeeID}.
This is a minimal superkey
So, employeeID is a candidate key.
EmployeeID can also uniquely identify the tuples.
FirstName and FatherName (for NADRA)

CS203 Database Systems 12


Candidate Keys
A candidate key is a key that determines all of the other
columns in a relation
Candidate key columns help in searching fewer duplicated or
unique records.
Examples
In PRODUCT relation
Prod# is a candidate key
Prod_Name is also a candidate key
In ORDER_PROD
(OrderNumber, Prod#) is a candidate key
If BirthDate (or DrivingLicense#) is known then Name,
address, telno etc. can be found from fewer searched records.

CS203 Database Systems 13


Identifying Candidate Keys

For Bank: AccountHolder (or Customer)


ACC#, Fname, Lname, DOB, CNIC#, Addr, City, TelNo, Mobile#,
DriveLic#
For PTCL: Customer (single telno holders)
Consumer#, Fname, Lname, DOB, CNIC#, Addr, City, TelNo,
Mobile#
For NADRA: Citizen (CNIC#, Fname, Lname, FatherName, DOB,
OldCNIC#, PAddr, PCity, TAddr, TCity, TelNo, Mobile#)

TelNo, Mobile#:
Old value, Null,
fewer duplications,
changeable
CS203 Database Systems 14
Identifying right record using
Candidate Key – an Example
Let’s analyze the following set of columns

{FirstName, LastName, DOB, TelNo, NatCardID}

CS203 Database Systems 15


Candidate Keys cont….
Example-1: Branch (branch-name, assets, branch-city)
Candidate Key: {branch-name}
Example-2: Customer (cname, natid, address, city, telno)
cname, address, city can be duplicated individually and cannot determine a
record.
The following combinations distinguish customer records or tuples.
{cname, telno}
{natid}
{natid, cname}
As {natid}  {natid, cname}, then {natid, cname} is not candidate key and
{natid} is a candidate key
Example-3: Employee(empno, name, birth_date, address, city, telno,
citizenship_id)
empno, telno, citizenship_id are possible candidate keys ???

Exercise:
Course (cid, cname, deptno)
Semester (sid, syear, startdate)
ClassAllocation (sid, cid, sec#, building#, room#)
Identify candidate keys in each of the above relations

CS203 Database Systems 16


Question: With one CNIC, there may be many consumer nos (houses/
shops) , explain?
CS203 Database Systems 17
Candidate Key during search

CS203 Database Systems 18


Candidate Key during search …

CS203 Database Systems 19


Dependents

For example: Dependent means family


members (wife, son, daughter etc.), who
depends on father. Father is an employee
in some organization

CS203 Database Systems 20


Primary Keys
A primary key is a candidate key selected as the
primary means of identifying rows in a relation:
There is one and only one primary key per relation
The primary key is NOT NULL, UNIQUE
The ideal primary key is short, numeric(alpha), indexed, fixed length and never
changes
The primary key may be a composite key

CS203 Database Systems 21


Primary Keys cont….

Although, candidate keys are STU_ID, CIT_ID. Organization ITSELF issues its own
identifier PK. PK is a property of an organization. Driving license# is a property of
Ministry of Motors/ transportation, it must not be used PK in university or other
organizations. It is a candidate key for other organization for search purposes.
Note: It is not recommended to create PKs for columns like Quantity, Salary, Price
and column that has datatype DATE
CS203 Database Systems 22
Primary Keys cont…
Example-1:
STUDENT(StuID, FirstName, FamilyName, DOB, …)

Example-2: Building (B#, BName, Location, Region),


B# is a primary key. Although, BName is unique, not null but it is not
short.
Example-3: Customer (cname, citizenid, address, city, telno)
This relation indicates the information about personal details. There is a
chance that cname is duplicated, some may have citizenid and telno as null.
This forces us to introduce new a attribute such as cust# that would be a
primary key.
Customer (cust#, cname, citizenid, address, city, telno)
Example-4:
BankBranch(Branch-Name, City, TotalAsset)
What will be a PK that you will suggest?
What is a candidate key?
Which attribute is unique
There may be many branches in one city then finalize this relation with possible
constraints

CS203 Database Systems 23


Primary Keys cont…

CS203 Database Systems 24


Primary Key Instances -
Samples
512235665 Social Security Number
610112654 (By Birth Asian, Europian, HomeLand)
2003389 Student ID (With Registration year)
DMM051 Product ID (with Manufacturing Loc, branches)
JUB004
312668852369 Bank Account# (followed by Branch Code)
100211 SKU# (By Category) or a barcode
300411
Serial# Serial# starts with 1 and incremented by 1
005384 Company has many branches with code 005, 007 etc.,
007004 invoices issued from branches are recognized with branch codes

CS203 Database Systems 25


Use of PK or Indexing

CS203 Database Systems 26


Surrogate Keys

A surrogate key as an artificial column added to


a relation to serve as a primary key:
DBMS supplied
Short, numeric and never changes – an ideal primary key!
Has artificial values that are meaningless to users
Normally hidden in forms and reports

CS203 Database Systems 27


Surrogate Keys cont….

RENTAL_PROPERTY without surrogate key:


RENTAL_PROPERTY (Street, City,
State/Province, Zip/PostalCode, Country, Rental_Rate)

RENTAL_PROPERTY with surrogate key:


RENTAL_PROPERTY (PropertyID, Street, City,State/Province,
Zip/PostalCode, Country, Rental_Rate)
Other examples include Invoice#, ComplaintRef#
ATMTransaction (Card#, Serial#, Amount, DrawDate)

CS203 Database Systems 28


Surrogate Keys cont….

Other examples include


Invoice#

ComplaintRef#

ATMTransaction (Card#, Serial#, Amount, TransDate,


TransType)

InsurancePaid(Policy#, PaidYear, PaidDate, VehicleReg#,


Amount)

CS203 Database Systems 29


ATM Transaction Sample

CS203 Database Systems 30


Surrogate Keys cont….

CS203 Database Systems 31


Surrogate Keys cont….

CS203 Database Systems 32


Example – End User View

CS203 Database Systems 33


Foreign Keys (FK)
A foreign key is an attribute that refers to a primary
key of same or different relation to form a link
(constraint) between the relations:
• A foreign key can be a single column or a composite key
• The term refers to the fact that key values are foreign to the relation in which
they appear as foreign key values
• Ideally Data type, Length/ Size of FK and referring PK must be same

CS203 Database Systems 34


Foreign Keys

NOTE: The primary keys of the relations are underlined and any foreign
keys are in italics in the relations below:
Name of FK column may be different from the name of referencing PK

Example-1:
DEPARTMENT (DeptID, DepartmentName, BudgetCode,
ManagerName)
EMPLOYEE (EmployeeNumber, EmployeeName, DeptID)

CS203 Database Systems 35


Customer-Orders

Example-2:
CUSTOMER(CustId, Name, Address, Telno, Email)
ORDERS(Invoice#, InvType, InvDate, CustomerId)

Can we call Invoice# as a Surrogated Key?


Does Invoice# always contain serial numbers?

CS203 Database Systems 36


Foreign Key with a
Referential Integrity Constraint

CS203 Database Systems 37


Foreign Key with a
Referential Integrity Constraint

CS203 Database Systems 38


Creating Foreign Key in
MS Access & Oracle

CS203 Database Systems 39


FK and Recursive Relationship

CS203 Database Systems 40


Recursive Relationship -
Example

Part (Part#, PName, Weight, MakeYear, Price, PType, RefPart#)


PType: Assembled product/ parts (e.g., Motorcycle) or single part

CS203 Database Systems 41


The Referential Integrity
Constraint
A referential integrity constraint is a
statement that limits the values of the foreign key
to those already existing as primary key values in
the corresponding relation

CS203 Database Systems 42


Foreign Key with a
Referential Integrity Constraint
NOTE: The primary key of the relation is underlined and any
foreign keys are in italics in the relations below:

ITEM (Item#, Item_Name, Department, Buyer)


ORDER_ITEM (OrderNumber, Item#, Quantity, Price, ExtendedPrice)

Where ORDER_ITEM.ITEM# must exist in ITEM.ITEM#


OrderNumber is an alternate name of InvoiceNumber

CS203 Database Systems 43


Integrity (Fig. from C.J. Date)

S# SNAME STATUS CITY S# P# QTY


S SP
S1 Smith 20 London S1 P1 300
S2 Jones 10 Paris S1 P2 200
S3 Blake 30 Paris S1 P3 400
S4 Clark 20 London S1 P4 200
S5 Adams 30 Athens S1 P5 100
S1 P6 100
S2 P1 300
P# PNAME COLOR WEIGHT CITY S2 P2 400
P P1 Nut Red 12 London S3 P2 200
P2 Bolt Green 17 Paris S4 P2 200
P3 Screw Blue 17 Rome S4 P4 300
P4 Screw Red 14 London S4 P5 400
P5 Cam Blue 12 Paris
P6 Cog Red 19 London
CS203 Database Systems 44
Master/ Parent – Child Tables
Master Tables
DEPARTMENT (DeptID, DeptName, BudgetCode, ManagerName, Loc, TelNo)
CUSTOMER(CustId, Name, Address, Telno, Email)
Part (Part#, PName, Weight, MakeYear, Price, PType, RefPart#)
Product (Item#, Name, Price, Department, Buyer)
Course(CourseID, Name, CrHrs)
Semester(SemID, StartDate, EndDate, SGPA)
Student(StuId, FName, LName, DOB, NIC#, Address, City, Telno, Deptno)
Patient(PatID, FName, LName, DOB, Address, City, Telno, NIC#, PType)
ATM(Machine#, MName, MType, Size, Brand, Colour)

Some of the above table’s and their Child Tables (FK with and without Comp. Keys)
CourseReg(StuID, SemID, CourseID, Sec#, Grade)
PatientVisit(PatID, DependSNO, VisitSNO, VDate, DocID, Diagnosis, VStatus)
EMPLOYEE(EmpNo, EmpName, DOB, Addr, City, TelNo, Job, HireDate, DeptID)
ATMTransaction (Card#, Serial#, Amount, TransDate, TransType, Machine#)

CS203 Database Systems 45


Integrity Rules

Entity integrity
No two rows with the same primary key value
No null values in a primary key
Referential integrity
Foreign keys must match candidate key of source table
Foreign keys in some cases can be null
The database must not contain any unmatched foreign key values.
If B references A, A must exist.

CS203 Database Systems 46


Referential Actions (Cont.)
When we update a tuple from a table, say S, that is
referenced by another table, say SP,
There are similar choices of referential actions:
ON UPDATE CASCADE
ON UPDATE RESTRICT

There could be other choices besides these three. , e.g.,


ON UPDATE SET DEFAULT.
ON UPDATE SET NULL.

CS203 Database Systems 47


Composite Keys

CS203 Database Systems 48


Composite Keys …
Course(CourseID, Name, CrHrs)
Semester(SemID, StartDate, EndDate)
Student(StuId, FName, LName, DOB, …)

Courses are issued or allocated for registration
CourseIssue(SemID, CID, Sec#, InstID, B#, R#, Days, Time, MaxStu)

Students will register in issued courses:


CourseReg(StuID, SemID, CID, Sec#, Grade)
(Student can see instructor of course by using SQL, SemID, CID and
Sec# will be FK refer to CourseIssue)

CS203 Database Systems 49


Meaningful Learning
Analysis and Semantics
Evaluations

Semantics: Creating two tables with relevant data


Analysis: Y.x=1 cannot be mapped exactly with
X.x records 1, 3 or 1, 6

Evaluations: Default FK Y.x cannot be created.


Trigger can be created on Y.x to make sure Y.x will
be a subset of X.x. Otherwise, data can be put in Y.x
which does not belong to or mapped to X.x.

CS203 Database Systems 50


Composite Key…

Meaningful Learning?

CS203 Database Systems 51


Composite Key …

How data is shown to a user?

How data is stored in a table?

CS203 Database Systems 52


Composite Keys and Foreign
Keys
PatientVisit(PatID, VisitSNO, Vdate, DocID,
Diagnosis,…)

LabTest(PatID, VisitSNO, TestID, Tdate, …)

LabTest has FKs PatID, VisitSNO, referring to same


corresponding composite keys in PatientVisit

CS203 Database Systems 53


Indexing Columns – Comp. Keys

CS203 Database Systems 54


Other Constraints
Null: Represents a value of an attribute that is currently unknown or is no
applicable for this tuple, means nothing.

Entity integrity Constraint: In a base relation, no attribute of a primary


key can be null.

Referential Integrity Constraint: If a foreign key exists in a relation,


either the foreign key value must match a candidate key (or PK) value of
some tuple in its home relation or the foreign key value must be wholly null.

Domain Constraint: Specifies that the value of attribute A must be an


atomic value from the domain DOM(A).

Additional Constraints: Default, NOT NULL, UNIQUE, CHECK; Business


Constraints using DB triggers and client’s programming. For performance
Indexes are also constraints.

CS203 Database Systems 55


Type or Domain Constraints
(Cont.)
The check clause in SQL permits domains to be restricted:
Use check clause to ensure that an hourly-wage domain
allows only values greater than a specified value.
create domain hourly-wage numeric(5,2)
constraint value-test check(value > = 4.00)
The domain hourly-wage is declared to be a decimal
number with 5 digits, 2 of which are after the decimal
point
The domain has a constraint that ensures that the hourly-
wage is greater than 4.00

CS203 Database Systems 56


Attribute Constraints
An attribute constraint is just a declaration to the effect that
a specified attribute is of a specified type.
For example:
create table account
(branch-name char(15),
account-numberchar(10) not null,
balance integer,
……)
Attribute constraints are part of the definition of the
attribute.
Any attempt to introduce an attribute value into the
database that is not a type of the relevant type will simply
rejected.
Such a situation should never arise.

CS203 Database Systems 57


Table Constraints

A table constraint is a constraint on an individual table.

Example:
Suppliers in London must have status 20.
Two attributes, CITY and STATUS, of table S are involved.

CS203 Database Systems 58


Other Constraints Examples
Unique constraint can be applied on single or multiple columns

CHECK (Max_Participants>=Enrolled_Participants)
CHECK (Deptno>0)
CHECK (RelType IN ('S', 'D', 'W'))
Default 'Y‘
Database Triggers

CS203 Database Systems 59

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