Presentation is loading. Please wait.

Presentation is loading. Please wait.

The SQL Database Grammar

Similar presentations


Presentation on theme: "The SQL Database Grammar"— Presentation transcript:

1 The SQL Database Grammar
Context Free Grammar for DB Queries Data Definition Language (DDL) Data Manipulation Language (DML) Review Sample DDL and DML Examine Corresponding Grammar Rules

2 DDL - Create Tables CREATE TABLE EMPLOYEE
( FNAME VARCHAR(15) NOT NULL , MINIT CHAR , LNAME VARCHAR(15) NOT NULL , SSN CHAR(9) NOT NULL , BDATE DATE ADDRESS VARCHAR(30) , SEX CHAR , SALARY DECIMAL(10,2) , SUPERSSN CHAR(9) , DNO INT NOT NULL , PRIMARY KEY (SSN) , FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE(SSN) , FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNUMBER) ) ;

3 DDL - Create Tables (continued)
CREATE TABLE DEPARTMENT ( DNAME VARCHAR(15) NOT NULL , DNUMBER INT NOT NULL , MGRSSN CHAR(9) NOT NULL , MGRSTARTDATE DATE , PRIMARY KEY (DNUMBER) , UNIQUE (DNAME) , FOREIGN KEY (MGRSSN) REFERENCES EMPLOYEE(SSN) ) ; CREATE TABLE DEPT_LOCATIONS (DNUMBER INT NOT NULL , DLOCATION VARCHAR(15) NOT NULL , PRIMARY KEY (DNUMBER, DLOCATION) , FOREIGN KEY (DNUMBER) REFERENCES DEPARTMENT(DNUMBER) ) ;

4 DDL - Create Tables (continued)
CREATE TABLE PROJECT (PNAME VARCHAR(15) NOT NULL , PNUMBER INT NOT NULL , PLOCATION VARCHAR(15) , DNUM INT NOT NULL , PRIMARY KEY (PNUMBER) , UNIQUE (PNAME) , FOREIGN KEY (DNUM) REFERENCES DEPARTMENT(DNUMBER) ) ; CREATE TABLE WORKS_ON (ESSN CHAR(9) NOT NULL , PNO INT NOT NULL , HOURS DECIMAL(3,1) NOT NULL , PRIMARY KEY (ESSN, PNO) , FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN) , FOREIGN KEY (PNO) REFERENCES PROJECT(PNUMBER) ) ;

5 Simple SQL Queries Query 0: Retrieve the Birthdate and Address of the Employee whose Name is 'John B. Smith'. SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME='John' AND MINIT='B’ AND LNAME='Smith’ Which Row(s) are Selected? Note: While All of these Next Queries are from Chapter 4, Some are From “Earlier” Edition B S C null W

6 Simple SQL Queries Query 1: Retrieve Name and Address of all Employees who work for the 'Research' Department SELECT FNAME, MINIT, LNAME, ADDRESS, DNAME FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO What Action is Being Performed?

7 Simple SQL Queries SELECT PNUMBER, DNUM, LNAME, BDATE,ADDRESS FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford' A S C null W B

8 INSERT SQL Queries More Sophisticated Insert can Involve Query
Update 3A: CREATE TABLE DEPTS_INFO (DEPT_NAME VARCHAR(10), NO_OF_EMPS INTEGER, TOTAL_SAL INTEGER); Update 3B: INSERT INTO DEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) SELECT DNAME, COUNT (*), SUM (SALARY) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME ;

9 DELETE SQL Queries Sample Deletes Include
DELETE FROM EMPLOYEE WHERE LNAME='Brown' DELETE FROM EMPLOYEE WHERE SSN=' ’ DELETE FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research') DELETE FROM EMPLOYEE No. of Tuples Deleted Dependent on WHERE Clause Referential Integrity is Enforced During DELETE

10 UPDATE SQL Queries Give all Employees in the 'Research' Dept. a 10% raise UPDATE EMPLOYEE SET SALARY = SALARY *1.1 WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research') Modified SALARY Value Depends on the Original SALARY Value in each Tuple SALARY = SALARY *1.1 - Use PL Interpretation

11 The SQL Database Grammar
BNF Grammar for SQL /* The grammar rules that follow have been taken from */ /* "System R", Appendix II, M.M. Astrahan, et al., ACM Trans */ /* on Database Systems, Vol. 1, No. 2, June */ /* The rules given below in BNF have the following assumptions: */ /* (1) all non-terminals are in lower-case, */ /* (2) all terminals (recognized by LEX/lex.yy.c) are in upper-case, */ <statement> ::= <dml-statement> | <ddl-statement> <dml-statement> ::= <selection> | <insertion> | <deletion> | <update> <selection> ::= <select-clause> FROM <from-list> <where-clause> <grp-ord-clause> <select-clause> ::= SELECT <select-list> <select-list> ::= <sel-expr-list> | MULT-OP <sel-expr-list> ::= <sel-expr> | <sel-expr-list> COMMA <sel-expr> <sel-expr> ::= <expr>

12 The SQL Database Grammar
<from-list> ::= <table-name> | <from-list> COMMA <table-name> <where-clause> ::= <empty> | WHERE <boolean> <nested-select> <nested-select> ::= <empty> | <and-or> <table-name> <comparison> LPAR <selection> RPAR <grp-ord-clause> ::= <empty> | GROUP BY <field-spec-list> | ORDER BY <field-spec-list> <insertion> ::= INSERT INTO <receiver> COLON <insert-spec-source> <receiver> ::= <table-name> <insert-spec-target> <insert-spec-target> ::= <empty> | LPAR <field-name-list> RPAR <field-name-list> ::= <field-name > | <field-name-list> COMMA <field-name> <insert-spec-source> ::= <literal>

13 The SQL Database Grammar
<deletion> ::= DELETE <table-name> <where-clause> <update> ::= UPDATE <table-name> <set-clause-list> <where-clause> <set-clause-list> ::= <set-clause> <set-clause> ::= SET <field-name> EQ <expr> <boolean> ::= <boolean-term> | <boolean> OR <boolean-term> <boolean-term> ::= <boolean-factor> | <boolean-term> AND <boolean-factor> <boolean-factor> ::= <boolean-primary> <boolean-primary> ::= <predicate> <predicate> ::= <expr> <comparison> <table-spec> <comparison> ::= <comp-op> <comp-op> ::= EQ | <relat-op> <all-any-opt> | <in-notin> <all-any-opt> ::= <empty> | <all-any>

14 The SQL Database Grammar
<all-any> ::= ALL |ANY <in-notin> ::= IN |NOT IN <relat-op> ::= NE |RWEDGE |GE |LWEDGE |LE <table-spec> ::= <literal> | <expr> <literal> ::= <lit-tuple> | LPAR <entry-list> RPAR <lit-tuple> ::= <entry> | LWEDGE <entry-list> RWEDGE <entry-list> ::= <entry> | <entry-list> COMMA <entry> <entry> ::= <constant> <expr> ::= <arith-term > | <expr> ADD-OP <arith-term> <arith-term> ::= <arith-factor> | <arith-term> MULT-OP <arith-factor> <arith-factor> ::= <opt-add-op> <primary> <opt-add-op> ::= <empty> |ADD-OP <and-or> ::= AND |OR

15 The SQL Database Grammar
<primary> ::= <field-spec> | <set-fn> LPAR <expr> RPAR | LPAR <expr> RPAR | <constant> <field-spec-list> ::= <field-spec> <field-spec> ::= <field-name> | <table-name> DOT <field-name> <set-fn> ::= AVG |MAX |MIN |SUM |COUNT <constant> ::= QUOTE <constant-value> QUOTE | INTEGER <constant-value> ::= IDENTIFIER |VALUE <field-name> ::= IDENTIFIER <table-name> ::= IDENTIFIER <empty> ::= EPSILON

16 The SQL Database Grammar
<ddl-statement> ::= <create-table> <create-table> ::= CREATE TABLE <table-name> COLON <field-defn-list> <field-defn-list> ::= <field-defn> | <field-defn-list> COMMA <field-defn> <field-defn> ::= <field-name> LPAR <type> <null-opt> RPAR <type> ::= CHAR LPAR INTEGER RPAR | INT LPAR INTEGER RPAR | FLOAT LPAR INTEGER RPAR <null-opt> ::= <empty> | COMMA NONULL


Download ppt "The SQL Database Grammar"

Similar presentations


Ads by Google