Download presentation
Presentation is loading. Please wait.
Published byBenedict Fox Modified over 9 years ago
1
COMP 3715 Spring 05
2
Working with data in a DBMS Any database system must allow user to Define data Relations Attributes Constraints Manipulate data Insert Delete Retrieval
3
Working with data in DBMS Thus a DBMS must provide Data Definition Language (DDL) Define tables structures Define attributes and domains Define constraints Data Manipulation Language (DML) To insert, delete and update data To query and retrieve data
4
Structured Query Language -- SQL The “standard” query language for DBMS Contains both DDL & DML Supported by every DBMS on the market Some system may have GUI But SQL is still the standard way for computer programs interact with the DBMS
5
Data Definition – Create table Need to specify Table name Attribute names Attribute domains Primary keys Constraints
6
Data Definition – Create table CREATE TABLE Customer (ssn CHAR(9); name CHAR(30); address CHAR(50); age INTEGER; income INTEGER; PRIMARY KEY (ssn))
7
Data Definition – Create table CREATE TABLE Book (title CHAR(30); isbn CHAR(20); author CHAR(30); quantity INTEGER; price REAL; PRIMARY KEY (isbn))
8
Data Definition – Create table CREATE TABLE Buy (isbn CHAR(20); customer CHAR(9); quantity INTEGER NOT NULL; date DATE; PRIMARY KEY (isbn, customer, date); FOREIGN KEY (isbn) REFERENCES Books FOREIGN KEY (customer) REFERENCES Customer(ssn))
9
Data Manipulation -- Query The standard query for SQL SELECT FROM WHERE ORDER BY Other parameters (to be discuss in senior class…)
10
Data Manipulation – Query: Selection (single table) “Find the ssn of the customer name John Doe” SELECT ssn FROM Customer WHERE Name = “John Doe”
11
Data Manipulation – Query: Selection (single table) “Find the quantity of books in the bookstore for “Tax break 2005” written by “Mike Rich” SELECT quantity FROM Book WHERE title = “Tax break 2005” AND author = “Mike Rich”
12
Data Manipulation – Query: Selection (single table) “Find the isbn of all books that is sold on 2/14/2005 and also return how many of each book is sold” SELECT isbn, quantity FROM Buy WHERE date = “2/14/2005”
13
Data Manipulation – Query: Selection (single table) “Find the ssn of the customers who bought the book with isbn = ‘A1234567’” SELECT ssn FROM Buy WHERE isbn = “A1234567”
14
Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A1234567’” SELECT name FROM Buy, Customer WHERE Buy.isbn = “A1234567” and Buy.customer = Customer.ssn
15
Data Manipulation – Query: Join (Multiple tables) Join Combining information of multiple tables Consider the Cartesian Product of the tuples from the table i.e. consider ALL pairs of tuples from the two tables For each possible pair, the condition is checked
16
Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A1234567’”, also return the name of the book SELECT Customer.name, Book.title FROM Buy, Customer, Book WHERE Buy.isbn = “A1234567” and Buy.customer = Customer.ssn and Book.isbn = Buy.isbn
17
Data Manipulation – Query: negative information “Find the customer who buy a book on a date other then “2/14/2005” SELECT customer FROM Buy WHERE Date ≠ “2/14/2005”
18
Data Manipulation – Query: negative information “Find the customer who does not buy a book on “2/14/2005” SELECT customer FROM Buy WHERE Date ≠ “2/14/2005” Does NOT work! Why? Each tuple is examined separately Whenever the condition is satisfied, the tuple is returned
19
Data Manipulation – Query: Aggregate queries Functions available for counting, averaging etc. AVG(), COUNT(), SUM (), MIN (), MAX() Example, find the most expensive book SELECT MAX(price) FROM Book
20
Data Manipulation – Query: Aggregate queries Example, find the most expensive book written by “J. K. Roaming” SELECT MAX(price) FROM Book WHERE author = “J. K. Roaming”
21
Data Manipulation – Query: Aggregate queries Example, find the most expensive book written by “J. K. Roaming” and return its title SELECT title, MAX(price) FROM Book WHERE author = “J. K. Roaming” This does NOT work. Convention in SQL, you cannot max aggregate and non-aggregate values (except using Group by)
22
Data Manipulation – Query: Aggregate queries Example, find the number of authors that has book in the bookstore SELECT COUNT(author) FROM Book This does NOT work. It will count duplicates as separate
23
Data Manipulation – Query: Aggregate queries Example, find the number of authors that has book in the bookstore SELECT COUNT(DISTINCT author) FROM Book
24
Data Manipulation – Query: Group By For reporting, we may want to separate data into groups For example, I may want to list the number of customers based on age Using “Group By” enable it
25
Data Manipulation – Query: Group By List the number of customer for each age SELECT age, COUNT(*) FROM Customer GROUP BY age
26
Data Manipulation – Query: Group By List the number of rich customers (with income > 100,000) for each age SELECT age, COUNT(*) FROM Customer WHERE income > 100,000 GROUP BY age
27
Data Manipulation – Query: Group By List the number of rich customers (with income > 100,000) for each age, also list the maximum salary for each group SELECT age, COUNT(*), MAX(salary) FROM Customer WHERE income > 100,000 GROUP BY age
28
Data Manipulation – Insertion, Deletion, Inserting a tuple INSERT INTO Books VALUES (“How to cut taxes”, “A123-456”, “R. Rich”, 3, 19.95) Deleting a tuple DELETE FROM Books WHERE author = “R. Rich” Delete all books from “R. Rich”
29
Data Manipulation – Update Update tuple Update Books SET price = price * 1.1 WHERE author = “R. Rich” Increase the price of all books from R. Rich for 10%
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.