Database Mid Sem
Database Mid Sem
INTRODUCTION TO DATABASE
SYSTEMS
Learning Objectives
3
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Basic terminologies
Database and Database system
An organization contains different set of data.
The organization must have accurate and reliable data
for effective decision making.
An organization can not run without data.
So the data in the organization should be maintained
properly.
Database is used to maintain the data in proper way.
A data base is a collection of data and a database
system is computerized record keeping system.
The data are stored in the secondary storage device
(permanent Storage) in the form of files, tables, objects,
etc. 4
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Basic terminologies
5
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Basic terminologies
6
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Basic terminologies
7
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Basic terminologies
8
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
1. Redundancy can be reduced
When data are redundant, we face two problems.
They are
i. Wastage of spaces in the disk
ii. Inconsistency problem
In data base system, redundancy can be avoided with a
mechanism called as Normalization.
Normalization is the procedure which removes the
unwanted duplicate data by splitting the table into
two or more tables. For Eg.
Assume that a table consists of the data of the students
who are doing a particular course.
9
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
Student table
10
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
The above table is split into two tables (Student and Staff)
as follows:
Student Staff
Stu# StuName dept# Coun# Coun# CounName
S101 Ram D1 ST002
S102 Bala D2 ST002 ST001 Dr. Venkat
S111 Madhu D1 ST001 ST002 Dr. Jackson
S122 Divya D1 ST001
S115 Hema D2 ST001
S103 Sachin D1 ST001
Duplicate data related to staff is reduced
11
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
2.Inconsistency is avoided
If the value of the attribute shows similar value when it
is accessed by two different applications, then data is
said to be Consistent.
Inconsistency may arise if they are redundant.
When duplication is avoided, there is no possibility of
Inconsistency (Single copy has a single value only).
Though multiple copies are available and when a
transaction accesses any one of the copy, a
procedure in data base system called as “Propagating
updates“ updates the latest value in all the copies.
It avoids inconsistency.
12
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
3.Data can be shared
Concurrently, data can be accessed by more than one
application.
Database which share data to multiple applications is
called as the distributed data base.
Distributed data base creates multiple mirror image
copies of data and are shared with different
applications that are executed in different locations.
When any modification is done on the mirror image
copies, they automatically updated in the original copy
of the data.
Example: Railway reservation system shares the data to
different reservation counters.
13
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
4.Data can be integrated
Data that exist in different files can be integrated
(Combined) to answer the query of an application.
Example:
Assume that tables Student and Staff consist of the data
about the students and staff who handle various
courses.
To answer a query , “List the students who have scored
more than 90% of marks in various subjects and the
information of the staff who handled the subject”, we
need the data from both the tables (Student and
Staff)
14
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
5. Security mechanism can be applied
Confidential data should be secured from unauthorized
users.
To protect them from unauthenticated access, data
base systems support a security mechanism known
as Control access mechanism.
The control mechanism is divided into two types such as
Software Controlling mechanism and hardware
control mechanism.
Software Control mechanism – Data are protected
with the help of a software.
15
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
Examples:
a. Authentication procedure which asks login name and
password to distinguish authorized and others.
b. Encryption procedure which encrypts the data into
unknown format known as Cipher text so that it can be
understood by the intruders
Hardware Control mechanism – Data are permitted to
be accessed if the user should use physical component.
Example:
Usage of Access Entry card to enter into the prohibited
area.
16
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
6.Integrity can be maintained
The problem of integrity is the problem of ensuring that
data in the database is correct.
When the input values (data) are given to be added into
the database, there is a possibility of giving invalid
values.
Invalid values create a lot of problem in the data base
management.
Some conditions called as integrity constraints in
Database system can be enforced at the time of
creating the schema of the table.
These constraints check whether the inputs given by the
user are valid or not. 17
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
If they are invalid, the constraints produce appropriate
error messages without storing the invalid values in to
the table.
So there is no possibility of keeping Invalid values in the
table.
Example: Assume that a table is created for keeping the
information about employees working in hourly basis
and one of the field is HOURS_WORKED which is used
to calculate the wage.
We know that the field may have the value from 0 to 24.
If invalid value is given for the field constructing the
record, database system displays the error message if
integrity constraints are set.
18
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
7.Conflicting requirements can be balanced
We know that data base can be accessed by more than
one users and applications.
They may require different storage structures and access
methods.
DBA chooses the best storage structure and access
method so that performance of the system is
improved.
8.Data Independence
In the conventional file system, all the database are
dependent data.
19
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
When the modification is to be performed on the storage
structure, or if we need to make changes in the application of
the data base also, the data is said to be dependent data.
If the application does not require any updating, the data is said
to be independent data. Database system supports data
dependence.
There are two types of data independence. They are
i. Logical data independence
ii. Physical data independence
20
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Advantages of Database
System
Logical data independence - When the modification is
performed on the application of the data base, If we
do not need any change in the storage structure,
then the data dependence is called as Logical data
independence
21
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
Components of a database system
1. Data – Data which can be shared between multiple
applications and integrated for a single application.
Example for Sharing, the Faculty file can be accessed by
the HR department application related to the
promotion or for doing some other HR related
operations.
Same data can be accessed by the application of the
Finance department to prepare payroll.
Example for integrating, Faculty and student files are to
be accessed for preparing the list which requires data
of both students and Faculty member.
22
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
2. Hardware – The hardware components of the system that
consists of
a. SSD (Secondary Storage Device) such as Magnetic tape,
Disk to hold data permanently
b. Processors for executing data(ALU and Control Unit)
c. Main memory for executing and storing data temporarily
d. Network – the mechanism to connect more than one
Computers
3. Software – It is a software which is used for performing
the operation on the database.
It is called as the database management system (DBMS).
23
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
The requests from the users to access the data in the
database like adding / removing data, retrieving data
and updating data are handled by the DBMS.
4. Users – The users in the database system are
classified into four types as Application
Programmers, Naïve users, Sophisticated Users and
database administrator.
Application Programmers – They are the computer
professionals who write the application program in a
database language or a programming language to
access the data in the database.
24
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
Naïve Users are unsophisticated users who interact with
the system by invoking one of the application programs
that have been written by the application programmer.
Sophisticated Users are the users who interact without
writing programs.
Instead, they form their requests in a query language.
They submit the query to the tool called as query
processor.
The query processor breaks the query into multiple
instructions that the storage manager can understand.
25
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
Database Administrator (DBA) is the person who has a
central control over the data and the programs that
can access the data.
The functions of the DBA include
• Schema definition – Schema represents the overall
structure of the database.
DBA is responsible for deciding what fields are to be
used, their data types and their length.
• Storage structure and access method – DBA is
responsible for deciding how the data are to be
stored in the database (disk) and the way in which the
data can be accessed (Sequential and random)
26
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
• Modification in Schema definition and storage structure
–
DBA is responsible for making any changes in the data
structure of the database and storage structure
• Granting authorization for data access – DBA has the
rights to grant the permission to the users who are
authorized to access the data
• Performing periodical operations – DBA is responsible for
performing routine maintenance activities such as
- Doing back up periodically for preventing loss of data
- Monitoring the performance
- Ensuring that enough free disk space is available
27
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Architecture of Database
system
naïve users application Sophisticated DBA
programmers Users
29
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
Database systems comprise of complex data structures.
Thus, to make the system efficient for retrieval of data
and reduce the complexity of the users, developers
use the method of Data Abstraction.
There are mainly three levels of data abstraction:
Internal Level: Actual PHYSICAL storage structure and
access paths.
Conceptual or Logical Level: Structure and constraints
for the entire database
External or View level: Describes various user views
30
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
31
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
Conceptual Level
Internal Level
32
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
External (PL/I) External (COBOL)
External Level DCL 1 EMPP 01 EMPRECORD
2 Emp# char (6) 02 Eno pic x(6)
2 Sal fixed Bin(5) 02 DeptNo pic x(20)
33
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
The purpose of each level is given below
Internal Level/Schema
The internal schema specifies the physical storage
structure of the database.
The internal schema is a very low-level representation of
the entire database.
It contains multiple occurrences of multiple types of
internal record.
These records are also called "stored records”.
Facts about Internal schema:
• The internal schema is the lowest level of data
abstraction
34
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
• It helps us to keep information about the actual
representation of the entire database.
• It is like the actual storage of the data on the disk in the
form of records
• The internal view tells us what data is stored in the
database and how they are stored
• It never deals with the physical devices.
• Instead, internal schema views a physical device as a
collection of physical pages
Conceptual Schema/Level
The conceptual schema mentions the Database structure
of the whole database for the community of users.
35
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
This schema hides information about the physical
storage structures and focuses on describing data
types, entities, relationships, etc.
This logical level lies between the user level and physical
storage view.
However, there is only single conceptual view of a single
database.
Facts about Conceptual schema:
• Defines all database entities, their attributes, and
their relationships
• Security and integrity information
36
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
• In the conceptual level, the data available to a user must
be contained in or derivable from the physical level
External Schema/Level
An external schema specifies the part of the database which
specific user is interested in.
It hides the unrelated details of the database from the user.
There may be "n" number of external views for each
database.
Each external view is defined using an external schema,
which consists of definitions of various types of external
record of that specific view.
37
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
An external view is just the content of the database as it
is seen by some specific particular user.
For example, a user from the sales department will see
only sales related data.
Facts about external schema:
• An external level is only related to the data which is
viewed by specific end users.
• This level includes some external schemas.
• External schema level is nearest to the user
• The external schema describes the segment of the
database which is needed for a certain user group
and hides the remaining details from the database
from the specific user group. 38
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
Goal of three level schema of Database
Here, are some Objectives of using Three schema
Architecture:
• Every user should be able to access the same data
but able to see a customized view of the data.
• The user need not to deal directly with physical
database storage detail.
• The DBA should be able to change the database
storage structure without disturbing the user's views.
• The internal structure of the database should remain
unaffected when changes made to the physical
aspects of storage.
39
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
Advantages of Database Schema
• We can manage data independent of the physical
storage
• Faster Migration to new graphical environments
• DBMS Architecture allows us to make changes on the
presentation level without affecting the other two
layers
• As each tier is separate, it is possible to use different
sets of developers.
• It is more secure as the client doesn't have direct
access to the database business logic
• In case of failure of one-tier, no data loss is acquired,
as we are always secure by accessing the other tier. 40
Database Design, Lecture-1 BITS Pilani, Pilani Campus
DBMS Schemas: Internal,
Conceptual, External
Disadvantages Database Schema
• Complete DB Schema is a complex structure which is
difficult to understand
• Difficult to set up and maintain
• The physical separation of the tiers can affect the
performance of the Database
41
Database Design, Lecture-1 BITS Pilani, Pilani Campus
Thanks
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
Database Design 3 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Relationship Model
Database Design 4 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ERD
Database Design 5 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity, Entity Set Attributes
Database Design 6 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Examples:
An entity STUDENT has the following attributes
StName (Student Name), Stu# (Student Number) , age and
dep(Department Name)
Database Design 7 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Database Design 8 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Relationship Model
9
Database Design 9 BITS Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Ent it y R elat ionshi p Model
•3. Relationship
•A relationship represents the association between two or more entities.
•Example: Assume that there are two entity sets: Customer and Loan as
Database Design 10 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Relationship Model
Database Design 11 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Relationship Model
Customer
Cust# Cust Name Address Balance(Rs)
C101 Mr.Ram Chennai-5 28235
C112 Mr.Raj Chennai-12 3140
C109 Mr.SreeRam Chennai-1 112
C107 Mr.Bala Chennai-2 12345
Database Design 15 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity
Database Design 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Existence Dependencies
Database Design 17 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Weak Entity
• A weak entity does not have a primary key of its own
• You can use a combination of the primary key of the dominant entity & an
attribute of the weak entity as the primary key of the weak entity.
• Thus a discriminator of Weak Entity + Prime attribute of the its Dominant
Entity. Emp_id
EMPLOYEE Emp_id+Nom_id+Relation
Has NOMINEE
Database Design 18 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relationships
• Eg:
Database Design 19 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Participation of an entity set in a relationship
Database Design 20 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Total & Partial Participation
amount
Soc SNo loan-no
Cust-name
cust-city
Database Design 21 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Degree of Relationship
Unary : Degree 1 EMPLOYEE
manages
Ternary : Degree 3
ITEM
ORDER Has
Order_dtls
N-ary : Degree n
Further Reading: https://www.gatevidyalay.com/tag/total-and-partial-participation-in-dbms/
Database Design 22 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Cardinality Ratio/ Mapping Cardinality
•Mapping Cardinality express the number of entities to which another entity can be
associated via a relationship set.
•Once the entities & relationship between the entities is identified you can represent
them in a ER diagram
Database Design 23 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER Example
Database Design 24 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER …(contd.)
No of hrs is an attribute on the relation “assigned to” between EMP and PROJECT
Database Design 25 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Relationship Model
Database Design 32 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Enhanced-ER (EER) Model Concepts
Database Design 34 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Attribute Inheritance in Superclass / Subclass Relationships
•An entity that is member of a subclass inherits all attributes of the entity
as a member of the superclass
•It also inherits all relationships.
Database Design 35 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Specialization
Database Design 36 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Specialization
Database Design 37 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example of a Specialization
Database Design 38 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Generalization
Database Design 39 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Constraints on Specialization and Generalization
Database Design 41 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Constraints on Specialization and Generalization
Database Design 42 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Constraints on Specialization and Generalization
2. Completeness Constraint:
– Total specifies that every entity in the superclass must be a member of some subclass in
the specialization/ generalization
– Shown in EER diagrams by a double line
– Partial participation − Not all entities are involved in the relationship.
– Partial participation is represented by single lines.
Database Design 43 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Constraints on Specialization and Generalization
Database Design 44 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example of disjoint partial Specialization
Database Design 45 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Categories (UNION TYPES – Multiple Inheritance)
Database Design 47 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example of
categories
(UNION
TYPES)
Database Design 48 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
UML Example for Displaying Specialization /
Generalization
Database Design 49 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Database Design 50 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Database Design
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
Database Design 2 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Session Outline
Which type of entity cannot exist in the database unless another type of
entity also exists in the database?
A. Weak entity
B. Strong entity
C. Dependent entity
D. Independent entity
Answer: A – Weak Entity - An entity that depends on some other entity for
its existence
Database Design 4 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Recap-QUIZ
Database Design 5 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Recap-QUIZ
Database Design 6 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Recap-QUIZ
Database Design 7 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Recap-QUIZ
• Generalization is
Database Design 8 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Recap-Exercise
We wish to create a database for BITS Data Science trainings. For this, we must store
data about the Participants and the Faculty. For each course participant, identified by an
ID, we want to store her social security number, surname, age, sex, place of birth,
employer’s name, address and telephone number, previous employers (and periods
employed), the courses attended and the final assessment for each course. If a participant
is self-employed, we need to know her area of expertise, and, if appropriate, her title. For
somebody who works for a company, we store the level and position held. Each course
has a code and a title and any course can be given any number of times. Each time a
particular course is given, we will call it an ‘instance’ of the course. For each instance, we
represent the start date, the end date, and the number of participants. For each Faculty,
we will show the SSN, surname, age, place of birth, the instance of the course currently
being taught, those taught in the past and the courses that the faculty is qualified to
teach. All the faculty’s telephone numbers are also stored. An instructor can be
permanently employed by BITS or can be a guest faculty.
1) Identify the entity sets.
2) Identify the relationships .
3) Identify the structural constraints of all the relationships
4) Identify the specializations /generalizations involved.
Database Design BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
9
Relational Model Concepts
Database Design 10 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Model Characteristics
Database Design 11 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Model Characteristics
Relation Schema is denoted by R (A1, A2, A3,……,An)
• Using the data type of each attribute, the definition is sometimes written as:
• STUDENT(Name: string, Ssn: string, Home_phone: string, Address:
string,Office_phone: string, Age: integer, Gpa: real)
Database Design 12 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Model Characteristics
Database Design 13 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relation instance
Database Design 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Model
• 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
Database Design 19 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Model - Keys
20
Database Design , Database Systems & Applications
20 ,Lecture 2, BITS
10-5-2020
Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Relational Model - keys
2. Composite or concatenated key
When a table has no primary key for unique identification, we can
combine more than one secondary keys to identify unique
record/tuple, the key is called as the Composite or Concatenated keys.
21
Database Design 21 BITS Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Relational Model - keys
22
Database Design 22 BITS Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Relational Model - keys
23
Database Design 23 BITS Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Relational Model - Rules
24
Database Design 24 BITS Pilani, Deemed to be University under Section
BITS Pilani,
3 of UGC
PilaniAct,
Campus
1956
Terminology
Database Design 25 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Constraints
Database Design 26 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Relational Integrity Constraints
• Constraints are conditions that must hold on all valid relation states.
• There are three main types of constraints in the relational model:
– Key constraints
– Entity integrity constraints
– Referential integrity constraints
• Another implicit constraint is the domain constraint
– Every value in a tuple must be from the domain of its attribute (or it could be
null, if allowed for that attribute)
Database Design 27 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Key Constraints
• Superkey of R:
– Is a set of attributes SK of R with the following condition:
• No two tuples in any valid relation state r(R) will have the same value for SK
• That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK]
• This condition must hold in any valid state r(R)
• Key of R:
• It is used to uniquely identify any record or row of data from the table.
• ID is used as a key in the Student table because it is unique for each student. In the
PERSON table, passport_number, license_number, SSN are keys since they are unique for
each person.
• It is also used to establish and identify relationships between tables.
– A key is a "minimal" superkey
– That is, a key is a superkey K such that removal of any attribute from K
results in a set of attributes that is not a superkey (does not possess the
superkey uniqueness property)
Database Design 28 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Key Constraints (continued)
• Example: Consider the CAR relation schema:
– CAR(State, Reg#, SerialNo, Make, Model, Year)
– CAR has two keys:
• Key1 = {State, Reg#}
• Key2 = {SerialNo}
– Both are also superkeys of CAR
– {SerialNo, Make} is a superkey but not a key.
• In general:
– Any key is a superkey (but not vice versa)
– Any set of attributes that includes a key is a superkey
– A minimal superkey is also a key.
Database Design 29 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Key Constraints (continued)
• If a relation has several candidate keys, one is chosen arbitrarily to be
the primary key.
– The primary key attributes are underlined.
• Example: Consider the CAR relation schema:
– CAR(State, Reg#, SerialNo, Make, Model, Year)
– We chose SerialNo as the primary key
• The primary key value is used to uniquely identify each tuple in a relation
– Provides the tuple identity
• Also used to reference the tuple from another tuple
– General rule: Choose as primary key the smallest of the candidate keys (in
terms of size)
– Not always applicable – choice is sometimes subjective
Database Design 30 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-1
Database Design 31 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity Integrity
• Entity Integrity:
– The primary key attributes PK of each relation schema R in S cannot
have null values in any tuple of r(R).
• This is because primary key values are used to identify the individual tuples.
• t[PK] null for any tuple t in r(R)
• If PK has several attributes, null is not allowed in any of these attributes
– Note: Other attributes of R may be constrained to disallow null
values, even though they are not members of the primary key.
Database Design 32 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Referential Integrity
Database Design 33 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Referential Integrity
Database Design 34 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Referential Integrity
Database Design 36 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Other Types of Constraints
Database Design 37 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Displaying a relational database schema and its constraints
Database Design 38 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
COMPANY DB-Relational Model
Database Design 39 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-2-Relational Model
Database Design 40 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-3-Relational Model
Database Design 41 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Operations on Relations and constraints
• The following operations update and modify the database
• INSERT a tuple.
• DELETE a tuple.
• MODIFY a tuple.
• Integrity constraints should not be violated by the update operations.
• Several update operations may have to be grouped together.
• Updates may propagate (AKA cascading) to cause other updates
automatically.
• This may be necessary to maintain integrity constraints.
Database Design 42 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Update Operations on Relations
Database Design 43 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Operations on Relations and constraints
Actions need to be taken when FK is set , on operations like update, insert, and
FK
delete. PK
R1 R2
a b c p q r
c in R1 refers to p in R2
• To insert a tuple in R1 where the value for c is not in p of R2, then don’t
allow.
Database Design 47 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Possible violations for each operation
• DELETE may violate only referential integrity:
– If the primary key value of the tuple being deleted is referenced from other
tuples in the database
• Can be remedied by several actions: RESTRICT, CASCADE, SET NULL
– RESTRICT option: reject the deletion
– CASCADE option: propagate the new primary key value into the foreign keys of the
referencing tuples
– SET NULL option: set the foreign keys of the referencing tuples to NULL
– One of the above options must be specified during database design for each
foreign key constraint
Database Design 48 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Possible violations for each operation
Database Design 49 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-4
• Suppose that each of the following Update operations is applied directly to
the database state shown below. Discuss all integrity constraints violated
by each operation, if any, and the different ways of enforcing these
constraints.
• Insert <‘Robert’, ‘F’, ‘Scott’, ‘943775543’, ‘1972-06-21’, ‘2365 Newcastle Rd, Bellaire, TX’, M,
58000, ‘888665555’, 1> into EMPLOYEE.
• Delete the EMPLOYEE tuple with Ssn = ‘123456789’.
Database Design 50 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-5
Suppose that each of the following Update operations is applied directly to
the database state shown below. Discuss all integrity constraints violated
by each operation, if any, and the different ways of enforcing these
constraints.
Database Design 51 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-6
Database Design 52 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-7
Database Design 53 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-8
Database Design 54 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise-9
• Suppose that each of the following Update operations is applied directly
to the database state shown below. Discuss all integrity constraints
violated by each operation, if any, and the different ways of enforcing
these constraints.
Give an example of an attribute (or set of attributes) that you can deduce
is not a candidate key, based on this instance being legal.
Is there any example of an attribute (or set of attributes) that you can
deduce is a candidate key, based on this instance being legal?
Database Design 56 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Summary
Presented Relational Model Concepts
– Definitions
– Characteristics of relations
Discussed Relational Model Constraints and Relational Database Schemas
– Domain constraints’ - It makes sure that the data value entered for that particular column matches with
the data type defined for that column .
– Key constraints - A unique constraint (also referred to as a unique key constraint) is a rule that forbids
duplicate values in one or more columns within a table.
– A primary key constraint is a column or combination of columns that has the same properties as a unique
constraint.
– Entity integrity - Entity integrity is concerned with ensuring that each row of a table has a unique and non-
null primary key value;
– Referential integrity - You can use a primary key and foreign key constraints to define relationships
between tables.
– The reference from a row in one table to another table must be valid.
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
ER to Relational Mapping
1.Mapping rules/guidelines for mapping ER
constructs
2.Mapping rules/guidelines for mapping
hierarchies
3.Examples
Database Design 3 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Entity
Relationship
Model:
COMPANY
database.
Database Design 4 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Database Design 5 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 1: Mapping of Regular Entity Types
• For each regular (strong) entity type E in the ER
schema, create a relation R that includes all the
simple attributes of E.
• Include only the simple component attributes of a
composite attribute
• Choose one of the key attributes of E as the primary
key for R
Database Design 6 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 1: Mapping of Regular Entity Types
• If the chosen key of E is a composite, then the set of
simple attributes that form it will together form the
primary key of R
• If multiple keys were identified for E during the
conceptual design, the information describing the
attributes that form each additional key is kept in
order to specify secondary (unique) keys of relation R.
Database Design 7 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Database Design 8 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 2: Mapping of Weak Entity Types
• For each weak entity type W in the ER schema with
owner entity type E, create a relation R and include all
simple attributes (or simple components of
composite attributes) of W as attributes of R.
• In addition, include primary key of E as foreign key
attributes of R (this takes care of mapping the
identifying relationship type of W)
• The primary key of R is the combination of the
primary key(s) of the owner(s) and the partial key of
the weak entity type W(if any)
Database Design 9 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Database Design 10 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 2: Mapping of Weak Entity Types
• If there is a weak entity type E2 whose owner is also a
weak entity type E1, then E1 should be mapped
before E2 to determine its primary key first
• It is common to choose the propagate (CASCADE)
option for the referential triggered action on the
foreign key in the relation corresponding to the weak
entity type, since a weak entity has an existence
dependency on its owner entity. This can be used for
both ON UPDATE and ON DELETE.
Database Design 11 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
• For each binary 1:1 relationship type R in the ER
schema, identify the relations S and T that correspond
to the entity types participating in R.
• There are three possible approaches to represent this
relationship types:
1) Foreign key approach
2) Merged relationship approach
3) Cross-reference or relationship relation approach
Database Design 12 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
1) Foreign key approach:
• Choose one of the relations—S, and include as a foreign
key in S the primary key of T
• It is better to choose an entity type with total
participation in R in the role of S
• Include all the simple attributes of the 1:1 relationship
type R as attributes of S.
Database Design 13 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
1) Foreign key approach:
Database Design 14 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
2) Merged relationship approach :
• Mapping of a 1:1 relationship type is to merge the two
entity types and the relationship into a single relation.
• This is possible when both participations are total, as
this would indicate that the two tables will have the
exact same number of tuples at all times.
Database Design 15 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
3) Cross-reference or relationship relation approach :
• set up a third relation R for the purpose of cross-
referencing the primary keys of the two relations S and T
representing the entity types.
• The relation R is called a relationship relation (or
sometimes a lookup table), because each tuple in R
represents a relationship instance that relates one tuple
from S with one tuple from T.
Database Design 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 3: Mapping of Binary 1:1 Relationship Types
3) Cross-reference or relationship relation approach :
• The relation R will include the primary key attributes of S
and T as foreign keys.
• The primary key of R will be one of the two foreign keys,
and the other foreign key will be a unique key of R.
• The drawback is having an extra relation, and requiring
an extra join operation when combining related tuples
from the tables.
Database Design 17 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 4: Mapping of Binary 1:N Relationship Types
• For each regular binary 1:N relationship type R, identify
the relation S that represents the participating entity
type at the N-side of the relationship type
• Include as foreign key in S the primary key of the
relation T that represents the other entity type
participating in R
• Include any simple attributes of the 1:N relationship
type as attributes of S.
Database Design 18 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 4: Mapping of Binary 1:N Relationship Types
Database Design 19 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 4: Mapping of Binary 1:N Relationship Types
Database Design 20 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship
Types
• For each binary M:N relationship type R, create a new
relation S to represent R.
• Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity types
• Their combination will form the primary key of S.
• Also include any simple attributes of the M:N
relationship type as attributes of S.
• we must create a separate relationship relation S.
Database Design 21 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship
Types
Database Design 22 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship
Types
• The propagate (CASCADE) option for the referential
triggered action should be specified on the foreign keys
in the relation corresponding to the relationship R
• This is due to each relationship instance has an
existence dependency on each of the entities it relates.
• This can be used for both ON UPDATE and ON DELETE.
Database Design 23 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship
Types
• Notice that we can always map 1:1 or 1:N relationships
in a manner similar to M:N relationships by using the
cross-reference (relationship relation) approach.
• This alternative is particularly useful when few
relationship instances exist, in order to avoid NULL
values in foreign keys.
• In this case, the primary key of the relationship relation
will be only one of the foreign keys that reference the
participating
Database Design entity relations.
24 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 5: Mapping of Binary M:N Relationship
Types
• For a 1:N relationship, the primary key of the
relationship relation will be the foreign key that
references the entity relation on the N-side.
• For a 1:1 relationship, either foreign key can be used as
the primary key of the relationship relation.
Database Design 25 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 6: Mapping of Multivalued Attributes
• For each multivalued attribute A, create a new relation R.
• This relation R will include an attribute corresponding to
A, plus the primary key attribute K—as a foreign key in R
—of the relation that represents the entity type or
relationship type that has A as a multivalued attribute.
• The primary key of R is the combination of A and K.
• If the multivalued attribute is composite, we include its
simple components.
Database Design 26 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 6: Mapping of Multivalued Attributes
Database Design 27 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 6: Mapping of Multivalued Attributes
• The propagate (CASCADE) option for the referential
triggered action should be specified on the foreign key
in the relation R corresponding to the multivalued
attribute for both ON UPDATE and ON DELETE.
• We should also note that the key of R when mapping a
composite, multivalued attribute requires some analysis
of the meaning of the component attributes.
Database Design 28 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 6: Mapping of Multivalued Attributes
• In some cases, when a multivalued attribute is
composite, only some of the component attributes are
required to be part of the key of R
• These attributes are similar to a partial key of a weak
entity type that corresponds to the multivalued attribute
Database Design 29 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 7: Mapping of N-ary Relationship Types
• For each n-ary relationship type R, where n > 2, create a
new relation S to represent R.
• Include as foreign key attributes in S the primary keys of
the relations that represent the participating entity
types.
• Also include any simple attributes of the n-ary
relationship type as attributes of S.
Database Design 30 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 7: Mapping of N-ary Relationship Types
• The primary key of S is usually a combination of all the
foreign keys that reference the relations representing
the participating entity types.
• However, if the cardinality constraints on any of the
entity types E participating in R is 1, then the primary
key of S should not include the foreign key attribute that
references the relation E corresponding to E
Database Design 31 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm
Step 7: Mapping of N-ary Relationship Types
Database Design 32 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
ER-to-Relational Mapping Algorithm: Summary
Database Design 33 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
Attrs(R) to denote the attributes of relation R
PK(R) to denote the primary key of R
Convert each specialization with m subclasses {S1, S2, ...,
Sm} and (generalized) superclass C, where the attributes of
C are {k, a1, ...an} and k is the (primary) key, into relation
schemas using one of the following options:
8A: Multiple relations—superclass and subclasses.
8B: Multiple relations—subclass relations only.
8C: Single relation with one type attribute.
8D:Design
Database Single relation with multiple type attributes.BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
34
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8A: Multiple relations—superclass and subclasses.
• Create a relation L for C with attributes Attrs(L) = {k,
a1, ..., an} and PK(L) = k.
• Create a relation Li for each subclass Si, 1≤i≤m , with
the attributes Attrs(Li) = {k} ꓴ {attributes of Si} and PK(Li)
= k.
• This option works for any specialization (total or partial,
disjoint or overlapping).
Database Design 35 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8A: Multiple relations—superclass and subclasses.
Database Design 36 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8B: Multiple relations— Subclasses relations only.
• Create a relation Li for each subclass Si, 1 ≤ I ≤ m , with
the attributes Attrs(Li) = {attributes of Si} ꓴ {k, a1, ..., an}
and PK(Li) = k.
• This option only works for a specialization whose
subclasses are total (every entity in the superclass must
belong to (at least) one of the subclasses).
Database Design 37 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8B: Multiple relations— Subclasses relations only.
• Additionally, it is only recommended if the specialization
has the disjointedness constraint.
• If the specialization is overlapping, the same entity may
be duplicated in several relations.
Database Design 38 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8B: Multiple relations— Subclasses relations only.
Database Design 39 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8C: Single relation with one type attribute.
• Create a single relation L with attributes Attrs(L) = {k,
a1, ..., an} ꓴ {attributes of S1} ꓴ ... ꓴ {attributes of Sm} ꓴ
{t} and PK(L) = k.
• The attribute t is called a type (or discriminating)
attribute whose value indicates the subclass to which
each tuple belongs, if any.
• This option works only for a specialization whose
subclasses
Database Design are disjoint, and has the potential for
40 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8C: Single relation with one type attribute.
Database Design 41 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8D: Single relation with multiple type attributes.
• Create a single relation schema L with attributes Attrs(L)
= {k, a1, ..., an} ∪ {attributes of S1} ∪ ... ∪ {attributes of
Sm} ∪ {t1, t2, ..., tm} and PK(L) = k.
• Each ti, 1 ≤ i ≤ m, is a Boolean type attribute indicating
whether a tuple belongs to subclass Si.
• This option is used for a specialization whose subclasses
are overlapping (but will also work for a disjoint
specialization).
Database Design 42 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Step 8: Mapping of
Specialization/Generalization
8D: Single relation with multiple type attributes.
Database Design 43 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Database Design 45 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Database Design 46 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mapping EER Model Constructs to Relations
Database Design 47 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 1. Map ER-Model to Relational Model
Database Design 48 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 2. Map ER-Model to Relational Model
Database Design 49 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 3. Map ER-Model to Relational Model
Database Design 50 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
https://www.youtube.com/watch?v=7LRH7DY1QbQ
Database Design 51 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
DATABASE DESIGN
BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus
SQL Commands
i. Definition
ii. Types of SQL commands
a. DDL commands b. DML Commands c. DQL commands d.
DCL commands and e. TCL commands
Setting the constraints in Create command
Definition of Relational Algebra
Types of Relational algebra
Basic Operations in Relational Algebra
i. Select ii. Project iii. Union iv. Set Difference v.
Cartesian product vi. Rename
3
Database Design - Contact Session - 5
BITS Pilani, Pilani Campus
SQL Commands
1. DDL commands
DDL commands are used to make or perform changes
on the physical structure of any table residing inside
a database. When these commands are executed,
the changes in the table are saved immediately. The
DDL commands are
(i) CREATE TABLE
(ii) ALTER TABLE
(iii) DROP TABLE
(iv) RENAME
5
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
Examples:
a. ALTER TABLE Student Add (dob date); // To
add a new attribute dob into the Student Table
b. ALTER TABLE Student Modify (average
number(5,2); // Change the size of the attribute
average in Student Table
c. ALTER TABLE Student drop Dept; // To delete a
attribute Dept from the Student Table
(iii) RENAME – It is used to rename the table or database
name. The syntax of the command is
RENAME Old table name to New table Name;
8
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
Example:
RENAME Student to Students; // The table Student
is renamed as Students
(iv) DROP Command – This command is used to delete
a table from the database.
Its syntax is
DROP TABLE table name;
Example:
DROP TABLE Students; // DROP command
deletes the table names Students from the database
permanently
9
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
2. DML commands
Once the tables are created using DDL Commands, the
manipulation inside those tables is done with the DML
commands. The advantages of using DML command is,
if we did any wrong changes, they can be rolled back
easily. The DML Commands are
(i)INSERT
(ii)DELETE
(iii)UPDATE
(iv)LOCK
10
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
(i) INSERT COMMAND – This DML command is used to
insert a tuple / record into the table. The general format
of the command is
INSERT INTO TABLE NAME (Attribute names )
VALUES(the values of the attributes );
The String and Date values are to be enclosed in Single
quotes.
Examples:
a. INSERT INTO Students VALUES
(‘Yuvaraj’,10001,’CSE’,98.78);
11
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
13
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
a. DELETE from Students WHERE Average<=90;
The DELETE command deletes all the student records who
have got average <= 90 from the table.
(iii) UPDATE COMMAND – This DML command is used to
Modify/change the content of a tuple / record in a table.
The general format of the command is
UPDATE TABLE NAME SET ATT = V WHERE
CONDITION;
Where
ATT is the attribute which requires modification
V is a value or an expression
14
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
Examples
a.UPDATE Student SET StName = ‘Yuvaraj
Singh’WHERE StName = ‘Yuvaraj’;
The UPDATE command modifies the StName,’Yuvaraj’
with ‘Yuvaraj Singh’
b. UPDATE Student SET Average = Average + 2.5
WHERE StName = ‘Yuvaraj Singh’;
The UPDATE command updates the attribute Average
with Average + 2.5. That is, 2.5 marks are added with
Average for all the records / tuples.
15
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
16
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
17
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
18
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
StNum Average
10001 98.78
10010 88.75
12345 88.00
19
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
20
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
22
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
SQL Commands
5. TCL commands
These commands are applied to rollback and commit
the changes what we have made in the database.
The TCL Commands are
(i)ROLLBACK – It is used to Undo the changes made in
the database
(ii)Commit – It is used to save the changes made in the
database.
23
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
Integrity Constraints
We know that it is possible to set some conditions
(constraints) at the time of creating the structure of the
table to maintain the integrity.
Integrity checks the validity of the value given for the
attribute of the table.
These constraints are called as Integrity Constraints.
When the values of the attributes are give, these
constraints check whether they are valid or not.
If they are valid, the values will be permitted to construct
the tuple.
24
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
After the tuple/record is constructed, it can be added into
the table.
If the value of the attribute is not valid(incorrect value) , the
constraint generates the error code and is displayed on
the user’s screen without giving the permission to
construct the tuple.
25
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
26
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
27
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
28
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Setting the Constraints
29
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Relational Algebra
Relational Algebra in DBMS
Relational algebra is a procedural query language that
performs operation on relational model.
The purpose of a query language is to retrieve data
from database or perform various operations such as
insert, update, delete on the data in the database.
Conversely relational calculus is a non-procedural
query language.
It specifies what data to be retrieved from the database
but doesn’t specify how to retrieve it.
30
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Relational Algebra
BASIC OPERATIONS
i. Select Operator
Select Operator is denoted by sigma (σ) and it is used to
find the tuples / rows in a relation / a table which satisfy
the
given condition. Syntax of Select Operator (σ)
σ Condition/Predicate(Relation/Table name)
Example for Select Operator
Assume that a customer table has the following data
Table: CUSTOMER
If the Query is
σ Customer_City="Agra" (CUSTOMER) ,
the output will be
Database Design - Contact Session - 5
Table 2: STUDENT
48
BITS Pilani, Pilani Campus
Relational Algebra - BASIC
OPERATIONS
49
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Relational Algebra - BASIC
OPERATIONS
50
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Relational Algebra - BASIC
OPERATIONS
51
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Relational Algebra - BASIC
OPERATIONS
52
Database Design - Contact Session - 5 BITS Pilani, Pilani Campus
Thanks
53
BITS Pilani, Pilani Campus
DATABASE DESIGN
BITS Pilani SESSION 6
Pilani Campus
BITS Pilani
Pilani Campus
3
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands
What are Joins?
JOINS in SQL are commands which are used to combine rows
from two or more tables, based on an associated column
between those tables. There are largely used when a user
tries to extract data from tables which have one-to-many or
many-to-many relationships between them.
Types of Joins
There are 4 types of Joins. They are
1. Inner Join
2. Full Join
3. Left Join
4. Right Join 4
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands
The following represents the various Joins
5
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands
EMPLOYEE
EmpId Name Age EmailId
E101 Mr. Dhoni 33 [email protected]
E102 Mr.Sachin 40 [email protected]
E103 Mr.Rohit Sharma 30 [email protected]
E104 Mr. Virat Kohli 28 [email protected]
PROJECT
ProjId EmpId Proj_Name Start_date
P1 E102 Project A 07-2-2018
P2 E104 Project B 03-8-2018
P3 E102 Project C 01-1-2019
P4 E120 Project D 10-8-2019
6
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands – Inner Join
I. INNER JOIN
INNER join returns those records which have matching
values in both tables. So, if you perform an INNER join
operation between the Employee table and the Projects
table,
all the tuples which have matching values in both the tables
will be given as output.
Syntax:
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1INNER JOIN Table2 ON
Table1.MatchingColumnName =
Table2.MatchingColumnName;
7
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands – Inner Join
Example: SELECT EMPLOYEE.NAME,EMPLOYEE.EmpId, PROJECT.
ProjId, PROJECT. Proj_Name FROM EMPLOYEE INNER JOIN
PROJECT ON EMPLOYEE.EmpId = PROJECT. EmpId;
The output of the above query is
Name EmpId ProjId Proj_Name
Mr.Sachin E102 P1 Project A
Mr. Virat Kohli E104 P2 Project B
Mr.Sachin E102 P3 Project C
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1 FULL JOIN Table2 ON
Table1.MatchingColumnName =
Table2.MatchingColumnName;
Example:
SELECT EMPLOYEE.NAME,EMPLOYEE.EmpId, PROJECT. ProjId,
PROJECT. Proj_Name FROM EMPLOYEE
FULL JOIN PROJECT ON
EMPLOYEE.EmpId = PROJECT. EmpId;
If the above query is executed , we get the following result:
9
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands – Left Join
Name EmpId ProjId Proj_Name
Mr. Dhoni E101 NULL NULL
Mr.Sachin E102 P1 Project A
Mr.Sachin E102 P3 Project C
Mr.Rohit Sharma E103 NULL NULL
Mr. Virat Kohli E104 P2 Project B
NULL NULL P4 Project D
12
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands – Right Join
s Syntax:
It
SELECT Table1.Column1,Table1.Column2,Table2.Column1,....
FROM Table1 RIGHT JOIN Table2 ON
Table1.MatchingColumnName =
Table2.MatchingColumnName;
Example:
SELECT EMPLOYEE.NAME,EMPLOYEE.EmpId, PROJECT. ProjId,
PROJECT. Proj_Name FROM EMPLOYEE
RIGHT JOIN PROJECT ON
EMPLOYEE.EmpId = PROJECT. EmpId;
If the above query is executed , we get the following result:
13
Database Design- Session 6 BITS Pilani, Pilani Campus
JOIN operation in SQL
Commands – Right Join
EmpName EmpId ProjId Proj_Name
Mr.Sachin E102 P1 Project A
Mr. Virat Kohli E104 P2 Project B
Mr.Sachin E102 P3 Project C
NULL NULL P4 Project D
14
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
17
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
Example
Assume a table CUSTOMER_TEMP has similar structure as
CUSTOMER table. To copy the complete CUSTOMER table
into the CUSTOMER_TEMP table, we can use the following
SQL subquery command
19
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
21
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
UPDATE CUSTOMER
SET SALARY = SALARY + SALARY * 0.10
WHERE AGE IN
(SELECT AGE FROM CUSTOMER_TEMP
WHERE AGE >= 27 );
The inner query is executed first which retrieve all the ages of
the records in the CUSTOMER table satisfying the condition
(>=27). Then the outer query is performed to update the
salary for the records. The resultant CUSTOMER table is
22
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
CUSTOMER
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 2200.00
2 Keshav 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitanya 25 Mumbai 6500.00
5 Rajesh 27 Bhopal 9350.00
6 Kamal 22 MP 4500.00
Its syntax is :
DELETE FROM TABLE_NAME [ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME) [ WHERE Condition) ]
Example
Assuming, we have a CUSTOMER_TEMP table available which
is a backup of the CUSTOMER table. The following example
deletes the records from the CUSTOMER table for all the
customers whose AGE is greater than or equal to 25.
DELETE FROM CUSTOMER
WHERE AGE IN (SELECT AGE FROM CUSTOMER_TEMP
WHERE AGE >= 25 );
24
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL NESTED QUERY
25
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Aggregate Functions
AGGREGATE FUNCTIONS
The aggregate functions are the predefined functions to
calculate average, sum and count the number of occurrences
of the records satisfying a cretria . The aggregate functions are
SQL COUNT(), AVG() and SUM() Functions
1.COUNT() function – It returns the number of rows that
Matches a specified criterion.
Its syntax is
SELECT COUNT(column_name) FROM table_name
WHERE condition;
AVG()
2. function – It returns the average value of a
numeric column. Its syntax is
26
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Aggregate Functions
SELECT AVG(column_name)
FROM table_name
WHERE condition;
3.SUM() function - It returns the total sum of a numeric
column. Its syntax is
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Examples for the aggregate functions:
Assume that we are given a CUSTOMER table as follows:
27
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Aggregate Functions
CUSTOMER
ID NAME AGE ADDRESS SALARY
1 Ramesh 35 Ahmedabad 2200.00
2 Keshav 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitanya 25 Mumbai 6500.00
5 Rajesh 27 Bhopal 9800.00
Example 1: If we execute the SQL command
SELECT COUNT(ID) FROM CUSTOMER
WHERE age >=25; , the output is
COUNT(ID)
4
28
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Aggregate Functions
32
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Exists / Not Exists
Condition
Using NOT with EXISTS
To fetch name of the customers who has not placed any
order.
CustName
Mr.Anand
33
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
The GROUP BY clause is used in the SELECT statement .
Optionally it is used in conjunction with aggregate functions to
produce summary reports from the database.
That's what it does, summarizing data from the database.
The queries that contain the GROUP BY clause are called
grouped queries and only return a single row for every
grouped item.
GROUP BY Syntax
Now that we know what the GROUP By clause is, let's look at
the syntax for a basic group by query.
SELECT statement... GROUP BY
column_name1[,column_name2,...] [HAVING condition];
34
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
Where
(i) "SELECT statement..." is the standard SQL SELECT
statement
(ii) "GROUP BY column_name1" is the clause that performs
the grouping based on a particular column which is
specified as column_name1.
(iii) "[,column_name2,...]" is optional; represents other
column names when the grouping is done on more than
one columns.
(iv) "[HAVING condition]" is optional; it is used to restrict the
rows affected by the GROUP BY clause. It is similar to the
WHERE clause.
35
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
Grouping using a Single Column
Assume that a table members has the data as follows:
Name MemNo Gender Age Place
Ram M1001 Male 25 Chennai-1
Hema M1010 Female 23 Chennai-10
Amrutha M1005 Female 19 Chennai-5
Akshitha M1111 Female 24 Chennai-45
Anand M0009 Male 45 Chennai-78
In order to help understand the effect of Group By clause, let
us execute a simple query that returns all the gender entries
from the members table.
SELECT Gender FROM members ;
36
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
It produces the following output:
Gender
Male
Female
Female
Female
Male
Suppose we want to get the unique values for genders. We can
use a following query -
SELECT Gender FROM members GROUP BY Gender;
It produces the following output:
37
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
Gender
Male
Female
Note that it returns only two results. This is because we only
have two gender types Male Female
and . The GROUP BY
clause grouped all the "Male" members together and returned
only a single row for it. It is same with the "Female" members.
If we use the SQL Query as
SELECT Gender,Avg(Age) FROM members GROUP BY
Gender;
it returns the average Age value of each Gender as
38
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
Gender Avg(Age)
Male 35 (25+45) / 2
Female 22 (23+19+24)/3
Grouping using a Multiple Columns
Assume that a table Orders has the data as follows:
SalesId CustIdAmount
S1000 C5001 8000
S1003 C5003 13000
S1000 C5010 28000
S1000 C5004 40000
S1000 C5001 70000
S1003 C5001 8000
39
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
SELECT SalesId,CustId,Sum(Amount),Avg(Amount) FROM
Orders GROUP BY SalesId, CustId;
it displays the output as
SalesId CustId Sum(Amount) Avg(Amount)
S1000 C5001 78000 39000
S1000 C5010 28000 28000
S1000 C5004 40000 40000
S1003 C5003 13000 13000
S1003 C5001 8000 8000
In the above query, the records are grouped with two
columns
/ attributes such as SalesId and CustId.
40
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Group
By option
If the same table is grouped with a single attribute as
SELECT SalesId,Sum(Amount),Average(Amount) FROM Orders
GROUP BY SalesId;
it displays the output as
SalesId Sum(Amount) Average(Amount)
S1000 146000 36500
S1003 21000 10500
41
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Having
option
We know that WHERE clause is used to select and reject the
individual rows from a table, the HAVING is also used to
select
and reject row groups. The format of the HAVING clause
parallels that of the WHERE clause, consisting of the keyword
HAVING followed by the search condition. Thus, the HAVING
clause specifies a search condition for groups.
If a query is asked as , “What is the average salesperson
whose
order total more than Rs 30,000?”, we can use the HAVING
clause as
SELECT SalesId,Sum(Amount),Average(Amount) FROM Orders
GROUP BY SalesId HAVING Sum(Amount)>30000; 42
Database Design- Session 6 BITS Pilani, Pilani Campus
SQL Command with Having
option
it displays the output as
SalesId Sum(Amount) Average(Amount)
S1000 146000 36500
43
Database Design- Session 6 BITS Pilani, Pilani Campus
Select Statement with Order
By Clause
The Order By clause in the Select statement is used to sort the
query result in an order of a column. The order may be in
ascending order or descending order. The default order is
ascending.
Assume that a table Student has the following data
StName Stno Dept Percent
Dravid S110 Mech 78
Meera S200 Cse 92
Levin S078 Mech 80
Leela S012 Mech 62
Raman S003 Mech 91
Pavithra S235 Cse 77
Pavithra S234 Cse 67
44
Database Design- Session 6 BITS Pilani, Pilani Campus
Select Statement with Order
By Clause
If we want to sort the records of the Student table according
to
the department, we use the Order by clause as
Select * from Student order by Dept;
The output of the above query is
StName Stno Dept Percent
Meera S200 Cse 92
Pavithra S235 Cse 77
Pavithra S234 Cse 67
Levin S078 Mech 80
Leela S012 Mech 62
Raman S003 Mech 91
Dravid S110 Mech 78
45
Database Design- Session 6 BITS Pilani, Pilani Campus
Select Statement with Order
By Clause
If we want to sort the records of the Student table according
to
the department and Stno within the department, we use the
Order by clause as Select * from Student order by
Dept,Stno; The output of the above query is
StName Stno Dept Percent
Meera S200 Cse 92
Pavithra S234 Cse 67
Pavithra S235 Cse 77
Raman S003 Mech 91
Leela S012 Mech 62
Levin S078 Mech 80
Dravid S110 Mech 78
46
Database Design- Session 6 BITS Pilani, Pilani Campus
Select Statement with Order
By Clause
If we want to sort the Student table according to the
descending values of percentage of the students,we use the
SQL statement as Select * from Student order by Percent
desc; The output of the above query is
StName Stno Dept Percent
Meera S200 Cse 92
Raman S003 Mech 91
Levin S078 Mech 80
Dravid S110 Mech 78
Pavithra S235 Cse 77
Pavithra S234 Cse 67
Leela S012 Mech 62
47
Database Design- Session 6 BITS Pilani, Pilani Campus
Thanks
48
BITS Pilani, Pilani Campus
Database Design
SESSION 7
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
BITS Pilani
Pilani | Dubai | Goa | Hyderabad
Contact Session 7:
Session Outline
• Exercises in Nested Query
• Trigger
• Views
Some queries require that existing values in the database be fetched and
then used in a comparison condition.
These nested queries can also appear in the WHERE clause or the FROM
clause or the SELECT clause or other SQL clauses as needed.
Database Design 4 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Nested Queries - Example
A Nested query for make a list of all project numbers for projects that involve
employee Smith either as worker or as a manager of the department that
controls the project:
Database Design 5 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Nested Queries - Example
If a nested query returns a single attribute and a single tuple, the query
result will be a single (scalar) value. In such cases, it is permissible to use
= instead of IN for the comparison operator.
To illustrate this, consider the following query:
This query will select the Essns of all employees who work the same (project, hours) combination
on some project that employee ‘John Smith’ (whose Ssn = ‘123456789’) works on.
Database Design 6 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Nested Queries - Example
Above query returns the names of employees whose salary is greater than the
salary of all the employees in department 5:
Database Design 7 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Nested Queries - Example
In above query, we must qualify E.Sex because it refers to the Sex attribute of EMPLOYEE from the
outer query, and DEPENDENT also has an attribute called Sex. If there were any unqualified
references to Sex in the nested query, they would refer to the Sex attribute of DEPENDENT.
Database Design 8 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
1. Correlated Nested Queries
Whenever a condition in the WHERE clause of a nested query references some
attribute of a relation declared in the outer query, the two queries are said to be
correlated
Queries that are nested using the = or IN comparison operator can be collapsed
into one single block: Example:
Example 1.3A:
Query: SELECT E.Fname, E.Lname
FROM EMPLOYEE AS E, DEPENDENT AS D
WHERE E.Ssn=D.Essn AND E.Sex=D.Sex AND
E.Fname=D.Dependent_name;
Database Design 9 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2. Introduction to Triggers in SQL
Database Design 10 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Introduction to Triggers: USE OF TRIGGERS
Database Design 11 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Introduction to Triggers: USE OF TRIGGERS
Database Design 12 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
3. Views (Virtual Tables) in SQL
Database Design 13 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Concept of a view in SQL
Single table derived from other tables called the defining tables
Considered to be a virtual table that is not necessarily populated
Database Design 14 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Specification of Views in SQL
Database Design 15 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Specification of Views in SQL
Once a View is defined, SQL queries can use the View relation in the FROM
clause
View is always up-to-date
Responsibility of the DBMS and not the user
DROP VIEW command
Dispose of a view
Database Design 16 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
View Implementation, View Update, and Inline Views
Disadvantage: inefficient for views defined via complex queries that are
time-consuming to execute
Database Design 17 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
View Implementation, View Update, and Inline Views
Database Design 18 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
View Implementation, View Update, and Inline Views
Database Design 19 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
View Update
Database Design 20 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Views as authorization mechanism
In a similar manner, a view can restrict a user to only see certain columns;
for
example, only the first name, last name, and address of an employee may
be visible
as follows:
Database Design 22 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Summary of SQL Syntax
Complex SQL:
Nested queries, joined tables (in the FROM clause), outer joins,
aggregate functions, grouping
Handling semantic constraints with CREATE ASSERTION and CREATE
TRIGGER
CREATE VIEW statement and materialization strategies
Schema Modification for the DBAs using ALTER TABLE , ADD and DROP
COLUMN, ALTER CONSTRAINT etc.
Copyright @2007 Ramez Elmasri, Shamkant B. Navathe
Database Design 25 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
SUBSTRING() Function
• The SUBSTRING() extracts a substring with a specified length starting from a
location in an input string.
• The syntax of the SUBSTRING() function:
SUBSTRING(input_string, start, length);
• In this syntax:
• input_string can be a character, binary, text, ntext, or image expression.
• start is an integer that specifies the location where the returned
substring starts. Note that the first character in the input_string is
1, not zero.
• length is a positive integer that specifies the number of characters of
the substring to be returned. The SUBSTRING() function raises an error
if the length is negative. If start + length > the length of input_string,
the substring will begin at the start and include the remaining
characters of the input_string.
Database Design 26 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
SUBSTRING() examples-1
A) Using SUBSTRING() function with literal strings
• This example extracts a substring with the length of 6, starting from the
fifth character, in the 'SQL Server SUBSTRING' string.
SELECT SUBSTRING('SQL Server SUBSTRING', 5, 6) result;
• output:
result
------
Server
(1 row affected)
Database Design 27 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
SUBSTRING() examples-2
B) Using SUBSTRING() function with table columns
• The example returns the names attribute in the first column, the first letter of
the name attribute in the second column, and the third and fourth characters
in the final column.
SELECT name, SUBSTRING(name, 1, 1) AS Initial ,
SUBSTRING(name, 3, 2) AS ThirdAndFourthCharacters
FROM sys.databases
WHERE database_id < 5;
• Output: name Initial ThirdAndFourthCharacters
master M St
tempdb T Mp
model M De
msdb M Db
Database Design 28 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
REPLACE() function
• Replace in SQL is a built-in function that allows you to replace all the
incidents of a substring within a specified string with a new substring.
• To replace all occurrences of a substring within a string with a new
substring.
• Syntax
REPLACE(input_string, substring, new_substring);
• In this syntax:
• input_string is any string expression to be searched.
• substring is the substring to be replaced.
• new_substring is the replacement string.
• The REPLACE() function returns a new string in which all occurrences of
the substring are replaced by the new_substring.
• It returns NULL if any argument is NULL.
Database Design 29 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Distinct Keyword
Database Design 30 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
SQL - LIKE Operator
• The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.
• There are two wildcards often used in conjunction with the LIKE
operator:
• The percent sign (%) represents zero, one, or multiple characters
• The underscore sign (_) represents one, single character
• You can also combine any number of conditions using AND or OR
operators.
• Syntax-
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Database Design 31 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Examples :LIKE operators with '%' and '_' wildcards:
LIKE Operator Description
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2
characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3
characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"
Database Design 32 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #1.1: (create)
Q1: Create the table suppliers that contains four columns: supplier_id,
supplier_name, city, state and supplier_id is primary key
Solution:
CREATE TABLE suppliers
( supplier_id int NOT NULL,
supplier_name char(50) NOT NULL,
city char(50),
state char(25),
CONSTRAINT suppliers_pk PRIMARY KEY (supplier_id)
);
Database Design 33 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #1.2 (insert)
Q2: Insert some records in the table suppliers that contains four columns:
supplier_id, supplier_name, city, state and supplier_id is primary key
Solution:
INSERT INTO suppliers (supplier_id, supplier_name, city, state)
VALUES (100, 'Microsoft', 'Redmond', 'Washington’),
(200, 'Google', 'Mountain View', 'California’),
(300, 'Oracle', 'Redwood City', 'California’),
(400, 'Kimberly-Clark', 'Irving', 'Texas’),
(500, 'Tyson Foods', 'Springdale', 'Arkansas’),
(600, 'SC Johnson', 'Racine', 'Wisconsin’),
(700, 'Dole Food Company', 'Westlake Village', 'California’),
(800, 'Flowers Foods', 'Thomasville', 'Georgia’),
(900, 'Electronic Arts', 'Redwood City', 'California');
Database Design 34 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #1.3: (select)
Q3: Based on the supplier table, select the unique city values that reside in
the state of California and order the results in descending order by city:
• Solution:
SELECT DISTINCT city
FROM suppliers
WHERE state = 'California'
ORDER BY city DESC;
• These are the results that you should see:
city
Westlake Village
Redwood City
Mountain View
Database Design 35 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #1.4: (update)
Q4: Based on the suppliers table populated with the following data, update
the city to 'Boise' and the state to "Idaho" for all records whose
supplier_name is "Microsoft".
Solution: UPDATE suppliers SET city = 'Boise’,
state = 'Idaho’ WHERE supplier_name = 'Microsoft’;
supplier_id supplier_name city state
The supplier table 100 Microsoft Boise Idaho
would now look like this200 Google Mountain View California
300 Oracle Redwood City California
400 Kimberly-Clark Irving Texas
500 Tyson Foods Springdale Arkansas
600 SC Johnson Racine Wisconsin
Dole Food
700 Westlake Village California
Company
800 Flowers Foods Thomasville Georgia
900 Electronic Arts Redwood City California
Database Design 36 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #1.5: (delete)
Q5: Based on the suppliers table, delete the supplier record whose state is
'California' and supplier_name is not Google:
Solution: DELETE FROM suppliers WHERE state = 'California’
AND supplier_name <> 'Google’;
There would be 3 records deleted and the suppliers table would now look like
this:
supplier_id supplier_name city state
Database Design 37 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #2
Sample employees table for the queries:
Database Design 38 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #2:
INSERT INTO employees
(employee_number, last_name, first_name, salary, dept_id, emailid)
VALUES
(1001, 'Smith', 'John', 62000, 500, ‘[email protected]’),
(1002, 'Anderson', 'Jane', 57500, 500, ‘[email protected]’),
(1003, 'Everest', 'Brad', 71000, 501, ‘[email protected]’),
(1004, 'Horvath', 'Jack', 42000, 501, ‘[email protected]’);
(1005, ‘Mohan', ‘Radha’, 55000, 502, ‘[email protected]’);
Database Design 39 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #2:
1. Select all fields from the employees table whose salary is less than or
equal to $52,500 (no sorting is required)
2. Select the details of the employee whose first name starts with B.
3. Print name and Id of employee whose email-id is in Gmail.
4. Select the details of the employee who work either for department 501
or 502.
5. Select all the employees working in department 501 in descending
order of their salary.
Database Design 40 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Solution for Practice Exercise #2
• Solution 1:
SELECT * FROM employees WHERE salary <= 52500;
• Output:
employee_number last_name first_name Salary dept_id Emailid
1004 Horvath Jack 42000 501 [email protected]
• Solution 2:
SELECT * FROM employees WHERE first_name like ‘b%’
• Output:-
employee_number last_name first_name salary dept_id emailid
1003 Everest Brad 71000 501 [email protected]
Database Design 41 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Solution for Practice Exercise #2
• Solution 3:
SELECT employee_number as E_no, last_name, first_name FROM
employees where emailid like '%@gmail.com’;
• Output E_no last_name first_name
1001 smith John
• Solution 4: 1002 Anderson Jano
Database Design 42 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Solution for Practice Exercise #2
• Solution 5:
SELECT * FROM employees WHERE dept=501 ORDER BY salary DESC;
• Output
Database Design 43 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #3
Sample customers table:
Database Design 44 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Practice Exercise #3:
1. Write a query to update the portion of the phone_number in the customers
table, within the phone number the substring '124' will be replaced by '999’.
2. Write a query to get the details of the customers where the length of the first
name greater than or equal to 6.
3. Write a query to extract the last 4 character of phone numbers.
4. Write a query that displays the first name and the length of the first name for
all customers whose name starts with the letters ‘D', 'J' or ‘N'. Give each
column an appropriate label. Sort the results by the employees' first names.
5. Write a query to display the length of first name for employees where
last name contain character ‘a' after 2nd position.
Database Design 45 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Solution Practice Exercise #3:
• Solution Q1:
UPDATE customers SET phone_number = REPLACE(phone_number, '124',
'999')
WHERE phone_number LIKE '%124%’;
• Solution Q2:
SELECT * FROM customers WHERE LENGTH(first_name) >= 6;
• Solution Q3:
SELECT SUBSTRING(phone_number, 9, 4) as ‘Ph.no’ FROM customers;
• Solution Q4:
SELECT first_name "Name", LENGTH(first_name) "Length" FROM customers
WHERE first_name LIKE ‘D%' OR first_name LIKE ‘J%' OR first_name LIKE ‘N%'
ORDER BY first_name ;
• Solution Q5:
SELECT first_name, last_name FROM customers WHERE INSTR(last_name,’A')
> 2;
Database Design 46 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Thank you
DEPENDENCY AND
NORMALIZATION
Learning Objectives
6
Database Design - Session 8 BITS Pilani, Pilani Campus
Full Functional Dependency
10
Database Design - Session 8 BITS Pilani, Pilani Campus
Transitive dependency
Transitive dependency
We know that a table consists of both key attributes and
non key attributes in which the key attributes are
used to identify /locate a record /a tuple uniquely.
All the non key attributes are functionally dependent on
the key attribute (s).
There are some instances in which a non key attribute
may depend on another non key attribute.
If a table has such type of attribute, the table is said to
be having transitive dependency.
FacName Qual
13
Database Design - Session 8 BITS Pilani, Pilani Campus
Functional Dependency
18
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION - Example
CASE PROBLEM
Let us consider the two report requirements namely , INVOICE
and REORDER REPORT of XYZ book house as shown in the
following figures:
XYZ Book House
Chennai – 600 001
Customer N0 : C10001
Customer Name : WIPRO Systems Date : XX / XX / XX
Customer Address : Chennai - 125
Book# Title Author’s Name No.of copies Price (Rs.) Amount
Rs.
B10001 Production and Operations Management Benher10 50 250
B10010 System Simulation Gordon 20 400 8000
B10011 Engineering Mathematics Benher 40 300 12000
20
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
The amount in the last column of the invoice for each book
can be computed by multiplying the number of copies
( Qty) with their respective unit price.
Similarly, the grand total at the bottom of the invoice
representing the invoice amount can be calculated by
summing the entries in the last column of the invoice.
Similarly, the date at the top right corner of the invoice can
be printed using the system clock.
A sample instance of the data of the INVOICE relation is
shown in the following table.
This is nothing but the file containing all the data items of the
INVOICE report.
21
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
22
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
23
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
26
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
28
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
So both the relations are in second normal form.
THIRD NORMAL FORM
The relation is said to be in the third normal form if it is already
in the second form and has no transitive dependency.
In a relation, there may be dependency among non – key fields.
Such dependency is called as transitive dependency.
In relation 4 (SALES relation ), there is only one non - key field
and hence there is no scope for any dependency between the
non – key fields.
It means that there is no transitive dependency.
Hence the relation 3 can be treated to be in 3NF without any
modification. But in relation 5 (BOOK-AUTHOR relation), the
field Auth_Coun depends on author’s name.
29
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
This situation is an example of the transitive dependency.
Transitive dependency will result into insertion update and
deletion anomalies.
Insertion Anomaly :- In case, the book has resident authors
who are in the process of developing new books, it will be
difficult to include the author’s detail in relation 5 .
It means that there should be at least one published book to
insert the details of a resident author.
Update Anomaly :- Since the author’s details are duplicated ,
there will be significant redundancy and inconsistency.
Some problem arises when the updation is done.
Deletion Anomaly :- If the only book of a resident author is
not reprinted , then the respective author’s data is lost.
30
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
31
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
Manmohan INDIA
Schildt US
Herbert US
Herbert US
32
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
33
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
X Y
where the attribute X is the determinant
Example: Assume that a student table consists of
student data with the following restrictions:
(i) An advisor can handle only one major subject
(ii) A Major Subject may have multiple advisors
According to the restrictions, we identify that Major is
functionally dependent on Advisor.
So we have the representation
Advisor Major
34
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
The table is given as
35
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
36
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
STUDENT
StuID AdvisorID Major_Grade
S1 A1 A+
S1 A2 A+
S3 A2 B
S4 A1 C
S5 A2 A
ADVISOR
AdvisorID Major_Sub
A1 M1
A2 M1
In both the tables every determinant is a candidate key. So
the
tables are in Boyce Codd Normal form 37
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
ADVANTAGES OF NORMALIZATION
Here we can see why normalization is an attractive prospect in
RDBMS concepts.
1) A smaller database can be maintained as normalization
eliminates the duplicate data. Overall size of the database is
reduced as a result.
2) Better performance is ensured which can be linked to the
above point. As databases become lesser in size, the passes
through the data becomes faster and shorter thereby
improving response time and speed.
3) Narrower tables are possible as normalized tables will be
fine-tuned and will have lesser columns which allows for
more data records per page.
38
Database Design - Session 8 BITS Pilani, Pilani Campus
NORMALIZATION
39
Database Design - Session 8 BITS Pilani, Pilani Campus
Exercise
42
Database Design - Session 8 BITS Pilani, Pilani Campus
Exercise
43
Database Design - Session 8 BITS Pilani, Pilani Campus
Exercise
44
Database Design - Session 8 BITS Pilani, Pilani Campus
Exercise
INTERNET
Date Sales
5-5-2020 45000
7-5-2020 34300
8-5-2020 10450
13-5-202 23325
16-5-2020 45000
Examples:
1.If we want to find out all the dates where there is a sales transaction,
the SQL command is
SELECT Date FROM STORES UNION SELECT Date FROM INTERNET;
46
Database Design - Session 8 BITS Pilani, Pilani Campus
Exercise
2. If we want to find out all the dates where there are both store sales
and internet sales, we use the following SQL command
SELECT Date FROM STORES INTERSECT
SELECT Date FROM INTERNET;
3. We want to find out all the dates where there are store sales, but no
internet sales. To do so, we use the following SQL statement:
SELECT Date FROM STORES MINUS
SELECT Date FROM INTERNET;
47
Database Design - Session 8 BITS Pilani, Pilani Campus
Thanks
48
BITS Pilani, Pilani Campus