Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.

Slides:



Advertisements
Similar presentations
1 More SQL Database Modification Defining a Database Schema Views.
Advertisements

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.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
Constraints and Triggers Foreign Keys Local and Global Constraints Triggers.
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)
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #4.
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.
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.
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.
Databases : SQL-Introduction 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
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.
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.
Winter 2006Keller, Ullman, Cushing9–1 Constraints Commercial relational systems allow much more “fine-tuning” of constraints than do the modeling languages.
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.
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.
Lu Chaojun, SJTU Relational Data Model 1. Lu Chaojun, SJTU What’s a Data Model? A notation (collection of conceptual tools) for describing data as seen.
Databases : Relational Algebra - Complex Expression 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof.
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 Views, Indexes Virtual and Materialized Views Speeding Accesses to Data.
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.
More SQL (and Relational Algebra). More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update.
1 Introduction to Database Systems, CS420 SQL Views and Indexes.
1 Introduction to Database Systems, CS420 SQL Constraints.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Database Design and Programming Jan Baumbach Adopted from previous slides of Peter Schneider-Kamp.
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
Virtual and Materialized Views Speeding Accesses to Data
Slides are reused by the approval of Jeffrey Ullman’s
COP4710 Database Systems Relational Model.
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
CPSC-310 Database Systems
Database Models Relational Model
CPSC-310 Database Systems
2018, Fall Pusan National University Ki-Joune Li
SQL OVERVIEW DEFINING A SCHEMA
IT 244 Database Management System
2018, Fall Pusan National University Ki-Joune Li
Defining a Database Schema
Virtual and Materialized Views Speeding Accesses to Data
CS4433 Database Systems Relational Model.
More SQL Extended Relational Algebra Outerjoins, Grouping/Aggregation
SQL – Constraints & Triggers
CPSC-608 Database Systems
CPSC-608 Database Systems
CMSC-461 Database Management Systems
Presentation transcript:

Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes via his course web page (

STEMPNU 2 Defining a Database Schema A database schema comprises declarations for the relations ( “ tables ” ) of the database. Many other kinds of elements may also appear in the database schema, including views, indexes, and triggers.

STEMPNU 3 Declaring a Relation Simplest form is: CREATE TABLE ( ); And you may remove a relation from the database schema by: DROP TABLE ;

STEMPNU 4 Elements of Table Declarations The principal element is a pair consisting of an attribute and a type. The most common types are:  INT or INTEGER (synonyms).  REAL or FLOAT (synonyms).  CHAR(n ) = fixed-length string of n characters.  VARCHAR(n ) = variable-length string of up to n characters. Example CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL );

STEMPNU 5 Dates and Times DATE and TIME are types in SQL. The form of a date value is: DATE ‘ yyyy-mm-dd ’  Example: DATE ‘ ’ for Sept. 30, 2002.

STEMPNU 6 Times as Values The 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.

STEMPNU 7 Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE.  These each say the attribute(s) so declared functionally determine all the attributes of the relation schema.  There are a few distinctions to be mentioned later. Single Attribute Key  Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute.  Example: CREATE TABLE Beers ( nameCHAR(20) UNIQUE, manfCHAR(20) );

STEMPNU 8 Declaring Multiattribute Keys A key declaration can also be another element in the list of elements of a CREATE TABLE statement. This form is essential if the key consists of more than one attribute.  May be used even for one-attribute keys. Example  The bar and beer together are the key for Sells: CREATE TABLE Sells ( barCHAR(20), beerVARCHAR(20), priceREAL, PRIMARY KEY (bar, beer) );

STEMPNU 9 PRIMARY KEY Versus UNIQUE DBMS viewpoint: The SQL standard allows DBMS implementers to make their own distinctions between PRIMARY KEY and UNIQUE.  Example: some DBMS might automatically create an index (data structure to speed search) in response to PRIMARY KEY, but not UNIQUE. Required Distinctions  However, 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.

STEMPNU 10 Other Declarations for Attributes Two other declarations we can make for an attribute are:  NOT NULL means that the value for this attribute may never be NULL.  DEFAULT says that if there is no specific value known for this attribute ’ s component in some tuple, use the stated. Example CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT ‘123 Sesame St.’, phone CHAR(16) );

STEMPNU 11 Effect of Defaults Suppose we insert the fact that Sally is a drinker, but we know neither her address nor her phone.  An INSERT with a partial list of attributes makes the insertion possible: INSERT INTO Drinkers(name) VALUES(‘Sally’);  But what tuple appears in Drinkers?  If we had declared phone NOT NULL, this insertion would have been rejected. nameaddrphone ‘ Sally ’‘ 123 Sesame St ’ NULL

STEMPNU 12 Adding Attributes We may change a relation schema by adding a new attribute ( “ column ” ) by: ALTER TABLE ADD ; Example: ALTER TABLE Bars ADD phone CHAR(16)DEFAULT ‘unlisted’;

STEMPNU 13 Deleting Attributes Remove an attribute from a relation schema by: ALTER TABLE DROP ;  Example: we don ’ t really need the license attribute for bars: ALTER TABLE Bars DROP license;

STEMPNU 14 Views A view is a “ virtual table, ” a relation that is defined in terms of the contents of other tables and views.  Declare by: CREATE VIEW AS ;  In contrast, a relation whose value is really stored in the database is called a base table. Example  View CanDrink(Drinker, Beer) is created CREATE VIEW CanDrink AS SELECT drinker, beer FROM Frequents, Sells WHERE Frequents.bar = Sells.bar;

STEMPNU 15 Example: Accessing a View You may query a view as if it were a base table.  There is a limited ability to modify views if the modification makes sense as a modification of the underlying base table. Example: SELECT beer FROM CanDrink WHERE drinker = ‘Sally’; PROJ beer SELECT drinker=‘Sally’ CanDrink PROJ drinker, beer JOIN FrequentsSells

STEMPNU 16 DMBS Optimization It 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. Key optimizations: 1. Push selections down the tree. 2. Eliminate unnecessary projections. PROJ beer JOIN SELECT drinker=‘Sally’ Sells Frequents Notice how most tuples are eliminated from Frequents before the expensive join.