M.P. Johnson, DBMS, Stern/NYU, Spring 20081 C20.0046: Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2008.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

1 Lecture 5: SQL Schema & Views. 2 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL)
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
CSE 544 Constraints Lecture #3 Friday, January 13, 2011 Dan Suciu , Winter
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #13 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Lecture 03: Advanced SQL. 2 Outline Unions, intersections, differences Subqueries, Aggregations, NULLs Modifying databases, Indexes, Views Reading:
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
1 Lecture 04 Entity/Relationship Modelling. 2 Outline E/R model (Chapter 5) From E/R diagrams to relational schemas (Chapter 5) Constraints in SQL (Chapter.
1 Introduction to Database Systems CSE 444 Lecture 05: Views, Constraints April 9, 2008.
1 Data Definition in SQL So far we have see the Data Manipulation Language, DML Next: Data Definition Language (DDL) Data types: Defines the types. Data.
1 Lecture 06: SQL Friday, January 14, Outline Indexes Defining Views (6.7) Constraints (Chapter 7) We begin E/R diagrams (Chapter 2)
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #14 M.P. Johnson Stern School of Business, NYU Spring, 2005.
1 Lecture 05: SQL Wednesday, October 8, Outline Outer joins (6.3.8) Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes.
Matthew P. Johnson, OCL2, CISDD CUNY, January OCL2 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY January, 2005.
1 INTERSECT and EXCEPT: (may no be in MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A,
Database Modifications A modification command does not return a result as a query does, but it changes the database in some way. There are three kinds.
Matthew P. Johnson, OCL4, CISDD CUNY, Sept OCL4 Oracle 10g: SQL & PL/SQL Session #5 Matthew P. Johnson CISDD, CUNY June, 2005.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #15 M.P. Johnson Stern School of Business, NYU Spring, 2005.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #12 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1 Lecture 4: More SQL Monday, January 13th, 2003.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
1 Lecture 6: Views Friday, January 17th, Updating Views How can I insert a tuple into a table that doesn’t exist? Employee(ssn, name, department,
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Constraints and Triggers. What’s IC? Integrity Constraints define the valid states of SQL-data by constraining the values in the base tables. –Restrictions.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 Introduction to Database Systems CSE 444 Lecture 04: SQL April 7, 2008.
1 Lecture 5: Outerjoins, Schema Creation and Views Wednesday, January 15th, 2003.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Lecture 05: SQL Wednesday, October 8, Outline Database Modifications (6.5) Defining Relation Schema in SQL (6.6) Indexes Defining Views (6.7)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
SQL.
Lecture 05: SQL Wednesday, January 12, 2005.
Cours 7: Advanced SQL.
Designing Tables for a Database System
Introduction to Database Systems CSE 444 Lecture 04: SQL
Lecture 06: SQL Systems Aspects
SQL Introduction Standard language for querying and manipulating data
Lecture 05 Views, Constraints
Lecture 12: SQL Friday, October 20, 2000.
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 06: SQL Monday, October 11, 2004.
Lecture 4: SQL Wednesday, April 10, 2002.
CSE544 SQL Monday, April 5, 2004.
Lecture 05: SQL Wednesday, October 9, 2002.
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 2008

M.P. Johnson, DBMS, Stern/NYU, Spring Agenda Last time: nulls, grouping & aggregation Today: creating tables, modifications, etc.

M.P. Johnson, DBMS, Stern/NYU, Spring Null/logic review TRUE AND UNKNOWN = ? TRUE OR UNKNOWN = ? UNKNOWN OR UNKNOWN = ? X = NULL = ?

M.P. Johnson, DBMS, Stern/NYU, Spring HAVING clauses Sometimes want to limit which rows may be grouped Q: How many mins. of film did each rich producer make?  Old = made movies before 1930 Q: Is HAVING necessary here? SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930 SELECT name, sum(length) total FROM Movie, MovieExec WHERE producerSsn = ssn GROUP BY name HAVING min(year) < 1930

M.P. Johnson, DBMS, Stern/NYU, Spring Review Examples from sqlzoo.netsqlzoo.net SELECT L FROM R 1, …, R n WHERE C SELECT L FROM R 1, …, R n WHERE C  L (  C (R 1 x … R n )

M.P. Johnson, DBMS, Stern/NYU, Spring New topic: Modifications Three kinds of modifications 1. Insertions 2. Deletions 3. Updates Sometimes “update” used as a synonym for “modification”

M.P. Johnson, DBMS, Stern/NYU, Spring Insertions General form: Missing attribute  NULL (or other default value) Example: Insert a new purchase to the database: INSERT INTO R(A1,…., An) VALUES(v1,….,vn) INSERT INTO Knights(name, britnatl, title) VALUES('Bill Gates', 'n', 'KBE')

M.P. Johnson, DBMS, Stern/NYU, Spring Insertions If we’re sure we have all values in the right order, can just say: Only do this if you’re sure of order in which the table fields were defined INSERT INTO R VALUES(v1,….,vn) INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE');

M.P. Johnson, DBMS, Stern/NYU, Spring Insertion the result of a query Product(name, etc.) Purchase(buyerssn, prodName, etc.) Maybe some purchases name missing products  add those to the Product table Subquery replaces VALUES INSERT INTO R(As) (query)

M.P. Johnson, DBMS, Stern/NYU, Spring Insertion example Product(name, listPrice, category) Purchase(prodName, buyerName, price) Premise: data corruption  lose some Product data  every product referred to in Purchase should exist in Product, but some are missing namelistPricecategory Canon D101000Camera Canon D202000Camera prodNamebuyerName Canon D10Bill Canon D10Hilary Canon D20George ProductPurchase

M.P. Johnson, DBMS, Stern/NYU, Spring Insertion example namelistPricecategory Canon D10NULL Canon D20NULL namelistPricecategory ProductProduct’ prodNamebuyerName Canon D10Bill Canon D20Hilary Canon D20George Purchase Canon D20NULL Q: Or do we get: A: Depends on implementation! INSERT INTO Product(name) SELECT prodName FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product) INSERT INTO Product(name) SELECT prodName FROM Purchase WHERE prodName NOT IN (SELECT name FROM Product)

M.P. Johnson, DBMS, Stern/NYU, Spring Deletions General form: E.g.: As usual, WHERE can contain subqueries  Depending on the DBMS Q: How do you delete just one row with SQL simpliciter?  Oracle has the ROWID/ROWNUM pseudo-field… DELETE FROM Table WHERE condition DELETE FROM Table WHERE condition INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); DELETE FROM Knights WHERE name = 'Bernard Kerik'; INSERT INTO Knights VALUES('R. Giuliani', 'n', 'KBE'); INSERT INTO Knights VALUES('Bernard Kerik', 'n', 'CBE'); DELETE FROM Knights WHERE name = 'Bernard Kerik';

M.P. Johnson, DBMS, Stern/NYU, Spring Updates General form: Example: As usual, WHERE can contain subqueries UPDATE Product SET field1 = value1, field2 = value2 WHERE condition UPDATE Product SET field1 = value1, field2 = value2 WHERE condition UPDATE Product SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date = DATE'Oct, 25, 1999') UPDATE Product SET price = price/2 WHERE Product.name IN (SELECT product FROM Purchase WHERE Date = DATE'Oct, 25, 1999')

M.P. Johnson, DBMS, Stern/NYU, Spring Next: defining schemata So far, have done queries and data manipulation Now doing data definition Recall data types:  INT or INTEGER (variant: SHORTINT)  FLOAT or REAL: floating-point numbers DOUBLE PRECISION: DECIMAL(n,d):  E.g. decimal(5,2): five decimal digits, with the decimal point two positions from the right: e.g  DATE and TIME  Character strings Fixed length: CHAR(n) Variable length: VARCHAR(n)

M.P. Johnson, DBMS, Stern/NYU, Spring Creating tables Form: E.g.: CREATE TABLE Table-name ( field1 field-type, field2 field-type, … fieldn field-type ) CREATE TABLE Table-name ( field1 field-type, field2 field-type, … fieldn field-type ) No comma! CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age INT, city VARCHAR(30), gender CHAR(1), dob DATE ) CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age INT, city VARCHAR(30), gender CHAR(1), dob DATE )

M.P. Johnson, DBMS, Stern/NYU, Spring Default Values Specify defaults when creating table: The default default: NULL CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age SHORTINT DEFAULT 100, city VARCHAR(30) DEFAULT 'New York', gender BIT(1), dob DATE DEFAULT DATE ' ' ) CREATE TABLE People ( name VARCHAR(30), ssn CHAR(9), age SHORTINT DEFAULT 100, city VARCHAR(30) DEFAULT 'New York', gender BIT(1), dob DATE DEFAULT DATE ' ' )

M.P. Johnson, DBMS, Stern/NYU, Spring Deleting and modifying schemata Delete data, indices, schema: Delete data and indices: Either way, exercise extreme caution! Add or delete attributes: Q: What’s put in the new fields? DROP TABLE Person TRUNCATE TABLE Person ALTER TABLE Person ADD phone CHAR(12) ALTER TABLE Person ADD phone CHAR(12) ALTER TABLE Person DROP age ALTER TABLE Person DROP age

M.P. Johnson, DBMS, Stern/NYU, Spring Constraints in SQL Constraints in SQL: Keys foreign keys Attribute-level constraints Tuple-level constraints Global constraints: assertions The more complex the constraint, the harder it is to check and to enforce simplest Most complex

M.P. Johnson, DBMS, Stern/NYU, Spring Constraints in SQL A constraint = a property that we’d like our database to hold The system will enforce the constraint by taking some actions:  forbid an update  or perform compensating updates

M.P. Johnson, DBMS, Stern/NYU, Spring Primary Keys & Unique Keys There is at most one PRIMARY KEY; there can be many UNIQUE  Primary key v. candidate keys Key fields get indices automatically (why?) CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category) ) CREATE TABLE Product ( productID CHAR(10), name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (productID), UNIQUE (name, category) )

M.P. Johnson, DBMS, Stern/NYU, Spring Keys Or: CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20) ) CREATE TABLE Product ( name CHAR(30) PRIMARY KEY, category VARCHAR(20) ) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name) ) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20) PRIMARY KEY (name) )

M.P. Johnson, DBMS, Stern/NYU, Spring Keys with Multiple Attributes NameCategoryPrice Canon D10Camera1200 Canon D20Camera2000 iPod G4MP3 Player250 Canon D20Camera800 CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category) ) CREATE TABLE Product ( name CHAR(30), category VARCHAR(20), price INT, PRIMARY KEY (name, category) )

M.P. Johnson, DBMS, Stern/NYU, Spring Foreign Key Constraints prodName is a foreign key to Product(name) name should be a key in Product Purchase ~ Product is many-one NB: referenced field specified with parens, not dot Referenced and referring fields should/must be indexed (why?) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME ) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME ) Referential integrity in SQL

M.P. Johnson, DBMS, Stern/NYU, Spring NameCategory Canon D10Camera Canon D20Camera iPod 4GMP3 Player ProdNameStore Canon D10Wiz Canon D10Wiz Canon D20Best Buy ProductPurchase

M.P. Johnson, DBMS, Stern/NYU, Spring Foreign Key Constraints Or: (name, category) must be a key (primary/unique) in Product (why?) CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category) CREATE TABLE Purchase ( prodName CHAR(30), category VARCHAR(20), date DATETIME, FOREIGN KEY (prodName, category) REFERENCES Product(name, category)

M.P. Johnson, DBMS, Stern/NYU, Spring NameCategory Canon D10Camera Canon D20Camera iPod 4GMP3 Player ProdNameStore Canon D10Wiz Canon D10Wiz Canon D20Best Buy ProductPurchase What happens during updates? Types of updates: In Purchase: insert/update In Product: delete/update

M.P. Johnson, DBMS, Stern/NYU, Spring What happens during updates? SQL has three policies for maintaining referential integrity: 1. Reject: violating modifications (default) 2. Cascade: after a delete/update do a delete/update 3. Set-null: set foreign-key field to NULL

M.P. Johnson, DBMS, Stern/NYU, Spring Constraints on attributes and tuples Constraints on attributes:  NOT NULL-- obvious meaning... Can combine NOT NULL with foreign key constraints  CHECK condition-- any condition on row itself Some DBMS support subqueries here, but many don’t Constraints on tuples  CHECK condition

M.P. Johnson, DBMS, Stern/NYU, Spring CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL ) CREATE TABLE Purchase ( prodName CHAR(30) CHECK (prodName IN SELECT Product.name FROM Product), date DATETIME NOT NULL ) How is this different from a Foreign Key? Tuple-level constraint

M.P. Johnson, DBMS, Stern/NYU, Spring General Assertions Defined in latest SQL standard, but isn’t actually implemented Approximated in MySQL and Oracle as stored procedures  PL/SQL in Oracle CREATE ASSERTION myAssert CHECK (NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) ) CREATE ASSERTION myAssert CHECK (NOT EXISTS( SELECT Product.name FROM Product, Purchase WHERE Product.name = Purchase.prodName GROUP BY Product.name HAVING count(*) > 200) )

M.P. Johnson, DBMS, Stern/NYU, Spring New: Indices Important for speeding up query processing Index on field(s) = data structure that makes searches/comparisons on those fields fast Suppose we have a relation Person (name, age, city) Sequential scan of the whole Person file may take a very long time SELECT * FROM Person WHERE name = 'Waksal, Sam' SELECT * FROM Person WHERE name = 'Waksal, Sam'

M.P. Johnson, DBMS, Stern/NYU, Spring Creating Indices Syntax: Here: Now searching by name is much faster  How much faster?  Log-time, say  Base-what? doesn’t matter, but say 2 If all New Yorkers, #comparisons:   log 2 ( ) ~= 23  (i.e., 2 23 ~= ) CREATE INDEX index-name ON R(fields) CREATE INDEX nameIndex ON Person(name)

M.P. Johnson, DBMS, Stern/NYU, Spring How do indices work? What the data structure?  Different possibilities 1 st intuition: index on field f is an ordered list of all values in the table’s f field  each item has address (“rowid”) of its row Where do we get the ordered list? 2 nd intuition: put all f values in a BST  searching BST takes log time (why?) DBMSs actually use a variant: B+Tree  Later…

M.P. Johnson, DBMS, Stern/NYU, Spring Creating Indices Indexes can be useful in range queries too: CREATE INDEX ageIndex ON Person (age) SELECT * FROM Person WHERE age > 25

M.P. Johnson, DBMS, Stern/NYU, Spring Using indices Indices can be created on multiple attributes: Helps in: And in: But not in: Idea: our sorted list is sorted on age;city, not city;age Q: In Movie, should index be on year;title or title;year? CREATE INDEX doubleNdx ON Person (lname, fname) SELECT * FROM Person WHERE fname='Sam' AND lname = 'Waksal' SELECT * FROM Person WHERE lname='Waksal' SELECT * FROM Person WHERE fname='Sam'

M.P. Johnson, DBMS, Stern/NYU, Spring The Index Selection Problem Indices speed up queries dramatically, so… Why not just index all (sequences of) fields?  how does the list/B+Tree stay up to date? Given a workload: a set of SQL queries and their freqs Q: What indices will speed up the workload? Answers:  Attributes in WHERE clauses (queries)  favor an index  Attributes in INSERT/UPDATE/DELETEs  discourage an index  In many DBMSs: your primary/foregin key fields get indexed automatically (why?)

M.P. Johnson, DBMS, Stern/NYU, Spring Next: Views Stored relations physically exist and persist Views are relations that don’t  in some texts, “table” = stored relation = “base table” Basically names/references given to queries  maybe a relevant subset of a table Employee(ssn, name, department, project, salary) Payroll has access to Employee, others only to Developers CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = 'Dev' CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = 'Dev'

M.P. Johnson, DBMS, Stern/NYU, Spring A Different View Person(name, city) Purchase(buyer, seller, product, store) Product(name, maker, category) We have a new virtual table: NYCview(buyer, seller, product, store) CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer

M.P. Johnson, DBMS, Stern/NYU, Spring A Different View Now we can query the view: CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera'

M.P. Johnson, DBMS, Stern/NYU, Spring What happens when we query a view? SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, NYCview.store FROM NYCview, Product WHERE NYCview.product = Product.name AND Product.category = 'Camera' SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = 'New York' AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = 'Camera' SELECT name, Purchase.store FROM Person, Purchase, Product WHERE Person.city = 'New York' AND Person.name = Purchase.buyer AND Purchase.poduct = Product.name AND Product.category = 'Camera'

M.P. Johnson, DBMS, Stern/NYU, Spring Can rename view fields CREATE VIEW NYCview(NYCbuyer, NYCseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer CREATE VIEW NYCview(NYCbuyer, NYCseller, prod, store) AS SELECT buyer, seller, product, store FROM Person, Purchase WHERE Person.city = 'New York' AND Person.name = Purchase.buyer

M.P. Johnson, DBMS, Stern/NYU, Spring Types of Views Views discussed here:  Used in databases  Computed only on-demand – slow at runtime  Always up to date Sometimes talk about “materialized” views  Used in data warehouses  Pre-computed offline – fast at runtime  May have stale data  Maybe more later…

M.P. Johnson, DBMS, Stern/NYU, Spring Updating Views How to insert a tuple into a table that doesn’t exist? Employee(ssn, name, department, project, salary) If we make the following insertion: It becomes: CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = 'Development' CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = 'Development' INSERT INTO Developers VALUES('Bill', ‘Word') INSERT INTO Employee(ssn, name, dept, project, sal) VALUES(NULL, 'Bill', NULL, 'Word', NULL) INSERT INTO Employee(ssn, name, dept, project, sal) VALUES(NULL, 'Bill', NULL, 'Word', NULL)

M.P. Johnson, DBMS, Stern/NYU, Spring Non-Updatable Views Person(name, city) Purchase(buyer, seller, product, store) How can we add the following tuple to the view? ('NYC', 'The Wiz') We don’t know the name of the person who made the purchase cannot set to NULL (why?) CREATE VIEW CityStore AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer CREATE VIEW CityStore AS SELECT Person.city, Purchase.store FROM Person, Purchase WHERE Person.name = Purchase.buyer

M.P. Johnson, DBMS, Stern/NYU, Spring Finally: R.A./SQL has limitations Can easily find Alice’s direct subordinates: But: find all of King’s underlings Cannot compute “transitive closure” Cannot express in R.A./SQL! SQL is not “Turing-Complete” NameJobBoss KingPresidentNULL JonesManagerKing BlakeManagerKing FordAnalystJones ScottAnalystJones

M.P. Johnson, DBMS, Stern/NYU, Spring Midterm Know all steps of project so far:  Draw E/R from dataset description  Convert E/R to relations  Normalize badly designed relations Indices:  Why important  How BSTs work Constraints  Why important  Basic concepts SQL!