fall 2005 ics184/eecs116 – notes 08 1 ics 184/eecs116: introduction to data management lecture...

26
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: ICS 184/EECS116: Introduction to Data Introduction to Data Management Management Lecture Note 8 SQL: Structured Query Language -- DDL

Upload: myra-dixon

Post on 16-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 – Notes 08 1

ICS 184/EECS116: ICS 184/EECS116: Introduction to Data ManagementIntroduction to Data Management

Lecture Note 8

SQL: Structured Query Language

-- DDL

Page 2: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 2

SQL -- historical PerspectiveSQL -- historical Perspective

• Developed by IBM in mid 70s• First standardized in 1986 by ANSI --- SQL1• Revised in 1992 --- SQL2. Approximate 580 pages

describing syntax and semantics• In 1999, ANSI/ISO released the SQL3. Many additions for:

– support for multimedia data– addition of abstract data types and object-orientation– support for calling programmed functions from within SQL

• Every vendor has a slightly different version of SQL• If you ignore the details, basic SQL is very simple and

declarative. Hence easy to use.

Page 3: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 3

SQL in Different Roles SQL in Different Roles

• Data definition language (DDL):– allows users to describe the relations and constraints.

• Constraint specification language:– commands to specify constraints ensured by DBMS

• Query language:– relationally complete, supports aggregation and grouping– declarative -- you specify what you want and not how to retrieve, easy

to learn and program • Updates:

– insert, delete, and update tables• View definition language:

– commands to define rules– updates through views generally not supported

Page 4: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 4

SQL in Different Roles (cont)SQL in Different Roles (cont)• Embedded SQL:

– has been embedded in a variety of host languages: C, C++, PERL, Smalltalk, Java (vendor dependent)

– Impedance mismatch: SQL manipulates relations that are sets/bags --- programming languages do not handle sets/bags as efficiently.

• Transaction Control:– commands to specify beginning and end of

transactions.

Page 5: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 5

SQL as DDLSQL as DDL

CREATE TABLE Dept(dno int,dname varchar(30) not null,mgr char(15));

CREATE TABLE emp (ename char(15) not null, dno int default 0, sal int);

Don’t allow null values

Default value is 0

Page 6: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 6

Domain typesDomain types• char(n): fixed length char string• varchar(n): variable-length char string with at most n chars• int or integer• smallint• numeric(p,d): fixed-point number of given precision• real, double precision• float(n): floats with a given precision• date: containing year,month, and date – yyyy-month-day• time: in hours, minutes, and seconds – hour:min:sec• Null value is part of each domain

Page 7: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 7

Define new domainsDefine new domains

CREAT DOMAIN personDom CHAR(20);

CREATE TABLE emp (ename personDom, dno int default 0, sal real);

Page 8: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 8

Schema DefinitionSchema DefinitionCREATE TABLE r (

A1 D1 [not null] [default V1]…An Dn [not null] [default Vn]<integrity constraint 1> …<integrity constraint k>

)

Each integrity constraints could be:

– primary key

– candidate key

– foreign key

– “check(predicate)”: specifies predicate to be satisfied by each tuple

Page 9: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 9

Declaring KeysDeclaring Keys

CREATE TABLE emp

( ssn int Primary Key,

name char(15),

dno int,

);

• PRIMARY KEY or UNIQUE

CREATE TABLE emp ( ssn int, name char(15), dno int, Primary Key (ssn));

CREATE TABLE emp ( ssn int, name char(15), dno int, Primary Key (dno,name));

Multiple attributes

Page 10: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 10

ComparisonComparison

• A table has only one “primary key” in a table, but many “uniques”

• Primary keys cannot have NULLs

• “Unique” may have NULLs. – Two different tuples cannot have the same nonnull values in the “unique”

attributes.

CREATE TABLE emp ( ssn int, name char(15), dno int, Unique (dno,name));

CREATE TABLE emp ( ssn int

UNIQUE, name char(15), dno int,);

eName Dno Sal Jack 111 50K Jack 555 90K Lisa 222 80K

eName: UNIQUE

Page 11: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 11

Enforcing Key ConstraintsEnforcing Key Constraints• Check constraint each time the table is modified

– Insertion: check

– Update: check

– Deletion: do not check

• Enforcing key constraints efficiently:– Suppose “ssn” is a primary of Emp.

– Every time we insert a new employee, do we want to scan the whole table to check if the ssn already exists? No!

– Using index on the key attribute(s) Advanced topic -- ignored eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Page 12: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 12

Foreign-Key ExamplesForeign-Key Examples

CREATE TABLE emp ( ssn int, name char(15), dno int REFERENCES dept(dno),);

CREATE TABLE emp ( ssn int, name char(15), dno int, FOREIGN KEY dno REFERENCES dept(dno));

• Allow multiple attributes in one foreign-key constraint.

• Allow multiple foreign-key constraints in one table.

Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

Page 13: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 13

Foreign-key ConstraintsForeign-key Constraints• Also called “referential integrity”

• Within an attribute:– REFERENCES <TABLE> (<attributes>)

• Separate declaration:– FOREIGN KEY <attributes> REFERENCES <table> (<attributes>)

• If R.A references S.B, then S.B must be a primary key.

• NULL values allowed for attributes in a foreign key.

– A foreign-key constraint is automatically satisfied if even one attribute in the foreign key is null.

Page 14: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 14

Enforcing Foreign-Key ConstraintsEnforcing Foreign-Key Constraints

• Modification (insert, update) of Emp

– If the new tuple’s dno does not exist in Dept.dno , then REJECT!

Emp.dno references Dept.dno

Emp (ename, dno, sal)

eName Dno Sal Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)

dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

Page 15: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 15

Enforcing Foreign-Key ConstraintsEnforcing Foreign-Key Constraints

Emp.dno references Dept.dno

Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

• Modification (delete, update) of Dept whose old “dno” is referenced by a record in Emp. There are 3 strategies:– Default: reject

– Cascade: change the Emp tuple correspondingly Delete in Dept: delete the referring record(s) in Emp Update in Dept: update the dno of the referring record(s) in Emp to the new dno

– Set Null: change dno value in referring record(s) in Emp to NULL

Page 16: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 16

Choosing a PolicyChoosing a Policy• Add “ON [DELETE,UPDATE] [CASCADE, SET NULL]” when

declaring a foreign key

• Which policy to choose depends on the application.

CREATE TABLE emp ( ssn int, name char(15), dno int, FOREIGN KEY dno REFERENCES dept(dno)

ON DELETE SET NULLON UPDATE CASCADE

);

Page 17: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 17

Attribute-based ChecksAttribute-based ChecksCREATE TABLE Emp (

name CHAR(30),dno int,gender CHAR(1) CHECK (gender in (‘F’,

‘M’)),age int CHECK (age > 18 AND age < 100));

• Constraints on attribute values.

• Checked when there is an insertion or update of the attribute.

Page 18: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 18

Attribute-based Checks (cont)Attribute-based Checks (cont)

Syntax: CHECK (condition)• Condition may involve the checked attribute

• Other attributes and relations may be used (in a query)

• Condition checked only when that associated attribute changesCREATE TABLE Emp (

ssn int,name CHAR(30),dno int CHECK (dno in (SELECT dno from

dept)));

– Condition checked when we insert/update Emp, but NOT when we modify dept.

Page 19: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 19

Tuple-Based ChecksTuple-Based ChecksCREATE TABLE Emp (

ssn int,gender CHAR(1), age int,dno int,

CHECK (gender in (‘F’, ‘M’)),CHECK (age > 18 AND age < 100));

• Checked whenever a tuple is inserted or updated

• Again, other relations may be used.

Page 20: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 20

Tuple-Based Checks (cont)Tuple-Based Checks (cont)

CREATE TABLE Emp (ssn int,gender CHAR(1), age int,dno int,CHECK (dno in (SELECT dno from dept)));

• If someone inserts an employee whose dno does not exist in Dept.dno, the insertion will be rejected.

• However, if we delete a record from Dept whose dno is used by an employee tuple, it will NOT be rejected.

Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

Page 21: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 21

Schema ModificationSchema Modification

• Delete a relation R:DROP TABLE R;

• Modify a relation:– Add new columns

ALTER TABLE emp ADD address CHAR(20);

– Delete existing columnsALTER TABLE emp DROP birthday;

Page 22: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 22

Database ModificationsDatabase Modifications• Insert• Delete• Update

Page 23: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 23

Insertion of a tupleInsertion of a tupleINSERT INTO R(A1,…,An) VALUES (v1,…,vn)

• Example:INSERT INTO Emp (ename, dno, sal)VALUES (’Tom’, 123, 45000)

• Can drop attribute names if we provide all of them in order.INSERT INTO EmpVALUES (’Tom’, 123, 45000)

• If we don’t provide all attributes, they will be filled with NULL.

INSERT INTO Emp (ename,sal)VALUES (‘Tom’, 45000)

Page 24: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 24

DeleteDeleteDELETE FROM relation [WHERE conditions];

• Example:DELETE FROM empWHERE dno = 123;

DELETE FROM emp; all tuples will be deleted

• There is no way to delete only a single occurrence of a tuple that appears twice in a relation.

Page 25: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 25

Delete (cont)Delete (cont)• Delete all employees working in a department with only one

employee.DELETE FROM emp AS E1WHERE NOT EXISTS

(SELECT enameFROM empWHERE dno = E1.dno AND ename <>

E1.ename);

• Note the relation renaming “E1”

Page 26: Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL

Fall 2005 ICS184/EECS116 -- Notes 08 26

UpdateUpdateUPDATE relation SET assignments WHERE

condition• “Change employees in dept 123 to dept 345.”

UPDATE emp SET dno = 345WHERE dno = 123;

• “Cut the salaries that are more than 100K by 10%.”UPDATE emp SET sal = sal * 0.9WHERE sal > 100000;

• Multiple assignments separated by “,”UPDATE emp SET dno = 345, sal = sal * 1.1WHERE dno = 123;