The SQL Database Grammar

Slides:



Advertisements
Similar presentations
SQL.1 CSE 4100 The SQL Database Grammar BNF Grammar for SQL /* The grammar rules that follow have been taken from */ /* "System R", Appendix II, M.M. Astrahan,
Advertisements

Fundamentals of Database Systems Fourth Edition El Masri & Navathe
SQL DESIGN AND IMPLEMENTATION CONTENT SOURCES: ELAMSARI AND NAVATHE, FUNDAMENTALS OF DATABASE MANAGEMENT SYSTEMSELAMSARI AND NAVATHE, FUNDAMENTALS OF.
Defined by Edgar Codd in 1970 Defined by Edgar Codd in 1970 Considered ingenious but impractical Considered ingenious but impractical Conceptually simple.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
COMPANY schema EMPLOYEE
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Database technology Lecture 2: Relational databases and SQL
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
Exploring Microsoft Access 2003 Chapter 4 Proficiency: Relational Databases, External Data, Charts, Pivot, and the Switchboard.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Review Database Application Development Access Database Development ER-diagram Forms Reports Queries.
CS 104 Introduction to Computer Science and Graphics Problems Introduction to Database (2) Basic SQL 12/05/2008 Yang Song.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Review: Application of Database Systems
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
Relational Algebra - Chapter (7th ed )
Onsdag The concepts in a relation data model SQL DDL DML.
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.
DatabaseDatabase cs453 Lab5 1 Ins.Ebtesam AL-Etowi.
SQL Chapter 8. SQL or SEQUEL - (Structured English Query Language) Based on relational algebra Developed in 1970's released in early 1980's Standardized.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Structured Query Language
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Introduction to Database Systems
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 16 A First Course in Database Systems.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
1 Chapter (8) SQL The Relational Database Standard Objectives Presenting the main features of SQL SQL is the Structured Query Language SQL commands SQL.
Chapter 10 SQL DDL.
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Retrieval Queries in SQL(DML)
376a. Database Design Dept. of Computer Science Vassar College
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
376a. Database Design Dept. of Computer Science Vassar College
Mapping ER Diagrams to Tables
11/9/2018.
Company Requirements.
لغة قواعد البيانات STRUCTURED QUERY LANGUAGE SQL))
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Chapter 4 & 5 Basic SQL More SQL
Chapter 4 Basic SQL. Chapter 4 Basic SQL Chapter 4 Outline Overview of SQL SQL Data Definition (DDL) for Specifying a Relational Database Schema CREATE.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Review: Application of Database Systems
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
1. Explain the following concepts: (a) superkey (b) key
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Presentation transcript:

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

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) ) ;

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) ) ;

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) ) ;

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

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?

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

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 ;

DELETE SQL Queries Sample Deletes Include DELETE FROM EMPLOYEE WHERE LNAME='Brown' DELETE FROM EMPLOYEE WHERE SSN='123456789’ 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

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

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 1976. */ /* 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>

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>

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>

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

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

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