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

Slides:



Advertisements
Similar presentations
1 Lecture 02: SQL. 2 Outline Data in SQL Simple Queries in SQL (6.1) Queries with more than one relation (6.2) Recomeded reading: Chapter 3, Simple Queries.
Advertisements

SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: SQL92, SQL2, SQL3. Vendors support.
1 Lecture 12: SQL Friday, October 26, Outline Simple Queries in SQL (5.1) Queries with more than one relation (5.2) Subqueries (5.3) Duplicates.
M.P. Johnson, DBMS, Stern/NYU, Spring C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring,
Introduction to Structured Query Language (SQL)
Matthew P. Johnson, OCL1, CISDD CUNY, F20041 OCL1 Oracle 8i: SQL & PL/SQL Session #3 Matthew P. Johnson CISDD, CUNY Fall, 2004.
M.P. Johnson, DBMS, Stern/NYU, Sp20041 C : Database Management Systems Lecture #10 Matthew P. Johnson Stern School of Business, NYU Spring, 2004.
1 Lecture 02: Basic SQL. 2 Outline Data in SQL Simple Queries in SQL Queries with more than one relation Reading: Chapter 3, “Simple Queries” from SQL.
Introduction to Structured Query Language (SQL)
SQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
MySQL Tutorial Introduction to Database. Learning Objectives  Read and write Data Definition grammar of SQL  Read and write data modification statements.
1 Lecture 2: SQL Wednesday, January 7, Agenda Leftovers from Monday The relational model (very quick) SQL Homework #1 given out later this week.
1 Lecture 3: More SQL Friday, January 9, Agenda Homework #1 on the web site today. Sign up for the mailing list! Next Friday: –In class ‘activity’
1 Information Systems Chapter 6 Database Queries.
Complex Queries (1) Product ( pname, price, category, maker)
Exercises Product ( pname, price, category, maker) Purchase (buyer, seller, store, product) Company (cname, stock price, country) Person( per-name, phone.
1. Midterm summary Types of data on the web: unstructured, semi- structured, structured Scale and uncertainty are key features Main goals are to model,
IM433-Industrial Data Systems Management Lecture 5: SQL.
Intro. to SQL DSC340 Mike Pangburn. Learning Objectives Understand the data-representation terminology underlying relational databases Understand core.
More SQL: Complex Queries, Triggers, Views, and Schema Modification UMM AL QURA UNIVERSITY College of Computer Dr. Ali Al Najjar 1.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
Lecture 2: E/R Diagrams and the Relational Model Wednesday, April 3 rd, 2002.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
1 Introduction to Database Systems CSE 444 Lecture 02: SQL September 28, 2007.
1 Lecture 02: SQL Friday, September 30, Administrivia Homework 1 is out. Due: Wed., Oct. 12 Did you login on IISQLSRV ? Did you change your password.
Lectures 2&3: Introduction to SQL. Lecture 2: SQL Part I Lecture 2.
Hassan Tariq INTRODUCTION TO SQL What is SQL? –When a user wants to get some information from a database file, he can issue a query. – A query is a user–request.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
SQL. SQL Introduction Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a.
CHAPTER 6: INTRODUCTION TO SQL © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer,
Standard language for querying and manipulating data Structured Query Language Many standards out there: ANSI SQL, SQL92 (a.k.a. SQL2), SQL99 (a.k.a. SQL3),
SQL.
Lecture 05: SQL Wednesday, January 12, 2005.
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
SQL Introduction Standard language for querying and manipulating data
Programming with Android:
Monday 3/27 and Wednesday 3/29, 2006
Class 2 Relational Data Languages
Lecture 2 (cont’d) & Lecture 3: Advanced SQL – Part I
Database SQL.
Cse 344 January 12th –joins.
March 30th – intro to joins
Cse 344 January 10th –joins.
Introduction to SQL Wenhao Zhang October 5, 2018.
CSE544 SQL Wednesday, March 31, 2004.
SQL Introduction Standard language for querying and manipulating data
Lecture 12: SQL Friday, October 20, 2000.
Introduction to Relational Databases
Lectures 3: Introduction to SQL 2
Introduction to Database Systems CSE 444 Lecture 02: SQL
Lecture 4: SQL Thursday, January 11, 2001.
Lecture 3 Monday, April 8, 2002.
Class 2 Relational Data Languages
CS639: Data Management for Data Science
Sampath Jayarathna Cal Poly Pomona
OCL3 Oracle 10g: SQL & PL/SQL Session #3
Lecture 03: SQL Friday, October 3, 2003.
Terminology Product Attribute names Name Price Category Manufacturer
Lectures 2: Introduction to SQL 1
DATABASE MANAGEMENT SYSTEM
Instructor: SAMIA ARSHAD
Lecture 14: SQL Wednesday, October 31, 2001.
Presentation transcript:

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

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

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

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 (800123456, ‘Bob’, 25, SIS); insert into students (SID, name, age, dept) values (800123456, ‘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 (800123456, ‘Bob’, 25, SIS), (800678912, ‘Alice’, 20, CS);

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’;

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

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”

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”

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

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)

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%’

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

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.

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 ?

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

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

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’

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

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)

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;

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)

Aggregate Functions A complete list is available at: http://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html

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

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 ‘800123456’ to ‘CS’ Update student set dept=‘CS’ where SID=800123456

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