Informatik II D-Mavt Database Queries With SQL
Informatik II D-Mavt Database Queries With SQL
Informatik II D-Mavt Database Queries With SQL
Database queries with SQL Program What is SQL? Creating, altering, and deleting tables Inserting, updating, and deleting data entries Querying the database Queries across multiple tables
Storing queries as data views
Learning goals Be able to understand SQL statements and translate them to clear text. Be able to write SQL statements to create/define tables in a database. Be able to write SQL statements to insert, modify, and delete data in a table. Be able to write SQL statements to query the database across several tables and with search conditions.
Further reading
Chapter 3, Simple Queries from SQL for Web Nerds, Philip Greenspun (http://philip.greenspun.com/sql/) Database Design for Mere Mortals, Michael J. Hernandez Information Modeling and Relational Databases, Terry Halpin Database Modeling and Design, Toby J. Teorey
What is SQL?
Non-procedural universal language for querying and manipulating data in RDBMS Structured Q uery Language Many standards out there: ANSI SQL SQL92 (a.k.a. SQL2) SQL99 (a.k.a. SQL3) Vendors support various sub- or super-sets of these What we discuss is common to all of them
Data in SQL
(see later)
Characters:
CHAR(20) VARCHAR(40) -- fixed length -- variable length
Numbers:
BIGINT, INT, SMALLINT, TINYINT REAL, FLOAT -- differ in precision MONEY
Table and column names cannot have spaces or be reserved words like TABLE, CREATE, etc.
Keys:
PRIMARY KEY KEY INDEX -- primary key of the table -- foreign key (will be indexed) -- field to be indexed for fast search -- field must be filled in -- value to be used if user gives none -- see later
Null values:
NOT NULL
Default value:
DEFAULT value
Automatic values:
AUTO_INCREMENT
Plan your tables very carefully! Once created, they may be difficult to change!
If you are going to use the relational nature of a database, dont forget you need to have a unique way to access records! There is a way to make the key automatically increment, so you dont have to worry about which one is next.
CREATE TABLE customer (cust_id INT, name VARCHAR(50) NOT NULL, address VARCHAR(256) NOT NULL, PRIMARY KEY (cust_id)); CREATE TABLE credit_card (cust_id INT NOT NULL, credit_card_type CHAR(5) NOT NULL, credit_card_num INT NOT NULL, KEY (cust_id));
Customer
1 have N
Database automatically gives sequential numbers to the entities Makes sure each number is unique Numbers will never change once assigned Number value has no real-world meaning
Credit card
card type card number
Modifying Tables
Generic form
| | | | | | | |
ALTER specifications
ADD [COLUMN] column_definition [FIRST | AFTER col_name ] ADD {INDEX|KEY} [index_type] (index_col_name,...) MODIFY [COLUMN] column_definition [FIRST | AFTER col_name] DROP [COLUMN] col_name DROP PRIMARY KEY DROP {INDEX|KEY} index_name DROP FOREIGN KEY fk_symbol RENAME [TO] new_tbl_name
UPDATE table SET column=expression WHERE condition; Example: UPDATE contacts SET company=AOL WHERE company=Time Warner;
Multiple tables can be joined in a single query Multiple conditions (search criteria) can be imposed Results can be sorted or grouped
selection
Powergizmo
Eliminating Duplicates
Category
SELECT PName, Price, Supplier FROM Product WHERE Price > 100
PName Price $149.99 $203.99 Supplier Canon Hitachi
Compare to:
Category
SingleTouch MultiTouch
Selection Operations
What goes in the WHERE clause: x = y, x < y, x <= y, etc
For numbers, they have the usual meanings For CHAR and VARCHAR: lexicographic ordering For dates and times: chronological ordering
Negation: NOT Pattern matching on strings: s LIKE p (also: NOT LIKE) Strings are enclosed in single quotes: string Multiple criteria can be joined by AND or OR Probe for empty fields with IS NULL Special operators: BETWEEN x AND y, IN(),
WHERE examples
The WHERE subclause allows you to select records based on a condition. SELECT * FROM contacts WHERE age<10;
Display records from contacts where age<10
WHERE examples
SELECT * FROM contacts WHERE name IS NULL; SELECT * FROM contacts WHERE zip IN (14454,12345);
ASC order in ascending order DESC in descending order List of several attributes can be given for nested ordering (e.g. order by price first and within all products of same price, order by supplier: ORDER BY price, supplier ASC)
10
Joins in SQL
Connect two or more tables:
Product
PName Gizmo Powergizmo SingleTouch MultiTouch Price $19.99 $29.99 $149.99 $203.99 CName GizmoWorks Canon Hitachi Category Gadgets Gadgets Photography Household StockPrice 25 65 15 Supplier GizmoWorks GizmoWorks Canon Hitachi Country USA Japan Japan
Company
What is the Connection between them ?
Joins
Product(pname,price,category,supplier) Company(cname,stockPrice,country) Find all products under $200 manufactured in Japan; return their names and prices. Join between Product and Company SELECT PName, Price FROM Product, Company WHERE Supplier=CName AND Country=Japan AND Price <= 200
Product
PName Gizmo Powergizmo SingleTouch MultiTouch Price $19.99 $29.99 $149.99 $203.99 Category Gadgets Gadgets
Joins in SQL
Company
Supplier GizmoWorks GizmoWorks Canon Hitachi Cname GizmoWorks Canon Hitachi StockPrice 25 65 15 Country USA Japan Japan
Photography Household
SELECT PName, Price FROM Product, Company WHERE Supplier=CName AND Country=Japan AND Price <= 200
PName SingleTouch Price $149.99
11
Joins
Product(pname,price,category,supplier) Company(cname,stockPrice,country) Find all countries that manufacture some product in the Gadgets category. SELECT Country FROM Product, Company WHERE Supplier=CName AND Category=Gadgets
Product
PName Gizmo Powergizmo SingleTouch MultiTouch Price $19.99 $29.99 $149.99 $203.99 Category Gadgets Gadgets
Joins in SQL
Company
Supplier GizmoWorks GizmoWorks Canon Hitachi Cname GizmoWorks Canon Hitachi StockPrice 25 65 15 Country USA Japan Japan
Photography Household
USA USA
Disambiguating Attributes
Sometimes two relations have the same attr: Person(pname,address,worksfor) Company(cname,address)
SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor=cname Which address ?
First the tables are joint and entries duplicated Then the selection is made!
USA USA
12
In fact
table names introduced automatically by the system:
People
Id 1 2 3 Name Joe Jane Chris Addressid 1 2 3
Company(cname,address)
SELECT cname FROM Company Becomes:
PhoneNumbers
PhoneID 1 2 Id 1 1 1 2 3 3 Phone 5532 2234 3211 3421 2341 3211 1 2 3
Address
AddressID Company ABC XYZ PDQ Address 123 456 789 Zip 12345 14454 14423
3 4 5 6
Multiple Joins
Product(pname,price,category,supplier) Purchase(buyer,seller,store,product) Person(persname,phoneNumber,city) Find names of people living in Seattle that bought some product in the Gadgets category, and the names of the stores they bought such product from SELECT DISTINCT persname, store FROM Person, Purchase, Product WHERE persname=buyer AND product=pname AND city=Seattle AND category=Gadgets
Examples
Students
Name John Alice Max 1 TutorID 2
Student.Name John Max Student.Name John Alice Max Student.Name Max John NULL Student.Name John Alice Max NULL
Tutor.Name Greg Bob Tutor.Name Greg NULL Bob Tutor.Name Bob Greg Fabiana Tutor.Name Greg NULL Bob Fabiana
inner
left outer
right outer
Tutors
ID 1 2 3 Name Bob Greg Fabiana
full outer
13
Natural Join
Attributes of the same name are automatically joined
14
Tuple Variables
Purchase(buyer,seller,store,product) Find buyer and seller of all purchases that were made in store BestBuy: SELECT DISTINCT x.buyer, x.seller FROM Purchase AS x WHERE x.store = BestBuy
Tuple Variables
Tuple variables introduced automatically by the system: Product(name,price,category,supplier) SELECT name FROM Product WHERE price > 100 Becomes: SELECT Product.name FROM Product AS Product WHERE Product.price > 100
They NEED to be specified manually when the same table appears more than once!
Tuple Variables
Product(pname,price,category,supplier) Purchase(buyer,seller,store,product) Person(persname,phoneNumber,city) Find all stores that sold at least one product that the store BestBuy also sold: SELECT DISTINCT x.store FROM Purchase AS x, Purchase AS y WHERE x.product = y.product AND y.store = BestBuy
Renaming Columns
Product
PName Gizmo Powergizmo SingleTouch MultiTouch Price $19.99 $29.99 $149.99 $203.99 Category Gadgets Gadgets Photography Household Supplier GizmoWorks GizmoWorks Canon Hitachi
SELECT Pname AS prodName, Price AS askPrice FROM Product WHERE Price > 100
prodName askPrice $149.99 $203.99
SingleTouch MultiTouch
15
GROUP BY/HAVING
The GROUP BY clause allows you to group results together before applying aggregation functions like:
AVG(), COUNT(), MAX(), MIN(), SUM() COUNT DISTINCT
GROUP BY/HAVING
SELECT customer, SUM(amount) FROM Sales;
Customer Oracle IBM Oracle Amount 17100 17100 17100
Sales
Customer
Oracle IBM
GROUP BY/HAVING
SELECT customer, SUM(amount) FROM Sales GROUP BY customer WHERE SUM(amount)>10000;
Customer Oracle IBM Customer Oracle IBM Oracle Amount 5500 4500 7100 Amount 12600 4500
GROUP BY Examples
SELECT company,count(company) FROM contacts GROUP BY company;
How many times does each company appear in the contacts table?
Sales
Oracle
16
Nested SELECTs
The WHERE subclause of a SELECT statement can contain another SELECT statement: SELECT * FROM contacts WHERE zip NOT IN ( SELECT zip FROM address WHERE state=NY );
(Select all contacts that have a ZIP code outside NY state.)
Nested SELECTs
RDBMS starts with the inner-most SELECT and executes it Results are pasted into query in place of the executed SELECT Progress outward recursively until outermost SELECT has been executed
Views in SQL
An SQL view is a virtual table that is derived from other real or virtual tables Real tables are defined by CREATE TABLE commands and are permanently stored in the database Virtual tables are defined by the CREATE VIEW command to avoid defining complex SQL retrieval expressions repeatedly Only the definition of a view is stored, but not the data inside. They are recomputed every time the view is used (opened)
17
18