1 Introduction to Database Systems CSE 444 Lecture 05: Views, Constraints April 9, 2008.

Slides:



Advertisements
Similar presentations
1 Lecture 4: Advanced SQL. 2 INTERSECT and EXCEPT: (missing from MySQL) (SELECT R.A, R.B FROM R) INTERSECT (SELECT S.A, S.B FROM S) (SELECT R.A, R.B FROM.
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)
1 Constraints, Triggers and Active Databases Chapter 9.
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.
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:
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
Lecture 03: Views and Constraints Wednesday, October 13, 2010 Dan Suciu -- CSEP544 Fall
CSE 544 Data Models and Views Lecture #4 Wednesday, January 19, 2011 Dan Suciu , Winter
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.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #12 M.P. Johnson Stern School of Business, NYU Spring, 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.
SQL April 25 th, Agenda Grouping and aggregation Sub-queries Updating the database Views More on views.
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.
Winter 2002Arthur Keller – CS 1809–1 Schedule Today: Jan. 31 (TH) u Constraints. u Read Sections , Project Part 3 due. Feb. 5 (T) u Triggers,
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,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Fall 2001Arthur Keller – CS 1809–1 Schedule Today Oct. 23 (T) Constraints. u Read Sections Assignment 4 due. Project Part 3 due Oct. 24 (W). Oct.
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.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
1 Lecture 4: More SQL Monday, January 13th, 2003.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
1 Lecture 7: End of Normal Forms Outerjoins, Schema Creation and Views Wednesday, January 28th, 2004.
SQL Constraints & Triggers May 10 th, Agenda Big picture –what are constraints & triggers? –where do they appear? –why are they important? In SQL.
SQL (almost end) April 26 th, Agenda HAVING clause Views Modifying views Reusing views.
SQL April 22 th, Agenda Union, intersections Sub-queries Modifying the database Views Modifying views Reusing views.
1 Lecture 04: SQL Wednesday, January 11, Outline Two Examples Nulls (6.1.6) Outer joins (6.3.8) Database Modifications (6.5)
Onsdag The concepts in a relation data model SQL DDL DML.
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.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
14-1 Goals of Database Design A database should provide for efficient storage, update, and retrieval of data. A database should be reliable—the stored.
Phase 2, Answering queries using views. February 2 nd, 2004.
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.
1 SQL Additional Notes. 2  1 Group and Aggregation*  2 Execution Order*  3 Join*  4 Find the maximum  5 Line Format SQL Additional Notes *partially.
Lecture 03: Normal Forms, Constraints, Views Wednesday, October 12, 2011 Dan Suciu -- CSEP544 Fall
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.
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)
1 Lecture 03 Views, Constraints Tuesday, January 23, 2007.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
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.
Lecture 05: SQL Wednesday, January 12, 2005.
Cse 344 March 2nd – E/r diagrams.
Introduction to Database Systems CSE 444 Lecture 04: SQL
SQL: Constraints and Triggers
Cse 344 May 14th – Entities.
Cse 344 May 18th – Loss and views.
Lecture 06: SQL Systems Aspects
Lecture 05 Views, Constraints
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:

1 Introduction to Database Systems CSE 444 Lecture 05: Views, Constraints April 9, 2008

2 Outline Views –Chapter 6.7 Constraints –Chapter 7

3 Views Views are relations, except that they are not physically stored. For presenting different information to different users 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 = ‘Development’ CREATE VIEW Developers AS SELECT name, project FROM Employee WHERE department = ‘Development’

4 CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname Example Purchase(customer, product, store) Product(pname, price) CustomerPrice(customer, price) “virtual table”

5 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 We can later use the view: Purchase(customer, product, store) Product(pname, price) CustomerPrice(customer, price)

6 Types of Views Virtual views: –Used in databases –Computed only on-demand – slow at runtime –Always up to date Materialized views –Used in data warehouses –Pre-computed offline – fast at runtime –May have stale data We discuss only virtual views in class

7 Queries Over Views: Query Modification SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname CREATE VIEW CustomerPrice AS SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname View: Query:

8 Queries Over Views: Query Modification SELECT u.customer, v.store FROM (SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname) u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT u.customer, v.store FROM (SELECT x.customer, y.price FROM Purchase x, Product y WHERE x.product = y.pname) u, Purchase v WHERE u.customer = v.customer AND u.price > 100 Modified query:

9 Queries Over Views: Query Modification SELECT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname SELECT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname Modified and rewritten query:

10 But What About This ? SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 ??

11 Answer SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT u.customer, v.store FROM CustomerPrice u, Purchase v WHERE u.customer = v.customer AND u.price > 100 SELECT DISTINCT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname SELECT DISTINCT x.customer, v.store FROM Purchase x, Product y, Purchase v, WHERE x.customer = v.customer AND y.price > 100 AND x.product = y.pname

12 Applications of Virtual Views Logical data independence: –Vertical data partitioning –Horizontal data partitioning Security –Table (view) V reveals only what the users are allowed to know

13 Vertical Partitioning SSNNameAddressResumePicture MaryHustonClob1…Blob1… SueSeattleClob2…Blob2… JoanSeattleClob3…Blob3… AnnPortlandClob4…Blob4… Resumes SSNNameAddress MaryHuston SueSeattle... SSNResume Clob1… Clob2… SSNPicture Blob1… Blob2… T1 T2T3

14 Vertical Partitioning CREATE VIEW Resumes AS SELECT T1.ssn, T1.name, T1.address, T2.resume, T3.picture FROM T1,T2,T3 WHERE T1.ssn=T2.ssn and T2.ssn=T3.ssn CREATE VIEW Resumes AS SELECT T1.ssn, T1.name, T1.address, T2.resume, T3.picture FROM T1,T2,T3 WHERE T1.ssn=T2.ssn and T2.ssn=T3.ssn When do we use vertical partitioning ?

15 Vertical Partitioning SELECT address FROM Resumes WHERE name = ‘Sue’ SELECT address FROM Resumes WHERE name = ‘Sue’ Which of the tables T1, T2, T3 will be queried by the system ?

16 Vertical Partitioning Applications: When some fields are large, and rarely accessed –E.g. Picture In distributed databases –Customer personal info at one site, customer profile at another In data integration –T1 comes from one source –T2 comes from a different source

17 Horizontal Partitioning SSNNameCityCountry MaryHoustonUSA SueSeattleUSA JoanSeattleUSA AnnPortlandUSA --FrankCalgaryCanada --JeanMontrealCanada Customers SSNNameCityCountry MaryHoustonUSA CustomersInHouston SSNNameCityCountry SueSeattleUSA JoanSeattleUSA CustomersInSeattle SSNNameCityCountry --FrankCalgaryCanada --JeanMontrealCanada CustomersInCanada

18 Horizontal Partitioning CREATE VIEW Customers AS CustomersInHouston UNION ALL CustomersInSeattle UNION ALL... CREATE VIEW Customers AS CustomersInHouston UNION ALL CustomersInSeattle UNION ALL...

19 Horizontal Partitioning SELECT name FROM Customers WHERE city = ‘Seattle’ SELECT name FROM Customers WHERE city = ‘Seattle’ Which tables are inspected by the system ? WHY ???

20 Horizontal Partitioning CREATE VIEW Customers AS (SELECT * FROM CustomersInHouston WHERE city = ‘Houston’) UNION ALL (SELECT * FROM CustomersInSeattle WHERE city = ‘Seattle’) UNION ALL... CREATE VIEW Customers AS (SELECT * FROM CustomersInHouston WHERE city = ‘Houston’) UNION ALL (SELECT * FROM CustomersInSeattle WHERE city = ‘Seattle’) UNION ALL... Better:

21 Horizontal Partitioning SELECT name FROM Customers WHERE city = ‘Seattle’ SELECT name FROM Customers WHERE city = ‘Seattle’ SELECT name FROM CustomersInSeattle SELECT name FROM CustomersInSeattle

22 Horizontal Partitioning Applications: Optimizations: –E.g. archived applications and active applications Distributed databases Data integration

23 Views and Security CREATE VIEW PublicCustomers SELECT Name, Address FROM Customers CREATE VIEW PublicCustomers SELECT Name, Address FROM Customers NameAddressBalance MaryHouston SueSeattle-240 JoanSeattle AnnPortland-520 Fred is allowed to see this Customers: Fred is not allowed to see this

24 Views and Security NameAddressBalance MaryHouston SueSeattle-240 JoanSeattle AnnPortland-520 CREATE VIEW BadCreditCustomers SELECT * FROM Customers WHERE Balance < 0 CREATE VIEW BadCreditCustomers SELECT * FROM Customers WHERE Balance < 0 Customers: John is allowed to see only <0 balances

25 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

26 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)) Product(name, category)

27 Keys with Multiple Attributes 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)) NameCategoryPrice GizmoGadget10 CameraPhoto20 GizmoPhoto30 GizmoGadget40 Product(name, category, price)

28 Other Keys 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)) There is at most one PRIMARY KEY; there can be many UNIQUE

29 Foreign Key Constraints CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) CREATE TABLE Purchase ( prodName CHAR(30) REFERENCES Product(name), date DATETIME) prodName is a foreign key to Product(name) name must be a key in Product Referential integrity constraints May write just Product (why ?)

30 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase

31 Foreign Key Constraints OR (name, category) must be a PRIMARY KEY 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)

32 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase What happens during updates ? Types of updates: In Purchase: insert/update In Product: delete/update

33 What happens during updates ? SQL has three policies for maintaining referential integrity: Reject violating modifications (default) Cascade: after a delete/update do a delete/update Set-null set foreign-key field to NULL READING ASSIGNMENT: 7.1.5, 7.1.6

34 Constraints on Attributes and Tuples Constraints on attributes: NOT NULL-- obvious meaning... CHECK condition-- any condition ! Constraints on tuples CHECK condition

35 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) What is the difference from Foreign-Key ?

36 General Assertions 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)