SQL (Full Syllabus Slideshow)

download SQL (Full Syllabus Slideshow)

of 99

Transcript of SQL (Full Syllabus Slideshow)

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    1/99

    Csci455Fall [email protected]

    1

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    2/99

    ` SQL standard Data Definitions, Constraints, and Schema Changes inSQL2

    Queries in SQL (basic and complex SQLQueries)

    Update operations (delete, insert, and updatestatements)

    Views (or Virtual Tables ) in SQL Specifying General Constraints as Assertions

    2

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    3/99

    ` SQL stands forStructured Query Language

    ` Developed by IBM

    ` adopted as standard language for commercial

    RDBMS: SQL-86 (orSQL-1) joint effort by ANSI and OSI

    SQL-92 (orSQL2)

    SQL-99( orSQL3)

    x CORE

    x OPTIONAL

    3

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    4/99

    A comprehensive non-procedural databaselanguage package that supports standard Supports both DDL and DML

    Provides facilities to specify security, authorization, andconstraints

    4

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    5/99

    ` 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

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    6/99

    ` SQL schema Used to group tables and related constructs

    identified by Schema

    x Name

    x Elements

    x tables

    x constraints

    x view, domains

    x

    authorization constructs

    6

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    7/99

    ` Schema is created using CREATE SCHEMA E.g.,

    x CREATE SCHEMA CompanyAUTHORIZATION JSMITH

    x JSMITH is the Schema Owner Catalog

    x Named collection of schemas

    x Information_schema

    7

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    8/99

    ` CREATE TABLE used to specify a new relation (or base table)

    x CREATE TABLE EMPLOYEE

    x

    CREATE TABLE COMPANY.EMPLOYEE CREATE VIEW

    x Used to create virtual tables

    Attributes are ordered

    8

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    9/99

    ` CREATE TABLE DEPARTMENT (

    DNAME VARCHAR(10) NOT NULL,

    DNUMBER INTEGER NOT NULL,

    MGRSSN CHAR(9),

    MGRSTARTDATE CHAR(9) );

    9

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    10/99

    ` Key attributes can be specified via the PRIMARY KEY and UNIQUEphrasesCREATE 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

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    11/99

    Basic Data typesx Numeric

    x Integer, Real

    x Char string

    x Fixed length (CHAR(n))

    x Varying length (VARCHAR(n)x bit-string,

    x Fixed (BIT(n)) or varying VARYING(n)

    x date/time

    x Boolean (T,F, Unknown)

    x Timestamps (includes both date and time)

    11

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    12/99

    User defined data type

    x Domain in SQLx CREATE DOMAIN SSN_TYPEAS CHAR (9);

    12

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    13/99

    ` Constraints and default values can be specified oneach attributes

    tuple table

    13

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    14/99

    ` NULLS can be used as attribute values

    ` A NOT NULL constraint can be used to specifythat NULL is not permitted

    ` DEFAULT Value

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    15/99

    15

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    16/99

    ` CHECK Clause Used to restrict attribute or domain values

    ` E.g., To restrict department numbers between integer 1-20 integer

    x DNUMBER INT NOT NULL CHECK (DNUMBER>0ANDDNUMBER 0ANDD_NUM

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    17/99

    PRIMARY KEY CLUASE used to specify PK

    x E.g., PRIMARY KEY (DNUMBER);

    x E.g., Dnumber INT PRIMARY KEY;

    FOREIGN KEY CLUASE used to specify FK

    x FOREIGN KEY (DNUMBER) REFERENCESDEPARTMENT (DUNMBER)

    UNIQUE CLAUSE

    x Used for secondary keys

    Figure 8.1

    17

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    18/99

    18

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    19/99

    ` Update/delete/insertion of tuples may violate referentialintegrity 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

    x SET DEFAULT

    x SET NULL

    x

    CASACA

    D

    E Option must be qualified

    x ON DELETE or ON UPDATE

    19

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    20/99

    20

    MINT

    EMPLOEE

    FNAME LNAME SSN BDATE ADDRESS SEX SUPERSSN DNO

    DEPT

    DEPT_LOC

    SNAME DNUM MGRSSNMSDATE

    DNUM DLOC

    DNO should be Null or

    should match a PK of DEPT

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    21/99

    21

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    22/99

    ` Names to constraints

    Used to identify/modify a particular

    constraint Works like CONSTANT declaration inProgram Languages

    Figure 8.2

    22

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    23/99

    23

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    24/99

    Used at the end of schema

    Applies to individual tuples

    Use CHECK for more general constraints

    x E.g.,

    x CHECK (DEPT_CREATE_DATE < MGRSTARTDATE);

    24

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    25/99

    ` THE DROP COMMAND CASCADE

    RESTRICT

    Used to drop the named schema elements fromdatabase schema

    E.g.,

    x DROP SCHEMA COMPANY CASCADE

    x DROP TABLE DEPENDENT CASCADE

    RESTRCITx Used to drop the schema if it has no elements in it

    Delete vs. Drop

    x Use delete if you want to delete the records but not the tabledefinition

    25

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    26/99

    ` Used to change the definition of base table E.g.,

    x ALTER TABLE COMPANY.EMPLOYEEADD JOBVARCHAR(12)

    x

    ALTER TAB

    LE COMPANY.EMP

    LOYEE

    DROPA

    DDRE

    SSCASCADE;

    x ALTER TABLE COMPANY.DEPARTMENTALTER MGRSSNDROP DEFAULT;

    x ALTER TABLE COMPANY.DEPARTMENTALTER MGRSSN

    SET DEFAULT 344556677x ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT

    EMPSUPERFKCASCADE

    26

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    27/99

    ` SQL has only one basic statement to retrieveinformation SELECT FROM

    WHERE` no relationship to the W operation of relational

    algebra` Important features

    supports the notion of multi-set (or Bag)

    27

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    28/99

    ` General Form: SELECT

    FROM

    WHERE ;

    28

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    29/99

    ` Get the birthday and address of the employee(s)whose name is John B. Smith SELECT BDATE, ADDRESS

    FROM EMPLOYEE

    WHERE FNAME=JohnAND MINIT =B ANDLNAME = SMITH

    29

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    30/99

    30

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    31/99

    ` Get the name and address of allemployee who work for the ResearchDept. SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME=ResearchAND

    DNUMBER =DNO

    31

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    32/99

    ` For every project located in Stafford, list theproject number, the controlling departmentnumber, and the department managers last

    name, address, and birthrate SELECT PNUMBER, DNUM, LNAME, ADDRESS, BDATE FROM PROJECT, DEPARTMENT, EMPLOYEE

    WHERE DNUM=DNUMBERAND MGRSSN=SSN

    AND PLOCATION = Stafford

    32

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    33/99

    ` In SQL, same name can be used For more than one attribute in different tables

    used in recursive queries

    33

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    34/99

    ` To remove ambiguity, we need to qualify theattributes : use . separator to qualify the attribute

    x e.g., suppose LNAME=NAME, and DNO=DNUMBER ,DNAME=NAME

    x SELECT FNAME, EMPLOYEE.NAME, ADDRESS

    x FROM EMPLOYEE, DEPARTMENT

    x WHERE DEPARTMENT.DNUMBER = EMPLOYEE.DNUMBERAND DEPARTMENT.NAME = Research

    34

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    35/99

    ` For each employee, find the employees first andlast name and the first and last name of her/hisimmediate supervisor SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME

    FROM EMPLOYEEAS E, EMPLOYEEAS S

    WHERE E.SUPERSSN=S.SSN

    35

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    36/99

    E.Fname E.Lname S.Fname S.Lname

    J n m Smit Fran lin n

    Fran lin n James B r

    li ia ela a Jennifer alla e

    .

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    37/99

    ` No WHERE-clause means no conditions

    ` No condition means Cross product operations (v)

    37

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    38/99

    ` Get all combinations of EMPLOYEE.SSN andDEPARTMENT.DNAME SELECT SSN, DNAME

    FROM EMPLOYEE, DEPARTMENT

    ` Get all Employee SSN SELECT SSN

    FROM EMPLOYEE

    38

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    39/99

    ` Use * to get all attributes

    ` Get all employees working forDept. 5 SELECT *

    FROM EMP

    LOYEE WHERE DNO=5

    ` E.g., 2

    S

    ELECT

    *

    FROM EMPLOYEE, DEPARTMENT;

    WHERE DNO=5 and Dname=Research

    39

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    40/99

    ` SQL treats tables as a multi-set (i.e., a set havingduplicates)

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

    ` SQLTable with a key is a

    SET by definition Why? Because key must be unique

    ` To treat tables as sets use DISTINCT in SELECTstatement

    40

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    41/99

    Retrieve the salary of every employee,x SELECTALL SALARY

    x FROM EMPLOYEE

    41

    Salary

    3000

    4000

    2500

    2500

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    42/99

    ` Get the salary of every employee USING distinct(set) SELECT DISTINCT SALARY

    FROM EMPLOYEE

    42

    Salary

    3000

    4000

    2500

    4300

    5500

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    43/99

    ` NULL represents Value is not known

    x e.g., an unknown address

    Value is not availablex E.g., unlisted phone number

    Value does not applyx E.g., Unmarried employee has no name for his/her spouse

    43

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    44/99

    ` 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 logicalexpressions involving

    AND OR NOT

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    45/99

    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

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    46/99

    ` In SELECT-PROJECT-JOIN queries, only thosecombinations of tuples that are evaluated to TRUEare selected

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    47/99

    ` Q18: Get the names of all employees who do nothave supervisors SELECT Fname, Lname

    FROM EMPLOYEE

    WHER Super_ssn IS NULL

    47

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    48/99

    ` Complex SQL queries can be formulated using UNION

    INTERSECTION

    EXECEPT

    48

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    49/99

    ` Make a list of Project numbers for projects thatinvolve an employee whose last name is Smith,either as a worker or as a manger of thedepartment that controls the project

    49

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    50/99

    (SELECT DISTINCT PNUMBER FROM PROJECT, DEPARTMENT, EMPLOYEE

    WHERE DNUM=DNUMBER AND MGRSSN=SSNANDLNAME=Smith)

    UNION

    (SELECT DISTINCT PNUMBER FROM WORKS_ON, EMPLOYEE , PROJECT WHERE PNUMBR = PNO AND ESSN=SSN ANDLNAME=Smith)

    50

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    51/99

    ` Some queries need the existing values in thedatabase to be retrieved & compared

    ` Nested queries

    used to formulate such queries

    51

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    52/99

    ` Make a list of Project numbers for projects thatinvolve an employee whose last name is Smith,eitheras a workeroras a manger of thedepartment that controls the project

    52

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    53/99

    x SELECT DISTINCT PNUMBER

    x FROM PROJECTWHERE PNUMBER IN

    (SELECT PNUMBERx FROM DEPARTMENT, EMPLOYEE , PROJECT

    x WHEREDNUM = DNUMBEANDMGRSSN = SSNAND LNAME = Smith)

    x OR

    x PNUMBER INx ( SELECT PNO

    x

    FROM WORKS

    _ON, EMPLOYEE

    x WHERE ESSN=SSNAND LNAME = Smith);

    53

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    54/99

    ` Correlated Queries: When a condition in WHERE clause of a nested query

    references attribute(s) of a relation defined in the outerquery

    54

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    55/99

    ` Get the name of each employee who has a dependent withthe same first name and the same sex as the employee

    ` SELECT E.FNAME, E.LNAME

    ` FROM EMPLOYEEAS E` WHERE E.SSN IN (SELECT ESSN

    FROM DEPENDENT

    WHERE E.FNAME=DEPENDENT_NAME

    AND E.SEX=SEX);

    55

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    56/99

    ` A nested query involving = orIN can be replaced bysimple query

    SELECT E.FNAME,E.LNAME

    FROM EMPLOYEEAS E, DEPEDENTAS D

    WHERE E.SSN=D.ESSNAND

    E.FNAME=D.DEPENDENT_NAMEAND

    E.SEX=D.SEX;

    56

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    57/99

    ` Get SSN of all employees who work on the samecombination (project, hours) on some project thatemployee John Smith with SSN=123456789 SELECT DISTINCT ESSN

    FROM WORKS_ON WHERE (PNO, HOURS) IN

    (SELECT PNO, HOURSFROM WORKS_ONWHERE ESSN=123456789)

    57

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    58/99

    ` EXISTS: Checks the result (i.e, SET) of the correlated nested

    query to see if it is empty or not

    x If the result (i.e., set) is non-empty it returns TRUE

    x If the result is empty it returns FALSE

    58

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    59/99

    ` Find the name of each employee who has adependent with the same first name and same sexas the employee SELECT E.FNAME, E.LNAME

    FROM EMPLOYEE AS E WHERE EXISTS ( SELECT * FROM DEPENDENT WHERE E.SSN=ESSNAND SEX=E.SEX

    ANDE.FNAME=DEPENT_NAME)

    59

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    60/99

    ` Find the name of each employee who has nodependent SELECT FNAME, LNAME

    FROM EMPLOYEE E

    WHERE NOT EXISTS

    (SELECT *

    FROM DEPENDENT

    WHERE SSN=ESSN)

    60

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    61/99

    ` Retrieve SSNs of all employees who work onproject number 12,22,or33 SELECT DISTINCT ESSN

    FROM WORKS_ON

    WHERE PNO IN (12,22,33)

    61

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    62/99

    ` Join operation can be specified in FROM clause Understandability

    UseAS construct to Rename join attributes

    Default is inner join

    62

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    63/99

    ` Find the name and address of employees whowork in Research department SELECT FNAME, LNAME, ADDRESS

    FROM (EMPLOYEE JOIN DEPARTMENT ON

    DNO=DNUMBER) WHERE DNAME = Research

    63

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    64/99

    SELECT FNAME, LNAME, ADDRESS

    FROM ( EMPLOYEE NATURALJOIN (DEPARTMENTAS DEPT (DNAME, DNO, MSSN, MSDATE)))

    WHERE DNAME =Research

    64

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    65/99

    ` ALL is comparison operators` Can be used with any other comparison operators

    E.g.x v>ALL V

    x

    where V is a SETx It returns TRUE if v > every element in V

    65

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    66/99

    ` get the names of all employees whose salary isgreater than the salary of ALL the employees indepartment 5 SELECT LNAME, FNAME

    FROM EMPLOYEE

    WHERE SALARY > ALL

    x (SELECT SALARY FROM EMPLOYEE WHERE DNO=5);

    66

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    67/99

    ` E.g., SELECT E.FNAME, E.LNAME FROM EMPLOYEEAS E WHERE E.SSN

    IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSNAND

    E.FNAM=DEPENDENT_NAMEANDSEX=E.SEX

    67

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    68/99

    ` Q20:Find the sum of the salaries of all employees in theResearch dept, as well as the max salary, the min salary, andaverage in that dept.

    SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY)

    AVG(SALARY) FROM (EMPLOYEE NATURALJOIN (DEPARTMENTASDEPT(Dname, Dno, Mssn, Msdate)))

    WHERE DNAME=Research

    68

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    69/99

    ` GROUP BY CLUASE Used to partition the relation into sub-relation

    Works with aggregate functions (e.g. COUNT)

    x grouping attribute

    x Grouping attribute (s) need to be specified in SELECT clause

    x Create separate group for the grouping attribute with NULL values

    69

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    70/99

    ` For each department, retrieve the departmentnumber, the number of employees in thedepartment, and their average salary

    SELECT

    D

    NO, COUNT(*), AVG(SALARY) FROM EMPLOYEE

    GROUP BY DNO;

    x Figure.8.6.(a)

    70

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    71/99

    71

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    72/99

    ` Used with GROUP BY clause

    ` Used as a condition on the sub-relations or groups Groups satisfying the conditions are selected

    72

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    73/99

    ` For each project on which more than two employeeswork, get the project number, the project name, thenumber 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

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    74/99

    74

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    75/99

    ` Count the totalnumber of employees whosesalaries exceed $40,000 in each department, butonly for department where more than fiveemployees work

    75

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    76/99

    ` SELECT DNAME, COUNT (*)

    ` FROM DEPARTMENT, EMPLOYEE

    ` WHERE DNUMBER=DNOAND SALARY>40000

    ` GROUP BY DNAME` HAVING COUNT (*)>5;

    ` Wrong Selects only department having more than 5 employees

    who ALL

    make more than 40K

    76

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    77/99

    ` SELECT DNUMBER, COUNT (*)

    ` FROM DEPARTMENT, EMPLOYEE

    ` WHERE DNUMBER=DNOAND SALARY>4000

    AND

    D

    NO IN (SELECT DNO FROM EMPLOYEE

    GROUP BY DNO

    HAVING COUNT(*)>5)

    GROUP BY DNUMBER

    77

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    78/99

    ` The processing steps 1) the WHERE Clause is executed to select individual

    tuples

    2) HAVING clause executed to select individual groups of

    tuples

    78

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    79/99

    ` The Insert Command To add a new tuple to employee

    x INSERT INTO EMPLOYEE

    x VALUES (Richard,K,Marini,653298653,30-dec-52, 98

    Oak Forest, Katy, TX,M, 37000,987654321, 4)

    79

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    80/99

    ` To enter a new tuple using known attributes INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)

    VALUES (Richard, Marini, 653298653);

    x the tuple is accepted, if no constraint is violated,

    80

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    81/99

    ` INSERT command can be used to load multiple tuplesas the same time Suppose

    x CREAT TABLE DEPT_INFOx (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

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    82/99

    ` Removes tuples from a relation one at a time DELETE FROM EMPLOYEE

    WHERE LNAME=Brown

    82

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    83/99

    ` One or more tuples can be deleted E.g.. Delete all employees who work for Research dept.

    x DELETE FROM EMPLOYEE

    x WHERE DNO IN

    x (SELECT DNUMBERx FROM DEPARTMENT

    x WHERE DNAME=Research);

    83

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    84/99

    ` DELETE FROM EMPLOYEE Creates empty table

    84

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    85/99

    ` Used to modify values of one (or multiple)selected tuples associated with ONE relation

    ` E.g.

    Change the location and controlling department number ofproject number 10 to Grand Forks and 5

    UPDATE PROJECT SET PLOCATION = Grand Forks, DNUM=5 WHERE PNUMBER=10;

    85

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    86/99

    ` E.g., Give all employees working in Research department a 10

    percent salary raise

    x UPDATE EMPLOYEE

    xSET SALARY = SALARY *1.1

    x WHERE DNO IN ( SELECT DNUMBER

    x FROM DEPARTMENT

    x WHERE DNAME = Research);

    86

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    87/99

    ` Specifying ASSERTIONS/Triggers

    ` Creating Views

    ` Writing programs

    ` Specifying physical

    DB

    ` Granting and revoking privileges

    ` Specifying Complex Objects

    ` Concurrency Control/Recovery mechanisms

    87

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    88/99

    ` general constraints can be specified as follows CREATE ASSETION SALARY_CONSTRAINT

    x CHECK ( NOT EXISTS ( SELECT *

    FROM EMPLOYEE AS E, EMPLOYEE

    AS M, DEPARTMENTAS DWHERE E.SALARY > M. SALARY

    AND E.DNO=D.NUMBERAND

    D.MGRSSN=M.SSN));

    88

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    89/99

    ` Constraints Prevent the data from being made inconsistent by any

    kind of statement

    ` Triggers

    Maintain database consistency and defined operationally

    89

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    90/99

    ` A view refers to a single table that is derived from other tables CREAT VIEW WORKS_ON1 ASSELECT FNAME, LNAME, PNAME, HOURS

    x FROM EMPLOYEE, PROJECT, WORKS_ON

    x WHERE SSN=ESSN AND PNO=PNUMBER

    90

    FNAME LNAME PNAME HOURS

    WORKS_ON1

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    91/99

    CREAT VIEW DEPT_INFO(DEPT_NAME, NO_OF_EMPS,TOTAL_SAL)

    ASSELECT DNAME, COUNT(*), SUM(SALARY)

    x FROM EMPLOYEE, DEPARTMENT

    x WHERE DNUMBER=DNO

    x GROUP BY DNAME;

    91

    DEPT_NAME TOTAL_SALNO_OF_EMPS

    DEPT_INFO

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    92/99

    ` SELECT Fname, Lname

    ` FROM WORKS_ON1

    ` WHERE Pname =ProjectX;

    92

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    93/99

    ` DROP VIEW WORKS_ON1;

    93

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    94/99

    ` Used as subroutine mechanism make a complex query easier to understand/reuse

    easier to debug

    ` used as a flexible mechanism foraccess

    CONTROL to the data (security)

    94

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    95/99

    ` A View is always up to date

    ` A view is realized at the time we execute a query

    95

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    96/99

    ` Two strategies to implement a view Query modification

    x Maps the view into the base tables

    View materialization

    x Create a temporary view tablex Uses incremental approach to keep a materialized table up-

    date

    x Removes the view table if it is not accessed for certainperiod of time

    96

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    97/99

    ` Query on SELECT Fname, Lname FROM WORKS_ON1 WHERE Pname =ProjectX;

    ` Translated to SELECT Fname, Lname,x FROM EMPLOYEE, PROJECT, WORKS_ON

    x WHERE SSN=ESSNAND PNO=PNUMBER

    AND Pname =ProjectX;

    97

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    98/99

    ` Updating the views can be complicated andambiguous

    ` an update on a view defined on a single tablewithout any aggregate functions can be mapped toan update on the base table

    98

  • 8/8/2019 SQL (Full Syllabus Slideshow)

    99/99