1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.

Slides:



Advertisements
Similar presentations
Union, Intersection, Difference (subquery) UNION (subquery) produces the union of the two relations. Similarly for INTERSECT, EXCEPT = intersection and.
Advertisements

1 More SQL Database Modification Defining a Database Schema Views.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
Chapter 7 Notes on Foreign Keys Local and Global Constraints Triggers.
1 Database Systems Relations as Bags Grouping and Aggregation Database Modification.
1 Introduction to SQL Multirelation Queries Subqueries Slides are reused by the approval of Jeffrey Ullman’s.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
1 Constraints Foreign Keys Local and Global Constraints Triggers Source: slides by Jeffrey Ullman.
Fall 2001Arthur Keller – CS 1808–1 Schedule Today Oct. 18 (TH) Schemas, Views. u Read Sections u Project Part 3 extended to Oct. 23 (T). Oct.
1 More SQL Defining a Database Schema Views. 2 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the.
Winter 2002Arthur Keller – CS 1808–1 Schedule Today: Jan. 29 (T) u Modifications, Schemas, Views. u Read Sections Assignment 3 due. Jan. 31 (TH)
1 Constraints Foreign Keys Local and Global Constraints Triggers.
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
1 Constraints Foreign Keys Local and Global Constraints Triggers.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
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.
CPSC-608 Database Systems Fall 2008 Instructor: Jianer Chen Office: HRBB 309B Phone: Notes #3.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
1 More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #2.
DB Modifications Modification = insert + delete + update. Insertion of a Tuple INSERT INTO relation VALUES (list of values). Inserts the tuple = list of.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
1 Transactions, Views, Indexes Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data This slides are from J. Ullman’s.
SQL 2014, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
CSCE 520- Relational Data Model Lecture 2. Relational Data Model The following slides are reused by the permission of the author, J. Ullman, from the.
1 IT 244 Database Management System Lecture 11 SQL Select-From-Where Statements Meaning of queries Subqueries Ref : -A First Course in Database System.
Winter 2006Keller Ullman Cushing8–1 Turning in Assignments Please turn in hard copy (use only in the direst of circumstances). I am not your secretary.
1 CE223 Database Systems Introduction DBMS Overview, Relational Model, Schemas, SQL Semistructured Model, XML.
1 Lecture 1 Introduction Based on
Constraints on Relations Foreign Keys Local and Global Constraints Triggers Following lecture slides are modified from Jeff Ullman’s slides
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
1 CS1368 Introduction* Relational Model, Schemas, SQL Semistructured Model, XML * The slides in this lecture are adapted from slides used in Standford's.
RELATIONAL DATA MODEL 1. 2 What is a Data Model? 1.Mathematical representation of data. wExamples: relational model = tables; semistructured model = trees/graphs.
SQL 2015, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web.
1 Introduction Relational Model, Schemas, SQL Semistructured Model, XML The slides were made by Jeffrey D. Ullman for the Introduction to Databases course.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
1 Database Systems Defining Database Schema Views.
1 Chapter 6 Constraints uForeign Keys uConstraints.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
CSCE 520- Relational Data Model Lecture 2. Oracle login Login from the linux lab or ssh to one of the linux servers using your cse username and password.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Introduction to SQL. 2 Why SQL? SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
1 Constraints, Views, Indexes Foreign Keys Constraints Triggers Virtual and Materialized Views Speeding Accesses to Data.
1 Introduction to Database Systems, CS420 SQL Constraints.
1 Data Modification with SQL CREATE TABLE, INSERT, DELETE, UPDATE Slides from
1 Introduction to Database Systems, CS420 SQL JOIN, Aggregate, Grouping, HAVING and DML Clauses.
1. 2 Design of databases. E/R model (entity-relationship model) Relational model (tables) UML (unified modeling language) Database programming. SQL, Relational.
CPSC-310 Database Systems
Slides are reused by the approval of Jeffrey Ullman’s
Outerjoins, Grouping/Aggregation Insert/Delete/Update
Foreign Keys Local and Global Constraints Triggers
Databases : More about SQL
CPSC-310 Database Systems
CPSC-608 Database Systems
CPSC-608 Database Systems
Database Models Relational Model
2018, Fall Pusan National University Ki-Joune Li
IT 244 Database Management System
Defining a Database Schema
CPSC-608 Database Systems
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Presentation transcript:

1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman

2 Database Modifications uA modification command does not return a result (as a query does), but changes the database in some way. uThree kinds of modifications: 1.Insert a tuple or tuples. 2.Delete a tuple or tuples. 3.Update the value(s) of an existing tuple or tuples.

3 Insertion uTo insert a single tuple: INSERT INTO VALUES ( ); uExample: add to Likes(consumer, candy) the fact that Sally likes Twizzlers. INSERT INTO Likes VALUES(’Sally’, ’Twizzler’);

4 Specifying Attributes in INSERT uWe may add to the relation name a list of attributes. uTwo reasons to do so: 1.We forget the standard order of attributes for the relation. 2.We don’t have values for all attributes, and we want the system to fill in missing components with NULL or a default value.

5 Example: Specifying Attributes uAnother way to add the fact that Sally likes Twizzlers to Likes(consumer, candy): INSERT INTO Likes(candy, consumer) VALUES(’Twizzler’, ’Sally’);

6 Inserting Many Tuples uWe may insert the entire result of a query into a relation, using the form: INSERT INTO ( );

7 Example: Insert a Subquery uUsing Frequents(consumer, store), enter into the new relation CoShoppers(name) all of Sally’s “co- shoppers,” i.e., those consumers who frequent at least one store that Sally also frequents.

8 Solution INSERT INTO CoShoppers (SELECT c2.consumer FROM Frequents c1, Frequents c2 WHERE c1.consumer = ’Sally’ AND c2.consumer <> ’Sally’ AND c1.store = c2.store ); Pairs of Consumer tuples where the first is for Sally, the second is for someone else, and the stores are the same. The other consumer (shopper)

9 Deletion uTo delete tuples satisfying a condition from some relation: DELETE FROM WHERE ;

10 Example: Deletion uDelete from Likes(consumer, candy) the fact that Sally likes Twizzlers: DELETE FROM Likes WHERE consumer = ’Sally’ AND candy = ’Twizzler’;

11 Example: Delete all Tuples uMake the relation Likes empty: DELETE FROM Likes; uNote no WHERE clause needed.

12 Example: Delete Many Tuples uDelete from Candies(name, manf) all candies for which there is another candy by the same manufacturer. DELETE FROM Candies c WHERE EXISTS ( SELECT name FROM Candies WHERE manf = c.manf AND name <> c.name); Candies with the same manufacturer and a different name from the name of the candy represented by tuple c.

13 Semantics of Deletion --- (1) uSuppose Hershey makes only Twizzlers and Kitkats. uSuppose we come to the tuple c for Twizzler first. uThe subquery is nonempty, because of the Kitkat tuple, so we delete Twizzler. uNow, when c is the tuple for Kitkat, do we delete that tuple too?

14 Semantics of Deletion --- (2) uAnswer: we do delete Kitkat as well. uThe reason is that deletion proceeds in two stages: wMark all tuples for which the WHERE condition is satisfied. wDelete the marked tuples.

15 Updates uTo change certain attributes in certain tuples of a relation: UPDATE SET WHERE ;

16 Example: Update uChange consumer Fred’s phone number to : UPDATE Consumers SET phone = ’ ’ WHERE name = ’Fred’;

17 Example: Update Several Tuples uMake $4 the maximum price for candy: UPDATE Sells SET price = 4.00 WHERE price > 4.00;

18 Defining a Database Schema uA database schema comprises declarations for the relations (“tables”) of the database. uSeveral other kinds of elements also may appear in the database schema, including views, indexes, and triggers, which we’ll introduce later.

19 Creating (Declaring) a Relation uSimplest form is: CREATE TABLE ( ); uTo delete a relation: DROP TABLE ;

20 Elements of Table Declarations uMost basic element: an attribute and its type. uThe most common types are: wINT or INTEGER (synonyms). wREAL or FLOAT (synonyms). wCHAR(n ) = fixed-length string of n characters. wVARCHAR(n ) = variable-length string of up to n characters.

21 Example: Create Table CREATE TABLE Sells ( storeCHAR(20), candyVARCHAR(20), priceREAL );

22 Dates and Times uDATE and TIME are types in SQL. uThe form of a date value is: DATE ’yyyy-mm-dd’  Example: DATE ’ ’ for Sept. 30, 2004.

23 Times as Values uThe form of a time value is: TIME ’hh:mm:ss’ with an optional decimal point and fractions of a second following.  Example: TIME ’15:30:02.5’ = two and a half seconds after 3:30PM.

24 Declaring Keys uAn attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. uEither says the attribute(s) so declared functionally determine all the attributes of the relation schema. uThere are a few distinctions to be mentioned later.

25 Declaring Single-Attribute Keys uPlace PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. uExample: CREATE TABLE Candies ( nameCHAR(20) UNIQUE, manfCHAR(20) );

26 Declaring Multiattribute Keys uA key declaration can also be another element in the list of elements of a CREATE TABLE statement. uThis form is essential if the key consists of more than one attribute. wMay be used even for one-attribute keys.

27 Example: Multiattribute Key uThe store and candy together are the key for Sells: CREATE TABLE Sells ( storeCHAR(20), candyVARCHAR(20), priceREAL, PRIMARY KEY (store, candy) );

28 PRIMARY KEY Versus UNIQUE uThe SQL standard allows DBMS implementers to make their own distinctions between PRIMARY KEY and UNIQUE. wExample: some DBMS might automatically create an index (data structure to speed search) in response to PRIMARY KEY, but not UNIQUE.

29 Required Distinctions uHowever, standard SQL requires these distinctions: 1.There can be only one PRIMARY KEY for a relation, but several UNIQUE attributes. 2.No attribute of a PRIMARY KEY can ever be NULL in any tuple. But attributes declared UNIQUE may have NULL’s, and there may be several tuples with NULL.

30 Some Other Declarations for Attributes uNOT NULL means that the value for this attribute may never be NULL. uDEFAULT says that if there is no specific value known for this attribute’s component in some tuple, use the stated.

31 Example: Default Values CREATE TABLE Consumers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ’123 Sesame St.’, phone CHAR(16) );

32 Effect of Defaults --- (1) uSuppose we insert the fact that Sally is a consumer, but we know neither her address nor her phone. uAn INSERT with a partial list of attributes makes the insertion possible: INSERT INTO Consumers(name) VALUES(’Sally’);

33 Effect of Defaults --- (2) uBut what tuple appears in Consumers? nameaddr phone Sally123 Sesame StNULL uIf we had declared phone NOT NULL, this insertion would have been rejected.

34 Adding Attributes uWe may add a new attribute (“column”) to a relation schema by: ALTER TABLE ADD ; uExample: ALTER TABLE Stores ADD phone CHAR(16)DEFAULT ’unlisted’;

35 Deleting Attributes uRemove an attribute from a relation schema by: ALTER TABLE DROP ; uExample: we don’t really need the license attribute for stores: ALTER TABLE Stores DROP license;

36 Views uA view is a “virtual table” = a relation defined in terms of the contents of other tables and views. uDeclare by: CREATE VIEW AS ; uAntonym: a relation whose value is really stored in the database is called a base table.

37 Example: View Definition uCanEat(consumer, candy) is a view “containing” the consumer-candy pairs such that the consumer frequents at least one store that sells the candy: CREATE VIEW CanEat AS SELECT consumer, candy FROM Frequents, Sells WHERE Frequents.store = Sells.store;

38 Example: Accessing a View uQuery a view as if it were a base table. wAlso: a limited ability to modify views if it makes sense as a modification of one underlying base table. uExample query: SELECT candy FROM CanEat WHERE consumer = ’Sally’;

39 What Happens When a View Is Used? uThe DBMS starts by interpreting the query as if the view were a base table. wTypical DBMS turns the query into something like relational algebra. uThe definitions of any views used by the query are also replaced by their algebraic equivalents, and “spliced into” the expression tree for the query.

40 Example: View Expansion PROJ candy SELECT consumer=‘Sally’ CanEat PROJ consumer, candy JOIN FrequentsSells

41 DMBS Optimization uIt is interesting to observe that the typical DBMS will then “optimize” the query by transforming the algebraic expression to one that can be executed faster. uKey optimizations: wPush selections down the tree. wEliminate unnecessary projections.

42 Example: Optimization PROJ candy JOIN SELECT consumer=’Sally’ Sells Frequents Notice how most tuples are eliminated from Frequents before the expensive join.