Chapter 8: SQL-99
reza@aero.und.edu
www.bookspar.com | Website for
students | VTU NOTES
1
Objectives

SQL standard
◦ Data Definitions, Constraints, and Schema
Changes in SQL2
◦ Queries in SQL (basic and complex SQL
Queries)
◦ Update operations (delete, insert, and update
statements)
◦ Views (or Virtual Tables ) in SQL
◦ Specifying General Constraints as Assertions
www.bookspar.com | Website for
students | VTU NOTES
2
History of SQL
SQL stands for Structured Query
Language
 Developed by IBM
 adopted as standard language for
commercial RDBMS:

◦ SQL-86 (or SQL-1) joint effort by ANSI and OSI
◦ SQL-92 (or SQL2)
◦ SQL-99( or SQL3)
 CORE
 OPTIONAL
www.bookspar.com | Website for
students | VTU NOTES
3
SQL (cont.)
A comprehensive non-procedural database
language package that supports standard
◦ Supports both DDL and DML
◦ Provides facilities to specify security,
authorization, and constraints
www.bookspar.com | Website for
students | VTU NOTES
4
SQL Data Definition and Data types

SQL uses
◦ Table (or relation)
◦ Row (or tuple)
◦ Column (or attribute)

Data Definition Commands
◦
◦
◦
◦
◦
◦
Create Schema
Create tables
Create Domain
Create view
Alter Table/Schema
Drop Table/Schema
www.bookspar.com | Website for
students | VTU NOTES
5
Schema

SQL schema
◦ Used to group tables and related constructs
◦ identified by Schema
 Name
 Elements




tables
constraints
view, domains
authorization constructs
www.bookspar.com | Website for
students | VTU NOTES
6
Create Schema

Schema is created using
◦ CREATE SCHEMA
◦ E.g.,
 CREATE SCHEMA Company AUTHORIZATION
JSMITH
 JSMITH is the Schema Owner
◦ Catalog
 Named collection of schemas
 Information_schema
www.bookspar.com | Website for
students | VTU NOTES
7
Create Table Command in SQL

CREATE TABLE
◦ used to specify a new relation (or base table)
 CREATE TABLE EMPLOYEE
 CREATE TABLE COMPANY.EMPLOYEE
◦ CREATE VIEW
 Used to create virtual tables
◦ Attributes are ordered
www.bookspar.com | Website for
students | VTU NOTES
8
Create table: example

CREATE TABLE DEPARTMENT (
DNAME
VARCHAR(10) NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9),
MGRSTARTDATE
CHAR(9) );
www.bookspar.com | Website for
students | VTU NOTES
9
More example

Key attributes can be specified via the PRIMARY KEY and UNIQUE
phrases
CREATE TABLE DEPT (
DNAME
VARCHAR(10) NOT NULL,
DNUMBER
INTEGER
NOT NULL,
MGRSSN
CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES
EMPLOYEE(SSN) );
www.bookspar.com | Website for
students | VTU NOTES
10
SQL: Basic Data Types
◦ Basic Data types
 Numeric
 Integer, Real
 Char string
 Fixed length (CHAR(n))
 Varying length (VARCHAR(n)
 bit-string,
 Fixed (BIT(n)) or varying VARYING(n)
 date/time
 Boolean (T,F, Unknown)
 Timestamps (includes both date and time)
www.bookspar.com | Website for
students | VTU NOTES
11
SQL: User Defined Data Type
◦ User defined data type
 Domain in SQL
 CREATE DOMAIN SSN_TYPE AS CHAR (9);
www.bookspar.com | Website for
students | VTU NOTES
12
Specifying Constraints using SQL

Constraints and default values can be
specified on each
◦ attributes
◦ tuple
◦ table
www.bookspar.com | Website for
students | VTU NOTES
13
Specifying NULL and Default Values
NULLS can be used as attribute values
 A NOT NULL constraint can be used to
specify that NULL is not permitted
 DEFAULT Value

www.bookspar.com | Website for
students | VTU NOTES
14
www.bookspar.com | Website for
students | VTU NOTES
15
15
CHECK Clause

CHECK Clause
◦ Used to restrict attribute or domain values

E.g.,
◦ To restrict department numbers between integer 1-20
integer
 DNUMBER INT NOT NULL CHECK (DNUMBER>0 AND
DNUMBER <21)
◦ Check clause can also be used to create the domain
 CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM>0 AND
D_NUM <21)
 D_NUM can be used as attribute domain for
 DNO
 Dnumber
 Dum
www.bookspar.com | Website for
students | VTU NOTES
16
Specifying Key and referential
Integrity Constraints
◦ PRIMARY KEY CLUASE used to specify PK
 E.g., PRIMARY KEY (DNUMBER);
 E.g., Dnumber INT PRIMARY KEY;
◦ FOREIGN KEY CLUASE used to specify FK
 FOREIGN KEY (DNUMBER) REFERENCES
DEPARTMENT (DUNMBER)
◦ UNIQUE CLAUSE
 Used for secondary keys
◦ Figure 8.1
www.bookspar.com | Website for
students | VTU NOTES
17
www.bookspar.com | Website for
students | VTU NOTES
18
Violation of Integrity Constraints (IC)

Update/delete/insertion of tuples may violate referential
integrity constraints
◦ The default action is Reject the operation

Schema Designer can specify an alternative action using
referential triggered action clause to any FK constraint
◦ Option include
 SET DEFAULT
 SET NULL
 CASACADE
◦ Option must be qualified
 ON DELETE or ON UPDATE
www.bookspar.com | Website for
students | VTU NOTES
19
Referential Integrity
DNO should be Null or
should match a PK of DEPT
EMPLOEE
FNAME
MINT
LNAME
SSN
BDATE ADDRESS SEX
SUPERSSN DNO
DEPT
SNAME DNUM MGRSSNMSDATE
DEPT_LOC
DNUM
DLOC
www.bookspar.com | Website for
students | VTU NOTES
20
20
www.bookspar.com | Website for
students | VTU NOTES
21
Named constraints
 Names
to constraints
◦ Used to identify/modify a particular
constraint
◦ Works like CONSTANT declaration
in Program Languages
◦ Figure 8.2
www.bookspar.com | Website for
students | VTU NOTES
22
www.bookspar.com | Website for
students | VTU NOTES
23
23
Specifying Constraints on Tuples
Using CHECK
◦ Used at the end of schema
◦ Applies to individual tuples
◦ Use CHECK for more general constraints
 E.g.,
 CHECK (DEPT_CREATE_DATE < MGRSTARTDATE);
www.bookspar.com | Website for
students | VTU NOTES
24
SCHEMA CHANGE COMMANDS
IN SQL

THE DROP COMMAND
◦ CASCADE
◦ RESTRICT
◦ Used to drop the named schema elements from
database schema
◦ E.g.,
 DROP SCHEMA COMPANY CASCADE
 DROP TABLE DEPENDENT CASCADE
◦ RESTRCIT
 Used to drop the schema if it has no elements in it
◦ Delete vs. Drop
 Use delete if you want to delete the records but not the
table definition
www.bookspar.com | Website for
students | VTU NOTES
25
The ALTER TABLE Command

Used to change the definition of base
table
◦ E.g.,
 ALTER TABLE COMPANY.EMPLOYEE ADD JOB
VARCHAR(12)
 ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS
CASCADE;
 ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN
DROP DEFAULT;
 ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN
SET DEFAULT “344556677”
 ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT
EMPSUPERFK CASCADE
www.bookspar.com | Website for
students | VTU NOTES
26
Basic Queries in SQL

SQL has only one basic statement to
retrieve information
◦ SELECT
◦ FROM
◦ WHERE
no relationship to the  operation of
relational algebra
 Important features

◦ supports the notion of multi-set (or Bag)
www.bookspar.com | Website for
students | VTU NOTES
27
The Select-From-Where Structure
of SQL Queries

General Form:
◦ SELECT <attributes list>
◦ FROM <tables list>
◦ WHERE <condition>;
www.bookspar.com | Website for
students | VTU NOTES
28
Some example: Query 0

Get the birthday and address of the
employee(s) whose name is ‘John B. Smith’
◦ SELECT BDATE, ADDRESS
◦ FROM EMPLOYEE
◦ WHERE FNAME=‘John’ AND MINIT =‘B’
AND LNAME = ‘SMITH’
www.bookspar.com | Website for
students | VTU NOTES
29
www.bookspar.com | Website for
students | VTU NOTES
30
Query 1
 Get
the name and address of all
employee who work for the
‘Research’ Dept.
◦ SELECT FNAME, LNAME, ADDRESS
◦ FROM EMPLOYEE, DEPARTMENT
◦ WHERE DNAME=‘Research’ AND
DNUMBER =DNO
www.bookspar.com | Website for
students | VTU NOTES
31
Query 2

For every project located in ‘Stafford’, list
the project number, the controlling
department number, and the department
manager’s last name, address, and
birthrate
◦
SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE
◦
FROM
PROJECT, DEPARTMENT, EMPLOYEE
◦ WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND PLOCATION = ‘Stafford’
www.bookspar.com | Website for
students | VTU NOTES
32
Ambiguous Attribute Names and Renaming

In SQL, same name can be used
◦ For more than one attribute in different
tables
◦ used in recursive queries
www.bookspar.com | Website for
students | VTU NOTES
33
Example 1: Same name different tables

To remove ambiguity, we need to qualify
the attributes : use ‘.’ separator to qualify
the attribute
 e.g., suppose LNAME=NAME, and
DNO=DNUMBER , DNAME=NAME
 SELECT FNAME, EMPLOYEE.NAME, ADDRESS
 FROM EMPLOYEE, DEPARTMENT
 WHERE DEPARTMENT.DNUMBER =
EMPLOYEE.DNUMBER AND DEPARTMENT.NAME =
‘Research
www.bookspar.com | Website for
students | VTU NOTES
34
Example 2: Recursive relationships

For each employee, find the employee’s
first and last name and the first and last
name of her/his immediate supervisor
◦ SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME
◦ FROM EMPLOYEE AS E, EMPLOYEE AS S
◦ WHERE E.SUPERSSN=S.SSN
www.bookspar.com | Website for
students | VTU NOTES
35
Result of Query
E.Fname
E.Lname
S.Fname
S.Lname
Jongm
Smith
Franklin
Wong
Franklin
Wong
James
Borg
Alicia
Zelaya
Jennifer
Wallace
…
…
…
…
.
www.bookspar.com | Website
for students | VTU NOTES
36
SQL: Unspecified WHERE-Clause
No WHERE-clause means no conditions
 No condition means Cross product
operations ()

www.bookspar.com | Website for
students | VTU NOTES
37
Example Q10: No WHERE
Clause

Get all combinations of EMPLOYEE.SSN
and DEPARTMENT.DNAME
◦ SELECT SSN, DNAME
◦ FROM
EMPLOYEE, DEPARTMENT

Get all Employee SSN
◦ SELECT
◦ FROM
SSN
EMPLOYEE
www.bookspar.com | Website for
students | VTU NOTES
38
Example: Use of ‘*’ in select
Use * to get all attributes
 Get all employees working for Dept. 5

◦ SELECT *
◦ FROM EMPLOYEE
◦ WHERE DNO=5

E.g., 2
◦ SELECT *
◦ FROM EMPLOYEE, DEPARTMENT;
www.bookspar.com | Website for
◦ WHERE DNO=5 and Dname=‘Research’
students | VTU NOTES
39
Tables as Set in SQL



SQL treats tables as a multi-set (i.e., a set having
duplicates)
Why?
◦ Duplicate elimination is an expensive operation (sort and
delete)
◦ user may be interested in the result of a query
◦ in case of aggregate function, we do not want to eliminate
duplicates
SQL Table with a key is a SET by definition
◦ Why? Because key must be unique

To treat tables as sets use DISTINCT in SELECT
statement
www.bookspar.com | Website for
students | VTU NOTES
40
Query 11 (ALL)
◦ Retrieve the salary of every employee,
 SELECT ALL SALARY
 FROM EMPLOYEE
Salary
3000
4000
2500
2500
…
www.bookspar.com | Website for
students | VTU NOTES
41
Query 12 (DISTINCT)

Get the salary of every employee USING
distinct (set)
◦ SELECT DISTINCT SALARY
◦ FROM EMPLOYEE
Salary
3000
4000
2500
4300
5500
…
www.bookspar.com | Website for
students | VTU NOTES
42
SQL and NULL

NULL represents
◦ Value is not known
 e.g., an unknown address
◦ Value is not available
 E.g., unlisted phone number
◦ Value does not apply
 E.g., Unmarried employee has no name for his/her spouse
www.bookspar.com | Website for
students | VTU NOTES
43
NULL and Comparison Operators

Comparisons involving NULL
◦ When NULL is involved in comparison, then the result
considered to be unknown
◦ Unknown (maybe true or maybe false)
 SQL uses 3-valued logic
◦ TRUE,
◦ FALSE,
◦ UNKNOWN
 How define the SQL evaluate the 3-valued logical
expressions involving
◦ AND
◦ OR
◦ NOT
www.bookspar.com | Website for
students | VTU NOTES
44
Truth table for 3-valued logic
x
y
x AND y
x OR y
Not x
TRUE
TRUE
TRUE
TRUE
FALSE
TRUE
UNKNOWN
UNKNOWN
TRUE
FALSE
TRUE
FALSE
FALSE
TRUE
FALSE
UNKNOWN
TRUE
UNKNOWN
TRUE
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
UNKNOWN
FALSE
FALSE
UNKNOWN
UNKNOWN
FALSE
TRUE
FALSE
TRUE
TRUE
FALSE
UNKNOWN
FALSE
UNKNOWN
TRUE
FALSE
FALSE
FALSE
FALSE
TRUE
www.bookspar.com | Website
for students | VTU NOTES
45
More on 3-value

In SELECT-PROJECT-JOIN queries, only those
combinations of tuples that are evaluated to
TRUE are selected
www.bookspar.com | Website
for students | VTU NOTES
46
Query involved NULL

Q18: Get the names of all employees
who do not have supervisors
◦ SELECT Fname, Lname
◦ FROM EMPLOYEE
◦ WHER Super_ssn IS NULL
www.bookspar.com | Website for
students | VTU NOTES
47
Query using Union, Intersection,
EXECEPT

Complex SQL queries can be formulated
using
◦ UNION
◦ INTERSECTION
◦ EXECEPT
www.bookspar.com | Website for
students | VTU NOTES
48
Example of UNION: Q4A

Make a list of Project numbers for
projects that involve an employee whose
last name is ‘Smith’, either as a worker or
as a manger of the department that
controls the project
www.bookspar.com | Website for
students | VTU NOTES
49
Query 4: Using Union Query
◦ (SELECT DISTINCT PNUMBER
◦ FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN
AND LNAME=‘Smith’)
◦ UNION
◦ (SELECT DISTINCT PNUMBER
◦ FROM WORKS_ON, EMPLOYEE , PROJECT
◦ WHERE PNUMBR = PNO AND ESSN=SSN AND
LNAME=‘Smith’)
www.bookspar.com | Website for
students | VTU NOTES
50
Complex SQL Queries: Nested
Queries
Some queries need the existing values in
the database to be retrieved & compared
 Nested queries

◦ used to formulate such queries
www.bookspar.com | Website for
students | VTU NOTES
51
Q4: Using Nested query

Make a list of Project numbers for
projects that involve an employee whose
last name is ‘Smith’, either as a worker or
as a manger of the department that
controls the project
www.bookspar.com | Website for
students | VTU NOTES
52
Query Q4A: Using nested query
 SELECT DISTINCT PNUMBER
 FROM PROJECT
WHERE PNUMBER IN
(SELECT PNUMBER
 FROM DEPARTMENT, EMPLOYEE , PROJECT
 WHERE DNUM = DNUMBE AND
MGRSSN = SSN AND LNAME = ‘Smith’)
 OR

PNUMBER IN
 ( SELECT PNO
 FROM WORKS_ON, EMPLOYEE
 WHERE ESSN=SSN AND LNAME = ‘Smith’);
www.bookspar.com | Website for
students | VTU NOTES
53
Correlated nested queries

Correlated Queries:
◦ When a condition in WHERE clause of a
nested query references attribute(s) of a
relation defined in the outer query
www.bookspar.com | Website for
students | VTU NOTES
54
Query 16:Example of Correlated
query

Get the name of each employee who has a dependent with
the same first name and the same sex as the employee
SELECT E.FNAME, E.LNAME
 FROM EMPLOYEE AS E
 WHERE E.SSN IN (SELECT ESSN

FROM DEPENDENT
WHERE E.FNAME=DEPENDENT_NAME
AND E.SEX=SEX);
www.bookspar.com | Website for
students | VTU NOTES
55
Q16B: Alternative Query

A nested query involving ‘=‘ or ‘IN’ can be replaced by
simple query
◦ SELECT E.FNAME,E.LNAME
◦ FROM EMPLOYEE AS E, DEPEDENT AS D
◦ WHERE E.SSN=D.ESSN AND
E.FNAME=D.DEPENDENT_NAME AND
E.SEX=D.SEX;
www.bookspar.com | Website for
students | VTU NOTES
56
More on Set comparisons

Get SSN of all employees who work on
the same combination (project, hours) on
some project that employee ‘John Smith’
with SSN=‘123456789’
◦ SELECT DISTINCT ESSN
◦ FROM WORKS_ON
◦ WHERE (PNO, HOURS) IN
(SELECT PNO, HOURS
FROM WORKS_ON
WHERE ESSN=‘123456789’)
www.bookspar.com | Website for
students | VTU NOTES
57
THE EXISTS AND UNIQUE
FUNCTIONS IN SQL

EXISTS:
◦ Checks the result (i.e, SET) of the correlated
nested query to see if it is empty or not
 If the result (i.e., set) is non-empty it returns TRUE
 If the result is empty it returns FALSE
www.bookspar.com | Website for
students | VTU NOTES
58
Query 16: example of EXISTS

Find the name of each employee who has
a dependent with the same first name and
same sex as the employee
◦ SELECT E.FNAME, E.LNAME
◦ FROM EMPLOYEE AS E
◦ WHERE EXISTS ( SELECT *
◦
FROM DEPENDENT
◦
WHERE E.SSN=ESSN AND
SEX=E.SEX
AND
E.FNAME=DEPENT_NAME)
www.bookspar.com | Website for
students | VTU NOTES
59
Query 16: example of NOT
EXISTS

Find the name of each employee who has
no dependent
◦ SELECT FNAME, LNAME
◦ FROM EMPLOYEE E
◦ WHERE NOT EXISTS
◦
(SELECT *
◦
FROM DEPENDENT
◦
WHERE SSN=ESSN)
www.bookspar.com | Website for
students | VTU NOTES
60
EXPLICIT SETS AND NULLS IN
SQL

Retrieve SSNs of all employees who work
on project number 12,22,or 33
◦ SELECT DISTINCT ESSN
◦ FROM WORKS_ON
◦ WHERE PNO IN (12,22,33)
www.bookspar.com | Website for
students | VTU NOTES
61
Join (revisited): FROM CLAUSE

Join operation can be specified in FROM
clause
◦ Understandability
◦ Use AS construct to Rename join attributes
◦ Default is inner join
www.bookspar.com | Website for
students | VTU NOTES
62
Example 1: Join in FORM

Find the name and address of employees
who work in Research department
◦ SELECT FNAME, LNAME, ADDRESS
◦ FROM (EMPLOYEE JOIN DEPARTMENT ON
DNO=DNUMBER)
◦ WHERE DNAME = ‘Research’
www.bookspar.com | Website for
students | VTU NOTES
63
Example 2: Renaming and
NATURAL JOIN
◦ SELECT FNAME, LNAME, ADDRESS
◦ FROM ( EMPLOYEE NATURAL JOIN
(DEPARTMENT AS DEPT (DNAME, DNO,
MSSN, MSDATE)))
◦ WHERE DNAME =‘Research’
www.bookspar.com | Website for
students | VTU NOTES
64
The keyword ALL
ALL is comparison operators
 Can be used with any other comparison
operators

◦ E.g.
 v> ALL V
 where V is a SET
 It returns TRUE if v > every element in V
www.bookspar.com | Website for
students | VTU NOTES
65
Example KEYWORD ALL

get the names of all employees whose
salary is greater than the salary of ALL
the employees in department 5
◦ SELECT LNAME, FNAME
◦ FROM EMPLOYEE
◦ WHERE SALARY > ALL
 (SELECT SALARY FROM EMPLOYEE WHERE
DNO=5);
www.bookspar.com | Website for
students | VTU NOTES
66
SCOPE of nested queries

E.g.,
◦
◦
◦
◦
◦
SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN
IN (SELECT ESSN FROM DEPENDENT
WHERE ESSN=E.SSN AND
E.FNAM=DEPENDENT_NAME
AND
SEX=E.SEX
www.bookspar.com | Website for
students | VTU NOTES
67
Aggregate Functions

Q20: Find the sum of the salaries of all employees in the
‘Research’ dept, as well as the max salary, the min salary, and
average in that dept.
◦ SELECT SUM(SALARY), MAX(SALARY),
MIN(SALARY)
AVG(SALARY)
◦ FROM (EMPLOYEE NATURAL JOIN
(DEPARTMENT AS DEPT(Dname, Dno, Mssn,
Msdate)))
◦ WHERE DNAME=‘Research’
www.bookspar.com | Website for
students | VTU NOTES
68
Example of grouping and
COUNT

GROUP BY CLUASE
◦ Used to partition the relation into sub-relation
◦ Works with aggregate functions (e.g. COUNT)
 grouping attribute
 Grouping attribute (s) need to be specified in SELECT clause
 Create separate group for the grouping attribute with NULL values
www.bookspar.com | Website for
students | VTU NOTES
69
QUERY 24 (GROUP BY)

For each department, retrieve the
department number, the number of
employees in the department, and their
average salary
◦ SELECT DNO, COUNT(*), AVG(SALARY)
◦ FROM EMPLOYEE
◦ GROUP BY DNO;
 Figure.8.6.(a)
www.bookspar.com | Website for
students | VTU NOTES
70
Figure 8.4
www.bookspar.com | Website for
students | VTU NOTES
71
HAVING Clause
Used with GROUP BY clause
 Used as a condition on the sub-relations
or groups

◦ Groups satisfying the conditions are selected
www.bookspar.com | Website for
students | VTU NOTES
72
Query 26

For each project on which more than two employees
work, get the project number, the project name, the
number of employees who work on the project
◦
◦
◦
◦
◦
◦
SELECT PNUMBER, PNAME, COUNT(*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNAME, PNUMBER
HAVING COUNT(*)>2;
Fig.8.6.(b) and (c)
www.bookspar.com | Website for
students | VTU NOTES
73
Figure 8.4
www.bookspar.com | Website for
students | VTU NOTES
74
Q28: COUNT and HAVING
Clauses (revisited)

Count the total number of employees
whose salaries exceed $40,000 in each
department, but only for department
where more than five employees work
www.bookspar.com | Website for
students | VTU NOTES
75
Version 1: ?
SELECT DNAME, COUNT (*)
 FROM DEPARTMENT, EMPLOYEE
 WHERE DNUMBER=DNO AND
SALARY>40000
 GROUP BY DNAME
 HAVING COUNT (*)>5;
 Wrong

◦ Selects only department having more than 5 employees
who ALL make more than 40K
www.bookspar.com | Website for
students | VTU NOTES
76
Version 2: Correct One
SELECT DNUMBER, COUNT (*)
 FROM DEPARTMENT, EMPLOYEE
 WHERE DNUMBER=DNO AND
SALARY>4000 AND DNO IN

◦ (SELECT DNO
◦ FROM EMPLOYEE
◦ GROUP BY DNO
◦ HAVING COUNT(*)>5)
GROUP BY DNUMBER
www.bookspar.com | Website for
students | VTU NOTES
77
Precedence rule

The processing steps
◦ 1) the WHERE Clause is executed to select
individual tuples
◦ 2) HAVING clause executed to select
individual groups of tuples
www.bookspar.com | Website for
students | VTU NOTES
78
SQL: Insert, Delete, and Update
commands

The Insert Command
◦ To add a new tuple to employee
 INSERT INTO EMPLOYEE
 VALUES (‘Richard’,’K’,’Marini’,653298653,’30-dec52’, ‘98 Oak Forest, Katy, TX’,’M’, 37000,’987654321’,
4)
www.bookspar.com | Website for
students | VTU NOTES
79
More on Insert

To enter a new tuple using known
attributes
◦ INSERT INTO EMPLOYEE (FNAME, LNAME,
SSN)
◦ VALUES (‘Richard’, ’Marini’, ‘653298653’);
 the tuple is accepted, if no constraint is violated,
www.bookspar.com | Website for
students | VTU NOTES
80
Insert Multiple tuples

INSERT command can be used to load multiple tuples
as the same time
◦ Suppose
 CREAT TABLE DEPT_INFO
 (DEPT_NAME VARCHAR(15),
NO_OF_EMPS INTEGER,
TOTAL_SAL
INTEGER);
INSERT INTO DEPTS_INFO (DEPT_NAME, NO_OF_EMPS,TOTAL_SAL)
SELECT
DNAME, COUNT(*), SUM (SALARY)
FROM
(DEPARTMENT JOIN EMPLOYEE ON DNUMBER=DNO)
GROUP BY DNAME;
www.bookspar.com | Website for
students | VTU NOTES
81
The DELECT Command

Removes tuples from a relation one at a
time
◦ DELETE FROM EMPLOYEE
◦ WHERE LNAME=‘Brown’
www.bookspar.com | Website for
students | VTU NOTES
82
Multiple DELETES

One or more tuples can be deleted
◦ E.g.. Delete all employees who work for
“Research” dept.
 DELETE FROM EMPLOYEE
 WHERE DNO IN
 (SELECT DNUMBER
 FROM DEPARTMENT
 WHERE DNAME=‘Research’);
www.bookspar.com | Website for
students | VTU NOTES
83
More on DELETE

DELETE FROM EMPLOYEE
◦ Creates empty table
www.bookspar.com | Website for
students | VTU NOTES
84
The UPDATE Command
Used to modify values of one (or
multiple) selected tuples associated with
ONE relation
 E.g.

◦ Change the location and controlling department number
of project number 10 to ‘Grand Forks’ and 5
◦ UPDATE PROJECT
◦ SET
PLOCATION = ‘Grand Forks’, DNUM=5
◦ WHERE PNUMBER=10;
www.bookspar.com | Website for
students | VTU NOTES
85
Example: Multiple changes

E.g.,
◦ Give all employees working in ‘Research’ department a 10
percent salary raise
 UPDATE EMPLOYEE
 SET
SALARY = SALARY *1.1
 WHERE DNO IN ( SELECT DNUMBER


FROM DEPARTMENT
WHERE DNAME = ‘Research’);
www.bookspar.com | Website for
students | VTU NOTES
86
Additional features
Specifying ASSERTIONS/Triggers
 Creating Views
 Writing programs
 Specifying physical DB
 Granting and revoking privileges
 Specifying Complex Objects
 Concurrency Control/Recovery
mechanisms

www.bookspar.com | Website for
students | VTU NOTES
87
Specifying General Constraints

general constraints can be specified as
follows
◦ CREATE ASSETION SALARY_CONSTRAINT
 CHECK ( NOT EXISTS ( SELECT *
FROM EMPLOYEE AS E, EMPLOYEE
AS M, DEPARTMENTAS D
WHERE E.SALARY > M. SALARY
AND E.DNO=D.NUMBER AND
D.MGRSSN=M.SSN));
www.bookspar.com | Website for
students | VTU NOTES
88
Constraints vs. Triggers

Constraints
◦ Prevent the data from being made
inconsistent by any kind of statement

Triggers
◦ Maintain database consistency and defined
operationally
www.bookspar.com | Website for
students | VTU NOTES
89
Views in SQL

A view refers to a single table that is derived from other
tables
◦ CREAT VIEW WORKS_ON1
◦ AS SELECT FNAME, LNAME, PNAME, HOURS


FROM EMPLOYEE, PROJECT, WORKS_ON
WHERE SSN=ESSN AND PNO=PNUMBER
WORKS_ON1
FNAME
LNAME
PNAME
HOURS
www.bookspar.com | Website for
students | VTU NOTES
90
Example 2
◦ CREAT VIEW DEPT_INFO(DEPT_NAME, NO_OF_EMPS,
TOTAL_SAL)
◦ AS SELECT DNAME, COUNT(*), SUM(SALARY)
 FROM EMPLOYEE, DEPARTMENT
 WHERE DNUMBER=DNO
 GROUP BY DNAME;
DEPT_INFO
DEPT_NAME NO_OF_EMPS
TOTAL_SAL
www.bookspar.com | Website for
students | VTU NOTES
91
Query on View
SELECT Fname, Lname
 FROM
WORKS_ON1
 WHERE Pname =‘ProjectX’;

www.bookspar.com | Website for
students | VTU NOTES
92
DROP VIEW

DROP VIEW WORKS_ON1;
www.bookspar.com | Website for
students | VTU NOTES
93
Database Views: applications

Used as subroutine mechanism
◦ make a complex query easier to
understand/reuse
◦ easier to debug

used as a flexible mechanism for access
CONTROL to the data (security)
www.bookspar.com | Website for
students | VTU NOTES
94
View Properties
A View is always up to date
 A view is realized at the time we execute
a query

www.bookspar.com | Website for
students | VTU NOTES
95
View Implementation

Two strategies to implement a view
◦ Query modification
 Maps the view into the base tables
◦ View materialization
 Create a temporary view table
 Uses incremental approach to keep a materialized
table up-date
 Removes the view table if it is not accessed for
certain period of time
www.bookspar.com | Website for
students | VTU NOTES
96
Query Modification

Query on
◦ SELECT Fname, Lname
◦ FROM WORKS_ON1
◦ WHERE Pname =‘ProjectX’;

Translated to
◦ SELECT Fname, Lname,
 FROM EMPLOYEE, PROJECT, WORKS_ON
 WHERE SSN=ESSN AND PNO=PNUMBER
AND Pname =‘ProjectX’;
www.bookspar.com | Website for
students | VTU NOTES
97
Updating of Views
Updating the views can be complicated
and ambiguous
 an update on a view defined on a single
table without any aggregate functions can
be mapped to an update on the base table

www.bookspar.com | Website for
students | VTU NOTES
98
Table: 8.1 (SQL Summary)
www.bookspar.com | Website for
students | VTU NOTES
99