More SQL Statements To Modify Tables. Library Database Books ISBN bkTitle bkPrice pubID Publishers pubID pubName pubPhone Authors auID auName auPone Books/Authors.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

N.G.Acharya & D.K.Marathe college Chembur-E, Mumbai-71
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
Session 4 SQL Structured Query Language. SQL Modes of use –Interactive –Embedded Purpose –Create database –Create, Read, Update, Delete.
Using SQL to create tables Ways of using Databases.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
1 LBSC 690: Week 9 SQL, Web Forms. 2 Discussion Points Websites that are really databases Deep vs. Surface Web.
Creating a Database in Access Creating a database involves 1.Logical design of tables and relationships 2.Physical design of tables and relationships 3.Populating.
Microsoft Access 2010 Chapter 7 Using SQL.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Xin  Syntax ◦ SELECT field1 AS title1, field2 AS title2,... ◦ FROM table1, table2 ◦ WHERE conditions  Make a query that returns all records.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
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.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Using Special Operators (LIKE and IN)
Entity-Relationship Model. Entity-Relationship Mode What is it? What is it? –Technique for developing an informal organization of tables How does it work?
Introduction to SQL. SQL What is SQL SQL Components Syntax & Conventions SQL Data Types INNER JOIN SELECT Statements.
Information Technologies and Microsoft SQL Server Day 2 by Alper Özpınar
SQL by Example By convention SQL keywords are written in uppercase. SELECT * FROM Books –This query returns all rows in the Books table. –SQL statements.
Intro to SQL Management Studio. Please Be Sure!! Make sure that your access is read only. If it isn’t, you have the potential to change data within your.
Subqueries Steve Perry 1.
Comp12 cont…. Using Quotes Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values.
IST 220 – Intro to Databases Lecture 3 Database Design Guidelines.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.
Assoc. Prof. Dr. Ahmet Turan ÖZCERİT.  Basic SQL syntax  Data retrieve  Data query  Data conditions  Arithmetic operations on data  Data transactions.
Structured Query Language
Populating and Querying tables Insert, Update, Delete and View (DML)
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Lecture Access – Queries. What’s a Query? A question you ask a database –ie: “Who are my Stockton customers?” –ie: “How much did Bob sell on the 14th?”
IMS 4212: Constraints & Triggers 1 Dr. Lawrence West, Management Dept., University of Central Florida Stored Procedures in SQL Server.
April 2002 Information Systems Design John Ogden & John Wordsworth 1 Database Design SQL (1) John Wordsworth Department of Computer Science The University.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Lecture 9 Using Structured Query Language (SQL) Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant.
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.
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.
1 Section 3 - Select Statement u The Select statement allows you to... –Display Data –Specify Selection Criteria –Sort Data –Group Data for reporting –Use.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
SQL Introduction SQL stands for “Structured Query Language” and can be pronounced as “SQL” or “sequel – (Structured English.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
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.
SQL Query Getting to the data ……..
Database Access with SQL
CS3220 Web and Internet Programming More SQL
Database Mysql Hayk Avdalyan.
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
The Database Exercises Fall, 2009.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Structured Query Language
CIS16 Application Programming with Visual Basic
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL – Entire Select.
Aggregations Various Aggregation Functions GROUP BY HAVING.
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Contents Preface I Introduction Lesson Objectives I-2
Query Functions.
Section 4 - Sorting/Functions
Indexes and more Table Creation
Shelly Cashman: Microsoft Access 2016
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

More SQL Statements To Modify Tables

Library Database Books ISBN bkTitle bkPrice pubID Publishers pubID pubName pubPhone Authors auID auName auPone Books/Authors ISBN auID

INNER JOIN Simplified in SELECT Query SELECT bkTitle, pubName FROM Books INNER JOIN Publishers ON Books.pubID = Publishers.pubID WHERE Books.bkPrice <= 50; SELECT bkTitle, pubName FROM Books, Publishers WHERE Books.pubID = Publishers.pubID AND Books.bkPrice <= 50; is equivalent to

INNER JOIN Simplified in SELECT Query (2) SELECT bkTitle, auName FROM Books INNER JOIN BooksAuthors INNER JOIN Authors ON BooksAuthors.auID = Authors.auID ON Books.ISBN = BooksAuthors.ISBN WHERE Books.bkPrice <= 50; SELECT bkTitle, auName FROM Books, BooksAuthors, Authors WHERE Books.ISBN = BooksAuthors.ISBN AND (BooksAuthors.auID = Authors.auID AND (Books.bkPrice <= 50) ); is equivalent to

UPDATE More Than One Column Change the price of “Iliad” to $50 and pubID to 3. UPDATE Books SET bkPrice = 50, pubID = 3 WHERE 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 Books SET bkPrice = bkPrice * 1.1 WHERE pubID in Subquery Note: subquery SELECT pubID FROM Publishers WHERE pubName = “Big House”

Raise the price of all books from publisher “Big House” by 10 % UPDATE Books SET bkPrice = bkPrice * 1.1 WHERE Books.pubID in (SELECT Publishers.pubID FROM Publishers WEHRE pubName = “Big House”)

Alternately… UPDATE Books INNER JOIN Publishers ON Books.pubID = Publishers.pubID SET Books.bkPrice = Books.bkPrice * 1.1 WHERE Publishers.pubName=”Big House";

UPDATE with Data from Another Table Update price column in Books table, with new prices from table NewPrices (ISBN, price). NewPrices ISBN price Books ISBN bkTitle bkPrice pubID

UPDATE with Data from Another Table Update price column in Books table, with new prices from table NewPrices (ISBN, price). UPDATE Books INNER JOIN NewPrices ON Books.ISBN = NewPrices.ISBN SET Books.bkPrice = NewPrices.price WHERE 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.ISBN WHERE BOOKS.bkPrice<>NewPrices.price;

Your Turn  Change author “Shakespeare’s” telephone number to “ ”.  Reduce the price of all books from publisher “Small House” by 10%.  Raise the price of all books by author “Shakespeare” by 10%.

Change author Shakespear's phone number to " “. UPDATE Authors SET auPhone = " “ WHERE auName = "Shakespeare";

Reduce the price of all books published by "Small House" by 10%. UPDATE Books SET Books.bkPrice = Books.bkPrice * 0.90 WHERE 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.pubID FROM Publishers WHERE 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. Books ISBN bkPrice BooksAuthos ISBN auID Authors auID auName SELECT BooksAuthors.ISBN FROM BooksAuthors WHERE BooksAuthors.auID = UPDATE Books SET bkPrice = bkPrice * 1.10 WHERE ISBN IN SELECT auID FROM Authors WHERE auName = “Shakespeare”

Raise by 10% the price of all books authored by "Shakespeare”. UPDATE Books SET Books.bkPrice = Books.bkPrice * 1.10 WHERE 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 Books SET bkPrice = bkPrice * 1.05 FROM Books INNER JOIN BooksAuthors INNER JOIN Authors ON BooksAuthors.auID = Authors.auID ON Books.ISBN = BooksAuthors.ISBN WHERE Authors.auName = “Shakespeare”;

Adding New Records INSERT INTO Statement INSERT INTO TableName VALUES (“value-1”, “value-2”, “value-3”) INSERT INTO TableName (field-2, field-3) VALUES (“value-2”, “value-3”)

Adding New Records INSERT INTO Statement INSERT INTO Books VALUES (“ ”, “SQL Is Fun”, 1, 25.00) INSERT INTO Books (ISBN, Title) VALUES (“ ”, “Born to Code”)

Your Turn  Add the following record to Books ISBN: 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 Authors INSERT into Authors (auName, auPhone) SELECT NewAuthors,auName, NewAuthors.auPhone FROM NewAuthors WHERE NewAuthors.new = true;

Removing Records DELETE 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 DELETE FROM Books WHERE ISBN=“ ”

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 1. Delete a book titled “Born in Maui” 2. Delete all books priced $50 or above 3. Delete all books published by “Small House” 4. Delete all books written by Shakespeare

Delete all books priced $50 or above. DELETE FROM Books WHERE bkPrice >= 50.00;

Delete all books published by “Small House” DELETE FROM Books WHERE Books.pubID IN (SELECT Publishers.pubID FROM Publishres WHERE Publishers.pubName=“Small House” );

Delete all books written by Shakespeare DELETE FROM Books WHERE 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 1.List all cars that were made in 2000 or later. 2.Who was the salesperson that took care of Customer “John Wayne”? 3.Who was the salesperson that sold “Ford” “Edsel” in carlot at “Windward Cars”? Update 1.Reduce price of all Honda cars by 30%. 2.Reduce the price of all non-Honda cars by 50%. 3.Mark as sold all cars which are located at “Windward Cars”

Your Turn Delete 1.Remove from Salespeople table an employee named “Linda” “Gingle”. 2.Remove from Cars table all cars which are located at “Windward Cars”. Insert 1.Add a customer whose name is “Zeke” “Zorro” with a phone number of “ ”. 2.Add to Cars table the cars from the Newcars table.

Solutions SELECT 1.List all cars that were made in 2000 or later. SELECT * FROM cars WHERE year >= 2000; 2.Who was the salesperson that took care of Customer “John Wayne”? SELECT salespeople.firstName, salespeople.lastName FROM salespeople, transactions, customers WHERE salespeople.salesID = transactions.salesID AND transactions.custID = customers.custID AND customers.nameFirst = “John” AND customers.nameLast = “Wayne”;

Solutions SELECT 3.Who was the salesperson that sold “Ford” “Edsel” in carlot at “Windward Cars”? SELECT salespeople.firstName, salespeople.lastName FROM salespeople, transactions, cars WHERE salespeople.salesID = transactions.salesID AND transactions.carID = cars.carID AND cars.make = “Ford” AND cars.model = “Edsel”;

Solutions Update 1.Reduce price of all Honda cars by 30%. UPDATE cars SET carListPrice = carListPrice * 0.7 WHERE carMake = 'Honda'; 2.Reduce the price of all non-Honda cars by 50%. UPDATE Cars SET carListPrice = carListPrice * 0.5 WHERE carMake <> 'Honda';

Solutions Update 3.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 1.Remove from Salespeople table an employee named “Elmo” “Jones”. DELETE FROM salePeople WHERE salNameFirst = 'Elmo' AND salNameLast = 'Jones'; 2.Remove from Cars table all cars which are located at “Lemons Are Us”. DELETE FROM Cars WHERE Cars.lotID = (SELECT Carlots.lotID FROM Carlots WHERE Carlots.lotName = 'Lemons Are Us');

Solutions Insert 1.Add a customer whose name is “Zeke” “Zorro” with a phone number of “ ”. INSERT INTO Customers (nameFirst, nameLast, phoneNumber) VALUES (“Zeke”, “Zorro”, “ ”); 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, field2 FROM Table WHERE someCondtion; SELECT Table1.field1, Table2.field1 FROM Table1, Table2 WHERE Table1.prKey = forKey AND someCondtion;

SQL Statements UPDATE Table SET field1 = value1, field2 = value2 WHERE someCondition UPDATE Table SET field1 = value1 WHERE (subquery)

SQL Statements INSERT INTO Table(field1, field2) VALUES (value1, value2);

SQL Statements DELETE FROM Table WHERE 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