500 likes | 562 Views
More SQL Statements To Modify Tables. Books ISBN bkTitle bkPrice pubID. Authors auID auName auPone. Books/Authors ISBN auID. Publishers pubID pubName pubPhone. Library Database. INNER JOIN Simplified in SELECT Query.
E N D
BooksISBN bkTitle bkPrice pubID AuthorsauID auName auPone Books/AuthorsISBNauID PublisherspubID pubName pubPhone Library Database
INNER JOIN Simplified in SELECT Query SELECT bkTitle, pubNameFROM Books INNER JOIN PublishersON Books.pubID = Publishers.pubIDWHERE Books.bkPrice <= 50; is equivalent to SELECT bkTitle, pubNameFROM Books, PublishersWHERE Books.pubID = Publishers.pubIDAND Books.bkPrice <= 50;
INNER JOIN Simplified in SELECT Query (2) SELECT bkTitle, auNameFROM Books INNER JOIN BooksAuthors INNER JOIN Authors ON BooksAuthors.auID = Authors.auIDON Books.ISBN = BooksAuthors.ISBN WHERE Books.bkPrice <= 50; is equivalent to SELECT bkTitle, auNameFROM Books, BooksAuthors, AuthorsWHERE Books.ISBN = BooksAuthors.ISBNAND (BooksAuthors.auID = Authors.auIDAND (Books.bkPrice <= 50) );
UPDATE More Than One Column • Change the price of “Iliad” to $50 and pubID to 3.UPDATE Books SET bkPrice = 50, pubID = 3WHERE title= ‘Iliad’; • What will result from the following statement?UPDATE Books SET bkPrice = 50;
UPDATE Statement with Subquery • Raise the price of all books from publisher “Big House” by 10 % • Required Tables: • Books (ISBN, bkTitle, bkPrice, pubID) • Publishers (pubID, pubName, pubPhone)
UPDATE Statement with Subquery • Raise the price of all books from publisher “Big House” by 10 % • UPDATE BooksSET bkPrice = bkPrice * 1.1WHERE pubID in SubqueryNote: subquery • SELECT pubIDFROM PublishersWHERE pubName = “Big House”
Raise the price of all books from publisher “Big House” by 10 % • UPDATE BooksSET bkPrice = bkPrice * 1.1WHERE Books.pubID in (SELECT Publishers.pubID FROM Publishers WEHRE pubName = “Big House”)
Alternately…UPDATE Books INNER JOIN Publishers ON Books.pubID = Publishers.pubIDSET Books.bkPrice = Books.bkPrice * 1.1WHERE Publishers.pubName=”Big House";
UPDATE with Data from Another Table • Update price column in Books table, with new prices from table NewPrices (ISBN, price). BooksISBN bkTitle bkPrice pubID NewPricesISBN price
UPDATE with Data from Another Table • Update price column in Books table, with new prices from table NewPrices (ISBN, price).UPDATE Books INNER JOIN NewPricesON Books.ISBN = NewPrices.ISBNSET Books.bkPrice = NewPrices.priceWHERE Books.bkPrice <> NewPrices.price
Previewing Update • To check which records will be modified by the UPDATE statement… SELECT Books.* FROM Books INNER JOIN NewPrices ON Books.ISBN = NewPrices.ISBNWHERE BOOKS.bkPrice<>NewPrices.price;
Change author “Shakespeare’s” telephone number to “123-4568”. Reduce the price of all books from publisher “Small House” by 10%. Raise the price of all books by author “Shakespeare” by 10%. Your Turn
Change author Shakespear's phone number to "123-4568“. UPDATE Authors SET auPhone = "123-4568“WHERE auName = "Shakespeare";
Reduce the price of all books published by "Small House" by 10%. UPDATE Books SET Books.bkPrice = Books.bkPrice * 0.90WHERE Books.pubID IN (SELECT Publishers.pubID FROM Publishers WHERE Publishers.pubName="Small House");
Difference Between “IN” and “=“ • WHERE Books.pubID = (single value) • But,SELECT Publishers.pubIDFROM PublishersWHERE Publishers.pubName = “XXX”OR Publishers.pubName = “YYY”may return multiple values. • WHERE Books.pubID IN (No1, No2)same as Books.pubID = No1 OR Books.pubID = No2
Raise by 10% the price of all books authored by Shakespeare. BooksISBNbkPrice BooksAuthosISBNauID AuthorsauIDauName UPDATE BooksSET bkPrice = bkPrice * 1.10WHERE ISBN IN SELECT BooksAuthors.ISBNFROM BooksAuthorsWHERE BooksAuthors.auID = SELECT auIDFROM AuthorsWHERE auName = “Shakespeare”
Raise by 10% the price of all books authored by "Shakespeare”. UPDATE Books SET Books.bkPrice = Books.bkPrice * 1.10WHERE Books.ISBN IN (SELECT BooksAuthors.ISBN FROM BooksAuthors WHERE BooksAuthors.auID = (SELECT Authors.auID FROM Authors WHERE Authors.auName = "Shakespeare“ ) );
Raise the price by 5% of all books by Author Shakespeare. UPDATE BooksSET bkPrice = bkPrice * 1.05FROM Books INNER JOIN BooksAuthors INNER JOIN Authors ON BooksAuthors.auID = Authors.auIDON Books.ISBN = BooksAuthors.ISBNWHERE Authors.auName = “Shakespeare”;
Adding New RecordsINSERT INTO Statement • INSERT INTO TableNameVALUES (“value-1”, “value-2”, “value-3”) • INSERT INTO TableName (field-2, field-3)VALUES (“value-2”, “value-3”)
Adding New RecordsINSERT INTO Statement • INSERT INTO BooksVALUES (“1-1111-1111-1”, “SQL Is Fun”, 1, 25.00) • INSERT INTO Books (ISBN, Title)VALUES (“2-2222-2222-2”, “Born to Code”)
Your Turn • Add the following record to Books • ISBN: 1-2345-6789-0 • title: SQL for Dummies • pubID: 3 • price: $35.50 • Add the following record to Authors • auID: 14 • auName: Brando
Inserting from Another Table • Create a new table called NewAuthors with same fields as Authors. • Add to NewAuthors a field called new (boolean) • Add 3 new authors in NewAuthors.
Inserting from Another Table • To copy data from NewAuthors to AuthorsINSERT into Authors (auName, auPhone) SELECT NewAuthors,auName, NewAuthors.auPhone FROM NewAuthors WHERE NewAuthors.new = true;
Removing RecordsDELETE Statement • Syntax DELETE FROM TableName WHERE Criteria • Deletes whole records (rows) • Criteria chooses the rows to be deleted • Can delete whole table data, but not structure
Delete Statement (cont) • Delete the book whose ISBN is 0-12-345678-9 • DELETE FROM BooksWHERE ISBN=“0-12-345678-9”
Delete Statement (cont) • Delete all books published by “Small House” • DELETE FROM Books WHERE Books.pubID = (Subquery to return pubID for “Small House” from Publishers table)
Delete Statement (cont) • Delete all books published by “Small House” • DELETE FROM Books WHERE Books.pubID = (SELECT Publishers.pubID FROM Publishers WHERE Publishers.pubName="Small House“ );
Your Turn • Delete a book titled “Born in Maui” • Delete all books priced $50 or above • Delete all books published by “Small House” • Delete all books written by Shakespeare
Delete all books priced $50 or above. DELETE FROM BooksWHERE bkPrice >= 50.00;
Delete all books published by “Small House” DELETEFROM BooksWHERE Books.pubID IN (SELECT Publishers.pubID FROM Publishres WHERE Publishers.pubName=“Small House” );
Delete all books written by Shakespeare DELETEFROM BooksWHERE Books.ISBN IN (SELECT BooksAuthors.ISBN FROM BooksAuthors WHERE BooksAuthors.auID = (SELECT Authors.auID FROM Authors WHERE Authors.auName = "Shakespeare“ ) );
Used-Car Dealership DB(available from Resources/320/ on the server)
Your Turn (dealership DB) • SELECT • List all cars that were made in 2000 or later. • Who was the salesperson that took care of Customer “John Wayne”? • Who was the salesperson that sold “Ford” “Edsel” in carlot at “Windward Cars”? • Update • Reduce price of all Honda cars by 30%. • Reduce the price of all non-Honda cars by 50%. • Mark as sold all cars which are located at “Windward Cars”
Your Turn • Delete • Remove from Salespeople table an employee named “Linda” “Gingle”. • Remove from Cars table all cars which are located at “Windward Cars”. • Insert • Add a customer whose name is “Zeke” “Zorro” with a phone number of “808-123-4567”. • Add to Cars table the cars from the Newcars table.
Solutions • SELECT • List all cars that were made in 2000 or later.SELECT *FROM carsWHERE year >= 2000; • Who was the salesperson that took care of Customer “John Wayne”?SELECT salespeople.firstName, salespeople.lastNameFROM salespeople, transactions, customersWHERE salespeople.salesID = transactions.salesIDAND transactions.custID = customers.custIDAND customers.nameFirst = “John”AND customers.nameLast = “Wayne”;
Solutions • SELECT • Who was the salesperson that sold “Ford” “Edsel” in carlot at “Windward Cars”?SELECT salespeople.firstName, salespeople.lastNameFROM salespeople, transactions, carsWHERE salespeople.salesID = transactions.salesIDAND transactions.carID = cars.carIDAND cars.make = “Ford”AND cars.model = “Edsel”;
Solutions • Update • Reduce price of all Honda cars by 30%.UPDATE cars SET carListPrice = carListPrice * 0.7 WHERE carMake = 'Honda'; • Reduce the price of all non-Honda cars by 50%.UPDATE Cars SET carListPrice = carListPrice * 0.5 WHERE carMake <> 'Honda';
Solutions • Update • Mark as sold all cars which are located at “Windward Cars”UPDATE cars SET carSold = true WHERE cars.lotID IN (SELECT carlots.lotID FROM cars, carlots WHERE cars.lotID = carLots.lotID AND carlots.lotName = 'Windward Cars');
Solutions • Delete • Remove from Salespeople table an employee named “Elmo” “Jones”.DELETE FROM salePeople WHERE salNameFirst = 'Elmo' AND salNameLast = 'Jones'; • Remove from Cars table all cars which are located at “Lemons Are Us”.DELETEFROM CarsWHERE Cars.lotID = (SELECT Carlots.lotID FROM Carlots WHERE Carlots.lotName = 'Lemons Are Us');
Solutions • Insert • Add a customer whose name is “Zeke” “Zorro” with a phone number of “808-123-4567”.INSERT INTO Customers (nameFirst, nameLast, phoneNumber)VALUES (“Zeke”, “Zorro”, “808-123-457”); Add to Cars table the cars from the Newcars table.INSERT INTO Cars (carMake, carModel, carYear, carDoors, carListPrice,lotID) (SELECT make, model, year, doors, listPrice, lotID FROM newcars );
SQL Statements • SELECT field1, field2FROM TableWHERE someCondtion; • SELECT Table1.field1, Table2.field1FROM Table1, Table2WHERE Table1.prKey = forKeyAND someCondtion;
SQL Statements • UPDATE TableSET field1 = value1, field2 = value2WHERE someCondition • UPDATE TableSET field1 = value1WHERE (subquery)
SQL Statements • INSERT INTO Table(field1, field2)VALUES (value1, value2);
SQL Statements • DELETE FROM TableWHERE someCondition;
Functions • Aggregate Functions—perform on set of data • Value Functions—perform on individual values
Aggregate Functions • COUNT – returns count of rows • SELECT COUNT(auName)FROM Authors • AVG – returns average value of column • SELECT AVG(bkPrice)FROM BOOKS • MAX • MIN • SUM
VALUE FUNCTIONS • String Value Functions • Numeric Value Functions • Datetime Value Functions
String Value Functions • SUBSTRING(sourceString FROM start [FOR length])sourceString=“Ala Moana”SUBSTRING(sourceString FROM 5 FOR 3) -- “Moa” • UPPER(sourceString) -- “ALA MOANA”
Numeric Value Function • POSITION (subString IN sourceString)sourceString = “Ala Moana”POSITION (“Moana” in sourceString) -- 5 • CHARACTER_LENGTH(sourceString)CHARACTER_LENGTH(“Ala Moana”) -- 9