Fundamentals, Design, and Implementation, 9/e Chapter 7 Relational Algebra and SQL applications Instructor: Dragomir R. Radev Fall 2005.

Slides:



Advertisements
Similar presentations
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 11-1 David M. Kroenke’s Chapter Eleven: Managing Databases with SQL Server.
Advertisements

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 9-1 COS 346 Day 20.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Ten: Managing Databases with SQL Server 2008.
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
How to Use MySQL CS430 March 18, SQL: “Structured Query Language”—the most common standardized language used to access databases. SQL has several.
© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2004 Dragomir R. Radev.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 8 Foundations of Relational Implementation.
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
Concepts of Database Management Sixth Edition
Introduction to Structured Query Language (SQL)
© 2002 by Prentice Hall 1 SI 654 Database Application Design Winter 2003 Dragomir R. Radev.
© 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)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
© 2002 by Prentice Hall 1 David M. Kroenke Database Processing Eighth Edition Chapter 13 Managing Databases with SQL Server 2000.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.
Database Technical Session By: Prof. Adarsh Patel.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
CSC 2720 Building Web Applications Database and SQL.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Design and Management CPTG /23/2015Chapter 12 of 38 Functions of a Database Store data Store data School: student records, class schedules,
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 8 Foundations of Relational Implementation David M. Kroenke Database Processing © 2000 Prentice Hall.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 IT420: Database Management and Organization SQL Views, Triggers and Stored Procedures 17 February 2006 Adina Crăiniceanu
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
David M. Kroenke and David J. Auer Database Processing: F undamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
SQL has several parts: Major ones: DDL – Data Definition Language {Defining, Deleting, Modifying relation schemas} DML – Data Manipulation Language {Inserting,
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
DBMS 3. course. Reminder Data independence: logical and physical Concurrent processing – Transaction – Deadlock – Rollback – Logging ER Diagrams.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Seven: SQL for Database Construction and Application.
CH 10 DB Application Design n 10.1 Functions n 10.2 Case Application n 10.3 Creating, Reading, Updating, Deleting View n 10.4 Form Design n 10.5 Report.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Chapter Seven: SQL for Database Construction and Application Processing.
SQL Query Getting to the data ……..
Database Systems: Design, Implementation, and Management Tenth Edition
SQL for Database Construction and Application Processing
Ch7. SQL for DB construction and Application Processing 데이터베이스시스템 (ND352) 교수 홍 기 형.
Database Processing: David M. Kroenke’s Chapter Seven:
Database Processing: David M. Kroenke’s Chapter Seven:
Chapter 7 Using SQL in Applications
Contents Preface I Introduction Lesson Objectives I-2
Chapter 7 Using SQL in Applications
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
Chapter 11 Managing Databases with SQL Server 2000
Presentation transcript:

Fundamentals, Design, and Implementation, 9/e Chapter 7 Relational Algebra and SQL applications Instructor: Dragomir R. Radev Fall 2005

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/2 Copyright © 2004 Review Relational Model Terminology  Relation is a two-dimensional table  Attributes are single valued  Each attribute belongs to a domain –A domain is a physical and logical description of permittable values  No two rows are identical  Order is unimportant  The row is called a tuple

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/3 Copyright © 2004 Relational Algebra  Relational algebra defines a set of operators that may work on relations.  Recall that relations are simply data sets. As such, relational algebra deals with set theory.  The operators in relational algebra are very similar to traditional algebra except that they apply to sets.

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/4 Copyright © 2004 Relational Algebra Operators  Relational algebra provides several operators: –Union –Difference –Intersection –Product –Projection –Selection –Join

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/5 Copyright © 2004 Union Operator  The union operator adds tuples from one relation to another relation  A union operation will result in combined relation  This is similar to the logical operator ‘OR’

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/6 Copyright © 2004 Union Operator JUNIOR and HONOR-STUDENT relations and their union: (a)Example of JUNIOR relation (b)Example HONOR- STUDENT relation (c)Union of JUNIOR and HONOR- STUDENT relations

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/7 Copyright © 2004 Difference Operator  The difference operator produces a third relation that contains the tuples that appear in the first relation, but not the second  This is similar to a subtraction

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/8 Copyright © 2004 Difference Operator JUNIOR relation HONOR- STUDENT relation JUNIOR minus HONOR- STUDENT relation

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/9 Copyright © 2004 Intersection Operator  An intersection operation will produce a third relation that contains the tuples that are common to the relations involved.  This is similar to the logical operator ‘AND’

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/10 Copyright © 2004 Intersection Operator JUNIOR relation HONOR- STUDENT relation Intersection of JUNIOR and HONOR- STUDENT relations

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/11 Copyright © 2004 Product Operator  A product operator is a concatenation of every tuple in one relation with every tuple in a second relation  The resulting relation will have n x m tuples, where… n = the number of tuples in the first relation and m = the number of tuples in the second relation  This is similar to multiplication

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/12 Copyright © 2004 Projection Operator  A projection operation produces a second relation that is a subset of the first.  The subset is in terms of columns, not tuples  The resulting relation will contain a limited number of columns. However, every tuple will be listed.

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/13 Copyright © 2004 Selection Operator  The selection operator is similar to the projection operator. It produces a second relation that is a subset of the first.  However, the selection operator produces a subset of tuples, not columns.  The resulting relation contains all columns, but only contains a portion of the tuples.

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/14 Copyright © 2004 Join Operator  The join operator is a combination of the product, selection, and projection operators. There are several variations of the join operator… –Equijoin –Natural join –Outer join Left outer join Right outer join

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/15 Copyright © 2004 Data for Join Examples SIDNameMajorGradeLevel 123JonesHistoryJR 158ParksMathGR 271SmithHistoryJR 105AndersonManagementSN StudentNumberClassNamePositionNumber 123H BA B4907

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/16 Copyright © 2004 Join Examples Equijoin Natural Join Left Outer Join

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/17 Copyright © 2004 Expressing Queries in Relational Algebra 1. What are the names of all students? STUDENT [Name] 2. What are the student numbers of all students enrolled in a class? ENROLLMENT [StudentNumber]

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/18 Copyright © 2004 Expressing Queries in Relational Algebra 3. What are the student numbers of all students not enrolled in a class? STUDENT [SID] – ENROLLMENT [StudentNumber] 4. What are the numbers of students enrolled in the class ‘BD445’? ENROLLMENT WHERE ClassName = ‘BD445’[StudentNumber]

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/19 Copyright © 2004 Expressing Queries in Relational Algebra 5. What are the names of the students enrolled in class ‘BD445’? STUDENT JOIN (SID = StudentNumber) ENROLLMENT WHERE ClassName = ‘BD445’[STUDENT.Name]

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/20 Copyright © 2004 Expressing Queries in Relational Algebra 6. What are the names and meeting times of ‘PARKS’ classes? STUDENT WHERE Name = ‘PARKS’ JOIN (SID=StudentNumber) ENROLLMENT JOIN (ClassName = Name) CLASS [CLASS.Name, Time]

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/21 Copyright © 2004 Expressing Queries in Relational Algebra 7. What are the grade levels and meeting rooms of all students, including students not enrolled in a class? STUDENT LEFT OUTER JOIN (SID = StudentNumber) ENROLLMENT JOIN (ClassName = Name) CLASS [GradeLevel, Room]

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/22 Copyright © 2004 Summary of Relational Algebra Operators

Fundamentals, Design, and Implementation, 9/e Using SQL in Applications Instructor: Dragomir R. Radev Winter 2005

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/24 Copyright © 2004 View Ridge Gallery  View Ridge Gallery is a small art gallery that has been in business for 30 years  It sells contemporary European and North American fine art  View Ridge has one owner, three salespeople, and two workers  View Ridge owns all of the art that it sells; it holds no items on a consignment basis

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/25 Copyright © 2004 Application Requirements  View Ridge application requirements –Track customers and their artist interests –Record gallery's purchases –Record customers' art purchases –List the artists and works that have appeared in the gallery –Report how fast an artist's works have sold and at what margin –Show current inventory in a Web page

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/26 Copyright © 2004 View Ridge Data Model

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/27 Copyright © 2004 View Ridge Data Model  Problems: the keys for WORK and TRANSACTION are huge and the key for CUSTOMER is doubtful as many customers may not have an address

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/28 Copyright © 2004 Surrogate Key Database Design

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/29 Copyright © 2004 Sample Values

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/30 Copyright © 2004 Sample Values

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/31 Copyright © 2004 Sample Values

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/32 Copyright © 2004 Sample Values

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/33 Copyright © 2004 Sample Values

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/34 Copyright © 2004 CHECK CONSTRAINT  CHECK CONSTRAINT defines limits for column values  Two common uses –Specifying a range of allowed values –Specifying an enumerated list  CHECK constraints may be used –To compare the value of one column to another –To specify the format of column values –With subqueries

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/35 Copyright © 2004 SQL Views  SQL view is a virtual table that is constructed from other tables or views  It has no data of its own, but obtains data from tables or other views  SELECT statements are used to define views –A view definition may not include an ORDER BY clause  SQL views are a subset of the external views –They can be used only for external views that involve one multi-valued path through the schema

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/36 Copyright © 2004 SQL Views  Views may be used to –Hide columns or rows –Show the results of computed columns –Hide complicated SQL statements –Provide a level of indirection between application programs and tables –Assign different sets of processing permissions to tables –Assign different sets of triggers

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/37 Copyright © 2004 Example: CREATE VIEW CREATE VIEW CustomerNameView AS SELECT Name AS CustomerName FROM CUSTOMER; SELECT * FROM CustomerNameView ORDER BY CustomerName;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/38 Copyright © 2004 Updating Views  Views may or may not be updatable  Rules for updating views are both complicated and DBMS-specific

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/39 Copyright © 2004 Updating Views  Guidelines:

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/40 Copyright © 2004 Embedding SQL In Program Code  SQL can be embedded in triggers, stored procedures, and program code  Problem: assigning SQL table columns with program variables  Solution: object-oriented programming, PL/SQL  Problem: paradigm mismatch between SQL and application programming language –SQL statements return sets of rows; an applications work on one row at a time  Solution: process the SQL results as pseudo-files

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/41 Copyright © 2004 Triggers  A trigger is a stored program that is executed by the DBMS whenever a specified event occurs on a specified table or view  Three trigger types: BEFORE, INSTEAD OF, and AFTER –Each type can be declared for Insert, Update, and Delete –Resulting in a total of nine trigger types  Oracle supports all nine trigger types  SQL Server supports six trigger types (only for INSTEAD OF and AFTER triggers)

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/42 Copyright © 2004 Firing Triggers  When a trigger is fired, the DBMS supplies –Old and new values for the update –New values for inserts –Old values for deletions  The way the values are supplied depends on the DBMS product  Trigger applications: –Checking validity (Figure 7-14) –Providing default values (Figure 7-15) –Updating views (Figure 7-16) –Enforcing referential integrity actions (Figure 7-17, 7-18)

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/43 Copyright © 2004 Stored Procedures  A stored procedure is a program that is stored within the database and is compiled when used –In Oracle, it can be written in PL/SQL or Java –In SQL Server, it can be written in TRANSACT-SQL  Stored procedures can receive input parameters and they can return results  Stored procedures can be called from –Programs written in standard languages, e.g., Java, C# –Scripting languages, e.g., JavaScript, VBScript –SQL command prompt, e.g., SQL Plus, Query Analyzer

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/44 Copyright © 2004 Stored Procedure Advantages  Greater security as store procedures are always stored on the database server  Decreased network traffic  SQL can be optimized by the DBMS compiler  Code sharing resulting in –Less work –Standardized processing –Specialization among developers

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/45 Copyright © 2004 Using SQL In Application Code  SQL can be embedded in application programs  Several SQL statements need to be executed to populate an external view  The application program causes the statements to be executed and then displays the results of the query in the form’s grid controls

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/46 Copyright © 2004 Using SQL In Application Code (cont.)  The application program also processes and coordinates user actions on a form, including –Populating a drop-down list box –Making the appropriate changes to foreign keys to create record relationships  The particulars by which SQL code is inserted into applications depend on the language and data-manipulation methodology used

Fundamentals, Design, and Implementation, 9/e MySQL Chapters 1 and 3 Introduction to MySQL Instructor: Dragomir R. Radev Winter 2005

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/48 Copyright © 2004 Overview –TcX - Michael Widenius (MySQL) –Hughes - David Hughes (mSQL) –Features: Mostly ANSI SQL2 compliant Transactions Stored procedures Auto_increment fields

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/49 Copyright © 2004 More features  Cross-database joins  Outer joins  API: C/C++, Eiffel, Java, PHP, Perl, Python, TCL  Runs on Windows, UNIX, and Mac  High performance

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/50 Copyright © 2004 SQL syntax  CREATE TABLE people (name CHAR(10))  INSERT INTO people VALUES (‘Joe’)  SELECT name FROM people WHERE name like ‘J%’

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/51 Copyright © 2004 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’)

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/52 Copyright © 2004 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)

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/53 Copyright © 2004 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’

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/54 Copyright © 2004 Loading text files  Comma-separated files (*.csv)  LOAD DATA LOCAL INFILE "whatever.csv" INTO TABLE tname

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/55 Copyright © 2004 Aggregate queries  SELECT position FROM people GROUP by position  SELECT position, AVG (salary) FROM people GROUP BY position HAVING AVG (salary) >

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/56 Copyright © 2004 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’);

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/57 Copyright © 2004 Advanced features  Transactions  Table locking  Functions  Unions  Outer joins

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/58 Copyright © 2004 Installing MySQL on Windows  /  utorials/sql/index.php3

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/59 Copyright © 2004 Useful pointers  Small example: mples.htm  MySQL documentation:  (official) MySQL tutorial:  Online, interactive tutorials:

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/60 Copyright © 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/61 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/62 Copyright © 2004 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);

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/63 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/64 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/65 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/66 Copyright © 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/67 Copyright © 2004 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');

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/68 Copyright © 2004 # 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/69 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/70 Copyright © 2004 # 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) );

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/71 Copyright © 2004 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;

Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 7/72 Copyright © 2004 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;