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

Lecture 3

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

Lecture 3

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

Database System

Saba Ghani
FAST-NU
Chiniot-Faisalabad campus
Week 3

► Relational Data Model.


► Relational Database Constraints.
► Relational Data Model Constraints.
► Relational Data base Schema
► Explain data design terminology
► entities, fields, records, files, tables, and key fields, constraints
Relational data model

► The relational model represents the database as a collection of


relations.
► The model was first proposed by Dr. E.F. Codd of IBM in 1970 in the
following paper:
"A Relational Model for Large Shared Data Banks," Communications of
the ACM, June1970.
Relational data model
Terminologies
► Relation
► a table with columns and rows.
► Attribute
► a named column of a relation.
► Domain
► a set of allowable values for one or more attributes.
► Tuple
► a row of a relation.
► Degree
► number of attributes in a relation.
► Cardinality
► number of tuples/records in a relation.
► Relational database
► collection of relations.
Example of Relation
Relational database

► Key of a Relation:
► Each row has a value of a data item (or set of items) that uniquely
identifies that row in the table
►Called the key
► In the STUDENT table, SSN is the key
Relational Database Schema
► Relational Database Schema:
► A set S of relation schemas that belong to the same database.
► S is the name of the whole database schema
► S = {R1, R2, ..., Rn}
► R1, R2, …, Rn are the names of the individual relation schemas within the database S
► Following slide shows a COMPANY database schema with 6 relation schemas
Relational Database Schema
Schema of Relational database

► The Schema (or description) of a Relation:


► Denoted by R(A1, A2, .....An)
► R is the name of the relation
► The attributes of the relation are A1, A2, ..., An
► The relation state r(R) refers to the present set of data/tuples in the relation.
► Example:
CUSTOMER (Cust-id, Cust-name, Address, Phone#)
► CUSTOMER is the relation name
► Defined over the four attributes: Cust-id, Cust-name, Address, Phone#
► Each attribute has a domain or a set of valid values.
► For example, the domain of Cust-id is numbers.
► A CUSTOMER state may include 100 CUSTOMERS; another 250 CUSTOMERs
Schema of Relational database

► All tuples in a relation state r(R) form a set.


► By definition, there cannot be duplicates, or identical tuples, in a
set.
► By definition, set elements (tuples) are not ordered, even though
tuples frequently appear to be in the tabular form.
► Ordering of attributes however is important
► We will consider the attributes in R(A1, A2, ..., An) and the values
in t=<v1, v2, ..., vn> to be ordered .
Characteristic of Relations

► Each relation in the same relational database schema has a distinct


name
► Each value in a tuple is
1. atomic
2. No Composite or multi valued attribute
► A special null value is used to represent values that are not available.
► 2 reasons in reality:
► Unknown
► Inapplicable
Characteristic of Relations

► 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.
Keys
► A key is a combination of one or more columns that is used to identify rows in
a relation
► They allow you to find the relation between two tables
► It is used to fetch or retrieved records/data-row from data table
► Types are
► Primary key
► Composite Key
► Candidate key
► Foreign key
► Secondary key
► Surrogate key
► Super key
Keys

► A Primary Key is a key that uniquely identify record in relation.


► it has two characteristics
1. Unique
2. Not Null

► There is one and only one primary key per relation


► The primary key is unique and not null.
► The ideal primary key is short, numeric(alpha), fixed length and never changes
► The primary key may be a composite key.
Primary Key
Primary Key
Keys

► A composite key is a key that consists of two or more columns.


► The combination of columns guarantees uniqueness, though individual uniqueness
is not guaranteed.
Keys
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)
Keys
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 unique,
and it is called the primary key
⮚ This is usually the best among the candidate keys to use for identification.

Candidate Keys

AccountN CNIC ContactNo Address Name Amount


o
Keys

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
Surrogate Key

► A surrogate key is an artificial column added to a relation to serve as a


primary key:
► It does not have contextual or business meaning.
► Oracle uses SEQUENCE, MySQL uses AUTO_INCREMENT and SQL server uses
IDENTITY to generate surrogate 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
► These kind of key are unique because they are created when you don’t have any natural
primary key.
Keys
► A foreign key is an attribute that refers to a primary key of a different relation
(parent/master) to form a link 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
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)
Super key
► A super key is a group of single or multiple keys which identifies rows in a table. A
table can have many Super Keys.
► Can be called composite key.
► A Super key may have additional attributes that are not needed for unique
identification.
► E.g. of Super Key
► ID
► ID, Name
► ID, Address
► ID, Department_ID
► ID, Salary............ So on
► A primary key can also be a super key

You might also like