1 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006.

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.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
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.
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),
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.
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.
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,
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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)
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.
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 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 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
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.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Phase 2, Answering queries using views. February 2 nd, 2004.
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.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
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 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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
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.
Cours 7: Advanced SQL.
SQL: Schema Definition and Constraints Chapter 6 week 6
Introduction to Database Systems CSE 444 Lecture 04: SQL
Cse 344 May 14th – Entities.
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.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
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 Lecture 06 Data Modeling: E/R Diagrams Wednesday, January 18, 2006

2 Outline Data Definition Language (6.6) Views (6.7) Constraints (Chapter 7) We begin E/R diagrams (Chapter 2)

3 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 definition: defining the schema. Create tables Delete tables Modify table schema Indexes: to improve performance

4 Creating Tables CREATE TABLE Person( name VARCHAR(30), social-security-number INT, age SHORTINT, city VARCHAR(30), gender BIT(1), Birthdate DATE ); CREATE TABLE Person( name VARCHAR(30), social-security-number INT, age SHORTINT, city VARCHAR(30), gender BIT(1), Birthdate DATE );

5 Deleting or Modifying a Table Deleting: ALTER TABLE Person ADD phone CHAR(16); ALTER TABLE Person DROP age; ALTER TABLE Person ADD phone CHAR(16); ALTER TABLE Person DROP age; Altering: (adding or removing an attribute). What happens when you make changes to the schema? Example: DROP Person; Example: Exercise with care !!

6 Default Values Specifying default values: CREATE TABLE Person( name VARCHAR(30), social-security-number INT, age SHORTINT DEFAULT 100, city VARCHAR(30) DEFAULT ‘Seattle’, gender CHAR(1) DEFAULT ‘?’, Birthdate DATE CREATE TABLE Person( name VARCHAR(30), social-security-number INT, age SHORTINT DEFAULT 100, city VARCHAR(30) DEFAULT ‘Seattle’, gender CHAR(1) DEFAULT ‘?’, Birthdate DATE The default of defaults: NULL

7 Indexes REALLY important to speed up query processing time. Suppose we have a relation Person (name, age, city) Sequential scan of the file Person may take long SELECT * FROM Person WHERE name = “Smith” SELECT * FROM Person WHERE name = “Smith”

8 Create an index on name: Indexes AdamBettyCharles….Smith…. B+ trees have fan-out of 100s: max 4 levels ! Will discuss in the second half of this course

9 Creating Indexes CREATE INDEX nameIndex ON Person(name) Syntax:

10 Creating Indexes Indexes can be useful in range queries too: B+ trees help in: Why not create indexes on everything? CREATE INDEX ageIndex ON Person (age) SELECT * FROM Person WHERE age > 25 AND age < 28

11 Creating Indexes Indexes can be created on more than one attribute: SELECT * FROM Person WHERE age = 55 AND city = “Seattle” Helps in: SELECT * FROM Person WHERE city = “Seattle” But not in: CREATE INDEX doubleindex ON Person (age, city) Example: SELECT * FROM Person WHERE age = 55 and even in:

12 The Index Selection Problem Why not build an index on every attribute ? On every pair of attributes ? Etc. ? The index selection problem is hard: balance the query cost v.s. the update cost, in a large application workload

13 Defining 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”

14 Person(name, city) Purchase(buyer, seller, product, store) Product(name, maker, price, category) CREATE VIEW Seattle-Purchase AS SELECT y.buyer, y.seller, y.product, y.store FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.name = y.buyer CREATE VIEW Seattle-Purchase AS SELECT y.buyer, y.seller, y.product, y.store FROM Person x, Purchase y WHERE x.city = ‘Seattle’ AND x.name = y.buyer Example Seattle-Purchase(buyer, seller, product, store)“virtual table”

15 SELECT v.name, u.store FROM Seattle-Purchase u, Product v WHERE u.product = v.name AND v.category = ‘shoes’ SELECT v.name, u.store FROM Seattle-Purchase u, Product v WHERE u.product = v.name AND v.category = ‘shoes’ We can later use the view:

16 What Happens When We Query a View ? SELECT v.name, y.store FROM Person x, Purchase y, Product v WHERE x.city = ‘Seattle’ AND x.name = y.buyer AND y.product = v.name AND v.category = ‘shoes’ SELECT v.name, y.store FROM Person x, Purchase y, Product v WHERE x.city = ‘Seattle’ AND x.name = y.buyer AND y.product = v.name AND v.category = ‘shoes’ SELECT v.name, u.store FROM Seattle-Purchase u, Product v WHERE u.product = v.name AND v.category = ‘shoes’ SELECT v.name, u.store FROM Seattle-Purchase u, Product v WHERE u.product = v.name AND v.category = ‘shoes’

17 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

18 Purchase(buyer, seller, product, store) Product(name, maker, price, category) CREATE VIEW Expensive-Product AS SELECT name, maker FROM Product WHERE price > 100 CREATE VIEW Expensive-Product AS SELECT name, maker FROM Product WHERE price > 100 Updating Views: Part 1 INSERT INTO Expensive-Product VALUES(‘Gizmo’, ‘Gadgets INC.’) INSERT INTO Product VALUES(‘Gizmo’, ‘Gadgets INC.’, NULL, NULL)

19 CREATE VIEW Toy-Product AS SELECT name, maker FROM Product WHERE category = ‘Toys’ CREATE VIEW Toy-Product AS SELECT name, maker FROM Product WHERE category = ‘Toys’ Updating Views: Part 2 INSERT INTO Toy-Product VALUES(‘Gizmo’, ‘Gadgets INC.’) INSERT INTO Product VALUES(‘Gizmo’, ‘Gadgets INC.’, NULL, NULL) Note this Purchase(buyer, seller, product, store) Product(name, maker, price, category)

20 CREATE VIEW Buyer-Maker AS SELECT x.buyer, y.maker FROM Purchase x, Product y WHERE x.product = y.name CREATE VIEW Buyer-Maker AS SELECT x.buyer, y.maker FROM Purchase x, Product y WHERE x.product = y.name Updating Views: Part 3 INSERT INTO Buyer-Maker VALUES(‘John Smith’, ‘Gadgets INC.’) ? ? ? ? ? Non-updateable view Purchase(buyer, seller, product, store) Product(name, maker, price, category) Most views are non-updateable

21 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

22 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

23 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)

24 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)

25 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

26 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 ?)

27 NameCategory Gizmogadget CameraPhoto OneClickPhoto ProdNameStore GizmoWiz CameraRitz CameraWiz ProductPurchase

28 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)

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

30 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 ASSIGNEMNT: 7.1.5, 7.1.6

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

32 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 ?

33 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)

34 Final Comments on Constraints Can give them names, and alter later –Read in the book !!! We need to understand exactly when they are checked We need to understand exactly what actions are taken if they fail