The UPDATE query is used to modify attribute values of one or more selected tuples, a WHERE clause is used to select the tuples to be modified. An additional SET clause specifies attributes to be modified and their new values.
The DELETE command removes tuples from a relation, it includes a WHERE clause to select the tuples to be deleted. Referential integrity should be enforced.
DELETE FROM <table>WHERE <condition>
Typically:
Tuples are deleted from only one table at a time unless CASCADE is specified on an integrity constraint.
A missing WHERE clause will delete all tuples.
The number of tuples deleted depends on the number of tuples that satisfy the clause.
A complete Select Query (SQL), otherwise known as a nested query, can be specified within the WHERE clause of another query (called the outer query).
There are different ways we can nest queries:
IN Clause (SQL)
The comparison operator IN compares a value v with a set (or multi-set) of values V and evaluates to TRUE if v is one of the elements in V:
SELECT NAME, ADDRESSFROM EMPLOYEEWHERE DNO IN ( SELECT DNUMBER FROM DEPARTMENT WHERE DNAME = 'Research')
The nested query selects the number of the ‘Research’ department, the other query selects an employee if its department number is in the result of the nested query.
In this example, the nested query is not correlated with the outer query.
A correlated nested query is where the condition in the WHERE clause of a nested query references an attribute of a relation declared in the outer query.
The result of a correlated nested query is different for each tuple of the relation in the outer query.
SELECT E.nameFROM EMPLOYEE AS EWHERE E.ssn IN ( SELECT essn FROM DEPENDENT WHERE essn=E.ssn AND E.name=dependent_name)
Although these can still be re-written as a single block query with a simple Join Condition (SQL).
In many cases, we want to apply Aggregate Functions (SQL) to subgroups of tuples in a relation, each subgroup of tuples consists of the set of tuples that have the same value for the grouping attributes.
We can use the GROUP BY clause for specifying the grouping attributes, which must also appear in the SELECT clause:
SELECT <attr list>FROM <table>[WHERE <cond>]GROUP BY <grouping attr>
General constraints are those that do not fit in the basic SQL categories.
It is useful for schema assertions: outside of the scope of the built-in relational model constraints (primary / unique keys, entity integrity, referential integrity)
Defines whether the state of the database is valid at any given point in time.
Create Assertion (SQL)
We can create a new assertion using CREATE ASSERTION, which includes a name, a check keyword, and a condition clause. Enforcing the assertion is up to the database implementation, such as rejecting a query that violates it.
CREATE ASSERTION SALARY_CONSTRAINTCHECK ( NOT EXISTS ( SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.salary > M.salary AND E.dno=D.number AND D.mgr_ssn=M.ssn ))
In this example, the salary of an employee must not be greater than the salary of the manager of the department that the employee works for.