0% found this document useful (0 votes)
35 views101 pages

Dbms Unit II

The document provides an overview of the Relational Model in database management systems, detailing how data is organized in tables (relations) with unique rows and domain-specific columns. It explains key concepts such as tuples, attributes, NULL values, domain constraints, and various types of key constraints (e.g., NOT NULL, UNIQUE, PRIMARY KEY). Additionally, it introduces relational algebra operations like projection and selection, as well as set theory operators for manipulating relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views101 pages

Dbms Unit II

The document provides an overview of the Relational Model in database management systems, detailing how data is organized in tables (relations) with unique rows and domain-specific columns. It explains key concepts such as tuples, attributes, NULL values, domain constraints, and various types of key constraints (e.g., NOT NULL, UNIQUE, PRIMARY KEY). Additionally, it introduces relational algebra operations like projection and selection, as well as set theory operators for manipulating relations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 101

UNIT-II

Relational Model
The most popular data model in DBMS is the
Relational Model.
• Data is stored in tables called relations.
• Each row in a relation contains a unique value.
• Each column in a relation contains values from
a same domain
Attributes
• Example of tabular data in the relational model
customer- customer- customer- account-
Customer-
name street city number
id
192-83-7465 rajesh East road vijayawada A-101
019-28-3746 suresh North Guntur A-215
192-83-7465 Johnson East road Tenali A-201
321-12-3123 Jones Main vijayawada A-217
019-28-3746 Smith North Guntur A-201
 A tuple is an ordered set of values (enclosed in
angled brackets ‘< … >’)
 Each value is derived from an appropriate
domain.
Formal Definitions - Domain
•Each attribute of a relation has a name
•The set of allowed values for each attribute is
called the domain of the attribute
•Attribute values are (normally) required to be
atomic; that is, indivisible
•E.g. the value of an attribute can be an account
number.
but cannot be a set of account numbers
• Domain is said to be atomic if all its members
are atomic
• The special value null is a member of every
domain.
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple

Table Definition Schema of a Relation


Populated Table State of the Relation
Slide 5- 8
NULL VALUES:
 The SQL NULL is the term used to represent a
missing value.
 A NULL value in a table is a value in a field
that appears to be blank.
• A field with a NULL value is a field with no
value.
• If a field in a table is optional, it is possible to
insert a new record or update a record
without adding a value to this field. Then, the
field will be saved with a NULL value.
 It is very important to understand that a NULL
value is different than a zero value or a field
that contains spaces.
• The basic syntax of NULL while creating a table.
• SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY REAL (18, 2),
PRIMARY KEY (ID) );
Example
• use the IS NULL or IS NOT NULL operators to
check for a NULL value.
IS NULL Syntax
• SELECT column_names
FROM table_name
WHERE column_name IS NULL;

IS NOT NULL Syntax


• SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Consider the following CUSTOMERS table having the records as shown below.

ID NAME AGE ADDRESS SALARY


1 ramu 32 Ahmedaba 20000.00
d
2 raju 25 Delhi 15000.00
3 venkat 23 Kota 20000.00
4 suresh 25 Mumbai 65000.00
5 rameh 27 Bhopal 8500.00
6 krishna 22 MP
7 komal 24 Indore

Sql> insert into customers values(6,’krishna’,22,’MP’,NULL);


• Now, following is the usage of the IS NOT NULL
operator.
• SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS WHERE SALARY IS NOT NULL;
ID NAME AGE ADDRESS SALARY
1 ramu 32 Ahmedabad 20000.00
2 raju 25 Delhi 15000.00
3 venkat 23 Kota 20000.00
4 suresh 25 Mumbai 65000.00
5 rameh 27 Bhopal 8500.00
Now, following is the usage of the IS
NULL operator.
SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS WHERE SALARY IS NULL;
ID NAME AGE ADDRESS SALARY
6 krishna 22 MP
7 komal 24 Indore
CONSTRAINTS

Domain constraints:
Columns in a table have a unique
name, often referred
as attributes in DBMS.
A domain is a unique set of
values permitted for an attribute
in a table.
 For example, a domain of
month-of-year can accept
January, February….December as
possible values.
• Domain constraints can be
defined as the definition of a
valid set of values for an
attribute.
• The data type of domain includes
string, character, integer, date,
real, etc.
CREATE TABLE EMPLOYEE
(EMPLOYEEID number(10) NOT NULL,
NAME varchar2(20) NOT NULL
SALARY number(20));
EXAMPLE:
EMPLOYEEID NAME SALARY
10001 Ramesh 15000

10002 Krishna 20000

10003 suresh 20000


EXAMPLE:
EMPLOYEEID NAME SALARY
10001 Ramesh 15000

10002 Krishna 20000

10003 suresh 20000

10004 Raju A

NOT allowed, because salary is an integer


KEY CONSTRAINTS:
• Constraints or nothing but the rules that
are to be followed while entering data
into columns of the database table

Example 1: if you want to maintain only


unique IDs in the employee table.
EXAMPLE 2: if you want to enter only age
under 18 in the student table etc.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
• We have 6 types of key
constraints in DBMS
1)NOT NULL:
2)UNIQUE :
3)DEFAULT:
4)CHECK :
5)PRIMARY KEY:
6)FOREIGN KEY:
• NOT NULL: ensures that the
specified column doesn’t contain
a NULL value.
CREATE TABLE EMPLOYEE
(EMPLOYEEID number(10) NOT NULL,
NAME varchar2(20) NOT NULL
SALARY number(20));
UNIQUE constraint ensures that all
values in a column are unique.

i.e
provides a unique/distinct
values to specified columns.
CREATE TABLE EMPLOYEE
(EMPLOYEEID number(10) UNIQUE,
NAME varchar2(20) NOT NULL
SALARY number(20));
DEFAULT: provides a default
value to a column if none
is specified
CREATE TABLE emp (
ID int NOT NULL,
Name varchar2(255),
Age int,
City varchar2(255) DEFAULT
'hyderabad' );
CHECK :checks for the
predefined conditions before
inserting the data inside the
table.
• Suppose in real-time if you want
to give access to an application
only if the age entered by the
user is greater than 18 this is
done at the back-end by using
a check constraint.
CREATE TABLE emp (
ID int NOT NULL,
Name varchar2(255),
Age int,
CHECK (Age >=18));
EXAMPLE:
ID NAME AGE
10001 Ramesh 24

10002 Krishna 22

10003 suresh 34

10004 Raju 36
Primary Key:
”it uniquely identifies a row in a
table”.
• A primary key is a constraint in a table
that uniquely identifies each row (record) in a
database table
CREATE TABLE emp (
ID int NOT NULL,
Name varchar2(255),
Age int,
PRIMARY KEY (ID));
EXAMPLE:
ID NAME AGE
10001 Ramesh 24

10002 Krishna 22

10003 suresh 34

10004 Raju 36
FOREIGN KEYS, REFERENTIAL INTEGRITY.
Foreign key is a column that references to a
column of the another table.( and it can be of
the same table also)
 Referential Integrity state that either foreign
key values must be NULL or must match the
values of the primary key attribute.
This constraint is enforced when a foreign key
references the primary key of a relation.
Here, relation “Student” references the relation “Department”
Foreign Key primary Key
Student Department
Branch Code Branch_Name
Roll Branch
Name Age
_no Code
Computer
CS
Science
1 Rahul 22 CS
Electronics
and
2 Anjali 21 CS ECE
communicatio
n Engineering
3 Bob 20 IT Information
IT
Technology
4 Raju 18 ECE Civil
CE
Engineering

Referencing Relation
Referenced Relation
• FOREIGN KEYS IN SQL
sql> CREATE TABLE student (
rollno varchar2(20),
name varchar2(20),
age number(10),
branchcode number(10),
PRIMARY KEY (Rollno), FOREIGN KEY
(branch code) REFERENCES
department(branch code) );
RELATIONAL ALGEBRA and CALCULUS
 Relational Algebra is a procedural query language
which takes a relation as an input and generates a
relation as an output.
 Queries in algebra are composed using a collection
of operators.
 A fundamental property is that every operator in
the algebra accepts one or two relation instances as
arguments and returns a relation instance as the
result
Projection Operator-
• Projection Operator (π) is a unary
operator in relational algebra that
performs a projection operation.
• It displays the columns of a relation or
table based on the specified attributes.
• Syntax-

π<attribute list>(R)
Example-
Consider the following Student relation-
Student

ID Name Subject Age


100 Ashish Maths 19
200 Rahul Science 20
300 Naina Physics 20
400 Sameer Chemistry 21
Query : πName, Age(Student)

RESULT

Name Age
Ashish 19
Rahul 20
Naina 20
Sameer 21
Query : πID , Name(Student)

RESULT
ID Name
100 Ashish
200 Rahul
300 Naina
400 Sameer
POINT-1:Projection operator performs
vertical partitioning of the relation.
POINT-2 : Projection operator
automatically removes all the duplicates
while projecting the output relation.
Selection Operator-

•Selection Operator (σ) is a unary


operator in relational algebra that
σ<selection_condition>(R)

performs a selection operation.


•It selects those rows or tuples from the
relation that satisfies the selection
condition.

Syntax-

σ<selection_condition>(R)
Example-
Consider the following Student relation-

Student
ID Name Subject Age
100 Ashish Maths 19
200 Rahul Science 20
300 Naina Physics 20
400 Sameer Chemistry 21
Example-1

Select tuples from a relation “STUDENT” where


subject is “SCIENCE”
σsubject = “SCIENCE” (STUDENT)

Example-2

Select tuples from a relation “STUDENT” where


subject is “SCIENCE” and AGE is 20
σsubject = “SCEINCE” ∧ AGE= 20(STUDENT)
Point-01:
We may use logical operators like ∧ , ∨ , !
and relational operators like = , ≠ , > , < ,
<= , >= with the selection condition.
Point-02:
Selection operator only selects
the required tuples according to
the selection condition.
Point-03:
Selection operator always selects the
entire tuple. It can not select a
section or part of a tuple.
RENAME OPERATION: rho (ρ).
The rename operation is used to rename the
output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to
rename STUDENT relation to STUDENT1.

ρ(STUDENT1, STUDENT)
• SQL | ALTER (RENAME)
Sometimes we may want to rename
our table to give it a more relevant
name. For this purpose we can
use ALTER TABLE to rename the
name of table.
Syntax(Oracle,MySQL)
ALTER TABLE table_name RENAME TO
new_table_name;

Columns can also be given new name with


the use of ALTER TABLE.
Syntax(Oracle):
• ALTER TABLE table_name RENAME
COLUMN old_name TO new_name;
SET THEORY OPERATORS
Condition For Using Set Theory Operators

1)To use set theory operators on two relations,


2) The two relations must be union compatible.
Union compatible property means-
 Both the relations must have same number of
attributes.
 The attribute domains (types of values
accepted by attributes) of both the relations
must be compatible.
1. Union Operator (∪)-

Let R and S be two relations.


Then-
 R ∪ S is the set of all tuples belonging to
either R or S or both.
 In R ∪ S, duplicates are automatically
removed.
Relation R
ID Name Subject
ID Name Subject
100 Ankit English
100 Ankit English
200 Pooja Maths
200 Pooja Maths
300 Komal Science
300 Komal Science
Relation S
400 Kajol French
ID Name Subject
Relation R ∪ S
100 Ankit English

400 Kajol French


2. Intersection Operator (∩)-

Let R and S be two relations.


Then-
R ∩ S is the set of all tuples belonging to both R and
S.

In R ∩ S, duplicates are automatically removed.


EXAMPLE

Relation R Relation S

ID Name Subject
ID Name Subject
100 Ankit English
100 Ankit English
200 Pooja Maths
400 Kajol French
300 Komal Science

ID Name Subject

100 Ankit English

Relation R ∩ S
3. Difference Operator (-)
Let R and S be two relations.
Then-
R – S is the set of all tuples belonging to R and
not to S.
In R – S, duplicates are automatically removed.
Relation R Relation S

ID Name Subject
ID Name Subject
100 Ankit English
200 Pooja Maths 100 Ankit English

300 Komal Science 400 Kajol French

ID Name Subject
200 Pooja Maths
300 Komal Science

Relation R – S
CARTESIAN PRODUCT (X)
 Cartesian Product is denoted by X symbol.
 Lets say we have two relations R1 and R2 then
the Cartesian product of these two relations
(R1 X R2) would combine each tuple of first
relation R1 with the each tuple of second
relation R2.
Syntax of Cartesian product (X)
R1 X R2
EMPLOYEE DEPARTMENT
EMP_ID EMP_NAME EMP_DEPT
DEPT_NO DEPT_NAME

1 Smith A A Marketing
2 Harry C B Sales

3 John B C Legal

EMPLOYEE X DEPARTMENT
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
JOINS
 The goal of creating a join condition is that it
helps you to combine the data from two or
more DBMS tables.

 Join operations are denoted by the


symbol ⋈.
TYPES OF JOINS:
• There are mainly two types of joins in
DBMS:
1) Inner Join:
Natural join
EQUI join
2) Outer Join:
Left outer join
Right outer join
Full outer join
• INNER JOIN is used to return rows from both
tables which satisfy the given condition.
Syntax
SELECT table1.column1, table1.column2, table2.co
lumn1,.... FROM
table1 INNER JOIN table2
ON table1.matching_column = table2.matching_c
olumn;
Query
Sql>SELECT
EMPLOYEE.EMP_NAME, PROJECT.DEPARTMEN
T FROM EMPLOYEE
INNER JOIN PROJECT ON
EMPLOYEE.EMP_ID = PROJECT.EMP_ID ;
EMPLOYEE
EMP_ID EMP_NAME CITY SALARY AG
EXAMPLE
E

1 Suresh vijayaw 20000 30


ada
2 Krishna Guntur 30000 26
3 RamaRao Hyderab 10000 42 OUTPUT
ad
4 Ashrith Tenali 50000 29
EMP_NAME DEPARTMENT
5 Vinod Vizag 20000 36
48 Suresh Testing
6 Satyam Kakinad 60000
a Krishna Development
RamaRao Designing
PROJECT
PROJECT_NO EMP_I DEPARTMENT Ashrith Development
D

101 1 Testing
102 2 Development
103 3 Designing
104 4 Development
2. OUTER JOIN
i) LEFT OUTER JOIN
• The SQL left outer join returns all the values
from left table and the matching values from
the right table.

• If there is no matching join value, it will return


NULL
Syntax
SELECT table1.column1, table1.column2, table2.column1,
....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Query
Sql>
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
LEFT JOIN PROJECT ON
EMPLOYEE.EMP_ID = PROJECT.EMP_ID ;
EMPLOYEE PROJECT
EXAMPLE:
EMP EMP_NA CITY SALA AGE
PROJ EMP DEPARTMENT
_ID ME RY ECT_ _ID
NO
1 Suresh vijayaw 2000 30
ada 00
101 1 Testing
2 Krishna Guntur 3000 26
00 102 2 Development
3 RamaRa Hydera 1000 42
o bad 00 103 3 Designing
4 Ashrith Tenali 5000 29
00 104 4 Development
5 Vinod Vizag 2000 36
00
6 Satyam Kakinad 6000 48 OUTPUT
a 00
EMP_NAME DEPARTMENT

Suresh Testing
Krishna Development
RamaRao Designing
Ashrith Development
Vinod NULL
Satyam NULL
ii) RIGHT OUTER JOIN
• In SQL, RIGHT OUTER JOIN returns all the
values from the rows of right table and the
matched values from the left table. If there is
no matching in both tables, it will return NULL.
Syntax
SELECT table1.column1, table1.column2, table2.column1
,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;

Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
RIGHT JOIN PROJECT
ON EMPLOYEE.EMP_ID = PROJECT.EMP_ID;
EMPLOYEE
EMP_I EMP_NAM CITY SALAR AGE
EXAMPLE
D E Y
1 Suresh vijayawa 20000 30
da 0 OUTPUT
2 Krishna Guntur 30000 26
0 EMP_NAME DEPARTMENT
3 RamaRao Hyderaba 10000 42
d 0
4 Ashrith Tenali 50000 29
0 Suresh Testing
5 Vinod Vizag 20000 36 Krishna Development
0
RamaRao Designing
6 Satyam Kakinada 60000 48
0 Ashrith Development

PROJECT
PROJECT_NO EMP_ID DEPARTMENT

101 1 Testing

102 2 Development

103 3 Designing

104 4 Development
iii) FULL OUTER JOIN
• In SQL, FULL JOIN is the result of a
combination of both left and right outer join.
Join tables have all the records from both
tables. It puts NULL on the place of matches
not found.
Syntax
SELECT table1.column1, table1.column2, table2.column1,
....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;

Query
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
FULL JOIN PROJECT
ON EMPLOYEE.EMP_ID = PROJECT.EMP_ID;
EMPLOYEE EXAMPLE
EMP_ID EMP_NA CITY SALAR AGE
ME Y
1 Suresh vijayawada 200000 30
2 Krishna Guntur 300000 26
3 RamaRao Hyderabad 100000 42 OUTPUT
4 Ashrith Tenali 500000 29
EMP_NAME DEPARTMENT
5 Vinod Vizag 200000 36
6 Satyam Kakinada 600000 48
Suresh Testing

Krishna Development
PROJECT
RamaRao Designing
PROJECT_NO EMP_ID DEPARTME
NT
Ashrith Development
101 1 Testing
102 2 Developme Vinad NULL
nt
103 3 Designing Satyam NULL

104 4 Developme
nt
Division Operator (÷)or(/) : Division operator
A÷B can be applied if and only if:
i) Attributes of B is proper subset of Attributes of
A.
ii) The relation returned by division operator will
have attributes = (All attributes of A – All
Attributes of B)
iii)The relation returned by division operator will
return those tuples from relation A which are
associated to every B’s tuple.
Example:
STUDENT_SPORTS
ROLL_NO SPORTS
1 Badminton
2 Cricket
2 Badminton
4 Badminton

ALL_SPORTS
SPORTS
Badminton
Cricket
To apply division operator as
STUDENT_SPORTS÷ ALL_SPORTS The operation is
valid as attributes in ALL_SPORTS is a proper
subset of attributes in STUDENT_SPORTS.
The attributes in resulting relation will have
attributes {ROLL_NO,SPORTS}-{SPORTS}=ROLL_NO
The tuples in resulting relation will have those
ROLL_NO which are associated with all B’s tuple
{Badminton, Cricket}. ROLL_NO 1 and 4 are
associated to Badminton only. ROLL_NO 2 is
associated to all tuples of B. So the resulting
relation will be:
STUDENT_SPORTS÷ ALL_SPORTS:

ROLL_NO

2
GATE question
Q: Consider the following relations A, B, C. How
many tuples does the result of the following
relational algebra expression contain?
Assume that the schema of A U B is the same
as that of A.
Table A
Id Name Age
12 Arun 60
15 Shreya 24 Table C
99 Rohit 11 Id Phone Area
10 2200 02
99 2100 01
Table B
Id Name Age
15 Shreya 24
25 Hari 40
98 Rohit 20
99 Rohit 11 (A) 7
(B) 4
(C) 5
(D) 9
Table A Table B
Id Name Age Id Name Age
12 Arun 60 15 Shreya 24
15 Shreya 24 25 Hari 40
99 Rohit 11 98 Rohit 20
99 Rohit 11

Result of AUB will be following table


Id Name Age
12 Arun 60
15 Shreya 24
99 Rohit 11
25 Hari 40
98 Rohit 20
AUB
Id Name Age Table C
12 Arun 60 Id Phone Area
15 Shreya 24 10 2200 02
99 Rohit 11 99 2100 01
25 Hari 40
98 Rohit 20
AUB⋈ C
Id Name Age Id Phone Area
12 ARUN 60 10 2200 02
12 Arun 60 99 2100 01
15 SHREYA 24 10 2200 02
15 Shreya 24 99 2100 01
99 ROHIT 11 10 2200 02
99 ROHIT 11 99 2100 01
25 HARI 40 10 2200 02
25 HARI 40 99 2100 01
98 ROHIT 20 10 2200 02
98 ROHIT 20 99 2100 01
Id Name Age Id Phone Area
12 Arun 60 10 2200 02
15 Shreya 24 10 2200 02
99 Rohit 11 10 2200 02
99 Rohit 11 99 2100 01
25 Hari 40 10 2200 02
98 Rohit 20 10 2200 02
98 Rohit 20 99 2100 01
RELATIONAL CALCULUS:
 Relational Algebra is a procedural query
language.
 Relational Calculus, is a non-procedural query
language.
What is Relational Calculus?
• Relational calculus is a non-procedural query
language that tells the system what data to be
retrieved but doesn’t tell how to retrieve it.
Types of Relational Calculus
1. Tuple Relational Calculus (TRC)
• Tuple relational calculus is used for selecting
those tuples that satisfy the given condition.
Table: Student
First_Name Last_Name Age
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Lets write relational calculus queries.
Query : write a query to display the last name of
those students where age is greater than 30 ?
In tuple relational calculus.

{ t.Last_Name | Student(t) AND t.age > 30 }


{ t.Last_Name | Student(t) AND t.age > 30 }
• In the above query you can see two parts separated
by | symbol. The second part is where we define the
condition and in the first part we specify the fields
which we want to display for the selected tuples.
The result of the above query would be:

Last_Name
---------
Singh
Query: write a query to display all the details of
students where Last name is ‘Singh’?

{ t | Student(t) AND t.Last_Name = 'Singh' }

Output:
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
2. Domain Relational Calculus (DRC)
In domain relational calculus the records are
filtered based on the domains.
Again we take the same table to understand
how DRC works.
Table: Student
First_Name Last_Name Age
---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Table: Student

First_Name Last_Name Age


---------- --------- ----
Ajeet Singh 30
Chaitanya Singh 31
Rajeev Bhatia 27
Carl Pratap 28
Query : write a query to find the first name and age of
students where student age is greater than 27

{< First_Name, Age > | ∈ Student ∧ Age > 27}

• Note:
The symbols used for logical operators are: ∧ for AND, ∨
for OR and ┓ for NOT.
Output:
First_Name Age
---------- ----
Ajeet 30
Chaitanya 31
Carl 28

You might also like