DBMS Unit3
DBMS Unit3
SQL: Form of Basic SQL Query – Examples of Basic SQL Queries, Introduction to Nested
Queries, Correlated Nested Queries, Set – Comparison Operators, Aggregate Operators, NULL
values – Comparison using Null values – Logical connectives – AND, OR and NOT – Impact on
SQL Constructs, Outer Joins, Disallowing NULL values, Complex Integrity Constraints in SQL
Triggers.
SQL Overview
SQL is a programming language for Relational Databases. It is designed over relational algebra
and tuple relational calculus. SQL comes as a package with all major distributions of RDBMS.
SQL comprises both data definition and data manipulation languages. Using the data definition
properties of SQL, one can design and modify database schema, whereas data manipulation
properties allows SQL to store and retrieve data from database.
SQL uses the following set of commands to define database schema − Examples of Basic SQL
Queries
CREATE
For example −
DROP
For example−
Drop object_type object_name;
Drop database tutorialspoint;
Drop table article;
Drop view for_students;
ALTER
For example−
This command adds an attribute in the relation article with the name subject of string type.
SQL is equipped with data manipulation language (DML). DML modifies the database instance
by inserting, updating and deleting its data. DML is responsible for all forms data modification
in a database. SQL contains the following set of commands in its DML section −
SELECT/FROM/WHERE
INSERT INTO/VALUES
UPDATE/SET/WHERE
DELETE FROM/WHERE
These basic constructs allow database programmers and users to enter data and information into
the database and retrieve efficiently using a number of filter options.
SELECT/FROM/WHERE
SELECT − This is one of the fundamental query command of SQL. It is similar to the
projection operation of relational algebra. It selects the attributes based on the condition
described by WHERE clause.
FROM − This clause takes a relation name as an argument from which attributes are to
be selected/projected. In case more than one relation names are given, this clause
corresponds to Cartesian product.
WHERE − This clause defines predicate or conditions, which must match in order to
qualify the attributes to be projected.
For example −
INSERT INTO/VALUES
This command is used for inserting values into the rows of a table (relation).
Syntax−
INSERT INTO table (column1 [, column2, column3 ...]) VALUES (value1 [, value2, value3 ... ])
or
INSERT INTO table VALUES (value1, [value2, ... ])
For example −
UPDATE/SET/WHERE
This command is used for updating or modifying the values of columns in a table (relation).
Syntax −
For example −
DELETE/FROM/WHERE
This command is used for removing one or more rows from a table (relation).
A nested query is a query that has another query embedded within it; the embedded query is
called a sub query. The embedded query can of course be a nested query itself.
A Scalar Sub query returns a single column and a single row; i.e., is a single
value. In principal, a scalar sub query can be used whenever a single value is
needed.
A row sub query returns multiple columns, but again only a single row. A row
sub query can be used whenever a row value constructor is needed.
A table sub query returns one or more columns and multiple rows. A table sub
query can be used whenever a table is needed.
Examples
A. Select S.sname from Sailors S where S.sid in (Select R.sid from Reserves R where
R.bid=103);
A. Select S.sname from Sailors S where S.sid in (select R.sid from Reserves R where R.bid in
(select b.bid from Boats b where b.color=’red’));
Q. Find the names of Sailors who have not reserved a red boat.
A. Select S.sname from Sailors S where S.sid NOT IN (select R.sid from Reserves R where
R.bid in (select b.bid from Boats b where b.color=’red’));
Q. Find the details of student who have paid the highest amount so far in their course.
A. select sno, sname, course, totalfee, feepaid form students where (course, feepaid) in (select
course, max (feepaid) from students group by course);
Q. Find the details of students whose TOTALFEE is more than average TOTALFEE.
A. select sno, sname from students where totalfee > (select avg (totalfee) from students).
A Correlated sub query is a nested query that receives a value from the main query and then
sends a value back to main query.
Q. Find the details of student whose total fee is more than the average total fee of their course.
That means, if totalfee of a student is 10000 and the average totalfee of his/her course is less than
10000 then the details of that student are to be displayed.
A. select sno, sname, totalfee, course from students x where totalfee > (select avg (totalfee) from
students where course=x.course);
Set-Comparison Operators:
EXISTS
NOT EXISTS
IN
UNIQUE
ANY
ALL
These two operators are exclusively used in correlated sub query. EXISTS checks whether any
row is existing in the sub query, and NOT EXISTS does the opposite.
EXISTS is different from other operators like IN, ANY etc. because it doesn’t compare values of
columns, instead, it checks whether any row is retrieved from sub query or not. If any row is
retrieved from sub query the EXISTS returns true otherwise it returns false.
The following will select student who have paid at least one instalment.
Select sno, sname from students where exists (select sno from instalments where
students.sno=sno);
IN operator:
UNIQUE
SYNTAX
The operator can be any one of the standard relational operators (=,>=,>,<,<=,!=) and list is a
series of values.
Example
A. select S.sid from sailors S where S.rating >= ALL (select S2.rating from sailors S2);
Select sno, sname, totalfee, feepaid from students where feepaid > ANY (select feepaid from
students);
The following list illustrates the result of ANY and ALL operator.
10
10
10
10
Executed only for once before main-query Executed once for each row of main-query
Sends a value to main-query Receives a value from main query and sends a
value to main-query
SQL aggregate functions return a single value, calculated from values in a column.
Demo Database
The following SQL statement gets the average value of the "Price" column from the "Products"
table:
Example
SELECT AVG(Price) AS PriceAverage FROM Products;
The COUNT(column_name) function returns the number of values (NULL values will not be
counted) of the specified column:
The COUNT(DISTINCT column_name) function returns the number of distinct values of the
specified column:
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with
Microsoft Access.
Demo Database
The following SQL statement counts the number of orders from "CustomerID"=7 from the
"Orders" table:
Example
SELECT COUNT(CustomerID) AS OrdersFromCustomerID7 FROM Orders
WHERE CustomerID=7;
The MAX() function returns the largest value of the selected column.
Demo Database
The following SQL statement gets the largest value of the "Price" column from the "Products"
table:
Example
SELECT MAX(Price) AS HighestPrice FROM Products;
The MIN() function returns the smallest value of the selected column.
Demo Database
The following SQL statement gets the smallest value of the "Price" column from the "Products"
table:
Example
SELECT MIN(Price) AS SmallestOrderPrice FROM Products;
Demo Database
The following SQL statement finds the sum of all the "Quantity" fields for the "OrderDetails"
table:
Example
SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;
The GROUP BY statement is used in conjunction with the aggregate functions to group the
result-set by one or more columns.
Example
SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders
LEFT JOIN Shippers
ON Orders.ShipperID=Shippers.ShipperID
GROUP BY ShipperName;
The HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword could not be used with
aggregate functions.
Demo Database
Now we want to find if any of the employees has registered more than 10 orders.
The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
a descending order, you can use the DESC keyword.
Demo Database
ORDER BY Example
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" column:
Example
SELECT * FROM Customers
ORDER BY Country;
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
Example
SELECT * FROM Customers
ORDER BY Country DESC;
The following SQL statement selects all customers from the "Customers" table, sorted by the
"Country" and the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
The following SQL statement selects all customers from the "Customers" table, sorted ascending
by the "Country" and descending by the "CustomerName" column:
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
4.2.4.9 Lecture -9
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN
returns all rows from multiple tables where the join condition is met.
Then, if we run the following SQL statement (that contains an INNER JOIN):
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
Before we continue with examples, we will list the types of the different SQL JOINs you can
use:
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right
table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left
table
FULL JOIN: Return all rows when there is a match in ONE of the tables
SQL INNER JOIN Keyword
The INNER JOIN keyword selects all rows from both tables as long as there is a match between
the columns in both tables.
or:
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
The following SQL statement will return all customers with orders:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in
the right table (table2). The result is NULL in the right side when there is no match.
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
Demo Database
The following SQL statement will return all customers, and any orders they might have:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows
in the left table (table1). The result is NULL in the left side when there is no match.
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
The following SQL statement will return all employees, and any orders they have placed:
Example
SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID
ORDER BY Orders.OrderID;
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right
table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
Demo Database
The following SQL statement selects all customers, and all orders:
TRIGGERS:
A trigger is an operation that is executed when some kind of event occurs to the database. It can
be a data or object change.
Creation of Triggers:
Triggers are created with the CREATE TRIGGER statement.
This statement specifies that the In which table trigger is defined and on which events
trigger will be invoked.
To drop Trigger one can use DROP TRIGGER statement.
Triggers Types:
a) Row level Triggers b) Statement Level Triggers
Row Level triggers :A row level trigger is fired each time the table is affected by the
triggering statement.
Example: If an UPDATE statement updates multiple rows of a table, a row
trigger s fired once for each row affected by the update statement.
A row trigger will not run, if a triggering statement affects no rows.
If FOR EACH ROW clause is written that means trigger is row level trigger.
Statement Level Triggers: A statement level trigger is fired once on behalf of the
triggering statement, regardless of the number of rows in the table that the triggering
statement affects, even If no rows are affected.
Example: If a DELETE statement deletes several rows from a table, a statement
level DELETE trigger is fired only once.
Default when FOR EACH ROW clause is not written in trigger that means
trigger is statement level trigger
Rules of Triggers:
Triggers cannot create or modify Database objects using triggers For example, cannot
perform “CREATE TABLE… or ALTER TABLE” sqlstatements under the triggers.
It cannot perform any administrative tasks o For example, cannot perform “BACKUP
DATABASE…” task under the triggers It cannot pass any kind of parameters.
It cannot directly call triggers WRITETEXT statements do not allow a trigger
Advantages of Triggers:
Triggers are useful for auditing data changes or auditing database as well as managing business
rules. Below are some examples:
1)Triggers can be used to enforce referential integrity (For example you may not be able to apply
foreign keys)
2)Can access both new values and old values in the database when going to do any insert, update
or delete
Disadvantages of Triggers
Triggers hide database operations.
For example when debugging a stored procedure, it’s possible to not be aware that a
trigger is on a table being checked for data changes
Executing triggers can affect the performance of a bulk import operation .
Schema Refinement: Schema Refinement is the process that re-defines the schema of a
relation.A refinement approach is based on decompositions. Decomposition can eliminate the
redundancy.
Decompositions should be used only when needed. The information to be stored consists of
SNLRWH tuples. If we just store the projections of these tuples onto SNLRH and RW, are there
any potential problems that we should be aware of?
There are three potential problems to consider:Some queries become more expensive.
Given instances of the decomposed relations, we may not be able to reconstruct the
corresponding instance of the original relation!Fortunately, not in the SNLRWH example.
Checking some dependencies may require joining the instances of the decomposed relations.
Functional Dependency
Functional dependency is represented by an arrow sign (→) that is, X→Y, where X functionally
determines Y. The left-hand side attributes determine the values of attributes on the right-hand
side.
Normalization
If a database design is not perfect, it may contain anomalies, which are like a bad dream for any
database administrator. Managing a database with anomalies is next to impossible.
Update anomalies − If data items are scattered and are not linked to each other properly,
then it could lead to strange situations. For example, when we try to update one data item
having its copies scattered over several places, a few instances get updated properly while
a few others are left with old values. Such instances leave the database in an inconsistent
state.
Deletion anomalies − We tried to delete a record, but parts of it was left undeleted
because of unawareness, the data is also saved somewhere else.
Insert anomalies − We tried to insert data in a record that does not exist at all.
Normalization is a method to remove all these anomalies and bring the database to a
consistent state. NORMALIZATION
Benefits:
Un-normalized Form: This is a relation which satisfies all the properties of a relation.
1 Normal Form: A Relation is said to be in the first normal form if it is already in un-
normalized form and it has no repeating groups.
2 Normal Form: A relation is said to be in the second normal form if it is already in the first
normal from and it has no partial dependency.
3 Normal Form: A relation is said to be in the third normal form if it is already in the
second normal from and it has no transitive dependency.
Partial Dependency: In a relation having more than one key field, a subset of non-key fields
may depend on all the key fields but another subset of non-key fields may depend on only
one of the key fields. Such dependency is called partial dependency.
Transitive Dependency: In a relation, there may be dependency among non-key fields. Such
dependency is called as transitive dependency.
BCNF: A relation is said to be in Boyce-Codd normal from if it is already in the third normal
form and every determinant is a candidate key.
Determinant: - The attribute on the left-hand side of the arrow in a functional dependency is
called a determinant. In the below example the combination of SNO, SUBJECT is a
determinant.
SNO, SUBJECT → MARKS.
(Or)
A Determinant is any field or combination of filed on which some other filed is fully
functionally dependent
Super Key: A Super key is a set if one or more attributes that, taken collectively, allows us
to identify uniquely an entity in the entity set.
For example, the social-security attribute of the entity set customer is sufficient to distinguish
one customer entity form another. Thus, social-security is a super key. Similarly, the
combination of customer-name and social-security is a super key for the entity set customer.
The customer-name attribute of customer is not a super key, because several people might
have the same name.
Candidate Key: Super key for which no proper subset is a super key. Such minimal super
keys are called Candidate keys.
Suppose that a combination of customer-name and customer-street is sufficient to distinguish
among member of the customer entity set. Then, both {social-security} and {customer-name,
customer-street} are candidate keys. Although the attributes social-security and customer-
name together can distinguish customer entities, their combination does not form a candidate
key, since the attribute social-security alone is a candidate key.
Fourth Normal Form: A relation is said to be in the fourth normal form if it is already in
Boyce-Codd normal form and it has no mutli valued dependency.
Multivalued Dependency: MVD represents a dependency between attributes. For example
A, B and C in a relation, such that for each value of A there is a set of values for B and a set
of values for C. However, the set of values for B and C are independent of each other.
First Normal Form:
An atomic value is a value that cannot be divided. For example, in the table shown below, the
values in the [Color] column in the first row can be divided into "red" and "green", hence
[TABLE_PRODUCT] is not in 1NF.
A repeating group means that a table contains two or more columns that are closely related.
For example, a table that records data on a book and its author(s) with the following
columns: [Book ID], [Author 1], [Author 2], [Author 3] is not in 1NF because [Author 1],
[Author 2], and [Author 3] are all repeating the same attribute.
1st Normal Form Example
How do we bring an unnormalized table into first normal form? Consider the following
example:
This table is not in first normal form because the [Color] column can contain multiple values.
For example, the first row includes values "red" and "green."
To bring this table to first normal form, we split the table into two tables and now we have
the resulting tables:
Now first normal form is satisfied, as the columns on each table all hold just one value.
To bring this table to second normal form, we break the table into two tables, and now we have
the following:
What we have done is to remove the partial functional dependency that we initially had. Now, in
the table [TABLE_STORE], the column [Purchase Location] is fully dependent on the primary
key of that table, which is [Store ID].
To bring this table to third normal form, we split the table into two as follows:
Now all non-key attributes are fully functional dependent only on the primary key. In
[TABLE_BOOK], both [Genre ID] and [Price] are only dependent on [Book ID]. In
[TABLE_GENRE], [Genre Type] is only dependent on [Genre ID].
For example, if we have two multi-valued attributes in a relation, we have to repeat each value of
one of the attributes with every value of the other attribute, to ensure that tuples of the relation
are consistent. This type of constraint is referred to as a multi-valued dependency and result in
data redundancy. Consider the following
Prof. Black
Projective Geometry
In the above example, teachers Prof. Green, Prof. Brown, Prof. Black belongs to physics course
and basic mechanics and principles of optics are physics text books only. However, as there is no
direct relationship between members of teacher and text at a given course, we create a tuple for
every combination of member of teacher and text to ensure that the relation is consistent. We
represent a MVD between attributes are
So, MVD exist in the above relation, for example, if we want to add a new text for physics we
would have to create three new tuples, one for each member of teacher, to ensure that the relation
remains consistent. This is an example of an update anomaly caused by the presence of MVD.
We decompose the CTX relation into CT, CX relations as given below:
CT
COURSE TEACHER
CX
COURSE TEXT
The above new relations are in 4NF there is no MVD. Note that the 4NF relations do not display
data redundancy and the potential for update anomalies is removed. For example, to add a new
text for physics, we simple create a single tuple in the CX relation.
Join Dependency:
A join dependency (JD) can be said to exist if the join of R1 and R2 over C is equal to relation R.
Where, R1 and R2 are the decompositions R1(A, B, C), and R2 (C,D) of a given relations R (A, B, C, D).
Alternatively, R1 and R2 is a lossless decomposition of R. In other words, *(A, B, C, D), (C, D) will be a
join dependency of R if the join of the join’s attributes is equal to relation R. Here, *(R1, R2, R3, ....)
indicates that relations R1, R2, R3 and so on are a join dependency (JD) of R
Inclusion Dependency:
An Inclusion Dependency is a statement of the form that some columns of a relation are
contained in other columns. A foreign key constraint is an example of inclusion dependency.