0% found this document useful (0 votes)
23 views31 pages

DB - Chapter 2

Uploaded by

Muhammad Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views31 pages

DB - Chapter 2

Uploaded by

Muhammad Shahbaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Relational Database

Structure of Relational Databases

The relational database uses a collection of tables to represent both data and the
relationships among those data
Table/Relation

● Tables are database objects that contain all the data in a database.
● In tables, data is logically organized in a row-and-column format similar
to a spreadsheet.
Relation : Employee

E_ID E_NAME E_CNIC E_SALARY E_CONTACT E_EMAIL

101 Ali 32203-8659771-5 50000 0347-5628911 [email protected]

102 Fahad 32403-5715124-9 37000 0308-1894104 [email protected]

103 Fatima 34101-0663098-9 48000 0302-5810306 [email protected]

104 Usama 35201-4559315-5 60000 0300-1749103 [email protected]

105 Fahad 35201-3226346-7 40000 0334-2754819 [email protected]


Row/Record/Tuple

Each row represents a unique record in a table.

Example:
The Employee table contain the following row:
101, Ali, 32203-8659771-5, 50000, 0347-5628911, [email protected]
Column/Attribute/Field

● A column is a list of values, usually belonging to a particular field,


displayed vertically in a table.
● Example:
E_ID, E_NAME, E_CNIC, E_SALARY, E_CONTACT, E_EMAIL
Relation : Employee
Column/Attribute/Field

E_ID E_NAME E_CNIC E_SALARY E_CONTACT E_EMAIL

101 Ali 32203-8659771-5 50000 0347-5628911 [email protected]

102 Fahad 32403-5715124-9 37000 0308-1894104 [email protected]

103 Fatima 34101-0663098-9 48000 0302-5810306 [email protected]

104 Usama 35201-4559315-5 60000 0300-1749103 [email protected]

105 Fahad 35201-3226346-7 40000 0334-2754819 [email protected]

Row/Record/Tuple
Degree, Cardinality

● The number of attributes in the relation is known as the degree of the


relation.
● The number of tuples in a relation is known as cardinality.
Relation Instance

● The set of tuples of a relation at a particular instance of time is called


a relation instance.
● The example table shows the relation instance of EMPLOYEE at a
particular time. It can change whenever there is an insertion, deletion,
or update in the database.
How will you increase the salary of
Fahad by 10%?
Relation : Employee

E_ID E_NAME E_CNIC E_SALARY E_CONTACT E_EMAIL

101 Ali 32203-8659771-5 50000 0347-5628911 [email protected]

102 Fahad 32403-5715124-9 37000 0308-1894104 [email protected]

103 Fatima 34101-0663098-9 48000 0302-5810306 [email protected]

104 Usama 35201-4559315-5 60000 0300-1749103 [email protected]

105 Fahad 35201-3226346-7 40000 0334-2754819 [email protected]


Key

● A key refers to an attribute/set of attributes that help us identify a row


uniquely in a table.
● A key is also used when we want to establish relationships between the
different columns and tables of a relational database.
Types of Keys

● Super Key ● Foreign Key


● Candidate Key ● Compound Key
● Primary Key
● Alternate Key
● Surrogate Key
● Unique Key
● Composite Key
Super Key

● Like a superset
● All possible keys that can be formed in a relation.
● Should be unique and not contain null values.
● May contain extraneous attributes.
Super Key

● Example:
{E_ID}, {E_CNIC},
{E_ID, E_CNIC} , {E_ID, E_NAME}, {E_ID, E_PHONE}, {E_NAME,
E_PHONE},{E_ID, E_EMAIL}
{E_NAME, E_CNIC, E_PHONE}, {E_ID, E_NAME, E_PHONE}
…..
Candidate Key

● Minimal superkey are called candidate keys.


● Have “no redundant attributes”
● Being the “minimal representation of the tuple”
● Example:
{E_ID}, {E_CNIC}
Primary Key

● A primary key is the column or columns that contain values that


uniquely identify each row in a table.
● Chosen from set of candidate key.
● DBA decides the primary key according to nature of database.
● Example:
E_ID
Rules For Defining the Primary Key

● Minimal
● Accessible
● NON NULL Value
● Time Invariant
● Unique
Alternate Key

● A candidate key that is not selected as the primary key.


● Serve as an alternate method to uniquely identify the records.
● Example:
E_CNIC
Unique Key

● A column or set of columns that uniquely identify each record in a table


● It can contain null values.
● Example:
E_CONTACT, E_EMAIL
Surrogate Key

● Synthetic Primary Key.


● Used when we do not have a natural primary key in a table.
● It is generated when a new record is inserted into a table automatically
by a database and can be declared as the primary key of that table.
● Example:
ORDER_ID in an order table. Whenever a new record is created it’s
order number is automatically incremented.
Relation : Department

● We may want to store information


D_ID D_NAME LOCATION regarding which employee works in
41 IT Karachi which department.
42 HR Islamabad ● The data is in two separate table.
● How to connect them?
43 Marketing Karachi
Foreign Key

● A foreign key generates a relationship between the parent and host


tables.
● Foreign key is an attribute which is a Primary key in its parent table, but
is included as an attribute in another host table.
● It acts as a cross-reference between tables.
● Example:
D_ID
Establishing a Relationship
Foreign Key
E_ID E_NAME E_CNIC E_SALARY E_CONTACT E_EMAIL D_ID

101 Ali 32203-8659771-5 50000 0347-5628911 [email protected] 42

102 Fahad 32403-5715124-9 37000 0308-1894104 [email protected] 43

103 Fatima 34101-0663098-9 48000 0302-5810306 [email protected] 43


Employee

104 Usama 35201-4559315-5 60000 0300-1749103 [email protected] 41

105 Fahad 35201-3226346-7 40000 0334-2754819 [email protected] 42

Primary Key
D_ID D_NAME LOCATION

Department
41 IT Karachi

42 HR Islamabad

43 Marketing Karachi
Maintaining Data Integrity

E_ID E_NAME E_CNIC E_SALARY E_CONTACT E_EMAIL D_ID

101 Ali 32203-8659771-5 50000 0347-5628911 [email protected] 42

102 Fahad 32403-5715124-9 37000 0308-1894104 [email protected] 43

103 Fatima 34101-0663098-9 48000 0302-5810306 [email protected] 43

104 Usama 35201-4559315-5 60000 0300-1749103 [email protected] 41


Employee

105 Fahad 35201-3226346-7 40000 0334-2754819 [email protected] 42

106 Alishba 34101-0402659-3 45000 0345-1480345 [email protected] 45


m

Can we add a new record with D_ID 45? D_ID D_NAME LOCATION

Department
41 IT Karachi

42 HR Islamabad

43 Marketing Karachi
Referential Integrity

● Referential integrity is a data quality concept that ensures that when you
make changes to data in one place, those changes are reflected in other
related records.
Referential Integrity

Referential integrity refers to the relationship between tables. There are


two referential integrity constraint:
● Insert Constraint: Value cannot be inserted in CHILD Table if the
value is not lying in PARENT Table
● Delete Constraint: Value cannot be deleted from PARENT Table if the
value is lying in CHILD Table
Relation: Project

P_ID P_NAME Start_Date End_Date E_ID

01 Redesign 2024-02-15 2024-04-30 104


Website
02 Marketing 2024-03-01 2024-06-15 102
Campaign
02 Marketing 2024-03-01 2024-06-15 103
Campaign

What will be primary key in this relation?


Composite Key

● A composite key is a key that is composed of two or more attributes and


is capable of uniquely identifying a table or a relation.
● Example:

{P_ID, E_ID}
Relation: Department_Manager

E_ID D_ID Start_Date End_Date


104 41 2024-02-15 2024-04-30

105 42 2024-03-01 2024-06-15

103 43 2024-01-03 2024-04-15

What will be primary key in this relation?


Compound Key

● A compound key is a composite key that is composed of two or more


attributes which are foreign keys.
● Example:

{P_ID, E_ID}

You might also like