Dbms Notes Unit 2
Dbms Notes Unit 2
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.
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.
Example:
1. Selection Operation
2. Projection Operation
3. Union Operation
4. Intersection Operation
5. Difference Operation
6. Cartisian product Operation
7. Rename Operation
Selection Operation:-
• The select operation selects tuples that satisfy a given condition.
• It is denoted by sigma (σ).
Notation:- σ p(r)
Where
σ is used for selection condition.
r is used for relation.
P is used for 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 LOAN_NO AMOUNT
Input:
σ BRANCH_NAME="perryride" (LOAN)
Output:
Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes is eliminated from the table.
o 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
Input:
∏ NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
Union Operation:
o 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.
o It eliminates the duplicate tuples. It is denoted by ∪.
Notation: R ∪ S
A union operation must hold the following condition:
o R and S must have the attribute of the same number.
o Duplicate tuples are eliminated automatically.
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all
tuples that are in both R & S.
o It is denoted by intersection ∩.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Smith
Jones
Set Difference:
o Suppose there are two tuples R and S. The set difference operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
Notation: R - S
Example Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
Cartesian product
o 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.
o It is denoted by X.
Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
EMPLOYEE X DEPARTMENT
Output:
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 Lega
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)
Join Operations:
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Example:
Table 1
ID NAME AGE
101 Alice 23
102 Bob 28
103 Charlee 32
Table 2
ID ADDRESS SALARY
1. Natural Join:
A Natural Join is a type of Join that matches columns with the same name in both tables.
Syntax of Natural Join
SELECT table1.column1, table2.column2FROM table1NATURAL JOIN table2;
Example: Let's use the above Table 1 table and Table 2 table:
Input:
SELECT Table1.ID, Table1.Name, Table1.Age, Table2.Address, Table2.SalaryFROM Table1
NATURAL JOIN Table2;
Output:
ID NAME AGE ADDRESS SALARY
102 Bob 28 New York 50000
103 Charlee 32 Boston 30000
2. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched data as
per the equality condition. The equi join uses the comparison operator (=).
Syntax of Equi Join
SELECT table1.column1, table2.column2FROM table1INNER JOIN
table2ONtable1.columnX = table2.columnY;
Example:Let's use the above Table 1 table and Table 2 table:
Input:
SELECT Table1.Name, Table2.AddressFROM Table1INNER JOIN Table2ON Table1.ID =
Table2.ID;
Output:
NAME ADDRESS
3. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
An outer join is basically of three types:
a. Left outer join
b. Right outer join
c. Full outer join
a. Left outer join:
o A Left Outer Join in DBMS returns all the rows from the left table and the matching
rows from the right table. If there is no match, NULL values are returned for the
missing rows.
o It is denoted by ⟕.
Syntax of Left Outer Join
SELECT table1.column1, table2.column2FROM table1LEFT JOIN table2ON
table1.columnX = table2.columnY;
Input:
SELECT Table1.Name, Table2.AddressFROM Table1LEFT JOIN Table2ON Table1.ID =
Table2.ID;
NAME ADDRESS
Alice Null
Charlee Boston
b. Right outer join: A Right Outer Join returns all the rows from the right table and the
matching rows from the left table. If there is no match, NULL values are returned for the
missing rows. It is denoted by ⟖.
Syntax of Right Outer Join
SELECT table1.column1, table2.column2FROM table1RIGHT JOIN table2ON
table1.columnX = table2.columnY;
Query:
SELECT Table1.Name, Table2.AddressFROM Table1RIGHT JOIN Table2ON Table1.ID =
Table2.ID;
Output:
NAME ADDRESS
Charlee Boston
A Full Outer Join returns all the rows from both tables and NULL values for the missing
rows. It is denoted by ⟗.
NAME ADDRESS
Alice Null
Relational Calculus
Relational calculus is a non procedural query language that tells the system what to retrieve
but does not tell how to retrieve.
Notation:
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345.
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
Query:
{t | Customer(t) ∧ t.Zipcode = 12345 }
Example 1: Write a TRC query to get Name of customers whose zip code is 56789.
Query:
{t.Name | Customer (t) ∧ t.Zipcode = 56789 }
Name
Rohit
Notation:
{ <a1, a2, a3, ..., an> | P (a1, a2, a3, ... ,an)}
Where
Here, <x1,x2,...,xn> represents the domain variables used to get the column values.
P(x1,x2,...,xn) is the predicate which is the condition for results.
Example 1: Write a DRC query to get Customer id,Name of customers whose zip code is
12345.
Query:
{ Customer_id, Name | ∈ Customer (t) ∧ t.Zipcode = 12345 }
Customer_id Name
1 Rohit
4. Amit
Introduction to SQL
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 databases and tables.
• All the RDBMS like MySQL, Informix, Oracle, MS Access and SQL Server use SQL
as their standard database language.
• SQL allows users to query the database in a number of ways, using English-like
statements.
Why SQL?
SQL is widely popular because it offers the following advantages −
• Allows users to access data in the relational database management systems.
• Allows users to describe the data.
• Allows users to define the data in a database and manipulate that data.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views.
Advantages of SQL
Standards Language
Long established are used by the SQL databases that are being used by ISO and ANSI.
High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of records
from a database.
SQL Commands
• SQL commands are instructions. It is used to communicate with the database. It is
also used to perform specific tasks, functions, and queries of data.
• SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1. Data Definition Language (DDL)
o DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
o CREATE
o ALTER
o DROP
o TRUNCATE
Syntax:
Syntax
ALTER: 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:
TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax:
o INSERT
o UPDATE
o DELETE
INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a
table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2,
value3, .... valueN);
For example:
UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
For example:
Syntax:
For example:
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
Syntax:
For example:Consider a scenario where you are database administrator and a student
table in the database suppose you want a specific user Reeta to only select for retrieve
the data from the student table.
Syntax:
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE
only.
These operations are automatically committed in the database that's why they cannot be used
while creating tables or dropping them.
o COMMIT
o ROLLBACK
o SAVEPOINT
Commit: Commit command is used to save all the transactions to the database.
Syntax:
COMMIT;
Example:
Rollback: Rollback command is used to undo transactions that have not already been saved
to the database.
Syntax:
ROLLBACK;
Example:
SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the
entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
o SELECT
SELECT: This is the same as the projection operation of relational algebra. It is used to
select the attribute based on the condition described by WHERE clause.
Syntax:
SELECT expressions
FROM TABLES
WHERE conditions;
For example:
SELECT emp_name
FROM employee
WHERE age > 20;