Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Keith Vander Linden, 2005 1 A language that doesn't affect the way you think about programming is not worth knowing. - Alan Perlis, “Epigrams in Computing”,

Similar presentations


Presentation on theme: "© Keith Vander Linden, 2005 1 A language that doesn't affect the way you think about programming is not worth knowing. - Alan Perlis, “Epigrams in Computing”,"— Presentation transcript:

1 © Keith Vander Linden, 2005 1 A language that doesn't affect the way you think about programming is not worth knowing. - Alan Perlis, “Epigrams in Computing”, SIGPLAN, 1982

2 © Keith Vander Linden, 2005 2 Database Languages ● Database languages provide features for: – Building a database schema – Retrieving data from a database – Manipulating data in a database ● Two common languages: Two common languages: – Query-By-Example (QBE) Query-By-Example (QBE) – Structured Query Language (SQL) (Sections 9.5-8) Structured Query Language (SQL)

3 © Keith Vander Linden, 2005 3 Image from www.hp.com July, 2001 Moshe M. Zloof Query-by-Example (QBE) ● Introduced by IBM in 1975 ● Graphical interface to SQL ● Has influenced the query interfaces of other DB systems: – Paradox – Access

4 © Keith Vander Linden, 2005 4 Query-By-Example

5 © Keith Vander Linden, 2005 5 Edgar F. Codd Relational Algebra/Calculus ● Developed from 1971-1974 ● Relational Algebra - a procedural language: – Relations – Relational operators ● Relational Calculus - a declarative language with equivalent power. Image from www.computer.org, July, 2001www.computer.org

6 © Keith Vander Linden, 2005 6 Structured Query Language

7 © Keith Vander Linden, 2005 7 The Database Schema

8 © Keith Vander Linden, 2005 8 Access Query Types ● Retrieval queries: – Select queries ● Modification queries: – Make-table queries – Delete queries – Update queries – Append queries

9 © Keith Vander Linden, 2005 9 Projection Queries

10 © Keith Vander Linden, 2005 10 Selection Queries

11 © Keith Vander Linden, 2005 11 Conditions

12 © Keith Vander Linden, 2005 12 Join Queries

13 © Keith Vander Linden, 2005 13 Combining Operations

14 © Keith Vander Linden, 2005 14 Sorting

15 © Keith Vander Linden, 2005 15 Grouping & Aggregate Functions

16 © Keith Vander Linden, 2005 16 Arithmetic

17 © Keith Vander Linden, 2005 17 SQL ● Structured Query Language: – Specially designed for data queries and updates – Command-line based ● It is the industry standard

18 © Keith Vander Linden, 2005 18 Access Jet Engine Using SQL Jet DB Interface

19 © Keith Vander Linden, 2005 19 MSDE EngineSQL ServerJet EngineOracle Engine Using SQL Oracle DBJet DB Access Interface SQL Server DB MSDE DB ODBC Driver ODBC Driver ODBC Driver ODBC Driver VB Application Java Applet JDBC Driver

20 © Keith Vander Linden, 2005 20 DB Engine Using SQL DB ODBC Driver JDBC Driver Web Browser Network Web Server

21 © Keith Vander Linden, 2005 21 Basic Query Types ● Single-table queries Single-table queries ● Multiple-table queries Multiple-table queries ● Aggregation and Grouping Aggregation and Grouping ● Set Operations Set Operations ● Database Modifications Database Modifications

22 © Keith Vander Linden, 2005 22 SELECT Syntax SELECT FROM [WHERE ] [GROUP BY [HAVING ] ] [ORDER BY ]

23 © Keith Vander Linden, 2005 23 Single-Table Queries Q: Get a list of all the products. SELECT * FROM Products;

24 © Keith Vander Linden, 2005 24 The SELECT Clause 1 Q: Get names, categories and prices of all the products. SELECT name, unitPrice, category FROM Products;

25 © Keith Vander Linden, 2005 25 The SELECT Clause 2 Q: Get the total value of each product in stock. SELECT name, (unitPrice * inStock) FROM Products;

26 © Keith Vander Linden, 2005 26 The SELECT Clause 3 Q: Can SELECT return duplicates or not? SELECT category FROM Products;

27 © Keith Vander Linden, 2005 27 The SELECT Clause 4 Q: Get a list of the category types for products. SELECT DISTINCT category FROM Products;

28 © Keith Vander Linden, 2005 28 The WHERE Clause 1 Q: Get the foreign customers. SELECT firstName, lastName, country FROM Customers WHERE country <> 'USA';

29 © Keith Vander Linden, 2005 29 The WHERE Clause 2 Q: Get the Customers at 100 Main Street, New York. SELECT firstName, lastName, street, city FROM Customers WHERE street='100 Main St.' AND city='New York';

30 © Keith Vander Linden, 2005 30 The WHERE Clause 3 Q: Get the products without images. SELECT name, image FROM Products WHERE image IS NULL;

31 © Keith Vander Linden, 2005 31 The ORDER BY Clause Q: Get the Employees in alphabetical order. SELECT lastName+', '+firstName AS fullName FROM Customers ORDER BY lastName, firstName;

32 © Keith Vander Linden, 2005 32 Multiple-Table Queries Q: Get the list of products for each customer order. SELECT orderId, name, quantity FROM Products, OrderDetails WHERE id=productID;

33 © Keith Vander Linden, 2005 33 Multiple-Table Queries 2 Q: Get the names of the products and customers that order them. SELECT name, firstName, lastName FROM Products, OrderDetails, Orders, Customers WHERE Products.ID=OrderDetails.productID AND OrderDetails.orderID = Orders.ID AND Orders.customerID = Customers.ID;

34 © Keith Vander Linden, 2005 34 Grouping and Aggregation 1 Q: Count the products in each category. SELECT category, Count(category) FROM Products GROUP BY category ORDER BY Count(category) DESC;

35 © Keith Vander Linden, 2005 35 Grouping and Aggregation 2 Q: Get the categories with more than 3 products. SELECT category, Count(category) FROM Products GROUP BY category HAVING Count(category) > 3;

36 © Keith Vander Linden, 2005 36 Set Operations Q: Get the names of all suppliers and customers. SELECT name FROM Suppliers UNION SELECT firstName+’ ’+lastName FROM Customers;

37 © Keith Vander Linden, 2005 37 Inserting Data Q: Add Wile E’s alter ego to the customers list. INSERT INTO Customers(id,firstName,lastName) VALUES (14,'Carnivorous','Vulgarus');

38 © Keith Vander Linden, 2005 38 Updating Data Q: Change Carnivorous’s address. UPDATE Customers SET street = '1 Cave Lane' WHERE id = 14;

39 © Keith Vander Linden, 2005 39 Deleting Data Q: Remove Carnivorous from the Customers table. DELETE FROM Customers WHERE id = 14;

40 © Keith Vander Linden, 2005 40 Importing External Data ● Frequently, data from other sources must be imported in bulk. ● Approaches: – an SQL INSERT command file – a specialized import facility


Download ppt "© Keith Vander Linden, 2005 1 A language that doesn't affect the way you think about programming is not worth knowing. - Alan Perlis, “Epigrams in Computing”,"

Similar presentations


Ads by Google