Está en la página 1de 99

Csci455

Fall 2008
reza@aero.und.edu

1
 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

2
 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

3
A comprehensive non-procedural database
language package that supports standard
◦ Supports both DDL and DML
◦ Provides facilities to specify security, authorization,
and constraints

4
 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

5
 SQL schema
◦ Used to group tables and related constructs
◦ identified by Schema
 Name
 Elements
 tables
 constraints
 view, domains
 authorization constructs

6
 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

7
 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

8
 CREATE TABLE DEPARTMENT (
DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );

9
 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) );

10
◦ 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)

11
◦ User defined data type
 Domain in SQL
 CREATE DOMAIN SSN_TYPE AS CHAR (9);

12
 Constraints and default values can be
specified on each
◦ attributes
◦ tuple
◦ table

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
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
◦ 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

17
18
 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

19
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

20
21
 Names to constraints
◦ Used to identify/modify a
particular constraint
◦ Works like CONSTANT declaration
in Program Languages
◦ Figure 8.2

22
23
◦ Used at the end of schema
◦ Applies to individual tuples
◦ Use CHECK for more general constraints
 E.g.,
 CHECK (DEPT_CREATE_DATE < MGRSTARTDATE);

24
 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

25
 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

26
 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)

27
 General Form:
◦ SELECT <attributes list>
◦ FROM <tables list>
◦ WHERE <condition>;

28
 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’

29
30
 Getthe 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

31
 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’

32
 In SQL, same name can be used
◦ For more than one attribute in different tables
◦ used in recursive queries

33
 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

34
 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

35
Result of Query
E.Fname E.Lname S.Fname S.Lname

Jongm Smith Franklin Wong


Franklin Wong James Borg
Alicia Zelaya Jennifer Wallace
… … … …
.
 No WHERE-clause means no conditions
 No condition means Cross product operations

()

37
 Get all combinations of EMPLOYEE.SSN and
DEPARTMENT.DNAME
◦ SELECT SSN, DNAME
◦ FROM EMPLOYEE, DEPARTMENT

 Get all Employee SSN


◦ SELECT SSN
◦ FROM EMPLOYEE

38
 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;
◦ WHERE DNO=5 and Dname=‘Research’

39
 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

40
◦ Retrieve the salary of every employee,
 SELECT ALL SALARY
 FROM EMPLOYEE

Salary
3000
4000
2500
2500

41
 Get the salary of every employee USING
distinct (set)
◦ SELECT DISTINCT SALARY
Salary
◦ FROM EMPLOYEE
3000
4000
2500
4300
5500

42
 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

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
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

45
More on 3-value
 In SELECT-PROJECT-JOIN queries, only those
combinations of tuples that are evaluated to
TRUE are selected
 Q18: Get the names of all employees who do
not have supervisors
◦ SELECT Fname, Lname
◦ FROM EMPLOYEE
◦ WHER Super_ssn IS NULL

47
 Complex SQL queries can be formulated
using
◦ UNION
◦ INTERSECTION
◦ EXECEPT

48
 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

49
◦ (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’)

50
 Some queries need the existing values in the
database to be retrieved & compared
 Nested queries

◦ used to formulate such queries

51
 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

52
 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’);

53
 Correlated Queries:
◦ When a condition in WHERE clause of a nested
query references attribute(s) of a relation defined in
the outer query

54
 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);

55
 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;

56
 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’)

57
 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

58
 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)

59
 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)

60
 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)

61
 Join operation can be specified in FROM
clause
◦ Understandability
◦ Use AS construct to Rename join attributes
◦ Default is inner join

62
 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’

63
◦ SELECT FNAME, LNAME, ADDRESS
◦ FROM ( EMPLOYEE NATURAL JOIN (DEPARTMENT AS
DEPT (DNAME, DNO, MSSN, MSDATE)))
◦ WHERE DNAME =‘Research’

64
 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

65
 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);

66
 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

67
 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’

68
 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

69
 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)

70
71
 Used with GROUP BY clause
 Used as a condition on the sub-relations or

groups
◦ Groups satisfying the conditions are selected

72
 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)

73
74
 Count the total number of employees whose
salaries exceed $40,000 in each department,
but only for department where more than five
employees work

75
 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

76
 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

77
 The processing steps
◦ 1) the WHERE Clause is executed to select individual
tuples
◦ 2) HAVING clause executed to select individual
groups of tuples

78
 The Insert Command
◦ To add a new tuple to employee
 INSERT INTO EMPLOYEE
 VALUES (‘Richard’,’K’,’Marini’,653298653,’30-dec-52’,
‘98 Oak Forest, Katy, TX’,’M’, 37000,’987654321’, 4)

79
 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,

80
 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;

81
 Removes tuples from a relation one at a time
◦ DELETE FROM EMPLOYEE
◦ WHERE LNAME=‘Brown’

82
 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’);

83
 DELETE FROM EMPLOYEE
◦ Creates empty table

84
 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;

85
 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’);

86
 Specifying ASSERTIONS/Triggers
 Creating Views
 Writing programs
 Specifying physical DB
 Granting and revoking privileges
 Specifying Complex Objects
 Concurrency Control/Recovery mechanisms

87
 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));

88
 Constraints
◦ Prevent the data from being made inconsistent by
any kind of statement
 Triggers
◦ Maintain database consistency and defined
operationally

89
 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

90
◦ 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

91
 SELECT Fname, Lname
 FROM WORKS_ON1
 WHERE Pname =‘ProjectX’;

92
 DROP VIEW WORKS_ON1;

93
 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)

94
 A View is always up to date
 A view is realized at the time we execute a

query

95
 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

96
 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’;

97
 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

98
99

También podría gustarte