Presentation on theme: "Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL."— Presentation transcript:
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/EECS Notes 08 2 SQL -- historical Perspective Developed by IBM in mid 70s First standardized in 1986 by ANSI --- SQL1 Revised in 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.
Fall 2005 ICS184/EECS Notes 08 3 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
Fall 2005 ICS184/EECS Notes 08 4 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.
Fall 2005 ICS184/EECS Notes 08 5 SQL as DDL CREATE TABLE Dept (dnoint, dnamevarchar(30) not null, mgrchar(15) ); CREATE TABLE emp (enamechar(15)not null, dnoint default 0, salint ); Don’t allow null values Default value is 0
Fall 2005 ICS184/EECS Notes 08 6 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
Fall 2005 ICS184/EECS Notes 08 7 Define new domains CREAT DOMAIN personDom CHAR(20); CREATE TABLE emp (enamepersonDom, dnoint default 0, salreal );
Fall 2005 ICS184/EECS Notes 08 8 Schema Definition CREATE TABLE r ( A1D1 [not null] [default V1] … AnDn [not null] [default Vn] … ) Each integrity constraints could be: –primary key –candidate key –foreign key –“check(predicate)”: specifies predicate to be satisfied by each tuple
Fall 2005 ICS184/EECS Notes Comparison 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 ( ssnint, namechar(15), dnoint, Unique (dno,name) ); CREATE TABLE emp ( ssnint UNIQUE, namechar(15), dnoint, ); eName: UNIQUE
Fall 2005 ICS184/EECS Notes 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
Fall 2005 ICS184/EECS Notes Foreign-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) Dept(dno, dname, mgr)
Fall 2005 ICS184/EECS Notes Foreign-key Constraints Also called “referential integrity” Within an attribute: –REFERENCES ( ) Separate declaration: –FOREIGN KEY REFERENCES ( ) 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.
Fall 2005 ICS184/EECS Notes Enforcing 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) Dept(dno, dname, mgr)
Fall 2005 ICS184/EECS Notes Enforcing Foreign-Key Constraints Emp.dno references Dept.dno Emp (ename, dno, sal) Dept(dno, dname, mgr) 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
Fall 2005 ICS184/EECS Notes 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 );
Fall 2005 ICS184/EECS Notes 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.
Fall 2005 ICS184/EECS Notes 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.
Fall 2005 ICS184/EECS Notes 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.
Fall 2005 ICS184/EECS Notes 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) Dept(dno, dname, mgr)
Fall 2005 ICS184/EECS Notes 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;
Fall 2005 ICS184/EECS Notes Database Modifications Insert Delete Update
Fall 2005 ICS184/EECS Notes 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)
Fall 2005 ICS184/EECS Notes 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.
Fall 2005 ICS184/EECS Notes 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”
Fall 2005 ICS184/EECS Notes 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 > ; Multiple assignments separated by “,” UPDATE emp SET dno = 345, sal = sal * 1.1 WHERE dno = 123;