comp 3715 spring 05. working with data in a dbms any database system must allow user to define data...

29
COMP 3715 Spring 05

Upload: benedict-fox

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

COMP 3715 Spring 05

Page 2: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Working with data in a DBMS Any database system must allow user to

Define data Relations Attributes Constraints

Manipulate data Insert Delete Retrieval

Page 3: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 4: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate 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

Page 5: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Definition – Create table Need to specify

Table name Attribute names Attribute domains Primary keys Constraints

Page 6: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Definition – Create table CREATE TABLE Customer

(ssn CHAR(9);

name CHAR(30);

address CHAR(50);

age INTEGER;

income INTEGER;

PRIMARY KEY (ssn))

Page 7: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Definition – Create table CREATE TABLE Book

(title CHAR(30);

isbn CHAR(20);

author CHAR(30);

quantity INTEGER;

price REAL;

PRIMARY KEY (isbn))

Page 8: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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))

Page 9: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Manipulation -- Query

The standard query for SQL

SELECT <attributes>

FROM <table>

WHERE <condition>

ORDER BY <attribute list> Other parameters (to be discuss in senior

class…)

Page 10: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Manipulation – Query: Selection (single table) “Find the ssn of the customer name John

Doe”

SELECT ssn

FROM Customer

WHERE Name = “John Doe”

Page 11: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 12: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 13: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 14: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 15: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 16: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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.titleFROM Buy, Customer, BookWHERE Buy.isbn = “A1234567” and

Buy.customer = Customer.ssn and Book.isbn = Buy.isbn

Page 17: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 18: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 19: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 20: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 21: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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 BookWHERE author = “J. K. Roaming”

This does NOT work. Convention in SQL, you cannot max aggregate and

non-aggregate values (except using Group by)

Page 22: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 23: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Manipulation – Query: Aggregate queries Example, find the number of authors that has

book in the bookstore

SELECT COUNT(DISTINCT author)

FROM Book

Page 24: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 25: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

Data Manipulation – Query: Group By List the number of customer for each age

SELECT age, COUNT(*)

FROM Customer

GROUP BY age

Page 26: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 27: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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

Page 28: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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”

Page 29: COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data

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%