Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.

Slides:



Advertisements
Similar presentations
Practice for SQL and Constraints (Product-PC-Laptop-Printer)
Advertisements

Oracle Labs ECS 242, 342, 360 –You can connect from home to the machines in the lab. –E.g.: ssh u-knoppix.csc.uvic.ca Execute “sh” to use the proper shell.
SQL This presentation will cover: A Brief History of DBMS View in database MySQL installation.
Database Modifications CIS 4301 Lecture Notes Lecture /30/2006.
SQL reviews. Stored Procedures Create Procedure Triggers.
Relational Algebra.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
Subqueries Example Find the name of the producer of ‘Star Wars’.
Indexes. An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a fixed value for attribute.
1 More SQL Database Modification Defining a Database Schema Views Source: slides by Jeffrey Ullman.
CMSC424: Database Design Instructor: Amol Deshpande
Representing Data Elements Gayatri Gopalakrishnan.
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)
SQL. 1.SQL is a high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
Operations in the Relational Model These operation can be expressed in an algebra, called “relational algebra”. In this algebra relations are the operands.
SQL SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in.
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.
Joins Natural join is obtained by: R NATURAL JOIN S; Example SELECT * FROM MovieStar NATURAL JOIN MovieExec; Theta join is obtained by: R JOIN S ON Example.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
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.
Correlated Queries SELECT title FROM Movie AS Old WHERE year < ANY (SELECT year FROM Movie WHERE title = Old.title); Movie (title, year, director, length)
Midterm Review/Practice for SQL and Constraints. Exercise – Suppliers and Parts Suppliers(sid,sname,address) Parts(pid,pname,color) Catalog(sid, pid,price)
SQL By: Toan Nguyen. Download Download the software at During the installation –Skip sign up for fast installation.
1 Relational Data Model CS 157B Nidhi Patel. 2 What is a Data Model? A notation for describing data or information A notation for describing data or information.
Oracle Data Definition Language (DDL)
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
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.
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.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Introduction to Indexes. Indexes An index on an attribute A of a relation is a data structure that makes it efficient to find those tuples that have a.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
ICS 321 Fall 2011 Constraints, Triggers, Views & Indexes Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Index Example From Garcia-Molina, Ullman, and Widom: Database Systems, the Complete Book pp
1 Database Systems Defining Database Schema Views.
1 More SQL uDatabase Modification uDefining a Database Schema uViews.
Referential Integrity checks, Triggers and Assertions Examples from Chapter 7 of Database Systems: the Complete Book Garcia-Molina, Ullman, & Widom.
Advanced SQL Concepts - Checking of Constraints CIS 4301 Lecture Notes Lecture /6/2006.
© D. Wong Normalization  Purpose: process to eliminate redundancy in relations due to functional or multi-valued dependencies.  Decompose relation.
1 CSCE Database Systems Anxiao (Andrew) Jiang The Database Language SQL.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
SQL Exercises – Part I April
Databases : SQL-Schema Definition and View 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey.
The Relational Model of Data Prof. Yin-Fu Huang CSIE, NYUST Chapter 2.
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 18 A First Course in Database Systems.
The Database Language SQL Prof. Yin-Fu Huang CSIE, NYUST Chapter 6.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 SQL: Concept and Usage. 2 SQL: an Overview SQL (Structured Query Language) –Also be pronounced as “sequel” –A relational database language –Consists.
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.
CPSC-310 Database Systems
CS 480: Database Systems Lecture 13 February 13,2013.
Introduction to Structured Query Language (SQL)
SQL: Concept and Usage.
SQL OVERVIEW DEFINING A SCHEMA
Defining a Database Schema
SQL This presentation will cover: View in database MySQL installation
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL – Constraints & Triggers
CMSC-461 Database Management Systems
Chapter 8 Views and Indexes
SQL (Structured Query Language)
Presentation transcript:

Database Modifications, Data Types, Views

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 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.

Insertion To insert a single tuple: INSERT INTO VALUES ( ); Example Consider MovieExec(name, address, cert#, netWorth) INSERT INTO MovieExec VALUES('Melanie Griffith', '34 Boston Blvd', 700, );

Specifying Attributes in INSERT We may add to the relation name a list of attributes. INSERT INTO MovieExec(name, address, cert, netWorth) VALUES('Melanie Griffith', NULL, 700, ); There are two reasons to do so: 1.We forget the standard order of attributes for the relation. 2.We don’t have values for all attributes.

Inserting Many Tuples We may insert the entire result of a query into a relation, using the form: INSERT INTO ( ); Example CREATE TABLE DisneyMovies( name VARCHAR2(25), year INT ); INSERT INTO DisneyMovies (SELECT title, year FROM Movie WHERE studioName = 'Disney' );

Deletion To delete tuples satisfying a condition from some relation: DELETE FROM WHERE ; Example Delete from the Movie table the Disney’s movies: DELETE FROM Movie WHERE studioName ='Disney';

Example: Delete all Tuples Make the relation Movie empty: DELETE FROM Movie; No WHERE clause needed here.

Updates To change certain attributes in certain tuples of a relation: UPDATE SET WHERE ; Example Change the length of 'Godzilla' to 200. UPDATE Movie SET length = 200 WHERE title = 'Godzilla';

Another Example Suppose that Brown’s movies have approximately 20 min of info before starting. So, let’s take that 20 min off. UPDATE Movie SET length = length - 20 WHERE (title, year) IN (SELECT title, year FROM Movie, Movieexec WHERE Movie.producerc = Movieexec.cert AND name = 'Brown');

Exercise Product(maker, model, type) PC(model, speed, ram, hd, rd, price) Laptop(model, speed, ram, hd, screen, price) Printer(model, color, type, price) a)Using two INSERT statements, store in the database the fact that PC model 1100 is made by manufacturer C, has speed 1800, RAM 256, hard disk 80, a 20x DVD, and sells for $2499. b)Insert the facts that for every PC there is a laptop with the same manufacturer, speed, RAM and hard disk, a 15-inch screen, a model number 1000 greater, and a price $500 more. c)Delete all PC’s with less than 20 GB of hard disk. d)Delete all laptops made a manufacturer that doesn’t make printers. e)Manufacturer A buys manufacturer B. Change all products made by B so they are now made by A. f)For each PC, double the amount of RAM and add 20 GB to the amount of hard disk. g)For each laptop made by manufacturer B, add one inch to the screen size and subtract $100 from the price.

Data Types The principal element in a table creation is a pair consisting of an attribute and a type. The most common types are: –INT or INTEGER (synonyms). –REAL –FLOAT –CHAR(n ) = fixed-length string of n characters. –VARCHAR(n ) = variable-length string of up to n characters. –DATE

Example: Create Table CREATE TABLE Movie( title CHAR(20), year INT, length INT, inColor CHAR(1), studioName CHAR(20), producerC INT, PRIMARY KEY (title, year) );

Oracle NUMBER NUMBER(p,s), where: –p is the precision, which in ORACLE is the total number of digits. –s is the scale, which in ORACLE is the number of digits to the right of the decimal point. –If the scale is negative, the actual data is rounded to the specified number of places to the left of the decimal point. Examples Actual Data Specified as Stored as NUMBER NUMBER(9) NUMBER(9,2) NUMBER(9,1) NUMBER(6) exceeds precision NUMBER(15,1) NUMBER(7,-2) NUMBER(7,2) exceeds precision CREATE TABLE A1( attribNUMBER(3,2) ); INSERT INTO A1 VALUES(100); What happens? NUMBER(p) is equivalent to NUMBER(p,0). INT is a synonym for NUMBER(38), i.e. NUMBER(38,0) A NUMBER(5) is not any different from a NUMBER(38) space-wise. –The 5 is just an "edit", a format, an integrity constraint. It doesn’t affect the physical storage at all. In absence of precision and scale, the default is the maximum range and precision for an Oracle number.

Dates and Times DATE and TIME are types in SQL. No TIME type in ORACLE, but DATE also keeps the time. CREATE TABLE Movie( title CHAR(20), year INT, length INT, inColor CHAR(1), studioName CHAR(20), producerC INT, my_date DATE DEFAULT SYSDATE, PRIMARY KEY (title, year) );

Getting a Date in/out INSERT INTO Movie(title, year, length, inColor, studioName, producerC, my_date) VALUES('Godzilla', 1998, , 'C', 'Paramount', 123, '12-Feb-1998'); INSERT INTO Movie(title, year, length, inColor, studioName, producerC, my_date) VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, ' '); VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, ' ') * ORA-01843: not a valid month INSERT INTO Movie(title, year, length, inColor, studioName, producerC, my_date) VALUES('Pretty Woman', 1990, 120, 'C', 'Disney', 234, TO_DATE(' ', 'dd-mm-yy'));

Getting a Date in/out (II) Getting the date and time out: SELECT TO_CHAR(my_date, 'DD-MON-YYYY:HH:MI:SS') FROM Movie; For more info:

Adding/Deleting/Modifying Attributes ALTER TABLE MovieExec ADD salary INT; ALTER TABLE MovieExec ADD phone CHAR(16) DEFAULT 'unlisted'; ALTER TABLE MovieExec DROP COLUMN phone; ALTER TABLE MovieExec MODIFY phone CHAR(18); Also in ORACLE: ALTER TABLE starsIN RENAME COLUMN title TO movieTitle;

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 CREATE VIEW DisneyMovie AS SELECT title, year, producerC FROM Movie WHERE studioName = 'Disney';

Accessing a View Query a view as if it were a base table. Examples SELECT title FROM DisneyMovie WHERE year = 1973; SELECT DISTINCT name FROM DisneyMovie, MovieExec WHERE producerC = cert;

View on more than one relation; renaming the attributes CREATE VIEW MovieProd(movieTitle, movieYear, prodName) AS SELECT title, year, name FROM Movie, MovieExec WHERE producerc = cert; Same as: CREATE VIEW MovieProd2 AS SELECT title AS movieTitle, year AS movieYear, name AS prodName FROM Movie, MovieExec WHERE producerc = cert;

Updateable Views Only when: 1.There is only one relation, say R, in the FROM clause (of the query defining the view). 2.There isn’t a subquery involving R in the WHERE clause (of the query defining the view). Not a problem for ORACLE. 3.The list in the SELECT clause includes enough attributes that for every tuple inserted into the view, we can fill the other attributes out with NULL or the default, and have a tuple that will yield the inserted tuple in the view. This is only checked for views defined WITH CHECK OPTION.

CREATE VIEW ParamountMovie AS SELECT title, year FROM Movie WHERE studioName = 'Paramount' WITH CHECK OPTION; INSERT INTO ParamountMovie VALUES ('Star Trek', 1979); This insertion will fail! Why this insertion is not possible? The rationale for this behavior is: The above insertion, were it allowed to get through, would insert a tuple with NULL for studioName in the underlying Movie table. However, such a tuple doesn't satisfy the condition for being in the ParamountMovie view! Thus, it shouldn't be allowed to get into the database through the ParamountMovie view.

CREATE VIEW ParamountMovie2 AS SELECT studioName, title, year FROM Movie WHERE studioName = 'Paramount' WITH CHECK OPTION; INSERT INTO ParamountMovie2 VALUES ('Paramount', 'Star Trek', 1979); Now it succeeds. Why?

Deleting DELETE FROM ParamountMovies WHERE year=2008; is translated into DELETE FROM Movie WHERE year=2008 AND studioName='Paramount';

Updating UPDATE ParamountMovies SET year = 1979 WHERE title= 'Star Trek the Movie'; is equivalent to the base-table update UPDATE Movies SET year = 1979 WHERE title = 'Star Trek the Movie' AND studioName = 'Paramount';

Materialized Views CREATE MATERIALIZED VIEW MovieProd AS SELECT title, year, name FROM Movie, MovieExec WHERE producerC =cert; Useful to speed up queries In principle, the DBMS needs to recompute a materialized view every time one of its base tables changes in any way. However, there are a number opportunities to do only incremental changes.

Example Suppose we insert a new movie into Movie, say –title = 'Kill Bill', year = 2003, and producerC = Then we only need to look up –cert=23456 in MovieExec. –Since cert is the key for MovieExec, there can be at most one name returned by the query SELECT name FROM MovieExec WHERE cert = 23456; As this query returns name = 'Quentin Tarantino', the DBMS can insert the proper tuple into MovieProd by: INSERT INTO MovieProd VALUES('Kill Bill', 2003, 'Quentin Tarantino');

Example Suppose we delete a movie from Movies, say the movie with –title ='Dumb&Dumber' and year = The DBMS has only to delete this one movie from MovieProd by: DELETE FROM MovieProd WHERE title = 'Dumb & Dumber' AND year = 1994;

Example Suppose we insert a tuple into MovieExec, and that tuple has –cert=34567 and name = 'MaxBialystock'. Then the DBMS may have to insert into MovieProd some movies that were not there because their producer was previously unknown. The operation is: INSERT INTO MovieProd SELECT title, year, 'Max Bialystock' FROM Movie WHERE producerC = 34567;

Example Suppose we delete the tuple with cert=45678 from MovieExec. Then the DBMS must delete from MovieProd all movies that have producerC= Thus, the DBMS executes: DELETE FROM MovieProd WHERE (title, year) IN (SELECT title, year FROM Movie WHERE producerC =45678);

Rewriting Queries to Use Materialized Views A materialized view can be referred to in the FROM clause of a query, just as a virtual view can. However, because a materialized view is stored in the database, it is good to try rewriting a query to use a materialized view, even if that view was not mentioned in the query as written. Such a rewriting may enable the query to execute much faster.

Example CREATE MATERIALIZED VIEW MovieProd AS SELECT title, year, name FROM Movie, MovieExec WHERE producerC =cert; Consider query Q that asks for the names of the stars of movies produced by Max Bialystock. Movie(title, year, length, genre, studioName, producerC) StarsIn(movieTitle, movie Year, starName) MovieExec(name, address, cert, net Worth) SELECT starName FROM StarsIn, Movies, MovieExec WHERE movieTitle = title AND movie Year = year AND producerC=cert AND name = 'Max Bialystock';

Example View-Based Rewriting: SELECTstarName FROM StarsIn,MovieProd WHERE movieTitle =title AND movieYear =year AND name= 'Max Bialystock';