1.01k likes | 1.24k Views
Chapter 8: SQL-99. reza@aero.und.edu. 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
E N D
Chapter 8: SQL-99 reza@aero.und.edu www.bookspar.com | Website for students | VTU NOTES
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
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
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
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
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
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
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
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
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
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
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
Specifying Constraints using SQL • Constraints and default values can be specified on each • attributes • tuple • table www.bookspar.com | Website for students | VTU NOTES
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
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 NULLCHECK (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
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
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
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 MGRSSN MSDATE DEPT_LOC DNUM DLOC www.bookspar.com | Website for students | VTU NOTES 20
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
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
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
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” • ALTERTABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE www.bookspar.com | Website for students | VTU NOTES
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
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
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
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’ ANDDNUMBER =DNO www.bookspar.com | Website for students | VTU NOTES
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
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
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
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 • WHEREE.SUPERSSN=S.SSN www.bookspar.com | Website for students | VTU NOTES
Result of Query www.bookspar.com | Website for students | VTU NOTES
SQL: Unspecified WHERE-Clause • No WHERE-clause means no conditions • No condition means Cross product operations () www.bookspar.com | Website for students | VTU NOTES
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 SSN • FROM EMPLOYEE www.bookspar.com | Website for students | VTU NOTES
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; • WHERE DNO=5 and Dname=‘Research’ www.bookspar.com | Website for students | VTU NOTES
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
Query 11 (ALL) • Retrieve the salary of every employee, • SELECT ALL SALARY • FROM EMPLOYEE www.bookspar.com | Website for students | VTU NOTES
Query 12 (DISTINCT) • Get the salary of every employee USING distinct (set) • SELECT DISTINCT SALARY • FROM EMPLOYEE www.bookspar.com | Website for students | VTU NOTES
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
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
Truth table for 3-valued logic www.bookspar.com | Website for students | VTU NOTES
In SELECT-PROJECT-JOIN queries, only those combinations of tuples that are evaluated to TRUE are selected More on 3-value www.bookspar.com | Website for students | VTU NOTES
Query involved NULL • Q18: Get the names of all employees who do not have supervisors • SELECT Fname, Lname • FROM EMPLOYEE • WHER Super_ssn ISNULL www.bookspar.com | Website for students | VTU NOTES
Query using Union, Intersection, EXECEPT • Complex SQL queries can be formulated using • UNION • INTERSECTION • EXECEPT www.bookspar.com | Website for students | VTU NOTES
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
Query 4: Using Union Query • (SELECTDISTINCTPNUMBER • 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