Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems



Similar documents
CSC 443 Data Base Management Systems. Basic SQL

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Part 4: Database Language - SQL

How To Create A Table In Sql (Ahem)

Introduction to SQL: Data Retrieving

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

Summary on Chapter 4 Basic SQL

Lab Assignment Creating a Relational Database Schema from ER Diagram, Populating the Database and Querying over the database with SQL

SQL-99: Schema Definition, Basic Constraints, and Queries

CS 338 Join, Aggregate and Group SQL Queries

SQL Nested & Complex Queries. CS 377: Database Systems

Instant SQL Programming

New York University Computer Science Department Courant Institute of Mathematical Sciences

Oracle SQL. Course Summary. Duration. Objectives

Structured Query Language (SQL)

IT2305 Database Systems I (Compulsory)

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

IT2304: Database Systems 1 (DBS 1)

Lab Manual. Database Systems COT-313 & Database Management Systems Lab IT-216

Chapter 5. SQL: Queries, Constraints, Triggers

Oracle Database: SQL and PL/SQL Fundamentals

ER & EER to Relational Mapping. Chapter 9 1

The Relational Algebra

VIEWS virtual relation data duplication consistency problems

Relational Database: Additional Operations on Relations; SQL

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

SQL: Queries, Programming, Triggers

Scheme G. Sample Test Paper-I

Database Query 1: SQL Basics

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle 10g PL/SQL Training

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

SQL: Queries, Programming, Triggers

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

More SQL: Assertions, Views, and Programming Techniques

Oracle Database 10g: Introduction to SQL

Information Systems SQL. Nikolaj Popov

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )


Introduction to Databases

SQL NULL s, Constraints, Triggers

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Basic Concepts of Database Systems

BCA. Database Management System

Oracle Database 12c: Introduction to SQL Ed 1.1

Introduction to SQL ( )

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Databases 2011 The Relational Model and SQL

SQL Server 2008 Core Skills. Gary Young 2011

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Programming with SQL

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

SQL Server. 1. What is RDBMS?

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

DBMS / Business Intelligence, SQL Server

EECS 647: Introduction to Database Systems

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Introduction to Microsoft Jet SQL

ICAB4136B Use structured query language to create database structures and manipulate data

Advance DBMS. Structured Query Language (SQL)

Database Administration with MySQL

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Lecture 6. SQL, Logical DB Design

Testing of the data access layer and the database itself

{ PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) }

Week 4 & 5: SQL. SQL as a Query Language

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

SQL. Short introduction

Ch.5 Database Security. Ch.5 Database Security Review

T-SQL STANDARD ELEMENTS

Conventional Files versus the Database. Files versus Database. Pros and Cons of Conventional Files. Pros and Cons of Databases. Fields (continued)

Figure 14.1 Simplified version of the

Introduction to tuple calculus Tore Risch

IINF 202 Introduction to Data and Databases (Spring 2012)

3. Relational Model and Relational Algebra

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Oracle Database 10g Express

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Netezza SQL Class Outline

There are five fields or columns, with names and types as shown above.

COURSE CODE: CIT 844 COURSE TITLE: ADVANCED DATABASE MANAGEMENT SYSTEM

CIS 631 Database Management Systems Sample Final Exam

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

5.1 Database Schema Schema Generation in SQL

Oracle Database 11g SQL

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

SQL Tables, Keys, Views, Indexes

10CS54: DATABASE MANAGEMENT SYSTEM

Using Temporary Tables to Improve Performance for SQL Data Services

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

UNIT 6. Structured Query Language (SQL) Text: Chapter 5

Databases What the Specification Says

1 Structured Query Language: Again. 2 Joining Tables

Transcription:

CSC 74 Database Management Systems Topic #0: SQL Part A: Data Definition Language (DDL) Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning Schema and Catalog Schema A collection of relations (tables) Identified by a schema name Include tables, constraints, views, domains, and others. CREATE SCHEMA COMPANY AUTHORIZATION Bob; Catalog A named collection of schemas. Integrity constraints can be defined between relations only if they exist in the same catalog. CREAT TABLE Define: a new relation Specify name and attributes Specify constraints Attribute constraints Relation constraints Primary key Other keys Referential integrity constraint Spring 00 CSC 74: DBMS by Dr. Peng Ning 3 Spring 00 CSC 74: DBMS by Dr. Peng Ning 4 CREAT TABLE EMPLOYEE ( FNAME VARCHAR(5) NOT NULL, LNAME VARCHAR(5) NOT NULL, CHAR(9) NOT NULL,, SUPER CHAR(9), DNO INT NOT NULL, PRIMARY KEY (), FOREIGN KEY (SUPER) REFERENCE EMPLOYEE(), FOREIGN KEY (DNO) REFERENCE DEPARTMENT (DNUMBER)); CREAT TABLE DEPARTMENT ( DNAME VARCHAR(5) NOT NULL, DNUMBER INT NOT NULL, MGR CHAR(9) NOT NULL, MGRSTARTDATE DATE, PRIMARY KEY(DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGR) REFERENCES EMPLOYEE()); Spring 00 CSC 74: DBMS by Dr. Peng Ning 5 Referential Triggered Actions Two events ON DELETE ON UPDATE Three options SET NULL SET DEFAULT CASCADE CREAT TABLE DEPARTMENT ( DNAME VARCHAR(5) NOT NULL, DNUMBER INT NOT NULL, MGR CHAR(9) NOT NULL, MGRSTARTDATE DATE, PRIMARY KEY(DNUMBER), UNIQUE (DNAME), FOREIGN KEY (MGR) REFERENCES EMPLOYEE() ON DELETE SET DEFAULT ON UPDATE CASCADE); Spring 00 CSC 74: DBMS by Dr. Peng Ning 6

Self-Study DROP SCHEMA DROP TABLE ALTER TABLE Alter attributes Alter constraints Part B: Data Manipulation Language (DML) Spring 00 CSC 74: DBMS by Dr. Peng Ning 7 Spring 00 CSC 74: DBMS by Dr. Peng Ning 8 DML Our focus is how to formulate queries. Self-study: INSERT DELETE UPDATE SELECT Used for retrieval Used to specify subqueries for retrieval and for the other operations Not quite the σ or select operator of the relational algebra SELECT tuple queries aggregate queries Spring 00 CSC 74: DBMS by Dr. Peng Ning 9 Spring 00 CSC 74: DBMS by Dr. Peng Ning 0 SELECT (Cont d) Basic paradigm SELECT column,, column n FROM table,, table m WHERE condition The WHERE condition can be a boolean combination of other conditions involving the tables table through table m 45000 List the names of the employees whose salary is more than 50,000. Relational Algebra: SELECT, Select + Project FROM WHERE > 50000; Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning

Alice Lnane Zelaya White 999-88-7777-33-4444 Dependent Eric Alex Lnane Zelaya List the names of the dependents of Alice Zelaya. SELECT Dependent., Dependent. FROM, Dependent WHERE. = Alice AND. = Zelaya AND. = Dependent.E; Relational Algebra: Select + Project + Join E 999-88-7777 Spring 00 CSC 74: DBMS by Dr. Peng Ning 3 Exercise. Find names of employees in the research dept (,,, Bdate, Address, Sex,, Super, Dno) Department(Dname, Dnumber, Mgr, MgrStartDate) SELECT, FROM, WHERE AND ; Spring 00 CSC 74: DBMS by Dr. Peng Ning 4 Exercise SQL Variants: * For every project in 'Stafford' list the controlling dept number and the dept manager's last name (,,, Bdate, Address, Sex,, Super, Dno) Department(Dname, Dnumber, Mgr, MgrStartDate) 45000 Project(Pname, Pnumber, Plocation, Dnum) SELECT, FROM,, WHERE AND AND ; Spring 00 CSC 74: DBMS by Dr. Peng Ning 5 SELECT * FROM WHERE > 50000; Spring 00 CSC 74: DBMS by Dr. Peng Ning 6 SQL Variants: ALL and DISTINCT SQL Variants: Renaming SELECT ALL FROM ; SELECT DISTINCT FROM ; (,,, Bdate, Address, Sex,, Super, Dno) Retrieve the employee s name and the names of their immediate supervisor. SELECT E., E., S., S. FROM AS E, AS S WHERE E.Super = S.; Spring 00 CSC 74: DBMS by Dr. Peng Ning 7 Spring 00 CSC 74: DBMS by Dr. Peng Ning 8 3

SQL Variants: IS [NOT] NULL SQL Variants: ORDER BY NULL NULL SELECT, FROM WHERE IS NULL; SELECT, FROM WHERE IS NOT NULL; SELECT, FROM WHERE IS NOT NULL ORDER BY DESC; Spring 00 CSC 74: DBMS by Dr. Peng Ning 9 Spring 00 CSC 74: DBMS by Dr. Peng Ning 0 Nested Queries A nested query (subquery): a query in the WHERE clause of another query. Some paradigms of subqueries are <column> [NOT] IN <subquery> <column> <op> <subquery> <column> <op> ANY ALL <subquery> [NOT] EXISTS<subquery> <subquery> CONTAINS <subquery> Nested Queries Example: Find all the dept 6 projects located where a dept 5 project is located Project(Pname, Pnumber, Plocation, Dnum) SELECT Project.Pnumber FROM Project WHERE AND Project.Plocation IN (SELECT Project.Plocation FROM Project WHERE ); Spring 00 CSC 74: DBMS by Dr. Peng Ning Spring 00 CSC 74: DBMS by Dr. Peng Ning Nested Queries Example: List the name of the employee who has the highest salary. SELECT E., E. FROM WHERE (SELECT FROM ); Subqueries Subqueries can't modify tables For <column> <op> <subquery>, <subquery> can return exactly one value from one column Correlated subquery: When the subquery includes a field that is supplied from the outer query Spring 00 CSC 74: DBMS by Dr. Peng Ning 3 Spring 00 CSC 74: DBMS by Dr. Peng Ning 4 4

Correlated Subqueries: (,,, Dno, Address, ) Department(Dno, Dname, Location, ) Find names of employees who live where their dept is located Taking a slight liberty with the address column SELECT E., E. FROM AS E WHERE (SELECT D.Dno FROM Department AS D WHERE D.Location = ); Correlated Subqueries: These are the most interesting kinds of subqueries Intuitively, the subquery is evaluated once for each tuple in the main query Often the same table will show up in both the main query and the subquery Here, the use of aliases is a necessity, not merely a convenience Spring 00 CSC 74: DBMS by Dr. Peng Ning 5 Spring 00 CSC 74: DBMS by Dr. Peng Ning 6 CONTAINS Example Find s of employees who work on all the projects controlled by dept 5. (,,, Bdate, Address, Sex,, Super, Dno) Works_on(E, Pno, Hours) Project(Pname, Pnumber, Plocation, Dnum) SELECT, FROM AS E WHERE ( (SELECT Pno FROM Works_on AS W WHERE ) CONTAINS (SELECT Pnumber FROM Project AS P WHERE ) ); Spring 00 CSC 74: DBMS by Dr. Peng Ning 7 Spring 00 CSC 74: DBMS by Dr. Peng Ning 8 Achtung! ANY means some Thus, "salary > ANY (...)" could hold for the second lowest salary from the subquery evaluated within the ANY EXISTS Check whether the result of a subquery is empty. List the names of managers who have at least one dependent. Department(Dno, Dname, Mgr, ) (,,, ) Dependent(, Lnane, E) SELECT, FROM AS E WHERE EXISTS (SELECT * FROM Dependent AS D WHERE ) AND EXISTS (SELECT * FROM Department AS D WHERE )); Spring 00 CSC 74: DBMS by Dr. Peng Ning 9 Spring 00 CSC 74: DBMS by Dr. Peng Ning 30 5

Aggregate Operators SQL includes operators that combine data from a single column of several tuples SUM (only numeric) AVG (only numeric) COUNT MAX and MIN All eliminate NULLs except COUNT(*) All include duplicates unless DISTINCT COUNT(*) includes NULLs and duplicates List the sum of the salaries of all employees, the highest, the lowest, and the average salary, SELECT SUM(), MAX(), MIN(), AVG () FROM ; Spring 00 CSC 74: DBMS by Dr. Peng Ning 3 Spring 00 CSC 74: DBMS by Dr. Peng Ning 3 GROUP BY SELECT... FROM WHERE GROUP BY columns The GROUP BY columns must appear in the SELECT list as well. List the highest salary, the lowest, and the average salary of each department. DNo SELECT Dno, MAX(), MIN(), AVG() FROM GROUP BY Dno; 3 36000 Spring 00 CSC 74: DBMS by Dr. Peng Ning 33 Spring 00 CSC 74: DBMS by Dr. Peng Ning 34 HAVING A selection condition that applies to different groups resulting from a GROUP BY. List the highest salary, the lowest, and the average salary of the departments whose numbers are less than 3. DNo 3 36000 SELECT Dno, MAX(), MIN(), AVG() FROM GROUP BY Dno HAVING Dno < 3; Spring 00 CSC 74: DBMS by Dr. Peng Ning 35 Spring 00 CSC 74: DBMS by Dr. Peng Ning 36 6

Calculus to SQL: A methodology for producing sound SQL queries Write calculus expressions Systematically map them to SQL queries normalize the queries Map to. map free variables map quantifiers Spring 00 CSC 74: DBMS by Dr. Peng Ning 37 Find s of employees who work on all the projects controlled by dept 5. (,,, Bdate, Address, Sex,, Super, Dno) Works_on(E, Pno, Hours) Project(Pname, Pnumber, Plocation, Dnum) {e. (e) ( p: Project(p) (p.dnum 5 ( w: Works_on(w) w.pno=p.pnumber w.e=e.))} ( p: COND(p)) ( p: COND(p)) (A B) ( A B) (A B) ( A B) Spring 00 CSC 74: DBMS by Dr. Peng Ning 38 Calculus to SQL: With enhancements, the calculus can also be used to formulate aggregate queries in SQL aggregate operators bound and free variables revisited Generating aggregate queries logically Potential ambiguities in distributive and collective readings Calculus to SQL: 3 We can use the calculus even within the modification statements of SQL Modification operators involve a simple command, which is applied to tuples selected via a subquery Subqueries can be formulated via the calculus just as for queries Spring 00 CSC 74: DBMS by Dr. Peng Ning 39 Spring 00 CSC 74: DBMS by Dr. Peng Ning 40 SQL Views An SQL view is a table derived from other tables base (physical) tables ultimately depend on these defining tables other views Views are usually virtual sometimes materialized View Specification Views are specified with the paradigm CREATE VIEW view AS SELECT The SELECT part is the defining query In a view definition, we can define column names of view use aggregation (GROUP BY) Spring 00 CSC 74: DBMS by Dr. Peng Ning 4 Spring 00 CSC 74: DBMS by Dr. Peng Ning 4 7

View Resolution Views are computed by a sort of macro expansion when needed view resolution query modification: compute fresh view materialization: store always up to date: modifications to the defining tables are automatically reflected in the view General Constraints SQL allows declarative constraints such as CREATE ASSERTION assertion-name CHECK (enhanced subselect query) The DBMS checks if any ASSERTION is ever violated Spring 00 CSC 74: DBMS by Dr. Peng Ning 43 Spring 00 CSC 74: DBMS by Dr. Peng Ning 44 8