1 Section 8 - Manipulating Data u The INSERT statement adds rows of data to the database u The UPDATE statement changes columns of existing data u The.

Slides:



Advertisements
Similar presentations
February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
Advertisements

Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Introduction to Structured Query Language (SQL)
Beginning SQL Tutorial Author Jay Mussan-Levy. What is SQL?  Structured Query Language  Communicate with databases  Used to created and edit databases.
Introduction to Structured Query Language (SQL)
Using SQL to create tables Ways of using Databases.
5 Chapter 5 Structured Query Language (SQL1) Revision.
Introduction to Structured Query Language (SQL)
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL Query Extras MIS 433. Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered.
1 Section 5 - Grouping Data u The GROUP BY clause allows the grouping of data u Aggregate functions are most often used with the GROUP BY clause u GROUP.
Oracle Data Definition Language (DDL)
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
ASP.NET Programming with C# and SQL Server First Edition
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Introduction to Databases Chapter 7: Data Access and Manipulation.
SQL/lesson 2/Slide 1 of 45 Retrieving Result Sets Objectives In this lesson, you will learn to: * Use wildcards * Use the IS NULL and IS NOT NULL keywords.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
SQL/Lesson 4/Slide 1 of 45 Using Subqueries and Managing Databases Objectives In this lesson, you will learn to: *Use subqueries * Use subqueries with.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
RDBMSSection Relational DBMS DATABASE DEVELOPMENT And Franchise Colleges By MANSHA NAWAZ.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
INFORMATION TECHNOLOGY DATABASE MANAGEMENT. Adding a new field 1Right click the table name and select design view 2Type the field information at the end.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Subqueries Steve Perry 1.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Populating and Querying tables Insert, Update, Delete and View (DML)
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
ADVANCED SQL.  The SQL ORDER BY Keyword  The ORDER BY keyword is used to sort the result-set by one or more columns.  The ORDER BY keyword sorts the.
1 Section 10 - Embedded SQL u Many computer languages allow you to embed SQL statements within the code (e.g. COBOL, PowerBuilder, C++, PL/SQL, etc.) u.
Delete Data Database Administration Fundamentals LESSON 3.4.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Joining Tables Steve Perry
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
1 Section 9 - Views, etc. u Part 1: Views u Part 2:Security Issues u Part 3:Transaction Management u Part 4:Set Operations u Part 5:Triggers and Stored.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Fundamental of Database Systems
Chapter 5 Introduction to SQL.
Insert, Update and the rest…
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
SQL Creating and Managing Tables
Introduction to Oracle9i: SQL
SQL Creating and Managing Tables
SQL Tutorial.
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
SQL Creating and Managing Tables
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Oracle Data Definition Language (DDL)
Section 4 - Sorting/Functions
IST 318 Database Administration
Indexes and more Table Creation
Permissions to database objects Indexes RUNSQLSTM Referential Constraint options ON DELETE RESTRICT ON DELETE CASCADE ON DELETE SET DEFAULT Lab.
Presentation transcript:

1 Section 8 - Manipulating Data u The INSERT statement adds rows of data to the database u The UPDATE statement changes columns of existing data u The DELETE statement deletes rows of data from the database

2 Add, Change, or Remove Data u Data Modification Statements –INSERT - adds rows to a table –UPDATE - changes information in a table –DELETE - deletes rows from a table u You can modify only ONE table per SQL statement –May be based on data from more than one table –You can use a SQL subquery

3 INSERT Statement u Adds Rows to a Table u There are two ways to use an insert statement... –the VALUES keyword –the Subquery [SELECT statement]

4 INSERT Syntax u INSERT INTO table_name [ (column_1, column_2, etc.) ] VALUES (constant_1, constant_2, etc.) u In embedded SQL, you may use variables instead of constants

5 INSERT Example 1 u If you are adding values to every column defined for a table you don't have to specify the column names –INSERT INTO publishers VALUES ( '1662', 'Jardin, Inc.', '55th Avenue', 'Camden', 'NJ');

6 INSERT Example 2 u If you add a row using less than all of the columns defined for a table you must specify the column names –INSERT INTO publishers ( (pub_id, pub_name) VALUES ('1756', 'Healthtext'); u Each column must match a value in the list u The columns do not have to be listed in CREATE TABLE statement order

7 INSERT Example 3 u Using the SELECT as a subquery... –INSERT INTO authors (au_id, au_lname, au_fname) SELECT ed_id, ed_lname, ed_fname FROM editors WHERE ed_lname = 'SMITH'; u This adds all the editors with the last name of Smith to the authors table

8 Compatible Structures u When Inserting from one table to another... –The matching columns must be of the same datatype –Or they must convert to the correct datatype (e.g. a character datatype column may be loaded into a numeric datatype column if all the data in the character column is numeric)

9 Exercise u Write a statement to add a row into the Titleauthors table

10 Discussion u If we want to add all the columns to the titleauthors table we can skip listing the column names... as long as we follow the CREATE TABLE column order u INSERT INTO titleauthors ('BU1234', ' ', 1, 1.00);

11 Exercise u Write a statement to add a row into the Titles table

12 Discussion u If wanted to add a row to the titles table with the minimum required columns specified… Example: here's how we might add a new book by Bill Gates... INSERT INTO titles (title_id, title, contract) VALUES ('BU1234', 'The Geek Shall Inherit the Earth", 1);

13 Exercise u Write a statement to make the author Albert Ringer an editor as well

14 Discussion u Here we must use a Subquery u First write the Select statement SELECT au_id, au_lname, au_fname FROM authors WHERE au_lname = 'Ringer' AND au_fname = 'Albert';

15 Discussion - cont. u Now place the correct INSERT statement above the SELECT INSERT INTO editors (ed_id, ed_lname, ed_fname) SELECT au_id, au_lname, au_fname FROM authors WHERE au_lname = 'Ringer' AND au_fname = 'Albert';

16 UPDATE Statement u Changes existing data for... –A Single row –A Group of rows –All rows in a table u Updates can come from constants, variables(in Embedded SQL) or other tables

17 UPDATE Syntax u UPDATE table_name SET column_name1 = expression1, column_name2 = expression2... [ WHERE search_conditions ] u Expressions must be of the same datatype as the column you're updating (or convert to the same datatype)

18 UPDATE Examples u Change the price on all the books in the titles table to $17.99 UPDATE titles SET price = 17.99;

19 Update Example 2 u Double the price and add $1,000 to the advance for all books that have sold more than 10,000 copies UPDATE title SET price = price * 2, advance = advance WHERE ytd_sales > 10000;

20 Exercise u Reduce the price of all books by 20% if they have sold under a thousand books this year.

21 Discussion u UPDATE titles SET price = price *.80 WHERE ytd_sales < 1000; This sets the column price to 80% of itself (20% off) where the year-to-sales is less than 1000 books sold.

22 Exercise u The publisher "New Age Books" has been relocated to Seattle, Washington. All of the editors have moved to a Seattle commune at 123 Flower Street. Make the appropriate changes to the editors table.

23 Discussion u This seems like a simple request, change the city and state to Seattle, WA if the editor is working for New Age Books, but let's look at the SQL required to do this.

24 Discussion u We know we want to update the editors table UPDATE editors SET city = 'Seattle', state = 'WA'...but only for New Age Book editors...

25 Discussion u Look at the chart on Page 43 of your book u First we must find out which books the editors are editing by looking at the titleditors table u Then we need to find out which publisher is is publishing that book by looking at the titles table u The we need to find out which of these publishers are named 'New Age Books'

26 Discussion u UPDATE editors SET city = 'SEATTLE', state = 'WA' WHERE ed_id IN (SELECT ed_id FROM titleditors WHERE title_id IN (SELECT title_id FROM titles WHERE pub_id = (SELECT pub_id FROM publishers WHERE pub_name = "New Age Books");

27 Discussion u Read the statement from the bottom up... –The pub_id is found for 'New Age Books' and passed up to the next SELECT statement –The title_ids for that pub_id are found and passed up to the next SELECT statement –The ed_ids associated with those books are found and passed up to the WHERE clause of the update statement. –Now the Update statement changes the city and state for the appropriate list of editors

28 Removing Data u Use the DELETE statement to remove data from a table u SYNTAX: –DELETE FROM table_name [WHERE search_conditions];

29 Examples u DELETE FROM titles WHERE price = 2.99; –This deletes all rows from the titles table where the price of the book is $2.99 u DELETE FROM titles; –This deletes all rows from the titles table, but does not delete the titles table itself

30 Example: Delete with Select u DELETE from authors WHERE au_lname IN (SELECT ed_lname FROM editors WHERE ed_pos = 'Managing'); This deletes all authors who have the same last name as any Managing Editor

31 Exercise u Delete all editors that live in Seattle, WA.

32 Discussion u DELETE FROM editors WHERE city = 'Seattle' AND state = 'WA';

33 Exercise u 'New Age Books' has gone out of business. Remove all relevant information from the database

34 Discussion u What's wrong with this? DELETE FROM publishers WHERE pub_name = 'New Age Books';

35 Discussion u There is nothing wrong with the statement, this will remove the 'New Age Books' publisher from the database, but what about all the books they were selling? What about the authors? u If we remove the publisher first we will lose the information we need to reference the other data that needs to be deleted

36 Discussion u We can find out the books published by matching with pub_id on the titles table, but look at how many places the title_id is referenced: the titleauthors table, the titleditors table, and the salesdetail table. u Do we remove the editors that were working on books for New Age Books? u Do we remove the authors who wrote books published by New Age Books? What if they've written for another publisher? u Do we still want to keep track of sales history information?

37 Constraints u Many DBMS's offer an extention to SQL called CONSTRAINTS u These enforce rules which preserve the referential integrity in a database. For example, you can specify that a publisher cannot be deleted, unless all the books that reference it have been deleted first.

38 Last Slide Section 8 u Please complete Assignment 7