Unit-3 CH I
Unit-3 CH I
Unit-3 CH I
CONSTRAINTS, TRIGGERS
DBMS U3 CH I
• FORM OF BASIC SQL QUERY
• A conceptual evaluation strategy is a way to evaluate the
query that is intended to be easy to understand, rather than
efficient.
• A DBMS would typically execute a query in a different
and more efficient way.
• The basic form of an SQL query is as follows:
SELECT [ DISTINCT ] select-list
FROM from-list
WHERE qualification
• Such a query intuitively corresponds to a relational algebra
expression involving selections, projections, and cross-
products.
• Every query must have a SELECT clause, which species
columns to be retained in the result, and a FROM clause,
which species a cross-product of tables.
• The optional WHERE clause species selection conditions on
the tables mentioned in the FROM clause. Let us consider a
simple query.
Sailors(sid: integer, snarne: string, rating: integer, age: real)
Boats( bid: integer, bnarne: string, coloT: string)
Reserves (sid: integer, bid: integer, day: date)
Find the names and ages of all sailors.
SELECT DISTINCT S.sname, S.age
FROM Sailors S
Find all sailors with a rating above 7.
SELECT S.sid, S.sname, S.rating, S.age
FROM Sailors AS S
WHERE S.rating > 7
ALL Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
• Product(ProductID,ProductName,SupplierID,CategoryID,Unit,Price)
• Orderdetails(OrderDetailID,OrderID, ProductID,Quantity)
• Example
SELECT ProductName FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE
Quantity = 10);
• Example
SELECT ProductName
FROM Products
WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE
Quantity = 10);
(Q22) Find sailors whose rating is better than some sailor called Horatio.
(Q23) Find sailors whose rating is better than every sailor' called Horatio.
Find the sailors with the highest rating.
SELECT S.sid
FROM Sailors S
WHERE S.rating >= ALL ( SELECT S2.rating
FROM Sailors S2 )
More Examples of Nested Queries
• This formulation of the query illustrates how queries involving
INTERSECT can be rewritten using IN, which is useful to know if
your system does not support INTERSECT.
• Queries using EXCEPT can be similarly rewritten by using NOT
IN.
• To Find the sids of sailors who have reserved red boats but not
green boats, we can simply replace the keyword IN in the previous
query by NOT IN.
AGGREGATE OPERATORS
• SQL supports five aggregate operations, which can be applied on
any column, say A, of a relation:
Count the number of sailors.
SELECT COUNT (*)
FROM Sailors S
Examples:
• select sec from student group by sec;
• select sec,count(sec) from student group by sec;
• select branch,sec from student group by branch,sec;
Having
• The HAVING clause is used in the SELECT statement to specify filter conditions for
a group of rows or aggregates.
• The HAVING clause is used with the GROUP BY clause to filter groups based on a
specified condition. If the GROUP BY clause is omitted, the HAVING clause behaves
like the WHERE clause.
• HAVING clause applies a filter condition to each group of rows, while the WHERE
clause applies the filter condition to each individual row.
Syntax:
SELECT expression1, expression2, ... expression_n, aggregate_function
(expression)
FROM tables [WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING condition;
Example:
select branch,sec,count(sec) from student group by branch,sec having
count(sec)>1;
• (Q31) Find the age of the youngest sailor for each rating level.
SELECT S.rating, MIN (S.age)
FROM Sailors S
GROUP BY S.rating
(QS2) Find the age of the youngest sailor who is eligible to vote for each rating level with
at least two such sailors.
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1
NULL VALUES
• we use null when the column value is either ‘Unknown or inapplicable’.
We can specify complex constraints over a single table using table constraints,
It has the form CHECK conditional-expression.
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats( bid: integer, bnarne: string, color: string)
Reserves (sid: integer, bid: integer, day: date)
we can name a constraint by preceding it with CONSTRAINT constraint-name. If the constraint is violated, the constraint
name is returned and can be used to identify the error.
Domain Constraints
rating ratingval
Assertions: ICs over Several Tables
• Table constraints are associated with a single table, although the
conditional expression in the CHECK clause can refer to other
tables.
• Table constraints are required to hold only if the associated
table is nonempty.
• Thus, when a constraint involves two or more tables, the table
constraint mechanism is sometimes cumbersome and not quite
what is desired.
• To cover such situations, SQL supports the creation of
assertions, which are constraints not associated with any one
table.
TRIGGERS AND ACTIVE DATABASES