Data Definition Language (DDL), Views and Indexes Instructor: Shel Finkelstein

Similar documents
SQL queries II. Set operations and joins

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

Set Operations, Union

Information Systems for Engineers. Exercise 10. ETH Zurich, Fall Semester Hand-out Due

Database Modifications and Transactions

Joins, NULL, and Aggregation

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Midterm 1 157A Fall /22

Information Systems Engineering. Other Database Concepts

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

Relational Model, Key Constraints

Programming and Database Fundamentals for Data Scientists

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

CMPS 182: Introduction to Database Management Systems. Instructor: David Martin TA: Avi Kaushik. Syllabus

Database Systems Architecture. Stijn Vansummeren

1. Given the name of a movie studio, find the net worth of its president.

Midterm Review. Winter Lecture 13

CSC 261/461 Database Systems Lecture 6. Fall 2017

AC61/AT61 DATABASE MANAGEMENT SYSTEMS DEC 2013

Lecture 04: SQL. Monday, April 2, 2007

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to Database Systems. Announcements CSE 444. Review: Closure, Key, Superkey. Decomposition: Schema Design using FD

Sample Question Paper

Chapter 1 SQL and Data

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

EECS-3421a: Test #2 Queries

COMP102: Introduction to Databases, 5 & 6

The Relational Model of Data (ii)

Lecture 04: SQL. Wednesday, October 4, 2006

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

COSC 304 Introduction to Database Systems. Views and Security. Dr. Ramon Lawrence University of British Columbia Okanagan

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

An Introduction to Structured Query Language

THE AUSTRALIAN NATIONAL UNIVERSITY. Mid-Semester Examination August 2006 RELATIONAL DATABASES (COMP2400)

Exam code: Exam name: Database Fundamentals. Version 16.0

Optimization of logical query plans Eliminating redundant joins

Introduction to Data Management. Lecture 16 (SQL: There s STILL More...) It s time for another installment of...

Oracle Database 10g: Introduction to SQL

Query Processing and Optimization

Data about data is database Select correct option: True False Partially True None of the Above

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

SQL STRUCTURED QUERY LANGUAGE

Relational Database Languages

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

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

An Introduction to Structured Query Language

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement


Basant Group of Institution

Solved MCQ on fundamental of DBMS. Set-1

Mahathma Gandhi University

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

Assignment Session : July-March

Database Management System 9

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

Database Systems CSE 303. Lecture 02

Niklas Fors The Relational Data Model 1 / 17

Database Systems CSE 303. Lecture 02

CS W Introduction to Databases Spring Computer Science Department Columbia University

Slides by: Ms. Shree Jaswal

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

High-Level Database Models. Spring 2011 Instructor: Hassan Khosravi

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Isolation Levels and Concurrency

UNIT-IV (Relational Database Language, PL/SQL)

Chapter 4: Intermediate SQL

An Introduction to Structured Query Language

Carnegie Mellon Univ. Dept. of Computer Science Database Applications. General Overview - rel. model. Overview - detailed - SQL

3ISY402 DATABASE SYSTEMS

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

Introduction to Data Management CSE 344

CSC 261/461 Database Systems Lecture 5. Fall 2017

2. (10 points) SQL. Some Quidditch League! [Exercise] Consider the Movie database with the schema in Figure 2 on page 15 for the questions below.

Database Technology. Topic 3: SQL. Olaf Hartig.

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

CSEN 501 CSEN501 - Databases I

Course Modules for MCSA: SQL Server 2016 Database Development Training & Certification Course:

Chapter 4: Intermediate SQL

Debapriyo Majumdar DBMS Fall 2016 Indian Statistical Institute Kolkata

Views. COSC 304 Introduction to Database Systems. Views and Security. Creating Views. Views Example. Removing Views.

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

1) Introduction to SQL

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Rochester Institute of Technology Golisano College of Computing and Information Sciences Department of Information Sciences and Technologies


Textbook: Chapter 4. Chapter 5: Intermediate SQL. CS425 Fall 2016 Boris Glavic. Chapter 5: Intermediate SQL. View Definition.

Exam I Computer Science 420 Dr. St. John Lehman College City University of New York 12 March 2002

MIE253S Midterm Exam

The Relational Model

SQL: Data De ni on. B0B36DBS, BD6B36DBS: Database Systems. h p:// Lecture 3

Creating and Managing Tables Schedule: Timing Topic

Lecture 3 SQL - 2. Today s topic. Recap: Lecture 2. Basic SQL Query. Conceptual Evaluation Strategy 9/3/17. Instructor: Sudeepa Roy

C Examcollection.Premium.Exam.58q

Where are we? Week -4: Data definition (Creation of the schema) Week -3: Data definition (Triggers) Week -1: Transactions and concurrency in ORACLE.

B.C.A DATA BASE MANAGEMENT SYSTEM MODULE SPECIFICATION SHEET. Course Outline

CS425 Fall 2017 Boris Glavic Chapter 5: Intermediate SQL

Transcription:

Data Definition Language (DDL), Views and Indexes Instructor: Shel Finkelstein Reference: A First Course in Database Systems, 3 rd edition, Chapter 2.3 and 8.1-8.4

Important Notices Reminder: Midterm is on Monday, Nov 6 is usual classroom, at usual time. No make-ups, no early/late exams. You may bring a single two-sided 8.5 x 11 sheet of paper with as much info written (or printed) on it as you can fit and read unassisted. No sharing of these sheets will be permitted. Hand in these sheets at the end of the exam. Midterm will cover all material up to and including Lecture 7 (Views/Indexes). No devices are permitted during exam. You must show your UCSC ID at the end of the exam. Hope that everyone who needs DRC accommodation has already submitted form to me. Winter 2017 Midterm has been posted on Piazza; solution will be posted next week. Gradiance #3 is due on Friday, November 3; one problem uses Views! Sign-up for LSS tutoring with Alexander Ou if there s room.

HAVING Example SELECT e.name, SUM(m.length) FROM MovieExec e, Movies m WHERE m.producerc# = e.cert# GROUP BY e.name HAVING MIN(m.year) < 1930; Find the total film length for just those producers who made at least one film prior to 1930.

Another HAVING Example SELECT e.name, SUM(m.length), MAX(m.year) FROM MovieExec e, Movies m WHERE m.producerc# = e.cert# GROUP BY e.name HAVING COUNT(DISTINCT m.year) >=3 AND MIN(m.year) < 1930; Find the total film length and the latest movie year, for just those producers who made movies in at least 3 different years, and made at least one film prior to 1930.

SQL Language Data Manipulation Language (DML) Access and modify data SELECT, INSERT, DELETE, UPDATE Data Definition Language (DDL) Modify structure of data CREATE, DROP, ALTER Data Control Language (DCL) Control access to the data (security) GRANT, REVOKE Databases also have Utilities, such as Backup/Restore Syntax not specified in the SQL standard

CREATE TABLE CREATE TABLE MovieStar ( name CHAR(30), address VARCHAR(255) DEFAULT Hollywood, gender CHAR(1), birthdate DATE NOT NULL DEFAULT 2001-12-30 PRIMARY KEY (name) ); PRIMARY KEY DEFAULT NOT NULL

Reminder: Some Facts About Nulls Almost all comparisons with NULL will evaluate to unknown. If Salary is NULL, then the following will be unknown (treated like false): Salary = 10 Salary <> 10 90 > Salary OR 90 <= Salary Salary = NULL Salary <> NULL Use of IS NULL and IS NOT NULL Salary IS NULL will be true if Salary is NULL, false otherwise Salary IS NOT NULL will be true if Salary isn t NULL, false otherwise ORDER BY works with attributes that can have NULL values NULL will probably be smallest or largest value Not specified by SQL standard, so it depends on the implementation GROUP BY also works with attributes that can have NULL values

DROP TABLE Dropping a table: DROP TABLE MovieStar; Don t assume that rolling back transaction will bring back the table! Interaction of DDL and transactions may depend on implementation.

ALTER TABLE Adding a column to a table: ALTER TABLE MovieStar ADD phone CHAR(16) DEFAULT unlisted ; Dropping a column from a table: ALTER TABLE MovieStar DROP birthdate; In some systems: ALTER TABLE MovieStar DROP COLUMN birthdate; In some SQL systems, dropping a column isn t allowed. Changing the type of a column: Some implementations let you change type of column in limited ways.

What Can You CREATE/DROP in SQL DDL? TABLE VIEW INDEX ASSERTION TRIGGER SCHEMA PROCEDURE/FUNCTION/TYPE SQL2003 standard, but there are significant variations in implementations in different systems

VIEWS: Motivation for Views Views help with logical data independence, allowing you to retrieve data as if it matched the description in the view. CREATE VIEW < view-name> AS < view-definition> ; CREATE VIEW ParamountMovies AS SELECT title, year FROM Movies WHERE studioname = Paramount ' ; You may now ask queries on ParamountMovies as if it were a table: SELECT title FROM ParamountMovies WHERE year=1976; Composition in SQL is powerful: Tables, Queries, Views

More Views Movies ( title, year, length, genre, studioname, producerc# ) MovieExec ( name, address, cert#, networth ) CREATE VIEW MovieProd AS SELECT m.title, e.name, m.genre FROM Movies m, MovieExec e WHERE m.producerc# = e.cert# ; SELECT DISTINCT genre FROM MovieProd WHERE name = George Lucas ;

Renaming Attributes in CREATE VIEW Movies ( title, year, length, genre, studioname, producerc# ) MovieExec ( name, address, cert#, networth ) CREATE VIEW MovieProd(movie_title, prod_name, movie_genre) AS SELECT m.title, e.name, m.genre FROM Movies m, MovieExec e WHERE m.producerc# = e.cert# ; SELECT DISTINCT movie_genre FROM MovieProd WHERE prod_name = George Lucas ;

What is a View? A view can include any SQL SELECT statement Including UNION, Aggregates, GROUP BY, HAVING, ORDER BY, etc. A view is not stored as a table The tables underlying the view are stored in the database, but only the description of the view is in the database although some systems support MATERIALIZED VIEWS But a view can be used in many (not all) of the same ways as tables Views can be queried Views can be defined on views, as well as on tables!

Queries on Views and Tables CREATE VIEW ParamountMovies AS SELECT title, year FROM Movies WHERE studioname = Paramount ' ; SELECT DISTINCT s.starname FROM ParamountMovies p, StarsIn s WHERE p.title = s.movietitle AND p.year = s.movieyear ; CREATE VIEW ParamountStars AS SELECT DISTINCT starname FROM ParamountMovies, StarsIn WHERE title = movietitle AND year = movieyear ;

DROP VIEW CREATE VIEW ParamountMovies AS SELECT title, year FROM Movies WHERE studioname = Paramount ' ; DROP View ParamountMovies; What happens if you execute the following after dropping that view? SELECT * FROM ParamountMovies; SELECT * FROM Movies;

View Updates Some modification operations on views work, but others do not, generally failing either because: Constraint on underlying table would be violated, or The effects of the View modification is not well-defined on the underlying tables. This is a complex topic, which we ll only discuss briefly. See Textbook Section 8.2 for more info.

View Update Problems Movies(title, year, length, genre, studioname, producerc#) CREATE VIEW ParamountMovies AS SELECT title, year FROM Movies WHERE studioname = Paramount ' ; INSERT INTO ParamountMovies VALUES ( StarTrek, 1979); The INSERT will fail if the other columns of Movies (besides title and year) don t have defaults, and also don t allow NULL values.

View Update Problems (continued) Ambiguous View Update example with Employees and Departments << We ll draw this on the board >>

INDEXES: Motivation for Indexes Searching an entire table may take a long time: SELECT * FROM Movies WHERE studioname = Disney AND year = 1990; If there were 100 Million movies, searching them might take a while. An index (e.g., a B-Tree) would allow faster access to matching movies. If a table is updated, all Indexes on that table are immediately automatically updated within the same transaction. Which indexes do you need to change on INSERT and DELETE? What about UPDATE?

CREATE INDEX SELECT * FROM Movies WHERE studioname = Disney AND year = 1990; How much would each of these indexes help? CREATE INDEX YearIndex ON Movies(year); CREATE INDEX StudioIndex ON Movies(studioName); CREATE INDEX YSIndex ON Movies(year,studioName); CREATE INDEX SYIndex ON Movies(studioName,year); How much would each of the indexes help if the WHERE clause was just year = 1990?

Indexes and Ordering SELECT * FROM Movies WHERE studioname = Disney AND year < 1990; How much would each of these indexes help? CREATE INDEX YearIndex ON Movies(year); CREATE INDEX StudioIndex ON Movies(studioName); CREATE INDEX YSIndex ON Movies(year,studioName); CREATE INDEX SYIndex ON Movies(studioName,year); How much would each of the indexes help if the WHERE clause was just year < 1990?

Disadvantages of Indexes? Why not put indexes on every attributes, or even on every combination of attributes that you might query on? Huge number of indexes Space for indexes Cache impact of searching indexes Update time for indexes when table is modified

Index Design Most Database Administrators (DBAs) pick a set of indexes that work well on expected workload, and there are tools that help pick good indexes But workloads change, so choice of indexes may need to change DROP INDEX YearIndex; Keys are indexed (automatically in many database systems) to: Help maintain uniqueness (primary key, unique) Check Foreign Key references to Primary Keys (Referential Integrity)

Index Utilization SQL statements don t specify use of indexes, so they don t have to be modified when you change what s indexed! Database Optimizer tries to figure out the best / a good way to execute each SQL query. All the tuples in a Relation can be scanned directly, without using indexes, so indexes aren t necessary except for performance. Some systems have ways that you can tell the Optimizer what to do. This has advantages and disadvantages. (What are they?) Many SQL systems (including PostgreSQL) have an EXPLAIN PLAN statement, so that you can see what plan the optimizer chooses for a SQL statement.