DBMS Unit 3
DBMS Unit 3
SQL: QUERIES, CONSTRAINTS, TRIGGERS: form of basic SQL query, UNION, INTERSECT,
and EXCEPT, Nested Queries, aggregation operators, NULL values, complex integrity constraints in
SQL, triggers and active databases.
1. SQLCOMMANDS
Structured Query Language (SQL) is the database language used to create a database and
to perform operations on the existing database. SQL commands are instructions used to
communicate with the database to perform specific tasks and queries with data. These SQL
commands are categorized into five categories as:
● CREATE: It is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
● DROP: It is used to delete objects from the database.
● ALTER: It is used to alter the structure of the database.
● TRUNCATE: It is used to remove all records values from a table, including all
spaces allocated for the records are removed.
ii. DQL (Data Query Language):. The purpose of DQL Command is to get data from some
schema relation based on the query passed to it.
The DQL commands are:
● SELECT–is used to retrieve data from the database.
iii. DML (Data Manipulation Language): The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation Language
and this includes most of the SQL statements. The DML commands are:
iv. DCL (Data Control Language): DCL includes commands which mainly deal with the
rights, permissions and other controls of the database system. The DCL commands are:
● GRANT-gives user’s access privileges to database.
● REVOKE-withdraw user’s access privileges given by using the GRANT command.
v. TCL (transaction Control Language):TCL commands deals with the transaction within
the database. The TCL commands are:
● COMMIT–commits a Transaction.
● ROLLBACK–rollbacks transaction in case of any error occurs.
● SAVEPOINT–sets a save point with in a transaction.
SET OPERATIONS:
UNION, INTERSECT, EXCEPT
There are mainly three set operations: UNION, INTERSECT, EXCEPT. You can refer to the
image below to understand the set operations in SQL.
● UNION: Combine two or more result sets into a single set, without duplicates.
● UNION ALL: Combine two or more result sets into a single set, including all duplicates.
● INTERSECT: Takes the data from both result sets which are in common
● EXCEPT: Takes the data from the first result set, but not in the second result set (i.e. no matching to
each other)
1. The result sets of all queries must have the same number of columns.
2. In every result set the data type of each column must be compatible (well matched) to the data type of
its corresponding column in other result sets.
Table A
Table B
UNION Operator:
The Union operator will return all the unique rows from both the queries. Notice that the duplicates are removed
from the result set.
UNION
The UNION ALL operator returns all the rows from both the queries, including the duplicates.
UNION ALL
INTERSECT Operator:
The INTERSECT operator retrieves the common unique rows from both the left and the right query. Notice the duplicates
are removed.
INTERSECT
EXCEPT Operator:
The EXCEPT operator will return unique rows from the left query that aren’t present in the right query’s results.
EXCEPT
If you want the rows that are present in Table B but not in Table A, reverse the queries.
EXCEPT
Note: UNION, INTERSECT or EXCEPT operations are possible if and only if first SELECT query and second SELECT
query produces same no of columns in same order, same column names and data type. Otherwise it gives an error.
AGGREGATE FUNCTIONS:
The SQL allows summarizing data through a set of functions called aggregate functions
1. count()
2. Sum()
3. Avg()
4. Min()
5. Max()
+-----+---------+------------+--------+--------+
| sid | sname | percentage | gender | branch |
+-----+---------+------------+--------+--------+
| 101 | hari | 99 | male | csm |
| 102 | deepu | 90 | female | ece
| 103 | pujith | 95 | male | csm |
| 104 | asritha | 78 | female csm |
| 105 | tarun | 88 | male | civil |
| 106 | mounika | 98 | female | EEE |
+-----+---------+------------+--------+--------+
+-----------------+
| count(sname) |
+-----------------+
| 6 |
+-----------------+
+---------------+
| count(branch) |
+---------------+
| 6 |
+---------------+
Example 3 : Count the number of distinct branch.
+------------------------+
| count(distinct branch) |
+------------------------+
| 4 |
+------------------------+
+----------+
| count(*) |
+----------+
| 6 |
+----------+
2 .Sum () : The sum function returns the total sum of a numeric column that you choose
+-----------------+
| sum(percentage) |
+-----------------+
| 548 |
+-----------------+
3.average () : The average function returns the average value of a numeric column that you choose
+-----------------+
| avg(percentage) |
+-----------------+
| 91.3333 |
+-----------------+
4.min() function: The min function returns the smallest value of the selected column in a table.
+-----------------+
| min(percentage) |
+-----------------+
| 78 |
+-----------------+
5.max() function: The max function returns the largest value of the selected column in a table.
+-----------------+
| max(percentage) |
+-----------------+
| 99 |
+-----------------+
NESTEDQUERIES
Nested queries are those queries which have an outer query and inner subquery. So, basically,
the sub query is a query which is nested within another query. They allow us to execute a query
within another query, making it easier to handle complex data operations. Nested queries are
commonly used for filtering data, performing calculations, or joining datasets indirectly.
Key Characteristics:
IN Operator
This operator checks if a column value in the outer query's result is present in the inner query's result. The final
result will have rows that satisfy the IN condition.
NOT IN Operator
This operator checks if a column value in the outer query's result is not present in the inner query's result. The
final result will have rows that satisfy the NOT IN condition.
ALL Operator
This operator compares a value of the outer query's result with all the values of the inner query's result and
returns the row if it matches all the values.
ANY Operator
This operator compares a value of the outer query's result with all the inner query's result values and returns the
row if there is a match with any value.
EXISTS Operator
This operator checks whether a subquery returns any row. If it returns at least one row. EXISTS operator returns
true, and the outer query continues to execute. If the subquery returns no row, the EXISTS operator returns
false, and the outer query stops execution.
NOT EXISTS Operator
This operator checks whether a subquery returns no rows. If the subquery returns no row, the NOT EXISTS
operator returns true, and the outer query continues to execute. If the subquery returns at least one row, the
NOT EXISTS operator returns false, and the outer query stops execution.
NULL Values
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 not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
Example
Let us create a table with the name CUSTOMERS in the SQL database using the CREATE statement as shown in
the query below −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY INT,
PRIMARY KEY (ID)
);
Let us insert some values into the above created table using the following query −INSERT INTO
CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000),
(2, 'Khilan', 25, 'Delhi', 1500),
(3, 'Kaushik', 23, 'Kota', 2000),
(4, 'Chaitali', 25, 'Mumbai', 6500),
(5, 'Hardik', 27, 'Bhopal', 8500),
(6, 'Komal', 22, 'Hyderabad', NULL),
(7, 'Muffy', 24, 'Indore', NULL);
Now, let us retrieve the records present in the table that are not null using the IS NOT NULL
operator −
In SQL, integrity constraints are rules that ensure data integrity in a database. They can be classified into two
categories: simple and complex.
Simple integrity constraints include primary keys, unique constraints, and foreign keys. These constraints
ensure that data in a table is unique and consistent, and they prevent certain operations that would violate these
rules.
On the other hand, complex integrity constraints are more advanced rules that cannot be expressed using simple
constraints. They are typically used to enforce business rules, such as limiting the range of values that can be
entered into a column or ensuring that certain combinations of values are present in a table.
There are several ways to implement complex integrity constraints in SQL, including:
1. Check Constraints: Check constraints are used to restrict the range of values that can be entered into a
column. For example, a check constraint can be used to ensure that a date column only contains dates in a
certain range.
In this example, we are creating a table called "Employee" with columns for ID, Name, Age, and Salary. We
have added two check constraints, one to ensure that the age is between 18 and 65, and another to ensure that
the salary is greater than or equal to 0.
2. Assertions: Assertions are used to specify complex rules that cannot be expressed using simple constraints.
They are typically used to enforce business rules, such as ensuring that a customer's age is greater than a certain
value.
CREATE TABLE Customer (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Gender VARCHAR(10),
CONSTRAINT CHK_Age CHECK (Age >= 18),
CONSTRAINT CHK_Gender CHECK (Gender IN ('M', 'F')),
CONSTRAINT CHK_Female_Age CHECK (Gender <> 'F' OR Age >= 21)
);
In this example, we are creating a table called "Customer" with columns for ID, Name, Age, and Gender. We
have added three constraints: one to ensure that the age is greater than or equal to 18, another to ensure that the
gender is either "M" or "F", and a third to ensure that if the gender is "F", the age is greater than or equal to 21.
3. Triggers: Triggers are special procedures that are executed automatically in response to certain events, such
as an insert or update operation on a table. Triggers can be used to implement complex business rules that
cannot be expressed using simple constraints.
TRIGGERS
A database trigger is a stored program which is automatically fired or executed when some events occur. A trigger can be
invoked either BEFORE or AFTER the data is changed by INSERT, UPDATE or DELETE statement.
Syntax:
CREATE TRIGGER [TriggerName]
[BEFORE | AFTER]
{INSERT | UPDATE | DELETE}
on [TableName]
[FOR EACH ROW]
[TriggerBody]
Types of triggers
BEFORE INSERT TRIGGER
Let us understand how to create a BEFORE INSERT trigger using the CREATE TRIGGER statement in
MySQL with an example.
Suppose we have created a table named employee as follows:
CREATE TABLE employee
(Name varchar (45) NOT NULL,
occupation varchar(35) NOT NULL,
Working_date date,
Working_hours varchar (10)
);
Next, we will insert some records into the employee table and then execute the SELECT statement to see the table data as
follows:
mysql>DELIMITER //
mysql>Create Trigger before_insert_occupation
BEFORE INSERT ON employee FOR EACH ROW
BEGIN
IF NEW.occupation='Scientist' THEN SET NEW.occupation='Doctor';
END IF;
END //
In this
output, we can see that on inserting the occupation column values as 'Scientist', the table will automatically fill
the 'Doctor' value by invoking a trigger.
Let us understand how to create an AFTER INSERT trigger using the CREATE TRIGGER statement in MySQL with an
example.
Mysql> DELIMITER //
Next, we will insert some records into the sales_info table as follows:
INSERT INTO sales_info(product, quantity, fiscalYear)
VALUES
('2003 Maruti Suzuki',110, 2020),
('2015 Avenger', 120,2020),
('2018 Honda Shine', 150,2020),
('2014 Apache', 150,2020);
Then, execute the SELECT statement to see the table data as follows:
Next, we will use a CREATE TRIGGER statement to create a BEFORE UPDATE trigger. This trigger is invoked
automatically before an update event occurs in the table.
DELIMITER $$
CREATE TRIGGER before_update_salesInfo
BEFORE UPDATE
ON sales_info FOR EACH ROW
BEGIN
DECLARE error_msg VARCHAR(255);
SET error_msg = ('The new quantity cannot be greater than 2 times the current quantity');
IF new.quantity > old.quantity * 2 THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = error_msg;
END IF;
END $$
First, we can use the following statements that update the quantity of the row whose id = 2:
mysql> create trigger after_sales_updates after update on sales for each row
begin
if old.quantity <> new.quantity then
insert into saleschanges(salesid,beforequantity,afterquantity)
values(old.id,old.quantity,new.quantity);
end if;
end//
Query OK, 0 rows affected (0.01 sec)
mysql> update sales set quantity=350 where id=1;
-> //
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Next, we will insert some records into this table using the below statement:
We will then create a BEFORE DELETE trigger that inserts a new record into the salary_archives table before a
row is deleted from the salaries table.
DELIMITER $$
CREATE TRIGGER before_delete_salaries
BEFORE DELETE
ON salaries FOR EACH ROW
BEGIN
INSERT INTO salary_archives (emp_num, valid_from, amount)
VALUES (OLD. emp_num, OLD.valid_from, OLD.amount);
END$$
Let us test the above created BEFORE DELETE trigger and how we can call them. So first, we will remove a
row from the salaries table:
mysql> DELETE FROM salaries WHERE emp_num = 105;
After executing a statement, we can see that the trigger was invoked successfully and inserted a new record into the
salary_archives table.
Third, we will remove all rows from the salaries table:
mysql> DELETE FROM salaries;
Finally, we will query data from the salary_archives table again. The trigger was called four times because the DELETE
statement removed four records from the salaries table. See the below output:
Third, we will create another table named total_salary_budget that keeps the salary information from the salaries
table.
Fourth, we will use the SUM() function that returns the total salary from the salaries table and keep this
information in the total_salary_budget table:
We will then create an AFTER DELETE trigger that updates the total salary into the total_salary_budget table after
a row is deleted from the salaries table.
DELIMITER $$
Active Databases
An active database is a database that uses triggers and other event-driven functionalities. The
term "active" signifies that the DBMS reacts automatically to changes in data and predefined
events. Triggers are a primary mechanism that makes a database "active."
Reactive Behavior: The database can react to changes without external applications or users
having to intervene, thanks to the ECA rules.
Flexibility: Active databases provide more flexibility in data management and ensure better data
integrity and security.
Let's understand the concept of redundancy in DBMS with a simple student table.
In this student table, we have repeated the same department details, dept_id, dept_name, and dept_head
in every student record. This causes redundancy in the student table.
dept_i dept_nam
student_id student_name student_age dept_head
d e
Computer
1 Deepak 18 05
Science
jegadeesan
Computer
2 Rahul 18 05
Science
jegadeesan
Ramakrishn
3 Asritha 18 03 Mechanical
a
An insertion anomaly occurs when specific details cannot be inserted into the database without
the other details.
Example: Without knowing the department details, we cannot insert the student details in the
above table. The student details (student_id, student_name, and student_age) depend on the
department details (dept_id, dept_name, and dept_head).
Deletion anomaly occurs when deleting specific details loses some unrelated information from the
database.
Example: If we delete the student with student_id 3 from the above student table, we also lose the
department details with dept_id 03. Deleting student details result in losing unrelated department details.
Updation anomaly occurs when there is data inconsistency resulting from a partial data update.
Example: We wanted to update the dept_head to Ramakrishna for dept_id 03; we need to update it in all
places. If the update didn't occur in all the places (partial update), it may result in data inconsistency.
2. Wastage of Memory: Disk space is wasted due to storing same copy multiple times.
3. Storage cost increases: When multiple copies of same data is stored, need more disk space and
storage cost increases.
The term decomposition refers to the process in which we break down a table in a database into various
elements or parts. Thus, decomposition replaces a given relation with a collection of various smaller
relations. Thus, in a database, we can make any table break down into multiple tables when we want to
collect a particular set of data.
In simpler words, the process of decomposition refers to dividing a relation X into {X1, X2 …Xn}.
Decomposition is dependency preserving as well as lossless. Decomposition in DBMS removes
redundancy, anomalies and inconsistencies from a database by dividing the table into multiple tables.
Types of Decomposition
a. Lossless Decomposition
b. Lossy Decomposition
Lossless Decomposition
Decomposition is lossless if it is feasible to reconstruct relation R from decomposed tables using Joins. This is the
preferred choice. The information will not lose from the relation when decomposed. The join would result in the
same original relation.
<DeptDetails>
Dept_ID Emp_ID Dept_Name
Dpt1 Emp1 Operations
Dpt2 Emp2 HR
Dpt3 Emp3 Finance
Therefore, the above relation had lossless decomposition i.e. no loss of information
Lossy Decomposition
As the name suggests, when a relation is decomposed into two or more relational schemas, the loss of
information is unavoidable when the original relation is retrieved.
<DeptDetails>
Dept_ID Dept_Name
Dpt1 Operations
Dpt2 HR
Dpt3 Finance
Now, you won’t be able to join the above tables, since Emp_ID isn’t part of the DeptDetails relation.
Therefore, the above relation has lossy decomposition.
1. Loss of Information:
During the decomposition process, it is possible to lose some information present in the original table.
This occurs when critical relationships or dependencies between attributes are not preserved properly in
the decomposed relations. As a result, queries might not return the expected results or might become
more complex.
2. Anomalies:
Decomposition can introduce anomalies like update, insertion, and deletion anomalies. These occur when
data inconsistencies arise due to poor decomposition choices. For example, if a table is decomposed into
two smaller tables, updating information that involves both of these tables may lead to inconsistencies.
3. Join Complexity:
When a table is decomposed into multiple smaller relations, it often requires performing joins to retrieve
the original data. If the decomposition is not done efficiently, these joins can become complex and slow
down query performance significantly.
4. Redundancy:
While the goal of decomposition is to eliminate redundancy, it is possible to introduce new redundancy if
the decomposition is not carefully planned. Redundancy can lead to wasted storage space and can cause
update anomalies.
In some cases, decomposition can lead to an increase in storage space. For example, if the same data
needs to be stored in multiple decomposed tables to maintain referential integrity, it can result in
duplication of data.
6. Dependency Preservation:
Decomposition should ideally preserve all the functional dependencies from the original relation.
However, it is not always possible to preserve all dependencies, and some may be lost during the
decomposition process. This can lead to integrity issues
Functional dependency
NORMALIZATION
● Normalization is the process of minimizing the redundancy from a relation or set of
relations.
● It is used to eliminate the Insertion, Update and Deletion Anomalies.
● Normalization divides the larger table into the smaller table and links the musing
relationship.
● Normalization is done with the help of different normal form.
The inventor of the relational model Edgar Codd proposed the theory of normalization with
the introduction of the First Normal Form, and he continued to extend theory with Second and
Third Normal Form. Later he joined Raymond F. Boyce to develop the theory of Boyce-Codd
Normal Form. In software industry, they are using only up to third normal form and sometimes
Boyce-Codd Normal Form.
1NF (First Normal Form) is the basic level of database normalization. It ensures that a table is well-structured
and avoids issues like data duplication or inconsistency.
● Each table cell contains only atomic values (single value).i,e it does not contain any composite or
multi-valued attribute.
● All Values in a Column Should Be of the Same Type
● Every Column Must Have a Unique Name
● Each record needs to be uniquely identified by the primary key.
1NF Example:
The above table is not in 1NF because 501 and 502 is having two values in mobile column. If we
add a new column as alternative mobile number to the above table, then for 503 alternative
mobile number is NULL. Moreover, if a student has ‘n’ mobile numbers, then adding ‘n’ extra
column is meaningless. It is better to add extra rows. If we add extra row for each 501 and 502
then the table looks like
HTNO FIRSTNAM LASTNAME MOBILE
E
501 Jhansi Rani 99999888
88
501 Jhansi Rani 77777999
99
502 Ajay Kumar 88888888
81
502 Ajay Kumar 78978978
97
503 Priya Verma 98989898
98
But the above table violates primary key constraint. Therefore instead of adding either columns
or rows, the best solution is to split the table into two tables as shown below. If we do as shown
below, if a student having ‘n’ number of mobile numbers also can be added.
HTN FIR LA
O ST ST
NA NA
ME ME
501 Jhansi Rani
502 Ajay Kumar
503 Priya Verma
HTN MOBIL
O E
501 99999888
88
501 77777999
99
502 88888888
81
502 78978978
97
503 98989898
98
Second Normal Form (2NF) is based on the concept of fully functional dependency. It is a way to
organize a database table so that it reduces redundancy and ensures data consistency.
A relation is said to be in 2-NF if and only if
2NF Example:
There are many courses having the same course fee. Here,
The candidate key for this table is {STUD_NO, COURSE_NO} because the combination of these two columns
uniquely identifies each row in the table.
COURSE_FEE is a non-prime attribute because it is not part of the candidate key {STUD_NO, COURSE_NO}.
But, COURSE_NO -> COURSE_FEE, i.e., COURSE_FEE is dependent on COURSE_NO, which is a proper
subset of the candidate key.
Therefore, Non-prime attribute COURSE_FEE is dependent on a proper subset of the candidate key, which is a
partial dependency and so this relation is not in 2NF.
To convert the above relation to 2NF, we need to split the table into two tables such as :
3NFExample:
BOOK_DETAILS
BookID GenreID GenreTyp Price
e
1 1 Gardening 250.00
2 2 Sports 149.00
3 1 Gardening 100.00
4 3 Travel 160.00
5 2 Sports 320.00
The above table is not in3NFbecausethereexist transitive dependency. In the table able,
BookID determines GenreID {BookID→GenreID}
GenreID determines GenreType. {GenreID→GenreType}
It implies that transitive functional dependency is existing and the structure does not
satisfy third normal form. To bring this table into third normal form, we split the table into
two as follows:
BOOK_DETAILS GENRE_DETAILS
A relation (table) is said to be in the BCNF if and only if it satisfy the following conditions:
PatientI EmailID
D
101 [email protected]
102 [email protected]
103 [email protected]
In other words we can also define BCNF as there should not be any overlapping between candidate
keys. If you consider the original table (before splitting), we can get two candidate keys {PateintID,
AdmittedDate} and{EmailID, AdmittedDate}.
As there exist over lapping in the candidate keys,the table is not in BCNF. To bring it into BCNF,
we split into two tables as shown above.
4-NF (FOURTHNORMALFORM)
A relation is said to be in 4-NFif and only if it satisfies the following conditions
● It should be in the Third Normal Form.
● The table should not have any Multi-valued Dependency.
i. A table should have at-least 3 columns for it to have a multi-valued dependency.
ii. For any dependency A →B,if there exists multiple value of B for a single value of A,
If all these three conditions are true for any relation (table), then it contains multi-valued
dependency. The multi-valued dependency can be explained with an example. Let the Relation R
containing three columns A, B, C
As shown in the above figure, if 501 opted fors ubjects like Java and C# and hobbies
of 501 are Cricket and Dancing. Similarly, If 502 opted for subjects like Python and
Android and hobbies of 501 are Chess and Singing, then it can be written into a table with
three columns as follows:
HTN Subjec Hobby
O t
501 Java Cricke
t
501 Java Danci
ng
501 C# Cricke
t
501 C# Danci
ng
502 Pytho Chess
n
502 Pytho Singin
n g
502 Andro Chess
id
502 Andro Singin
id g
As there exist multivalued dependency,the above table is decomposed into two tables such
that
HTN Subjec
O t
501 Java
501 C#
502 Pytho
n
502 Andro
id
HTN Hobby
O
501 Cricke
t
501 Danci
ng
502 Chess
502 Singin
g
4. 5NF
A relation is said to be in 5-NFif and only if it satisfies the followingconditions
A table is decomposed into multiple small tables to eliminate redundancy, and when
we re-join the decomposed tables, there should not be any loss in the original data or
shold not create any new data. In simple words, joining two or more decomposed table
should not lose records nor create new records.
Example:Consider a table which contains are cord of Subject, Professor and Semester in three
columns. The primary key is the combination of all three columns. No column itself is not a
candidate key or a super key.
Inthetable,DBMS is taught by Ravindar and UmaRani in semester4,DS by Sindhusha and Venu
in sem 3. In this case, the combination of all these fields required to identify valid data.
So to make the table into 5NF,we can decompose it into three relations,
The above table is decomposed into three tables as follows to bring it into 5-NF.
Subje Professo
ct r
C Srilatha
DBMS Ravindar
DS Sindhusha
DBMS UmaRani
CN Srikanth
DS Venu
WT Srinivas
Semest Professor
er
2 Srilatha
3 Ravindar
5 Sindhusha
3 UmaRani
5 Srikanth
2 Venu
5 Srinivas
Semes Subject
ter
2 C
4 DBMS
3 DS
5 CN
5 WT