Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language.

Slides:



Advertisements
Similar presentations
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
Advertisements

Virtual training week 4 structured query language (SQL)
SQL Review Sections 1 - SQL and other basic statements.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 8: Correlated Subqueries.
Using Relational Databases and SQL
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Definition Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Aggregates.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 5: Subqueries and Set Operations.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 7:
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 8: Subqueries.
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 What is database 2? What is normalization? What is SQL? What is transaction?
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 6: Midterm Review.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
CS 122: Lecture 3 Joins (Part 1) Tarik Booker CS 122 California State University, Los Angeles October 7, 2014.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language Data Definition.
Lecture 7: Subqueries Tarik Booker California State University, Los Angeles.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Joins (Part II) Tarik Booker California State University, Los Angeles.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 4. Subqueries and joins.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
CS122 Using Relational Databases and SQL
Using Relational Databases and SQL
SQL – CRUD.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Introduction to Oracle9i: SQL
CS122 Using Relational Databases and SQL
Structured Query Language (SQL) William Klingelsmith
“Manipulating Data” Lecture 6.
مقدمة في قواعد البيانات
CS122 Using Relational Databases and SQL
“Manipulating Data” Lecture 6.
CS122 Using Relational Databases and SQL
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Presentation transcript:

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language

Miscellany School is almost over Last 3 lectures are pretty relaxing DML (How to update and modify a database) DDL (How to create a database)‏ Database Design (How to design a database)‏

Topics for Today Inserting Records (Pages 164 – 167)‏ Deleting Records (Pages 167 – 168)‏ Updating Records (Pages 168 – 173)‏ Transactions (Pages 173 – 179)‏

Before We Start When inserting and deleting data, you are going to mess up because nobody is perfect If you mess up there are two ways to restore the original database: Remove and restore the tables Use transactions (use BEGIN and ROLLBACK)‏ I prefer using BEGIN and ROLLBACK

Inserting Records Two syntaxes: INSERT INTO Insert one record at a time INSERT SELECT Insert one or more records at a time

Inserting Records INSERT INTO Syntax -- Form #1: Insert whole record. INSERT INTO tablename VALUES(value1, value2,..., valuen); -- Form #2: Insert partial record. INSERT INTO tablename(field1, field2,..., fieldn) VALUES(value1, value2,..., valuen);

Inserting Records INSERT SELECT Syntax -- Form #1: Insert whole record. INSERT INTO destination_table SELECT field1, field2,..., fieldn FROM source_tables WHERE conditions; -- Form #2: Insert partial record. INSERT INTO destination_table(df1, df2,..., dfn) SELECT sf1, sf2,..., sfn FROM source_tables WHERE conditions;

INSERT Examples Examples: Josh Scott Wagner was just hired by Lyric Music as a salesperson working under supervisor Bob Bentley. Base salaries for new recruits are set at $ Add Josh to the database. INSERT INTO SalesPeople VALUES(5, 'Josh', 'Wagner', 'jsw', 50.00, 1); Note that we have to use 1 instead of using a subquery. MySQL doesn't allow INSERT INTO and a subquery selection from the same table. SELECT SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley';

INSERT Examples Way around it is to use INSERT SELECT INSERT INTO + subquery from same table = NO! INSERT INTO SalesPeople VALUES(5, 'Josh', 'Wagner', 'jsw', 50.00, (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley')); INSERT SELECT from same table = OK! INSERT INTO SalesPeople SELECT 5, 'Josh', 'Wagner', 'jsw', 50.00, SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley';

INSERT Examples Example: Bobby Crum, a member of 21 West Elm, has also decided to join The Neurotics. Update the table XrefArtistsMembers to reflect this. Bobby won't be the responsible member for the artist he his joining. INSERT INTO XrefArtistsMembers VALUES((SELECT ArtistID FROM Artists WHERE ArtistName = 'The Neurotics'), (SELECT MemberID FROM Members WHERE FirstName = 'Bobby' AND LastName = 'Crum'), 0);

INSERT Examples Example: -- All members of 21 West Elm have decided to also become members of The Neurotics (as non- responsible members). Update the table XrefArtistsMembers to reflect this. INSERT INTO XrefArtistsMembers SELECT MemberID, (SELECT ArtistID FROM Artists WHERE ArtistName = 'The Neurotics'), 0 FROM Members JOIN XrefArtistsMembers USING(MemberID) JOIN Artists USING(ArtistID) WHERE ArtistName = '21 West Elm';

Deleting Records Deletes one or more rows from a table Deletes all rows without WHERE condition Single-Table DELETE Syntax DELETE FROM tablename WHERE conditions; Multi-Table DELETE Syntax Not in book Complicated and ugly! On next slide

Deleting Records Multi-table DELETE Syntax #1 (Preferred)‏ DELETE T1, T2,..., Tn FROM T1 JOIN T2 JOIN... JOIN Tn WHERE conditions; Note: If you use table alias in the FROM clause, you must use the alias in the DELETE clause as well (see examples later on). Multi-table DELETE Syntax #2 (Ugly)‏ DELETE FROM T1, T2,..., Tn USING T1 JOIN T2 JOIN... JOIN Tn WHERE conditions;

DELETE Examples Examples: -- Delete all rows from the Artists table. DELETE FROM Artists; -- Delete all titles by The Neurotics (subquery). DELETE FROM Titles WHERE ArtistID = (SELECT ArtistID FROM Artists WHERE ArtistName = 'The Neurotics'); -- Delete all titles by The Neurotics (multi-table). DELETE T FROM Titles T JOIN Artists A WHERE A.ArtistID = T.ArtistID AND A.ArtistName = 'The Neurotics';

DELETE Examples Examples: -- Delete all titles and tracks by the The Neurotics. DELETE T, K FROM Artists A JOIN Titles T JOIN Tracks K WHERE A.ArtistID = T.ArtistID AND T.TitleID = K.TitleID AND A.ArtistName = 'The Neurotics'; -- Delete The Neurotics from the Artists table and all titles and tracks by the them as well. DELETE A, T, K FROM Artists A JOIN Titles T JOIN Tracks K WHERE A.ArtistID = T.ArtistID AND T.TitleID = K.TitleID AND A.ArtistName = 'The Neurotics';

Updating Records To update existing records: -- Single-table syntax. UPDATE tablename SET field1 = value1, field2 = value2,... WHERE conditions; -- Multi-table equi-join syntax. UPDATE tablename1, tablename2,... SET field1 = value1, field2 = value2,... WHERE conditions; -- Multi-table subquery syntax. UPDATE tablename SET field1 = subquery1, field2 = subquery2,... WHERE conditions;

Updating Examples Example: -- The Bullets have decided to change their name to The Rockets. Update the database to reflect this change. UPDATE Artists SET ArtistName = 'The Rockets' WHERE ArtistName = 'The Bullets';

Updating Examples Example: -- The Bullets have decided to change their name to The Rockets and update their web domain to Update the database to reflect these changes. UPDATE Artists SET ArtistName = 'The Rockets', WebAddress = ' WHERE ArtistName = 'The Bullets';

Updating Examples Example: -- All members that had Bob Bentley as a sales contact will now have Scott Bull as a sales contact. Update the database to reflect these changes. Use the subquery syntax. UPDATE Members SET SalesID = (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Scott' AND LastName = 'Bull') WHERE SalesID = (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Bob' AND LastName = 'Bentley');

Updating Examples Example: -- All members that had Bob Bentley as a sales contact will now have Scott Bull as a sales contact. Update the database to reflect these changes. Use the join syntax. UPDATE Members M, SalesPeople S SET M.SalesID = (SELECT SalesID FROM SalesPeople WHERE FirstName = 'Scott' AND LastName = 'Bull') WHERE M.SalesID = S.SalesID AND S.FirstName = 'Bob' AND S.LastName = 'Bentley';

Transactions ACID Atomicity Consistency Isolation Durability SQL Keywords BEGIN/START TRANSACTION COMMIT ROLLBACK