COMP 3715 Spring 05
Working with data in a DBMS Any database system must allow user to Define data Relations Attributes Constraints Manipulate data Insert Delete Retrieval
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
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
Data Definition – Create table Need to specify Table name Attribute names Attribute domains Primary keys Constraints
Data Definition – Create table CREATE TABLE Customer (ssn CHAR(9); name CHAR(30); address CHAR(50); age INTEGER; income INTEGER; PRIMARY KEY (ssn))
Data Definition – Create table CREATE TABLE Book (title CHAR(30); isbn CHAR(20); author CHAR(30); quantity INTEGER; price REAL; PRIMARY KEY (isbn))
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))
Data Manipulation -- Query The standard query for SQL SELECT FROM WHERE ORDER BY Other parameters (to be discuss in senior class…)
Data Manipulation – Query: Selection (single table) “Find the ssn of the customer name John Doe” SELECT ssn FROM Customer WHERE Name = “John Doe”
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”
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”
Data Manipulation – Query: Selection (single table) “Find the ssn of the customers who bought the book with isbn = ‘A ’” SELECT ssn FROM Buy WHERE isbn = “A ”
Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A ’” SELECT name FROM Buy, Customer WHERE Buy.isbn = “A ” and Buy.customer = Customer.ssn
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
Data Manipulation – Query: Join (Multiple tables) “Find the name of the customers who bought the book with isbn = ‘A ’”, also return the name of the book SELECT Customer.name, Book.title FROM Buy, Customer, Book WHERE Buy.isbn = “A ” and Buy.customer = Customer.ssn and Book.isbn = Buy.isbn
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”
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
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
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”
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)
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
Data Manipulation – Query: Aggregate queries Example, find the number of authors that has book in the bookstore SELECT COUNT(DISTINCT author) FROM Book
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
Data Manipulation – Query: Group By List the number of customer for each age SELECT age, COUNT(*) FROM Customer GROUP BY age
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
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
Data Manipulation – Insertion, Deletion, Inserting a tuple INSERT INTO Books VALUES (“How to cut taxes”, “A ”, “R. Rich”, 3, 19.95) Deleting a tuple DELETE FROM Books WHERE author = “R. Rich” Delete all books from “R. Rich”
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%