Presentation is loading. Please wait.

Presentation is loading. Please wait.

Server-Side Application and Data Management IT IS 3105 (FALL 2009)

Similar presentations


Presentation on theme: "Server-Side Application and Data Management IT IS 3105 (FALL 2009)"— Presentation transcript:

1 Server-Side Application and Data Management IT IS 3105 (FALL 2009)
Introduction to Databases (Cont.) Mohamed Shehab Lecture 5

2 Create Table An SQL relation is defined using the CREATE TABLE command: Create table [tablename] (A1 T1,A2 T2, … An Tn, (integrity-constraint1), …, (integrity-constraintk)) Each Ai is an attribute name in the table Each Ti is the data type of values for Ai Example Create table students (SID char(9) not null, name varchar(30), age integer, dept varchar(20), primary key (SID) );

3 Drop and Alter Table The DROP TABLE command deletes all information about the dropped relation from the database The ALTER TABLE command is used to add attributes to or remove attributes from an existing relation (table): alter table tablename actions where actions can be one of following actions: ADD Attribute DROP Attribute ADD PRIMARY KEY (Attribute_name1,…) DROP PRIMARY KEY

4 Insert into a Table Add a new tuple to a table: Example:
insert into table name values(V1, V2, V3); insert into table name (C1, C2, C3) values(V1, V2, V3); Example: insert into students values ( , ‘Bob’, 25, SIS); insert into students (SID, name, age, dept) values ( , ‘Bob’, 25, SIS); Inserting multiple tuples: insert into table name (C1, C2, C3) values(V1, V2, V3), (V1, V2, V3), (V1, V2, V3); Example insert into students (SID, name, age, dept) values ( , ‘Bob’, 25, SIS), ( , ‘Alice’, 20, CS);

5 Writing Database Queries
A typical SQL query has the form: select A1, A2, …, An from table1, table2, …, tablem where P Ai represents an attribute tablei represents a table P is a constraints (condition) Example select SID, name from student where dept=‘SIS’;

6 SQL Query Basic form: SELECT attributes
FROM relations (possibly multiple, joined) WHERE conditions (selections)

7 Simple SQL Query SELECT * FROM Product WHERE category=‘Gadgets’
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT * FROM Product WHERE category=‘Gadgets’ PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 “selection”

8 Simple SQL Query Product PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi SELECT PName, Price, Manufacturer FROM Product WHERE Price > 100 PName Price Manufacturer SingleTouch $149.99 Canon MultiTouch $203.99 Hitachi “selection” and “projection”

9 A Notation for SQL Queries
Input Schema Product(PName, Price, Category, Manfacturer) SELECT Name, Price, Manufacturer FROM Product WHERE Price > 100 Answer(PName, Price, Manfacturer) Output Schema

10 Selections What goes in the WHERE clause:
x = y, x < y, x <= y, etc For number, they have the usual meanings For CHAR and VARCHAR: lexicographic ordering Expected conversion between CHAR and VARCHAR For dates and times, what you expect... Pattern matching on strings: s LIKE p (next)

11 The LIKE operator s LIKE p: pattern matching on strings
p may contain two special symbols: % = any sequence of characters _ = any single character Product(Name, Price, Category, Manufacturer) Find all products whose name mentions ‘gizmo’: SELECT * FROM Products WHERE PName LIKE ‘%gizmo%’

12 Eliminating Duplicates
Category Gadgets Photography Household SELECT DISTINCT category FROM Product Compare to: Category Gadgets Photography Household SELECT category FROM Product

13 Ordering the Results SELECT pname, price, manufacturer FROM Product WHERE category=‘gizmo’ AND price > 50 ORDER BY price, pname Ordering is ascending, unless you specify the DESC keyword. Ties are broken by the second attribute on the ORDER BY list, etc.

14 Joins in SQL Connect two or more tables: What is the Connection
PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Product Company CName StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 What is the Connection between them ?

15 Join between Product and Company
Joins Product (pname, price, category, manufacturer) 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 Manufacturer=CName AND Country=‘Japan’ AND Price <= 200

16 Joins in SQL Product Company PName Price Category Manufacturer Gizmo $19.99 Gadgets GizmoWorks Powergizmo $29.99 SingleTouch $149.99 Photography Canon MultiTouch $203.99 Household Hitachi Cname StockPrice Country GizmoWorks 25 USA Canon 65 Japan Hitachi 15 SELECT PName, Price FROM Product, Company WHERE Manufacturer=CName AND Country=‘Japan’ AND Price <= 200 PName Price SingleTouch $149.99

17 Joins Product (pname, price, category, manufacturer)
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’

18 Disambiguating Attributes
Sometimes two relations have the same attr: Person(pname, address, worksfor) Company(cname, address) Which address ? SELECT DISTINCT pname, address FROM Person, Company WHERE worksfor = cname SELECT DISTINCT Person.pname, Company.address FROM Person, Company WHERE Person.worksfor = Company.cname

19 Tuple Variables Product (pname, price, category, manufacturer)
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’ Answer (store)

20 Aliases for Columns and Tables
SELECT col1 alias1, col2 alias2 from tablename; Example: SELECT SID id, name nm, age, dept dno from students; SELECT col1, col2 from tablename AS alias1; SELECT SID, name from students as t1;

21 Aggregate Functions Aggregate functions operate on the multiset of values of a attribute and return a value avg(attribute): average value min(attribute): minimum value max(attribute): maximum value sum(attribute): sum of values count(attribute): number of values To obtain the value when duplicates are removed, insert the keyword distinct before attribute name: avg(distinct attribute)

22 Aggregate Functions A complete list is available at:

23 Modifying the Database
Three cases: Add a tuple INSERT INTO table_name VALUES (Val1, Val2, … , Valn) Change tuples UPDATE table_name SET A1=val1, A2=val2, …, An=valn WHERE tuple_selection_predicate Remove tuples DELETE FROM table_name

24 UPDATE Set all department to ‘computer science’
update student set dept=‘computer science’ In table student(SID, name, age, dept), increase the age of everyone by 1. update student set age=age+1 In table student, update the department for the user with SID ‘ ’ to ‘CS’ Update student set dept=‘CS’ where SID=

25 DELETION Delete records of all students in the university
delete from student Delete the students who study computer science where dept=‘CS’


Download ppt "Server-Side Application and Data Management IT IS 3105 (FALL 2009)"

Similar presentations


Ads by Google