260 likes | 341 Views
ICS 184/EECS116: Introduction to Data Management. Lecture Note 8 SQL: Structured Query Language -- DDL. SQL -- 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
E N D
ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL ICS184/EECS116 – Notes 08
SQL -- 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. ICS184/EECS116 -- Notes 08
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 ICS184/EECS116 -- Notes 08
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. ICS184/EECS116 -- Notes 08
SQL 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 ICS184/EECS116 -- Notes 08
Domain 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 ICS184/EECS116 -- Notes 08
Define new domains CREAT DOMAIN personDom CHAR(20); CREATE TABLE emp (ename personDom, dno int default 0, sal real ); ICS184/EECS116 -- Notes 08
Schema Definition CREATE 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 ICS184/EECS116 -- Notes 08
Declaring 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 ICS184/EECS116 -- Notes 08
Comparison CREATE TABLE emp ( ssn int, name char(15), dno int, Unique (dno,name) ); CREATE TABLE emp ( ssn int UNIQUE, name char(15), dno int, ); • 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. eName: UNIQUE ICS184/EECS116 -- Notes 08
Enforcing 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 ICS184/EECS116 -- Notes 08
Foreign-Key Examples Emp (ename, dno, sal) CREATE TABLE emp ( ssn int, name char(15), dno int REFERENCES dept(dno), ); Dept(dno, dname, mgr) 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. ICS184/EECS116 -- Notes 08
Foreign-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. ICS184/EECS116 -- Notes 08
Enforcing Foreign-Key Constraints Emp (ename, dno, sal) Dept(dno, dname, mgr) Emp.dno references Dept.dno • Modification (insert, update) of Emp • If the new tuple’s dno does not exist in Dept.dno , then REJECT! ICS184/EECS116 -- Notes 08
Enforcing Foreign-Key Constraints Emp (ename, dno, sal) Dept(dno, dname, mgr) Emp.dno references Dept.dno • 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 ICS184/EECS116 -- Notes 08
Choosing 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 NULL ON UPDATE CASCADE ); ICS184/EECS116 -- Notes 08
Attribute-based Checks CREATE 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. ICS184/EECS116 -- Notes 08
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 changes CREATE 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. ICS184/EECS116 -- Notes 08
Tuple-Based Checks CREATE 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. ICS184/EECS116 -- Notes 08
Tuple-Based Checks (cont) Emp (ename, dno, sal) Dept(dno, dname, mgr) 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. ICS184/EECS116 -- Notes 08
Schema Modification • Delete a relation R: DROP TABLE R; • Modify a relation: • Add new columns ALTER TABLE emp ADD address CHAR(20); • Delete existing columns ALTER TABLE emp DROP birthday; ICS184/EECS116 -- Notes 08
Database Modifications • Insert • Delete • Update ICS184/EECS116 -- Notes 08
Insertion of a tuple INSERT 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 Emp VALUES (’Tom’, 123, 45000) • If we don’t provide all attributes, they will be filled with NULL. INSERT INTO Emp (ename,sal) VALUES (‘Tom’, 45000) ICS184/EECS116 -- Notes 08
Delete DELETE FROM relation [WHERE conditions]; • Example: DELETE FROM emp WHERE 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. ICS184/EECS116 -- Notes 08
Delete (cont) • Delete all employees working in a department with only one employee. DELETE FROM emp AS E1 WHERE NOT EXISTS (SELECT ename FROM emp WHERE dno = E1.dno AND ename <> E1.ename); • Note the relation renaming “E1” ICS184/EECS116 -- Notes 08
Update UPDATE relation SET assignments WHERE condition • “Change employees in dept 123 to dept 345.” UPDATE emp SET dno = 345 WHERE dno = 123; • “Cut the salaries that are more than 100K by 10%.” UPDATE emp SET sal = sal * 0.9 WHERE sal > 100000; • Multiple assignments separated by “,” UPDATE emp SET dno = 345, sal = sal * 1.1 WHERE dno = 123; ICS184/EECS116 -- Notes 08