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
Virtual training week 4 structured query language (SQL)
Advertisements

Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 7: Subqueries and Set Operations.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Introduction to Structured Query Language (SQL)
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
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 8: Correlated Subqueries.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 6: Set Functions.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 3: Joins Part I.
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 9: Data Manipulation Language.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 4: Joins Part II.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
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.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
MySQL Database Connection
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
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.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Using Relational Databases and SQL Department of Computer Science California State University, Los Angeles Lecture 9: Data Manipulation Language Data Definition.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
Using Relational Databases and SQL
Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Introduction to Oracle9i: SQL
Web Programming Week 3 Old Dominion University
CS1222 Using Relational Databases and SQL
Chapter 8 Working with Databases and MySQL
“Manipulating Data” Lecture 6.
مقدمة في قواعد البيانات
“Manipulating Data” Lecture 6.
HAVING,INDEX,COMMIT & ROLLBACK
Contents Preface I Introduction Lesson Objectives I-2
Web Programming Week 3 Old Dominion University
CS1222 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
Lesson Objectives Aims You should know about: 1.3.2: (a) indexing (d) SQL – Interpret and Modify (e) Referential integrity (f) Transaction processing,
CS1222 Using Relational Databases and SQL
Updating Databases With Open SQL
Web Programming Week 3 Old Dominion University
Updating Databases With Open SQL
CS1222 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

DML for short Contains commands for modifying table data Insertion commands (INSERT INTO) Deletion commands (DELETE) Update commands (UPDATE) Not a query Queries extract data from the database Commands do not extract data from the database

Before We Start When modifying the database 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 Use BEGIN before entering DML commands Use ROLLBACK to undo all changes USE COMMIT to accept all changes

Transactions ACID Atomicity Consistency Isolation Durability SQL Keywords BEGIN/START TRANSACTION COMMIT 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. Non-specified fieldnames are assigned default values. INSERT INTO tablename(field1, field2,..., fieldn) VALUES(value1, value2,..., valuen); IGNORE You can use the IGNORE keyword between INSERT and INTO to suppress duplicate error messages.

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. Non-specified fieldnames are assigned default values. INSERT INTO destination_table(df1, df2,..., dfn) SELECT sf1, sf2,..., sfn FROM source_tables WHERE conditions;

INSERT INTO Example Example: Add Kung Fu Panda into the database. INSERT INTO Movies VALUES(7, 'Kung Fu Panda', ' ', 'G', 92, 'USA', 'English', (SELECT CompanyID FROM Companies WHERE Name = 'Dreamworks Pictures'));

INSERT INTO and Subqueries As in the previous example, subqueries in the INSERT command work, but only if the update table and the subquery table are different. This rule only applies to MySQL, other database management systems may behave differently.

INSERT SELECT Example Example: Associate all movies that have ‘The X Files’ anywhere in the title with the romance genre. INSERT IGNORE INTO XRefGenresMovies SELECT MovieID, 'Romance' FROM Movies WHERE Title LIKE '%The X Files%';

Deleting Records Deletes one or more rows from a table Deletes all rows without WHERE condition Two syntaxes Single-Table DELETE Syntax Multi-Table DELETE Syntax

Single-Table DELETE Syntax Deletes one or more rows from a table Deletes all rows without WHERE condition Syntax: DELETE FROM tablename WHERE conditions;

Single-Table DELETE Syntax Examples: Delete all ratings. Delete all ratings by semory.

Single-Table DELETE Syntax Solutions: -- Delete all ratings. DELETE FROM Ratings; -- Delete all ratings by semory. DELETE FROM Ratings WHERE MemberID = (SELECT MemberID FROM Members WHERE Username = 'semory');

Multi-Table DELETE Syntax Deletes rows from multiple tables You must be very cautious or else you may delete something you didn’t want to delete Syntax: 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 Examples: -- Delete all ratings by semory (use multi-table delete syntax instead of using single-table delete syntax with a subquery). -- Delete all directors from the database (from both the People and XRefDirectorsMovies tables).

Multi-Table DELETE Syntax Examples: -- Delete all ratings by semory. DELETE Ratings FROM Members JOIN Ratings USING(MemberID) WHERE Username = 'semory'; -- Delete all ratings by semory (alternate). DELETE R FROM Members A JOIN Ratings R USING(MemberID) WHERE Username = 'semory'; -- Delete all directors from the database. DELETE P, D FROM People P JOIN XRefDirectorsMovies D ON P.PersonID = D.DirectorID;

Multi-Table DELETE Syntax There is a big problem in the last example. Multi-table delete can lead to orphaned records if misused (a foreign key with no primary key). DELETE P, D FROM People P JOIN XRefDirectorsMovies D ON P.PersonID = D.DirectorID; Jonathan Frakes is both an actor and a director. If we delete him from the People and XRefDirectorsMovies tables, we orphan him in the XRefActorsMovies and Spouses table!

Multi-Table DELETE Syntax Therefore, be careful what you delete! If you delete a record, and that record is referenced somewhere else, you have an orphaned record! Best solution is to only delete records from the XRefDirectorsMovies table and leave the People table alone since these people may be referenced somewhere else. DELETE FROM XRefDirectorsMovies;

Updating Records To update existing records, you may use one of the following syntaxes: Single-table syntax. Multi-table equi-join syntax. Only equi-join is supported. You may not use any other join syntax (JOIN ON, JOIN USING, etc.)

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

Updating Records You may use the IGNORE keyword immediately after UPDATE to ignore errors when an update produces duplicate primary keys. Example: -- ERROR! BEEP! UPDATE Movies SET MovieID = 1 WHERE MovieID = 2; -- Error will be ignored! UPDATE IGNORE Movies SET MovieID = 1 WHERE MovieID = 2;

Updating Records Examples: -- The user ojisan has decided to change his username to uncle_steve. Update the database to reflect this change. -- The user colderstone has decided to change his username and password to jackstone and slsev0812z3, respectively. Update the database to reflect this change. -- Dreamworks Pictures went bankrupt and was bought out by Paramount Pictures. Modify all CompanyIDs in the Movies table to reflect this change.

Updating Records Examples: -- The user ojisan has decided to change his username to uncle_steve. Update the database to reflect this change. UPDATE Members SET Username = 'uncle_steve' WHERE Username = 'ojisan';

Updating Records Examples: -- The user colderstone has decided to change his username and password to jackstone and slsev0812z3, respectively. Update the database to reflect this change. UPDATE Accounts SET Username = 'jackstone', Password = 'slsev0812z3' WHERE Username = 'colderstone';

Updating Records Examples: -- Dreamworks Pictures went bankrupt and was bought out by Paramount Pictures. Modify all CompanyIDs in the Movies table to reflect this change. UPDATE Movies SET CompanyID = (SELECT CompanyID FROM Companies WHERE Name = 'Paramount Pictures') WHERE CompanyID = (SELECT CompanyID FROM Companies WHERE Name = 'Dreamworks Pictures');