UNIT-2
DBMS
CS-212
Integrity Constraints
• Integrity constraints are a set of rules. It is used to
maintain the quality of information.
• Integrity constraints ensure that the data insertion,
updating, and other processes have to be performed in
such a way that data integrity is not affected.
• Thus, integrity constraint is used to guard against
accidental damage to the database.
Types of Integrity Constraint
1. Domain constraints
• 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, time, date, currency, etc.
The value of the attribute must
be available in the
corresponding domain.
2. 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.
3. 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.
4. 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 contain a unique
value in the relational
table.
Relational Model in DBMS
• Relational model can represent as a table with columns
and rows. Each row is known as a tuple. Each table of
the column has a name or attribute.
• Domain: It contains a set of atomic values that an
attribute can take.
• Attribute: It contains the name of a column in a
particular table. Each attribute Ai must have a domain,
dom(Ai)
• Relational instance: In the relational database
system, the relational instance is represented by a finite
set of tuples. Relation instances do not have duplicate
tuples.
• Relational schema: A relational schema contains the name of
the relation and name of all columns or attributes.
• Relational key: In the relational key, each row has one or more
attributes. It can identify the row in the relation uniquely.
• Example: STUDENT Relation
• In the given table, NAME, ROLL_NO, PHONE_NO, ADDRESS, and
AGE are the attributes.
• The instance of schema STUDENT has 5 tuples.
• t3 = <Laxman, 33289, 8583287182, Gurugram, 20>
NAME ROLL_NO PHONE_NO ADDRESS AGE
Ram 14795 7305758992 Noida 24
Shyam 12839 9026288936 Delhi 35
Laxman 33289 8583287182 Gurugram 20
Mahesh 27857 7086819134 Ghaziabad 27
Ganesh 17282 9028 9i3988 Delhi 40
Properties of Relations
• Name of the relation is distinct from all other relations.
• Each relation cell contains exactly one atomic (single)
value
• Each attribute contains a distinct name
• Attribute domain has no significance
• tuple has no duplicate value
• Order of tuple can have a different sequence
Relational Algebra
• Relational algebra is a procedural query language. It
gives a step by step process to obtain the result of the
query. It uses operators to perform queries.
• Types of Relational operation
1. Select Operation:
• The select operation selects tuples that satisfy a given
predicate.
• It is denoted by sigma (σ).
• Notation: σ p(r)
• Where:
• σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use
connectors like: AND OR and NOT. These relational can
use as relational operators like =, ≠, ≥, <, >, ≤.
For example: LOAN Relation
σ BRANCH_NAME="perryride" (LO
AN)
BRANCH_NAME LOAN_N AMOUNT BRANCH_NAME LOAN_NO AMOUNT
O
Downtown L-17 1000
Perryride L-15 1500
Redwood L-23 2000
Perryride L-16 1300
Perryride L-15 1500
Downtown L-14 1500
Mianus L-13 500
Roundhill L-11 900
Perryride L-16 1300
2. Project Operation:
• This operation shows the list of those attributes that we
wish to appear in the result. Rest of the attributes are
eliminated from the table.
• It is denoted by ∏.
• Notation: ∏ A1, A2, An (r)
Where
A1, A2, A3 is used as an attribute name of relation r.
Example: CUSTOMER RELATION
∏ NAME, CITY (CUSTOMER)
NAME STREET CITY
NAME CITY
Jones Main Harrison Jones Harrison
Smith Rye
Smith North Rye
Hays Harrison
Hays Main Harrison Curry Rye
Johnson Brooklyn
Curry North Rye
Brooks Brooklyn
Johnson Alma Brooklyn
Brooks Senator Brooklyn
3. Union Operation:
• Suppose there are two tuples R and S. The union
operation contains all the tuples that are either in R or S
or both in R & S.
• It eliminates the duplicate tuples. It is denoted by ∪.
• Notation: R ∪ S
• A union operation must hold the following condition:
R and S must have the attribute of the same number.
Duplicate tuples are eliminated automatically.
Example:
DEPOSITOR CUSTOMER_NAME
BORROW
RELATION
RELATION
CUSTOMER_ ACCOUNT_ Johnson
NAME NO CUSTOMER LOAN_NO
_NAME Smith
Johnson A-101
Jones L-17 Hayes
Smith A-121
Smith L-23
Mayes A-321 Turner
Hayes L-15
Turner A-176 Jones
Jackson L-14
Johnson A-273
Curry L-93 Lindsay
Jones A-472
Smith L-11 Jackson
Lindsay A-284
Williams L-17 Curry
Williams
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPO
Mayes
SITOR)
4. Set Intersection:
• Suppose there are two tuples R and S. The set
intersection operation contains all tuples that are in
both R & S.
• It is denoted by intersection ∩.
• Notation: R ∩ S
• Example: Using the above DEPOSITOR table and
BORROW table
• ∏ CUSTOMER_NAME
CUSTOMER_NAME(BORROW) ∩ ∏ CUSTOMER_NAME (D
EPOSITOR) Smith
Jones
5. Set Difference:
• Suppose there are two tuples R and S. The set
intersection operation contains all tuples that are in R
but not in S.
• It is denoted by intersection minus (-).
• Notation: R - S
• Example: Using the above DEPOSITOR table and
BORROW table
CUSTOMER_NAME
• ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DE
Jackson
POSITOR)
Hayes
Willians
Curry
6. Cartesian product
• The Cartesian product is used to combine each row in
one table with each row in the other table. It is also
known as a cross product.
• It is denoted by X.
• Notation: E X D
Example:
EMPLOYEE X DEPARTM
ENT
EMP_ID EMP_NAME EMP_DEPT DEPT_NO DEPT_NAME
EMPLOYEE
EMP_ID EMP_NAME EMP_DEPT
1 Smith A A Marketing
1 Smith A
2 Harry C 1 Smith A B Sales
3 John B 1 Smith A C Legal
2 Harry C A Marketing
DEPARTMENT
DEPT_NO DEPT_NAME 2 Harry C B Sales
A Marketing 2 Harry C C Legal
B Sales 3 John B A Marketing
C Legal
3 John B B Sales
3 John B C Legal
7. Rename Operation:
• 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)
•Question:
•Consider the following relational database schema consisting of the four relation
schemas:
• passenger ( pid, pname, pgender, pcity)
• agency ( aid, aname, acity)
• flight (fid, fdate, time, src, dest)
• booking (pid, aid, fid, fdate)
•Answer the following questions using relational algebra queries;
a) Get all names of passengers.
b)Get the complete details of all flights to New Delhi.
c) Get the details about all flights from Chennai to New Delhi.
d) Get name and city details of agencies.
a) Get all names of passengers.
Π pname(passenger)
b) Get the complete details of all flights to New Delhi.
σ dest=′NewDelhi′(flight)
c) Get the details about all flights from Chennai to New Delhi.
σsrc=′Chennai′∧dest=′NewDelhi′(flight)
d) Get name and city details of agencies.
Π aname,acity(agency)
Join Operation
• Join is an operation in DBMS(Database Management
System) that combines the row of two or more tables
based on related columns between them.
• The main purpose of Join is to retrieve the data from
multiple tables in other words Join is used to perform
multi-table query.
• It is denoted by ⨝.
Types of Join operations:
Relational Calculus
• Relational calculus is a non-procedural query language.
• In the non-procedural query language, the user is concerned
with the details of how to obtain the end results.
• The relational calculus tells what to do but never explains how
to do.
• Many of the calculus expressions involves the use of
Quantifiers. There are two types of quantifiers:
Universal Quantifiers: The universal quantifier denoted by ∀ is
read as for all which means that in a given set of tuples exactly
all tuples satisfy a given condition.
Existential Quantifiers: The existential quantifier denoted by ∃
is read as for all which means that in a given set of tuples there
is at least one occurrences whose value satisfy a given condition.
Types of Relational calculus:
1. Tuple Relational Calculus (TRC)
• It is a non-procedural query language which is based on
finding a number of tuple variables also known as range
variable for which predicate holds true.
• It describes the desired information without giving a
specific procedure for obtaining that information.
• The tuple relational calculus is specified to select the
tuples in a relation. In TRC, filtering variable uses the
tuples of a relation. The result of the relation can have
one or more tuples.
• Notation:
• A Query in the tuple relational calculus is expressed as
following notation
{T | P (T)} or {T | Condition (T)}
Where
T is the resulting tuples
P(T) is known as predicate and these condition used to
fetch T.
• For example:
{ [Link] | Author(T) AND [Link] = 'database' }
Output: This query selects the tuples from the AUTHOR
relation. It returns a tuple with 'name' from Author who
has written an article on 'database'.
Operations
• P(t) may have various conditions logically combined with OR(˅),
AND(˄), NOT(┓).
• It also uses quantifiers:
• ∃T ∈ r(Q(t))=“there exists” a tuple in t in relation r such
that predicate Q(t) is true.
• ∀T ∈ r(Q(t))=Q(t) is true “for all” tuples in relation r.
Example
• Supplier(SID,Sname,address)
• Parts(PID,Pname,color)
• Catalog(SID,PID)
1) Write a query in SQL to display Sname of suppliers? {[Link]|Supplier(S)}
2) Write a query in SQL to display Pname of Parts whose color is Red? {[Link]|
Parts(p)^[Link]=‘Red’}
3) Write a query in SQL to display SID of suppliers whose name is ‘Varun’ and address
is ‘Chandigarh’? {[Link]|Supplier(S)^[Link]=‘Varun’^[Link]=‘Chandigarh’}
4) Write a query in SQL to display SID of suppliers who supplied some parts?{[Link]|
Catalog(c)}
5) Write a query in SQL to display Sname of suppliers who supplied some parts?
{[Link]|Supplier(s)^ ∃ c(Catalog(c))^[Link]=[Link]}
6) Write a query in SQL to display Sname of suppliers who supplied some red parts?
{[Link]|Supplier(s)^ ∃ c(Catalog(c)) ∃
p(Parts(p))^[Link]=[Link]^[Link]=[Link]^[Link]=‘Red’}
2. Domain Relational Calculus (DRC)
• The second form of relation is known as Domain
relational calculus. In domain relational calculus,
filtering variable uses the domain of attributes.
• Domain relational calculus uses the same operators as
tuple calculus. It uses logical connectives ∧ (and), ∨ (or)
and ┓ (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
• Example
{<article,page,subject>/ ∈ Author^subject=‘database’}
{<name,age>/ ∈ Student^age>27}
UNIT-1 TOPIC
Structure of DBMS
Three Parts that make up the Database System
are:
• Query Processor
• Storage Manager
• Disk Storage
Query Processor
• The Query Processor receives the queries (requests) from the user
and interprets them in the form of instructions. It also executes the
instructions received from the DML Compiler. It has the following
four components:
[Link] Compiler: It converts the DML (Data Manipulation
Language) Instructions into Machine Language (low-level
language).
[Link] Interpreter: It interprets the DDL (Data Definition
Language) Instructions and stores the record in a data dictionary
(in a table containing meta-data)
[Link] Optimizer: It executes the DML Instructions and picks the
lowest cost evaluation plan out of all the alternatives present.
[Link] DML Pre-compiler: It translates the DML statements
embedded in Application Programs into procedural function calls.
Storage Manager
[Link] and Integrity Manager: It checks the
authority of various users who access data and the
Integrity Constraints of the database.
[Link] Manager: Its job is to assure the system
remains in a proper state during the transaction process.
It also ensures that concurrent transactions are executed
without any conflict.
[Link] Manager: It manages the space allocation of files
in disk and data structures which stores information in
the database.
[Link] Manager: It manages the transfer of data
between the secondary and main memory. It also decides
what data should be cached in the memory.
Disk Storage
• The Disk Storage in the Structure of DBMS
represents the space where data is stored. It has
the following components:
[Link]: These are responsible for storing the
data.
[Link] Dictionary: It is the repository that
maintains the information of the database object
and maintains the metadata.
[Link]: These are the keys that are used for
faster retrieval of data.
SQL
• SQL stands for Structured
Query Language. It is
used for storing and
managing data in
relational database
management system
(RDMS).
• It is a standard language
for Relational Database
System. It enables a user
to create, read, update
and delete relational
Characteristics of SQL
• SQL is easy to learn.
• SQL is used to access data from relational database management
systems.
• SQL can execute queries against the database.
• SQL is used to describe the data.
• SQL is used to define the data in the database and manipulate it
when needed.
• SQL is used to create and drop the database and table.
• SQL is used to create a view, stored procedure, function in a
database.
• SQL allows users to set permissions on tables, procedures, and views.
Advantages of SQL
• High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of records from a
database.
• 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.
• Well defined standards
Long established are used by the SQL databases that are being used by ISO and ANSI.
• 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.
• Multiple data view
Using the SQL language, the users can make different views of the database structure.
SQL Datatype
1. Binary Datatypes
Data Type Description
binary It has a maximum length of 8000 bytes. It contains fixed-length binary data.
varbinary It has a maximum length of 8000 bytes. It contains variable-length binary data.
image It has a maximum length of 2,147,483,647 bytes. It contains variable-length binary
data.
2. Approximate Numeric Datatype :
Data type From To Description
float -1.79E + 308 1.79E + 308 It is used to specify a
floating-point value e.g. 6.2,
2.9 etc.
real -3.40e + 38 3.40E + 38 It specifies a single precision
floating point number
3. Exact Numeric Datatype
Data type Description
int It is used to specify an integer value.
smallint It is used to specify small integer value.
bit It has the number of bits to store.
decimal It specifies a numeric value that can have a decimal
number.
numeric It is used to specify a numeric value.
4. Character String Datatype
Data type Description
char It has a maximum length of 8000 characters. It contains Fixed-length non-unicode characters.
varchar It has a maximum length of 8000 characters. It contains variable-length non-unicode characters.
text It has a maximum length of 2,147,483,647 characters. It contains variable-length non-unicode
characters.
5. Date and time Datatypes
Datatype Description
date It is used to store the year, month,
and days value.
time It is used to store the hour, minute,
and second values.
timestamp It stores the year, month, day, hour,
minute, and the second value.
SQL Commands
SQL Operator
SQL Arithmetic Operators
Operator Description Example
+ It adds the value of both operands. a+b will give 30
- It is used to subtract the right-hand operand from the left-hand a-b will give 10
operand.
* It is used to multiply the value of both operands. a*b will give 200
/ It is used to divide the left-hand operand by the right-hand operand. a/b will give 2
% It is used to divide the left-hand operand by the right-hand operand a%b will give 0
and returns reminder.
SQL Comparison Operators:
Operato Description Example
r
= It checks if two operands values are equal or not, if the values are queal then condition (a=b) is not true
becomes true.
!= It checks if two operands values are equal or not, if values are not equal, then (a!=b) is true
condition becomes true.
<> It checks if two operands values are equal or not, if values are not equal then condition (a<>b) is true
becomes true.
> It checks if the left operand value is greater than right operand value, if yes then (a>b) is not true
condition becomes true.
< It checks if the left operand value is less than right operand value, if yes then condition (a<b) is true
becomes true.
>= It checks if the left operand value is greater than or equal to the right operand value, if (a>=b) is not true
yes then condition becomes true.
<= It checks if the left operand value is less than or equal to the right operand value, if yes (a<=b) is true
then condition becomes true.
!< It checks if the left operand value is not less than the right operand value, if yes then (a!=b) is not true
condition becomes true.
!> It checks if the left operand value is not greater than the right operand value, if yes (a!>b) is true
then condition becomes true.
SQL Logical Operators
Operator Description
ALL It compares a value to all values in another value set.
AND It allows the existence of multiple conditions in an SQL statement.
ANY It compares the values in the list according to the condition.
BETWEEN It is used to search for values that are within a set of values.
IN It compares a value to that specified list value.
NOT It reverses the meaning of any logical operator.
OR It combines multiple conditions in SQL statements.
EXISTS It is used to search for the presence of a row in a specified table.
LIKE It compares a value to similar values using wildcard operator.
SQL Table
• SQL Table is a collection of data which is organized in
terms of rows and columns. In DBMS, the table is known
as relation and row as a tuple.
• Table is a simple form of data storage. A table is also
considered as a convenient representation of relations.
• Let's see an example of the EMPLOYEE table: In the table,
"EMPLOYEE" is the table name, "EMP_ID", "EMP_NAME", "CITY",
"PHONE_NO" are the column names. The combination of data of
multiple columns forms a row, e.g., 1, "Kristen", "Washington"
and 7289201223 are the data of one row.
EMP_ID EMP_NAME CITY PHONE_NO
1 Kristen Washington 7289201223
2 Anna Franklin 9378282882
3 Jackson Bristol 9264783838
4 Kellan California 7254728346
5 Ashley Hawaii 9638482678
Operation on Table
[Link] table
[Link] table
[Link] table
[Link] table
SQL Create Table
• SQL create table is used to create a table in the database. To
define the table, you should define the name of the table and
also define its columns and column's data type.
• Syntax EXAMPLE:
create table "table_name"SQL> CREATE TABLE EMPLOYEE (
EMP_ID INT NOT NULL,
("column1" "data type", EMP_NAME VARCHAR (25) NOT NULL,
"column2" "data type", PHONE_NO INT
ADDRESS CHAR (30),
NOT NULL,
"column3" "data type", PRIMARY KEY (ID)
);
...
"columnN" "data type");
Drop table
• A SQL drop table is used to delete a table definition and
all the data from a table. When this command is
executed, all the information available in the table is
lost forever, so you have to very careful while using this
command.
• Syntax DROP TABLE "table_name";
• EXAMPLE: SQL>DROP TABLE EMPLOYEE;
SQL DELETE table
• In SQL, DELETE statement is used to delete rows from a
table. We can use WHERE condition to delete a specific
row from a table. If you want to delete all the records
from the table, then you don't need to use the WHERE
clause.
• Syntax DELETE FROM table_name WHERE condition;
Example
SQL> DELETE FROM EMPLOYEE WHERE EMP_ID = 3;
Views in SQL
• Views in SQL are considered as a virtual table. A view
also contains rows and columns.
• To create the view, we can select the fields from one or
more tables present in the database.
• A view can either have specific rows based on certain
condition or all the rows of a table.
Advantages of View:
[Link]: Views help to reduce the complexity. Different
views can be created on the same base table for different users.
[Link]: It increases the security by excluding the sensitive
information from the view.
[Link] Simplicity: It helps to simplify commands from the user.
A view can draw data from several different tables and present it
as a single table.
[Link]: A view can present a consistent, unchanged
image of the structure of the database. Views can be used to
rename the columns without affecting the base table.
[Link] Integrity: If data is accessed and entered through a view,
the DBMS can automatically check the data to ensure that it
meets the specified integrity constraints.
[Link] Capacity: Views take very little space to store the
data.
Creating view
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
SQL Index
• Indexes are special lookup tables. It is used to retrieve data
from the database very fast.
• An Index is used to speed up select queries and where clauses.
But it shows down the data input with insert and update
statements. Indexes can be created or dropped without
affecting the data.
• An index in a database is just like an index in the back of a
book.
• For example: When you reference all pages in a book that
discusses a certain topic, you first have to refer to the index,
which alphabetically lists all the topics and then referred to one
or more specific page numbers.
Create Index statement
• Syntax
CREATE INDEX index_name
ON table_name (column1, column2, ...);
• Example
CREATE INDEX idx_name
ON Persons (LastName, FirstName);