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.

Slides:



Advertisements
Similar presentations
SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
Advertisements

Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
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.
Database Modifications, Data Types, Views. Database Modifications A modification command does not return a result as a query does, but it changes the.
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.
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)
Winter 2002Judy Cushing8–1 Schedule Jan. 30 (Wed) u Modifications, Schemas, Views. Read Sections This Week (Feb 4ff) u Constraints Read Sections.
Movies length titleyearfilmType Voices isa Cartoons isa MurderMystery weapon toStar Our Movie Example.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
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.
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.
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)
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.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Introduction to SQL  SQL or sequel  It is a standardised language with thousands of pages in the standard  It can be in database system through GUI,
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.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Databases 1 Fourth lecture. Rest of SQL Defining a Database Schema Views Foreign Keys Local and Global Constraints Triggers 2.
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.
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.
Advanced Database CS-426 Week 1 - Introduction. Database Management System DBMS contains information about a particular enterprise Collection of interrelated.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
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.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
1 Chapter 6 More SQL uDatabase Modification uDefining a Database Schema uViews.
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.
Aggregation SELECT Sum(price) FROM Product WHERE manufacturer=“Toyota” SQL supports several aggregation operations: SUM, MIN, MAX, AVG, COUNT Except COUNT,
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Databases and SQL The relational model and the most common SQL commands.
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)
2018, Fall Pusan National University Ki-Joune Li
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:
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL – Constraints & Triggers
CMSC-461 Database Management Systems
SQL (Structured Query Language)
Presentation transcript:

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: 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: Using the Movie relation, enter into a new relation DisneyMovies(name, year) all of Disney’s movies: INSERT INTO DisneyMovies (SELECT title, year FROM Movie WHERE studioName = 'Disney' ); Before we have to create DisneyMovies table: CREATE TABLE DisneyMovies(name VARCHAR2(25), year INT);

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

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

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

Updates (example) Suppose that Brown’s movies have approximately 20 min of info before starting. So, a modification would be to change the movie lengths by taking 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');

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

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 –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 I The NUMBER datatype stores zero, positive, and negative fixed and floating-point numbers with magnitudes between 1.0 x and x (38 nines followed by 88 zeroes) with 38 digits of precision. Specify a fixed-point number using the following form: NUMBER(p,s) where: –p is the precision, or the total number of digits. –s is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127.

Oracle NUMBER II Examples Actual DataSpecified 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 If the scale is negative, the actual data is rounded to the specified number of places to the left of the decimal point. –For example, a specification of (10,-2) means to round to hundreds.

Oracle NUMBER III We can specify an integer using the following form: NUMBER(p) –This represents a fixed-point number with precision p and scale 0 and is equivalent to NUMBER(p,0). INT is a synonym for NUMBER(38), i.e. NUMBER(38,0) Specify a floating-point number using the following form: NUMBER The absence of precision and scale designators specifies the maximum range and precision for an Oracle number.

Oracle NUMBER IV CREATE TABLE A1( attribNUMBER(3,2) ); INSERT INTO A1 VALUES(100); What happens? CREATE TABLE A2( attrib1NUMBER, attrib2INT ); DESC A2; INSERT INTO A2 VALUES (100.34, 200);

Oracle NUMBER V A number(5) is not any different from a number(38) if the number(38) only holds data the number(5) can. The 5 is just an "edit", a format, an integrity constraint. Nothing more, Nothing less. It doesn’t affect the physical storage at all.

Dates and Times DATE and TIME are types in SQL. No TIME type in ORACLE, but DATE also keeps the time. The form of a date value is: 'yyyy-mm-dd' –Example: DATE ' ' for Sept. 30, 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 out: SELECT TO_CHAR(my_date, 'DD-MON-YYYY:HH:MI:SS') FROM movie; For more info:

PRIMARY KEY Versus UNIQUE 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.

Example CREATE TABLE T5 ( A1NUMBER(5) PRIMARY KEY, A2 NUMBER(5) UNIQUE ); SQL> desc t5 Name Null? Type A1 NOT NULL NUMBER(5) A2 NUMBER(5)

Other Declarations for Attributes Two other declarations we can make for an attribute are: 1.NOT NULL means that the value for this attribute may never be NULL. 2.DEFAULT says that if there is no specific value known for this attribute’s component in some tuple, use the stated.

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

Deleting Attributes Remove an attribute from a relation schema by: ALTER TABLE DROP COLUMN ; ALTER TABLE MovieExec DROP COLUMN phone;

Modifying Attributes Remove an attribute from a relation schema by: ALTER TABLE MODIFY ; ALTER TABLE MovieExec MODIFY phone CHAR(18);

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

Example: Accessing a View You may 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, prodName) AS SELECT title, name FROM Movie, MovieExec WHERE producerc = cert; Same as: CREATE VIEW MovieProd2 AS SELECT title AS movieTitle, name AS prodName FROM Movie, MovieExec WHERE producerc = cert;

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

Updateable Views Only when: 1.There is only one relation in the WHERE clause of the view. 2.There isn’t a subquery 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 of the view.

INSERT INTO DisneyMovie VALUES ('Star Trek', 1979); Why this insertion is not possible? This insertion will fail. 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 DisneyMovie view! Thus, it shouldn't be allowed to get into the database through the DisneyMovie view.

CREATE VIEW DisneyMovie2 AS SELECT studioName, title, year FROM Movie WHERE studioName = 'Disney'; DELETE FROM DisneyMovie2 WHERE title = 'Star Trek 2'; INSERT INTO DisneyMovie2 VALUES ('Disney', 'Star Trek 2', 2005); Both succeed.

Example: View Expansion PROJ title SELECT year=1973 DisneyMovie PROJ title, year SELECT Movie