380 likes | 400 Views
Section 8 - Manipulating Data. The INSERT statement adds rows of data to the database The UPDATE statement changes columns of existing data The DELETE statement deletes rows of data from the database. Add, Change, or Remove Data. Data Modification Statements INSERT - adds rows to a table
E N D
Section 8 - Manipulating Data • The INSERT statement adds rows of data to the database • The UPDATE statement changes columns of existing data • The DELETE statement deletes rows of data from the database
Add, Change, or Remove Data • Data Modification Statements • INSERT - adds rows to a table • UPDATE - changes information in a table • DELETE - deletes rows from a table • 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
INSERT Statement • Adds Rows to a Table • There are two ways to use an insert statement... • the VALUES keyword • the Subquery [SELECT statement]
INSERT Syntax • INSERT INTO table_name [ (column_1, column_2, etc.) ]VALUES (constant_1, constant_2, etc.) • In embedded SQL, you may use variables instead of constants
INSERT Example 1 • If you are adding values to every column defined for a table you don't have to specify the column names • INSERT INTO publishersVALUES ( '1662', 'Jardin, Inc.', '55th Avenue', 'Camden', 'NJ');
INSERT Example 2 • 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'); • Each column must match a value in the list • The columns do not have to be listed in CREATE TABLE statement order
INSERT Example 3 • 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'; • This adds all the editors with the last name of Smith to the authors table
Compatible Structures • 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)
Exercise • Write a statement to add a row into the Titleauthors table
Discussion • 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 • INSERT INTO titleauthors('BU1234', '149-48-9082', 1, 1.00);
Exercise • Write a statement to add a row into the Titles table
Discussion • 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);
Exercise • Write a statement to make the author Albert Ringer an editor as well
Discussion • Here we must use a Subquery • First write the Select statementSELECT au_id, au_lname, au_fnameFROM authorsWHERE au_lname = 'Ringer'AND au_fname = 'Albert';
Discussion - cont. • Now place the correct INSERT statement above the SELECT INSERT INTO editors(ed_id, ed_lname, ed_fname)SELECT au_id, au_lname, au_fnameFROM authorsWHERE au_lname = 'Ringer'AND au_fname = 'Albert';
UPDATE Statement • Changes existing data for... • A Single row • A Group of rows • All rows in a table • Updates can come from constants, variables(in Embedded SQL) or other tables
UPDATE Syntax • UPDATE table_name SET column_name1 = expression1, column_name2 = expression2...[ WHERE search_conditions ] • Expressions must be of the same datatype as the column you're updating (or convert to the same datatype)
UPDATE Examples • Change the price on all the books in the titles table to $17.99UPDATE titles SET price = 17.99;
Update Example 2 • Double the price and add $1,000 to the advance for all books that have sold more than 10,000 copiesUPDATE title SET price = price * 2, advance = advance + 1000 WHERE ytd_sales > 10000;
Exercise • Reduce the price of all books by 20% if they have sold under a thousand books this year.
Discussion • UPDATE titlesSET price = price * .80WHERE 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.
Exercise • 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.
Discussion • 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.
Discussion • We know we want to update the editors tableUPDATE editorsSET city = 'Seattle', state = 'WA'...but only for New Age Book editors...
Discussion • Look at the chart on Page 43 of your book • First we must find out which books the editors are editing by looking at the titleditors table • Then we need to find out which publisher is is publishing that book by looking at the titles table • The we need to find out which of these publishers are named 'New Age Books'
Discussion • UPDATE editorsSET 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");
Discussion • 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
Removing Data • Use the DELETE statement to remove data from a table • SYNTAX: • DELETE FROM table_name[WHERE search_conditions];
Examples • DELETE FROM titlesWHERE price = 2.99; • This deletes all rows from the titles tablewhere the price of the book is $2.99 • DELETE FROM titles; • This deletes all rows from the titles table, but does not delete the titles table itself
Example: Delete with Select • DELETE from authorsWHERE 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
Exercise • Delete all editors that live in Seattle, WA.
Discussion • DELETE FROM editorsWHERE city = 'Seattle'AND state = 'WA';
Exercise • 'New Age Books' has gone out of business. Remove all relevant information from the database
Discussion • What's wrong with this?DELETE FROM publishersWHERE pub_name = 'New Age Books';
Discussion • 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? • If we remove the publisher first we will lose the information we need to reference the other data that needs to be deleted
Discussion • 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. • Do we remove the editors that were working on books for New Age Books? • Do we remove the authors who wrote books published by New Age Books? What if they've written for another publisher? • Do we still want to keep track of sales history information?
Constraints • Many DBMS's offer an extention to SQL called CONSTRAINTS • 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.
Last Slide Section 8 • Please complete Assignment 7