Unit-2 DBMS
Unit-2 DBMS
Languages
Relational model can be represented as a table with columns and
rows. Each row is known as a tuple and column as an attribute.
Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.
20-02-2021 Side 1
Relational Data Model and
Languages
Example: STUDENT Relation
NAME ROLL_NO PHONE_NO ADDRESS AGE
20-02-2021 Side 2
Properties of Relations
Name of the relation is distinct from all other relations.
20-02-2021 Side 3
Integrity Constraints
Integrity constraints are a set of rules.
20-02-2021 Side 4
Types of Integrity Constraints
20-02-2021 Side 5
Domain Constraints
Domain constraints can be defined as the definition of a valid
set of values for an attribute.
20-02-2021 Side 6
Entity integrity constraints
The entity integrity constraint states that primary key value
can't be null.
This is because the primary key value is used to identify
individual rows in relation and if the primary key has a null
value, then we can't identify those rows.
A table can contain a null value other than the primary key
field.
Example:
20-02-2021 Side 7
Referential Integrity Constraints
A referential integrity constraint is specified between two
tables.
In the Referential integrity constraints, if a foreign key in
Table 1 refers to the Primary Key of Table 2, then every
value of the Foreign Key in Table 1 must be null or be
available in Table 2. Example:
20-02-2021 Side 8
Key Constraints
Keys are the entity set that is used to identify an entity
within its entity set uniquely.
An entity set can have multiple keys, but out of which one
key will be the primary key. A primary key can contain a
unique value in the relational table. Example:
20-02-2021 Side 9
Relational Calculus
Relational calculus is a non-procedural query language. In
the non-procedural query language, the user is
notconcerned with the details of how to obtain the end
results.
The relational calculus tells what to do but never explains
how to do. There are two types of relational calculus.
20-02-2021 Side 10
Tuple Relational Calculus
The tuple relational calculus is specified to select the
tuples in a relation.
Notation:
(not).
It uses Existential (∃) and Universal Quantifiers (∀) to
bind
the variable.
Notation:
{ a1, a2, a3, ..., an | P (a1, a2, a3, ... ,an)}
Where
a1, a2 are attributes
P stands for formula built by inner attributes
20-02-2021 Side 13
Example of Domain Relational Calculus
(DRC)
20-02-2021 Side 14
Structured Query Language (SQL)
SQL is used to perform operations on the records stored in the
database such as updating records, deleting records, creating
and modifying tables, views, etc.
SQL is just a query language not a database. To perform SQL
queries, we need to install any database, for example, Oracle,
MySQL, MongoDB, PostGre SQL, SQL Server, DB2, etc.
SQL stands for Structured Query Language.
It is designed for managing data in a relational
database management system (RDBMS).
It is pronounced as S-Q-L or sometime See-Qwell.
SQL is a database query language, it is used for
database
creation, deletion, fetching rows, and modifying rows, etc.
SQL is based on relational algebra and tuple
relational calculus.
20-02-2021 Side 15
Need of SQL
To create new databases, tables and views
No coding needed
In the standard SQL, it is very easy to manage the
database system. It doesn't require a substantial amount
of code to manage the database system.
20-02-2021 Side 19
Advantages of SQL
Portability
SQL can be used in laptop, PCs, server and even some
mobile phones.
Interactive language
SQL is a domain language used to communicate with the
database. It is also used to receive answers to the complex
questions in seconds.
20-02-2021 Side 20
SQL Data Types and Literals
SQL Datatype
SQL Datatype is used to define the values that a column can
contain.
Every column is required to have a name and data type in the
database table. There are various types of SQL Datatypes.
20-02-2021 Side 21
Binary Datatypes
There are Three types of binary Datatypes which are given
below:
Data Type Description
20-02-2021 Side 22
Approximate Numeric Datatype
The subtypes are given below:
Data type From To Description
20-02-2021 Side 23
Exact Numeric Datatype
The subtypes are given below:
20-02-2021 Side 24
Character String Datatype
The subtypes are given below:
20-02-2021 Side 25
Date and time Datatype
The subtypes are given below:
Datatype Description
20-02-2021 Side 26
SQL Commands
SQL commands are instructions.
SQL can perform various tasks like create a table, add data
to tables, drop the table, modify the table, set permission
for users etc.
20-02-2021 Side 27
Types of SQL Commands
There are five types of SQL commands: DDL, DML,
DCL, TCL, and DQL.
20-02-2021 Side 28
Data Definition Language
DDL(DDL)
changes the structure of the table like creating a
table, deleting a table, altering a table, etc.
CREATE
ALTER
DROP
TRUNCATE
20-02-2021 Side 29
Data Definition Language
(DDL)
1. CREATE: It is used to create a new table in
database.
the
Syntax:
Example:
20-02-2021 Side 30
Data Definition Language
(DDL)
2. DROP: It is used to delete both the structure and record
stored
in the table.
Syntax:
Example:
20-02-2021 Side 31
Data Definition Language
3. ALTER:(DDL)
It is used to alter the structure of the database. This
change could be either to modify the characteristics of an
existing attribute or probably to add a new attribute.
Syntax:
1. To add a new column in the table
ALTER TABLE table_name ADD column_name Datatype(Size);
2. To modify existing column in the table:
ALTER TABLE MODIFY(COLUMN DEFINITION....);
EXAMPLE:
20-02-2021 Side 32
Data Definition Language
(DDL)
3. TRUNCATE: It is used to delete all the rows from the table
and free the space containing the table.
Syntax:
Example:
20-02-2021 Side 33
SQL Operators
SQL statements generally contain some reserved words or
characters that are used to perform operations such as
comparison and arithmetical operations etc. These reserved
words or characters are known as operators.
- It subtracts right hand operand from left a-b will give -50
hand operand
20-02-2021 Side 35
SQL Comparison Operators
Operator Description Example
= Examine both operands value that are equal or not, if yes condition (a=b) is true
become true.
!= This is used to check the value of both operands equal or not,if not (a!=b) is true
condition become true.
<> Examines the operand's value equal or not, if values are not equal (a<>b) is true
condition is true
> Examine the left operand value is greater than right Operand, if yes (a>b) is not true
condition becomes true
< Examines the left operand value is less than right Operand, if yes (a<="" td="">
condition becomes true
>= Examines that the value of left operand is greater than or equal to the (a>=b) is not true
value of right operand or not,if yes condition become true
<= Examines that the value of left operand is less than or equal to the (a<=b) is true
value of right operand or not, if yes condition becomes true
!< Examines that the left operand value is not less than the right operand (a!<="" td="">
value
!> Examines that the value of left operand is not greater than the value of (a!>b) is true
right operand
20-02-2021 Side 36
SQL Logical Operators
Operator Description
ALL this is used to compare a value to all values in another value set.
AND this operator allows the existence of multiple conditions in an SQL
statement.
ANY this operator is used to compare the value in list according to
the condition.
BETWEEN this operator is used to search for values, that are within a set
of values
IN this operator is used to compare a value to that specified list value
NOT the NOT operator reverse the meaning of any logical operator
OR this operator is used to combine multiple conditions in SQL
statements
EXISTS the EXISTS operator is used to search for the presence of a row in a
specified table
LIKE this operator is used to compare a value to similar values using
wildcard operator
20-02-2021 Side 37
SQL Table
Table is a collection of data, organized in terms of rows and
columns.
Employe
EMP_NAME e
ADDRESS SALARY
output parameter.
SQL CREATE TABLE
SQL CREATE TABLE statement is used to create table in a
database.
If you want to create a table, you should name the table and
define its column and each column's data type.
type");
SQL CREATE TABLE
The data type of the columns may vary from one database to
another. For example, NUMBER is supported in Oracle
database for integer value whereas INT is supported in MySQL.
ID Int(11) NO PRI
NAME Varchar(2 NO
0)
AGE Int(11) NO
20-02-2021 Side 44
SQL CREATE TABLE
CREATE TABLE Employee (
EmployeeID number(10),
FirstName varchar2(255),
LastName varchar2(255),
Email varchar2(255),
AddressLine varchar2(255),
City varchar2(255)
);
20-02-2021 Side 45
Create a Table using another table
CREATE TABLE table_name
AS SELECT column1,
column2,...
FROM old_table_name
WHERE ..... ;
The following SQL creates a copy of the employee
table. CREATE TABLE Employee_New AS
SELECT Employee_ID, First_Name, Email
FROM Employee;
20-02-2021 Side 46
SQL DROP TABLE
A SQL DROP TABLE statement is used to delete a table
definition and all data from a table.
20-02-2021 Side 47
SQL DELETE TABLE
The DELETE statement is used to delete rows from a table. If
you want to remove a specific row from a table you should use
WHERE condition.
The DELETE statement only deletes the rows from the table
based on the condition defined by WHERE clause or delete all
the rows from the table when condition is not specified.
You can also use SQL ALTER TABLE command to add and
drop various constraints on an existing table.
If you want to add columns in SQL table, the SQL alter table
syntax is given below:
20-02-2021 Side 56
SQL ALTER TABLE
SQL ALTER TABLE Modify Column
20-02-2021 Side 58
SQL SELECT STATEMENT
The most commonly used SQL command is SELECT
statement. It is used to query the database and retrieve selected
data that follow the conditions we want.
SELECT expressions
FROM tables
WHERE conditions;
Here
expression is
the
column
20-02-2021 Side 59
SQL SELECT STATEMENT
There are some optional clauses in SELECT statement:
20-02-2021 Side 62
SQL SELECT COUNT
The SQL COUNT() function is used to return the number of
rows in a query.
20-02-2021 Side 66
SQL SELECT from Multiple Tables
This statement is used to retrieve fields from multiple tables. To do so, we
need to use join query to get data from multiple tables.
Customer1 table
Cus_id Name1
1 Jack
2 Jill
20-02-2021 Side 67
SQL SELECT from Multiple Tables
Customer2 table
Cus_id Name2
1 Sandy
2 Venus
Product table
20-02-2021 Side 68
SQL SELECT from Multiple Tables
SQL>SELECT p. p_id, p.cus_id, p.p_name, c1.name1, c2.name2
FROM product AS p LEFT JOIN customer1 AS c1 ON
p.cus_id=c1.cus_id
LEFT JOIN customer2 AS c2 ON p.cus_id = c2.cus_id ;
20-02-2021 Side 69
SQL WHERE
SQL> UPDATE suppliers
SET supplier_name = 'HP' WHERE supplier_name = 'IBM' AND offices =
8;
1 John 24 25000
2 Nick 22 22000
3 Amara 25 15000
4 Nick 22 22000
5 John 24 25000
John 50000
Nick 44000
Amara 15000
20-02-2021 Side 71
SQL HAVING CLAUSE
SQL> SELECT NAME, SUM(SALARY) FROM Employee
GROUP BY NAME HAVING SUM(SALARY)>45000;
Name SUM(SALARY)
John 50000
The GROUP BY Clause is used to group the rows, which have the
same values.
The SELECT statement in SQL is used with the GROUP BY clause.
In the Group BY clause, the SELECT statement can
use constants, aggregate functions, expressions, and column names.
The GROUP BY Clause is called when the HAVING clause is used to
reduce the results.
20-02-2021 Side 72
Relational Algebra
Relational algebra is a procedural query language, which
takes instances of relations as input and yields instances of
relations as output.
20-02-2021 Side 73
Relational Algebra
SELECT(σ)
Projection(π)
Rename (ρ)
Union operation (υ)
Set Difference (-)
Intersection
Cartesian product(X)
Join Operations
Inner Join
Theta Join
EQUI join
NATURAL JOIN (⋈)
OUTER JOIN
Left Outer Join(A B)
Right Outer Join ( A B )
Full Outer Join: ( A B)
20-02-2021 Side 74
Relational Algebra
SELECTION (σ)
Example 2
Example 3
σ sales > 50000 (Customers)
Output - Selects tuples from Customers where sales is
greater than 50000
20-02-2021 Side 77
Relational Algebra
Projection(π)
1 Google Active
2 Amazon Active
3 Apple Inactive
4 Alibaba Active
Google Active
Amazon Active
Apple Inactive
Alibaba Active
20-02-2021 Side 78
Relational Algebra
Rename (ρ)
20-02-2021 Side 79
Relational Algebra
Union operation (U)
B would be expressed as A 𝖴 B.
It also eliminates duplicate tuples. So, set A UNION set
A𝖴 B
gives
Table A 𝖴 B
column 1 column 2
1 1
1 2
1 3
20-02-2021 Side 81
Relational Algebra
Set Difference (-)
20-02-2021 Side 82
Relational Algebra
Table A - B
column 1 column 2
1 2
Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are
in both A and B. However, A and B must be union-
compatible.
Table A ∩ B
column 1 column 2
1 1
20-02-2021 Side 83
Relational Algebra
Cartesian Product(X) in DBMS
20-02-2021 Side 84
Relational Algebra
Table A Table B
column 1 column 2 column 1 column 2
A B C D
B C E F
Table A X B
column 1 column 2 Column 3 Column 4
A B C D
A B E F
B C C D
B C E F
20-02-2021 Side 85
Relational Algebra
Join Operations
Join operation is essentially a cartesian product
R1 ⋈θ R2
Notation
20-02-2021 Side 88
Relational Algebra
SUBJECT
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
20-02-2021 Side 89
Relational Algebra
Student_detail
SID Name Std Class Subject
101 Alex 10 10 Math
101 Alex 10 10 English
102 Maria 11 11 Music
102 Maria 11 11 Sports
2. Equijoin
When Theta join uses only equality comparison operator, it
is said to be equijoin. The above example corresponds to
equijoin.
20-02-2021 Side 90
Relational Algebra
3. Natural Join (⋈)
Natural join does not use any comparison operator.
It does not concatenate the way a Cartesian product does.
We can perform a Natural Join only if there is at least one
common attribute that exists between two relations.
In addition, the attributes must have the same name
and
domain.
Natural join acts on those matching attributes where
the values of attributes in both the relations are same.
20-02-2021 Side 91
Relational Algebra
Courses
CID Course Dept
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Maya
EE Mira
20-02-2021 Side 92
Relational Algebra
Courses ⋈ HoD
Dept CID Course Head
OUTER JOIN
Theta Join, Equijoin, and Natural Joins are called inner joins.
An inner join includes only those tuples with matching
attributes and the rest are discarded in the resulting relation.
Therefore, we need to use outer joins to include all the tuples
from the participating relations in the resulting relation. There
are three kinds of outer joins − left outer join, right outer join,
and full outer join.
20-02-2021 Side 93
Relational Algebra
1. Left Outer Join(R Left Outer Join S)
All the tuples from the Left relation, R, are included in the
resulting relation. If there are tuples in R without any
matching tuple in the Right relation S, then the S-attributes of
the resulting relation are made NULL.
Left
A B
100 Database
101 Mechanics
102 Electronics
20-02-2021 Side 94
Relational Algebra
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
20-02-2021 Side 95
Relational Algebra
Right Outer Join: ( R Right Outer Join S )
All the tuples from the Right relation, S, are included in the
resulting relation. If there are tuples in S without any matching
tuple in R, then the R-attributes of resulting relation are made
NULL.
Courses HoD
A B C D
100 Database 100 Alex
102 Electronics 102 Maya
--- --- 104 Mira
20-02-2021 Side 96
Relational Algebra
Full Outer Join: ( R Full Outer Join S)
All the tuples from both participating relations are included
in the resulting relation.
If there are no matching tuples for both relations,
their respective unmatched attributes are made NULL.
Courses HoD
A B C D
100 Database 100 Alex
101 Mechanics --- ---
102 Electronics 102 Maya
--- --- 104 Mira
20-02-2021 Side 97