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



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

New York University Computer Science Department Courant Institute of Mathematical Sciences

CSC 443 Data Base Management Systems. Basic SQL

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

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

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

Introduction to Databases

Chapter 6: Integrity Constraints

How To Create A Table In Sql (Ahem)

Lecture 6. SQL, Logical DB Design

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Basic Concepts of Database Systems

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

Introduction to SQL ( )

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

Oracle 10g PL/SQL Training

Data Modeling. Database Systems: The Complete Book Ch ,

Instant SQL Programming

A basic create statement for a simple student table would look like the following.

Scheme G. Sample Test Paper-I

Oracle(PL/SQL) Training

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

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

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

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

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

5. CHANGING STRUCTURE AND DATA

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Physical Database Design Process. Physical Database Design Process. Major Inputs to Physical Database. Components of Physical Database Design

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

Oracle Database 10g: Introduction to SQL

The Relational Model. Why Study the Relational Model?

Part 4: Database Language - SQL

Oracle Database 10g Express

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

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

IT2304: Database Systems 1 (DBS 1)

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

How To Manage Data In A Database System

BCA. Database Management System

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

IT2305 Database Systems I (Compulsory)

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

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

Teach Yourself InterBase

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

Chapter 1: Introduction

Introduction to Microsoft Jet SQL

14 Triggers / Embedded SQL

Exercise 1: Relational Model

Elena Baralis, Silvia Chiusano Politecnico di Torino. Pag. 1. Active database systems. Triggers. Triggers. Active database systems.

Databases What the Specification Says

ATTACHMENT 6 SQL Server 2012 Programming Standards

Introduction to SQL: Data Retrieving

Databases and BigData

SQL NULL s, Constraints, Triggers

Oracle SQL. Course Summary. Duration. Objectives

Developing Microsoft SQL Server Databases 20464C; 5 Days

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

Testing of the data access layer and the database itself

Foundations of Information Management

1. INTRODUCTION TO RDBMS

Oracle Database: SQL and PL/SQL Fundamentals

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

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

DATABASE MANAGEMENT SYSTEMS. Question Bank:

Database System Concepts

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

20464C: Developing Microsoft SQL Server Databases

Conceptual Design Using the Entity-Relationship (ER) Model

Review: Participation Constraints

INFO/CS 330: Applied Database Systems

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Chapter 4. SQL Concepts & Facilities. Is SQL an End User Tool? AS/400 ANSI SQL Advanced Facilities

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

SQL Server. 1. What is RDBMS?

5.1 Database Schema Schema Generation in SQL

IINF 202 Introduction to Data and Databases (Spring 2012)

Course 20464: Developing Microsoft SQL Server Databases


Oracle Database: SQL and PL/SQL Fundamentals NEW

Summary on Chapter 4 Basic SQL

Databases. DSIC. Academic Year

CPS352 Database Systems: Design Project

Developing Microsoft SQL Server Databases

3. Relational Model and Relational Algebra

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

How to Implement Multi-way Active/Active Replication SIMPLY

Chapter 1: Introduction. Database Management System (DBMS)

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

The Relational Data Model and Relational Database Constraints

Database Programming with PL/SQL: Learning Objectives

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Developing Microsoft SQL Server Databases (20464) H8N64S

Oracle Database: SQL and PL/SQL Fundamentals

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Database Concepts. Database & Database Management System. Application examples. Application examples

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

Oracle Database 12c: Introduction to SQL Ed 1.1

Transcription:

CSI 2132 Tutorial 6 The Structured Query Language (SQL) The Structured Query Language De facto standard used to interact with relational DB management systems Two major branches DDL (Data Definition Language) Contains statements that define the structure of the database (create, alter or delete tables, relationships, constraints, etc) DML (Data Manipulation Language) Retrieve and modify data 2 1

Exercise 4.6 Consider the schema for the AIRLINE database in Fig. 3.8 What are the referential integrity constraints that should hold on the schema? Write appropriate SQL DDL statements to define the database. 3 4 2

5 6 3

7 8 4

9 10 5

11 12 6

13 14 7

15 We use the following notation to describe the foreign key constraints. FLIGHT_LEG.(FLIGHT_NUMBER) --> FLIGHT.(NUMBER) SEAT_RESERVATION.(FLIGHT_NUMBER, LEG_NUMBER, LEG_DATE) --> LEG_INSTANCE.(FLIGHT_NUMBER, LEG_NUMBER, LEG_DATE) 16 8

Exercise 4.7 Consider the schema for the LIBRARY database in Fig. 4.6 Choose the appropriate action (reject, cascade, set to null, set to default) for each referential integrity constraint, both for a deletion of a referenced tuple and for the update of a primary key attribute value in a referenced tuple. Justify your choices. 17 18 9

BOOK_AUTHORS.(BookId) --> BOOK.(BookId) ON DELETE CASCADE ON UPDATE CASCADE Automatically propagate the deletion or change of a BOOK to the referencing BOOK_AUTHORS. 19 BOOK.(PublisherName) --> PUBLISHER.(Name) ON DELETE REJECT ON UPDATE CASCADE Do not delete a PUBLISHER tuple which has linked BOOK tuples. Update the PUBLISHER s name on all BOOK tuples which refer to it. 20 10

BOOK_LOANS.(BookID) --> BOOK.(BookID) ON DELETE CASCADE ON UPDATE CASCADE If a BOOK record is deleted, then delete all its associated BOOK_LOAN records. Idem with updates. REJECT on DELETE also possible! 21 BOOK_COPIES.(BookID) --> BOOK.(BookID) ON DELETE CASCADE ON UPDATE CASCADE If a BOOK record is deleted, then delete all its associated BOOK_COPIES tuples. Do likewise with updates. 22 11

BOOK_LOANS.(CardNo) --> BORROWER.(CardNo) ON DELETE CASCADE ON UPDATE CASCADE If a BORROWER record is deleted, then delete all its associated BOOK_LOANS tuples. Do likewise with updates. REJECT on DELETE also possible! 23 BOOK_COPIES.(BranchID) --> LIBRARY_BRANCH.(BranchID) ON DELETE CASCADE ON UPDATE CASCADE If a LIBRARY_BRANCH record is deleted, then delete all its linked BOOK_COPIES tuples. Do likewise with updates. REJECT on DELETE also possible! 24 12

BOOK_LOANS.(BranchID) --> LIBRARY_BRANCH.(BranchID) ON DELETE CASCADE ON UPDATE CASCADE If a LIBRARY_BRANCH record is deleted, then delete all its linked BOOK_LOANS tuples. Do likewise with updates. REJECT on DELETE also possible! 25 Exercise 4.15 Consider the EMPLOYEE table s constraint EMPSUPERFK as in Fig 4.2 26 13

27 Exercise 4.15 If the constraint is changed to read as follows: 28 14

Exercise 4.15 What happens when the following command is run on the COMPANY database state shown in Fig. 3.6? 29 30 15

Answer: The table gets emptied. Triggers a deletion of all subordinate records in James Borg s supervision hierarchy. 31 Exercise 4.15 Is it better to CASCADE or SET NULL in case of EMPSUPERFK constraint ON DELETE? 32 16

Answer: SET NULL is preferred, since an EMPLOYEE is not fired (deleted) when his/her supervisor is deleted. Instead, the SUPERSSN field should be SET NULL so a new supervisor could be assigned later on. 33 Exercise 5.5 Specify the following additional SQL queries on the COMPANY database of Fig. 3.5 34 17

35 Exercise 5.5 a) For each department whose average employee salary is over 30K, retrieve the department name and the number of employees working for it. 36 18

Exercise 5.5 a) For each department whose average employee salary is over 30K, retrieve the department name and the number of employees working for it. 37 Exercise 5.5 b) Suppose we want the number of male employees in each department rather than all employees. Can we specify this in SQL? Why or why not? 38 19

Yes, via a nested query 39 Exercise 5.6 Specify the following SQL queries on the UNIVERSITY database schema of Fig. 1.2 40 20

41 42 21

Exercise 5.6 a) Retrieve the names and major departments of all straight-a students (i.e. those who got A in all their courses) 43 Exercise 5.6 a) Retrieve the names and major departments of all straight-a students (i.e. those who got A in all their courses) 44 22

Exercise 5.6 b) Retrieve the names and major departments of all students who do not have any grade of A in any of their courses. 45 Exercise 5.6 b) Retrieve the names and major departments of all students who do not have any grade of A in any of their courses. 46 23

Another way 47 Exercise 5.7 In SQL, specify the following queries on the COMPANY database in Fig. 3.5 using the concept of nested queries a) Retrieve the names of all employees who work in the department that has the employee with the highest salary among all employees. 48 24

49 a) Retrieve the names of all employees who work in the department that has the employee with the highest salary among all employees. 50 25

Exercise 5.7 b) Retrieve the names of all employees whose supervisor s supervisor has 888665555 for SSN. 51 Exercise 5.7 b) Retrieve the names of all employees whose supervisor s supervisor has 888665555 for SSN. 52 26

Exercise 5.7 c) Retrieve the names of employees who make at least 10K more than the employee who is paid the least in the company. 53 Exercise 5.7 c) Retrieve the names of employees who make at least 10K more than the employee who is paid the least in the company. 54 27

CSI 2132 Tutorial 6 The Structured Query Language (SQL) 28