© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2004 Dragomir R. Radev.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
How to Use MySQL CS430 March 18, SQL: “Structured Query Language”—the most common standardized language used to access databases. SQL has several.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Databases Using MySQL Creating Tables Queries. Databases  A database is a collection of data organized for efficient access  A relational database is.
Structured Query Language Part I Chapter Three CIS 218.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Introduction to Structured Query Language (SQL)
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 6 th Edition.
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2003 Dragomir R. Radev.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Microsoft Access 2010 Chapter 7 Using SQL.
Structured Query Language
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Introduction to SQL J.-S. Chou Assistant Professor.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 5 th Edition.
Chapter 3 Single-Table Queries
Structured Query Language Chapter Three DAVID M. KROENKE and DAVID J. AUER DATABASE CONCEPTS, 4 th Edition.
Microsoft Access 2010 Chapter 7 Using SQL. Change the font or font size for SQL queries Create SQL queries Include fields in SQL queries Include simple.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Relational Algebra and SQL applications Instructor: Dragomir R. Radev Fall 2005.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Fundamentals, Design, and Implementation, 9/e CPE 481 Database Processing Chapter 6 Structured Query Language (SQL) Instructor:Suthep Madarasmi, Ph.D.
CSC 2720 Building Web Applications Database and SQL.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Chapter 9 Structured Query Language David M. Kroenke Database Processing © 2000 Prentice Hall.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: 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 “Structured Query Language; standard language for relational data manipulation” DB2, SQL/DS, Oracle, INGRES, SYBASE, SQL Server, dBase/Win, Paradox,
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
(C) 2000, The University of Michigan 1 Database Application Design Handout #5 February 4, 2000.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL has several parts: Major ones: DDL – Data Definition Language {Defining, Deleting, Modifying relation schemas} DML – Data Manipulation Language {Inserting,
Structured Query Language
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL Query Getting to the data ……..
Database Application Design
CH 9 SQL 9.1 Querying A Single Table 9.2 Querying Multiple Tables
Course logistics © 2002 by Prentice Hall.
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
Chapter 7 Introduction to Structured Query Language (SQL)
Introduction To Structured Query Language (SQL)
MySQL Database System Installation Overview SQL summary
The University of Akron College of Applied Science & Technology Dept
Presentation transcript:

© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2004 Dragomir R. Radev

© 2002 by Prentice Hall 2 David M. Kroenke Database Processing Eighth Edition Chapter 9 Structured Query Language

© 2002 by Prentice Hall 3 Structure Query Language Structure Query Language is known as either –Its acronym, SQL, or –SEQUEL, the name of the original version of SQL SEQUEL was developed by IBM in the mid-1970s.

© 2002 by Prentice Hall 4 SQL, not a Procedural Programming Language SQL is not a programming language itself, it is a data access language SQL may be embedded in traditional procedural programming languages (like COBOL)

© 2002 by Prentice Hall 5 SQL Syntax SQL is not case sensitive. SELECT field(s) ‘what columns will be retrieved FROM table(s); ‘which table contains the column data e.g., SELECT Name, Phone FROM Student;

© 2002 by Prentice Hall 6 The DISTINCT Qualifier Eliminating duplicate rows on the output… SELECT DISTINCT StateAddress FROM Employee;

© 2002 by Prentice Hall 7 The WHERE Clause Reducing the output based on specified criteria… SELECT StudentName FROM Students WHERE GradePointAverage >= 3.0;

© 2002 by Prentice Hall 8 Comparison Operators Equals = Not equals <> Greater than > Less than < Greater than or equal to >= Less than or equal to <= Within a list of values IN A logical NOT Within a range BETWEEN

© 2002 by Prentice Hall 9 IN a List of Values SELECT StudentName FROM Student WHERE State IN [‘PA’, ‘MA’, ‘CA’];

© 2002 by Prentice Hall 10 The Logical NOT SELECT StudentName FROM Students WHERE State NOT IN [‘NJ’, ‘NM’, ‘NY’]; SELECT StudentName FROM Students WHERE NOT GradePointAverage >= 3.0;

© 2002 by Prentice Hall 11 Within a Range of Values SELECT StudentName FROM Student WHERE StudentID BETWEEN 250 and 300;

© 2002 by Prentice Hall 12 Using Wildcard Character Substitutions The LIKE keyword is used in place of the = sign when you use wildcard characters. The underscore character (_) is a single character substitution The percent character (%) is a multi- character substitution

© 2002 by Prentice Hall 13 Using LIKE SELECT StudentID FROM Student WHERE StudentName LIKE ‘K%’; SELECT PartName FROM Part WHERE PartNumber LIKE ‘_ABC%’;

© 2002 by Prentice Hall 14 NULL Means Nothing A NULL character means that nothing has been entered. This is different from a space or a zero. SELECT Name FROM Student WHERE Major IS NULL;

© 2002 by Prentice Hall 15 ORDER BY… Sorting Outputs Sorting in descending order… SELECT StudentID, Name FROM Student ORDER BY Name DESC; Sorting in ascending order… SELECT StudentID, Name FROM Student ORDER BY Name ASC;

© 2002 by Prentice Hall 16 Built-in Functions Counting number of rows COUNT Adding the values in a column SUM Averaging the values in a column AVG Finding the maximum value in a column MAX Finding the minimum value in a column MIN

© 2002 by Prentice Hall 17 Built-in Functions SELECT Count (*) FROM Student WHERE State = ‘WI’; SELECT Sum (Amount) FROM SalesReceipt; SELECT Max (Score) FROM Assignments;

© 2002 by Prentice Hall 18 Grouping the Output SELECT Name, State FROM Student GROUP BY State;

© 2002 by Prentice Hall 19 Reducing the Groups Displayed SELECT Name, State FROM Student GROUP BY State HAVING Count (*) > 4;

© 2002 by Prentice Hall 20 Sub-Queries SELECT Name FROM Student WHERE SID IN (SELECT StudentNumber FROM Enrollment WHERE ClassName = ‘MIS445’);

© 2002 by Prentice Hall 21 Joining Tables SELECT Student.SID, Student.Name, Enrollment.ClassName FROM Student, Enrollment WHERE Student.SID = Enrollment.StudentNumber AND Student.State = ‘OH’;

© 2002 by Prentice Hall 22 EXISTS SELECT DISTINCT StudentNumber FROM Enrollment A WHERE EXISTS (SELECT * FROM Enrollment B WHERE A.StudentNumber = B.StudentNumber AND A.ClassName NOT = B.ClassName);

© 2002 by Prentice Hall 23 Entering Data INSERT INTO Enrollment VALUES (400, ‘MIS445’, 44);

© 2002 by Prentice Hall 24 Deleting Data DELETE Student WHERE Student.SID = 100;

© 2002 by Prentice Hall 25 Modifying Data UPDATE Enrollment SET SeatNumber = 44 WHERE SID = 400;

© 2002 by Prentice Hall 26 MySQL Ch1 & Ch3

© 2002 by Prentice Hall 27 Overview –TcX - Michael Widenius (MySQL) –Hughes - David Hughes (mSQL) –Features: Mostly ANSI SQL2 compliant Transactions Stored procedures Auto_increment fields

© 2002 by Prentice Hall 28 More features Cross-database joins Outer joins API: C/C++, Eiffel, Java, PHP, Perl, Python, TCL Runs on Windows, UNIX, and Mac High performance

© 2002 by Prentice Hall 29 SQL syntax CREATE TABLE people (name CHAR(10)) INSERT INTO people VALUES (‘Joe’) SELECT name FROM people WHERE name like ‘J%’

© 2002 by Prentice Hall 30 SQL commands SHOW DATABASES SHOW TABLES Data types: INT, REAL, CHAR(l), VARCHAR(l), TEXT(l), DATE, TIME ALTER TABLE mytable MODIFY mycolumn TEXT(100) ENUM(‘cat’,’dog’,’rabbit’,’pig’)

© 2002 by Prentice Hall 31 SQL commands CREATE DATABASE dbname CREATE TABLE tname (id NOT NULL PRIMARY KEY AUTO_INCREMENT) CREATE INDEX part_of_name ON customer (name(10)) INSERT INTO tname (c1, …, cn) values (v1, …, vn)

© 2002 by Prentice Hall 32 JOINs and ALIASing SELECT book.title, author.name FROM author, book WHERE books.author = author.id SELECT very_long_column_name AS col FROM tname WHERE col=‘5’

© 2002 by Prentice Hall 33 Loading text files Comma-separated files (*.csv) LOAD DATA LOCAL INFILE "whatever.csv" INTO TABLE tname

© 2002 by Prentice Hall 34 Aggregate queries SELECT position FROM people GROUP by position SELECT position, AVG (salary) FROM people GROUP BY position HAVING AVG (salary) >

© 2002 by Prentice Hall 35 Full text search CREATE TABLE WebCache ( url VARCHAR (255) NOT NULL PRIMARY KEY, ptext TEXT NOT NULL, FULLTEXT (ptext)); INSERT INTO WebCache (url, ptext) VALUES (‘index.html’, ‘Welcome to the University of Michigan’); SELECT url from WebCache WHERE MATCH (ptext) against (‘Michigan’);

© 2002 by Prentice Hall 36 Advanced features Transactions Table locking Functions Unions Outer joins

© 2002 by Prentice Hall 37 Installing MySQL on Windows sql.html tutorials/sql/index.php3

© 2002 by Prentice Hall 38 Useful pointers Small example: _examples.htm MySQL documentation: (official) MySQL tutorial: ml Online, interactive tutorials:

© 2002 by Prentice Hall 39 use test; CREATE TABLE STATION (ID INTEGER PRIMARY KEY, CITY CHAR(20), STATE CHAR(2), LAT_N REAL, LONG_W REAL); DESCRIBE STATION; INSERT INTO STATION VALUES (13, 'Phoenix', 'AZ', 33, 112); INSERT INTO STATION VALUES (44, 'Denver', 'CO', 40, 105); INSERT INTO STATION VALUES (66, 'Caribou', 'ME', 47, 68); SELECT * FROM STATION; SELECT * FROM STATION WHERE LAT_N > 39.7;

© 2002 by Prentice Hall 40 SELECT ID, CITY, STATE FROM STATION; ID CITY STATE ; SELECT ID, CITY, STATE FROM STATION WHERE LAT_N > 39.7; CREATE TABLE STATS (ID INTEGER REFERENCES STATION(ID), MONTH INTEGER CHECK (MONTH BETWEEN 1 AND 12), TEMP_F REAL CHECK (TEMP_F BETWEEN -80 AND 150), RAIN_I REAL CHECK (RAIN_I BETWEEN 0 AND 100), PRIMARY KEY (ID, MONTH)); INSERT INTO STATS VALUES (13, 1, 57.4, 0.31); INSERT INTO STATS VALUES (13, 7, 91.7, 5.15); INSERT INTO STATS VALUES (44, 1, 27.3, 0.18); INSERT INTO STATS VALUES (44, 7, 74.8, 2.11); INSERT INTO STATS VALUES (66, 1, 6.7, 2.10); INSERT INTO STATS VALUES (66, 7, 65.8, 4.52); SELECT * FROM STATS;

© 2002 by Prentice Hall 41 SELECT * FROM STATION, STATS WHERE STATION.ID = STATS.ID; SELECT MONTH, ID, RAIN_I, TEMP_F FROM STATS ORDER BY MONTH, RAIN_I DESC; SELECT LAT_N, CITY, TEMP_F FROM STATS, STATION WHERE MONTH = 7 AND STATS.ID = STATION.ID ORDER BY TEMP_F; SELECT MAX(TEMP_F), MIN(TEMP_F), AVG(RAIN_I), ID FROM STATS GROUP BY ID; SELECT * FROM STATION WHERE 50 < (SELECT AVG(TEMP_F) FROM STATS WHERE STATION.ID = STATS.ID);

© 2002 by Prentice Hall 42 CREATE VIEW METRIC_STATS (ID, MONTH, TEMP_C, RAIN_C) AS SELECT ID, MONTH, (TEMP_F - 32) * 5 /9, RAIN_I * FROM STATS; SELECT * FROM METRIC_STATS; SELECT * FROM METRIC_STATS WHERE TEMP_C < 0 AND MONTH = 1 ORDER BY RAIN_C; UPDATE STATS SET RAIN_I = RAIN_I ; SELECT * FROM STATS; UPDATE STATS SET TEMP_F = 74.9 WHERE ID = 44 AND MONTH = 7;

© 2002 by Prentice Hall 43 SELECT * FROM STATS; COMMIT WORK; UPDATE STATS SET RAIN_I = 4.50 WHERE ID = 44; SELECT * FROM STATS; ROLLBACK WORK; SELECT * FROM STATS; UPDATE STATS SET RAIN_I = 4.50 WHERE ID = 44 AND MONTH = 7; COMMIT WORK; SELECT * FROM STATS;

© 2002 by Prentice Hall 44 DELETE FROM STATS WHERE MONTH = 7 OR ID IN (SELECT ID FROM STATION WHERE LONG_W < 90); DELETE FROM STATION WHERE LONG_W < 90; COMMIT WORK; SELECT * FROM STATION; SELECT * FROM STATS; SELECT * FROM METRIC_STATS;

© 2002 by Prentice Hall 45 CREATE TABLE animals ( id MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(30) NOT NULL, PRIMARY KEY (id) ); INSERT INTO animals (name) VALUES ("dog"),("cat"),("penguin"), ("lax"),("whale"),("ostrich"); SELECT * FROM animals; CREATE TABLE shop ( article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, dealer CHAR(20) DEFAULT '' NOT NULL, price DOUBLE(16,2) DEFAULT '0.00' NOT NULL, PRIMARY KEY(article, dealer)); INSERT INTO shop VALUES (1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),(3,'C',1.69), (3,'D',1.25),(4,'D',19.95); SELECT * FROM shop;

© 2002 by Prentice Hall 46 CREATE TABLE articles ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, title VARCHAR(200), body TEXT, FULLTEXT (title,body) ); INSERT INTO articles VALUES (NULL,'MySQL Tutorial', 'DBMS stands for DataBase...'), (NULL,'How To Use MySQL Efficiently', 'After you went through a...'), (NULL,'Optimizing MySQL','In this tutorial we will show...'), (NULL,'1001 MySQL Tricks','1. Never run mysqld as root '), (NULL,'MySQL vs. YourSQL', 'In the following database comparison...'), (NULL,'MySQL Security', 'When configured properly, MySQL...'); SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('database');

© 2002 by Prentice Hall 47 # What's the highest item number? SELECT MAX(article) AS article FROM shop; # Find number, dealer, and price of the most expensive article. SELECT MAX(price) FROM shop; SELECT article, dealer, price FROM shop WHERE price=19.95; SELECT article, dealer, price FROM shop ORDER BY price DESC LIMIT 1; # What's the highest price per article? SELECT article, MAX(price) AS price FROM shop GROUP BY article;

© 2002 by Prentice Hall 48 CREATE TEMPORARY TABLE tmp ( article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, price DOUBLE(16,2) DEFAULT '0.00' NOT NULL); LOCK TABLES shop READ; INSERT INTO tmp SELECT article, MAX(price) FROM shop GROUP BY article; SELECT shop.article, dealer, shop.price FROM shop, tmp WHERE shop.article=tmp.article AND shop.price=tmp.price; UNLOCK TABLES; DROP TABLE tmp; SELECT article, SUBSTRING( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 7) AS dealer, 0.00+LEFT( MAX( CONCAT(LPAD(price,6,'0'),dealer) ), 6) AS price FROM shop GROUP BY article;

© 2002 by Prentice Hall 49 # find the articles with the highest and lowest price SELECT FROM shop; SELECT * FROM shop WHERE OR # foreign keys CREATE TABLE person ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, name CHAR(60) NOT NULL, PRIMARY KEY (id) ); CREATE TABLE shirt ( id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, style ENUM('t-shirt', 'polo', 'dress') NOT NULL, color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL, owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id), PRIMARY KEY (id) );

© 2002 by Prentice Hall 50 INSERT INTO person VALUES (NULL, 'Antonio Paz'); INSERT INTO shirt VALUES (NULL, 'polo', 'blue', LAST_INSERT_ID()), (NULL, 'dress', 'white', LAST_INSERT_ID()), (NULL, 't-shirt', 'blue', LAST_INSERT_ID()); INSERT INTO person VALUES (NULL, 'Lilliana Angelovska'); INSERT INTO shirt VALUES (NULL, 'dress', 'orange', LAST_INSERT_ID()), (NULL, 'polo', 'red', LAST_INSERT_ID()), (NULL, 'dress', 'blue', LAST_INSERT_ID()), (NULL, 't-shirt', 'white', LAST_INSERT_ID()); SELECT * FROM person; SELECT * FROM shirt;

© 2002 by Prentice Hall 51 SELECT s.* FROM person p, shirt s WHERE p.name LIKE 'Lilliana%' AND s.owner = p.id AND s.color <> 'white'; # unions select id, style from shirt where color = 'blue' union select id, style from shirt where color = 'orange' # visits per day CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL, day INT(2) UNSIGNED ZEROFILL); INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2), (2000,2,23),(2000,2,23); SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1 GROUP BY year,month;