CS311 Database Management system

Slides:



Advertisements
Similar presentations
Aggregate Functions Presenter: Sushma Bandekar CS 157(A) – Fall 2006.
Advertisements

CSC271 Database Systems Lecture # 11.
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
Data Warehousing/Mining 1 Data Warehousing/Mining Comp 150 Aggregation in SQL (not in book) Instructor: Dan Hebert.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
10/15/2012ISC239 Isabelle Bichindaritz1 SQL Queries.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Introduction to Internet Databases MySQL Database System Database Systems.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 7 SQL: Data Manipulation Chapter#6 in the text book Pearson Education © 2009.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Chapter 7 Introduction to SQL(Wk11_12) © Pearson Education Limited 1995, 2005.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
Chapter 5 SQL: Data Manipulation. 2 Chapter - Objectives u Purpose and importance of SQL. u How to retrieve data from database using SELECT and: –Use.
C HAPTER 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
Chapter Name SQL: Data Definition
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Chapter 5 Relational Algebra Pearson Education © 2014.
CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,
SQL : Data Manipulation Pertemuan 09 s/d 12 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Chapter 6 SQL – Data Manipulation Pearson Education © 2014.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
Teacher Workshop Database Design Pearson Education © 2014.
Chapter 11 SQL: Data Manipulation
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Tahun : <<2007>> Versi : <<2/1>>
Chapter Name SQL: Data Manipulation
CS 3630 Database Design and Implementation
Chapter Name SQL: Data Manipulation
Working with Tables: Join, Functions and Grouping
SQL – Data Manipulation
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Minggu 5, Pertemuan 9 SQL: Data Definition
Relational Algebra and Relational Calculus
SQL Pertemuan 6 9/17/2018 Sistem Basis Data.
Introduction to SQL Chapter 3.
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
SQL-2 Week 9-11.
© Pearson Education Limited, 2004
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
SQL – Data Manipulation
Chapter Name SQL: Data Manipulation
The Relational Algebra
Chapter Name SQL: Data Manipulation Transparencies JOINS
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation Transparencies
Database Management system
Chapter Name SQL: Data Manipulation
Presentation transcript:

CS311 Database Management system Lecture 9 CS311 Database Management system Somchai Thangsathityangkul

Aggregates ISO standard defines five aggregate functions: COUNT returns number of values in specified column. SUM returns sum of values in specified column. AVG returns average of values in specified column. MIN returns smallest value in specified column. MAX returns largest value in specified column.

Use of COUNT(*) SELECT COUNT(*) AS myCount FROM PropertyForRent How many properties cost more than £350 per month to rent? SELECT COUNT(*) AS myCount FROM PropertyForRent WHERE rent > 350;

Use of COUNT(column) If we use count(*) , this command will include null value. If we use count(column name) , this will not include null value.

Use of COUNT(DISTINCT) How many different properties viewed in May ‘04? SELECT COUNT(DISTINCT propertyNo) AS myCount FROM Viewing WHERE viewDate BETWEEN ‘1-May-04’ AND ‘31-May-04’;

Use of COUNT and SUM Find number of Managers and sum of their salaries. SELECT COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff WHERE position = ‘Manager’;

Use of MIN, MAX, AVG SELECT MIN(salary) AS myMin, Find minimum, maximum, and average staff salary. SELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvg FROM Staff;

Grouping Use GROUP BY clause to get sub-totals. SELECT and GROUP BY closely integrated: each item in SELECT list must be single- valued per group, and SELECT clause may only contain: column names aggregate functions constants expression involving combinations of the above.

Grouping All column names in SELECT list must appear in GROUP BY clause unless name is used only in an aggregate function. If WHERE is used with GROUP BY, WHERE is applied first, then groups are formed from remaining rows satisfying predicate.

Use of GROUP BY Find number of staff in each branch and their total salaries. SELECT branchNo,COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff GROUP BY branchNo ORDER BY branchNo;

Restricted Groupings – HAVING clause HAVING clause is designed for use with GROUP BY to restrict groups that appear in final result table. Similar to WHERE, but WHERE filters individual rows whereas HAVING filters groups. Column names in HAVING clause must also appear in the GROUP BY list or be contained within an aggregate function.

Use of HAVING For each branch with more than 1 member of staff, find number of staff in each branch and sum of their salaries. SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff GROUP BY branchNo HAVING COUNT(staffNo) > 1 ORDER BY branchNo;

Multi-Table Queries If result columns come from more than one table must use a join. To perform join, include more than one table in FROM clause. Use comma as separator and typically include WHERE clause to specify join column(s). Also possible to use an alias for a table named in FROM clause. Alias is separated from table name with a space. Alias can be used to qualify column names when there is ambiguity.

Simple Join List names of all clients who have viewed a property along with any comment supplied. SELECT c.clientNo, fName, lName, propertyNo, comment FROM Client c, Viewing v WHERE c.clientNo = v.clientNo;

Three Table Join For each branch, list staff who manage properties, including city in which branch is located and properties they manage. SELECT b.branchNo, b.city, s.staffNo, fName, lName, propertyNo FROM Branch b, Staff s, PropertyForRent p WHERE b.branchNo = s.branchNo AND s.staffNo = p.staffNo ORDER BY b.branchNo, s.staffNo, propertyNo;

Multiple Grouping Columns Find number of properties handled by each staff member. SELECT s.branchNo, s.staffNo,COUNT(*) AS myCount FROM Staff s, PropertyForRent p WHERE s.staffNo = p.staffNo GROUP BY s.branchNo, s.staffNo ORDER BY s.branchNo, s.staffNo;

Create a script file After start text editor such as Notepad, we can type any command and save file as .sql Let do the create command. Create table parent ( p_id tinyint auto_increment primary key, p_name varchar(25) ) ; Create table child ( c_id tinyint auto_increment primary key, c_name varchar(20) not null, p_id tinyint not null );

Create a script file Now , save file as c:/pc.sql In mySQL console type command : source c:/pc.sql

Database Design with ER model Book Collection The following data model is designed to hold information relating to a personal book collection. The Entities required should include: Authors Books Book Categories Publishers

The Entities information should have : Authors first name , last name, email Book title, publisher, year published, pages, price, isbn, category description The Entities are related as follows: An Author can write many Books A Book can have many Authors A Book Category can have many Books A Book can have one Categories associated with it A Publisher can publish many Books

Relational Schema:

Drop table If Exists book_author, book, publisher, category, author ; Create table publisher ( pub_ID int auto_increment primary key, pubName varchar(30) not null ); Create table category ( cate_ID int auto_increment primary key, description text not null ); Create table author ( authorID int auto_increment primary key, firstName varchar(20) not null, lastName varchar(30) not null );

Create table book ( ISBN char(13) primary key, Title varchar(100) not null, Pages int , Price decimal(6,2), Yr_published varchar(4), Pub_ID int , Cate_ID int, Num_book smallint not null default 0, Foreign key ( Pub_ID ) references Publisher( Pub_ID ), Foreign key ( Cate_ID ) references Category( Cate_ID ) );

Create table book_author ( ISBN char(13), AuthorID int, Primary key ( ISBN,AuthorID ) , Foreign key ( ISBN ) references book( ISBN ), Foreign key ( AuthorID ) references Author( AuthorID ) );

Insert into Author values( 200001,'Donald','Trump' ), ( null,'James','Blunt' ), ( null, 'Jenny','Lee' ), ( null, 'Stephen', 'King' ), ( null, 'Sunny','Be cool'), ( null, 'Stefan', 'Hinz'), ( null, 'Tom', 'Brady'), ( null, 'Joanne', 'Rowling' ),(null,'Paul','DuBois'), ( null, 'Matthew' ,'Stover'),(null,'Stephen','Hawking'),(null,'Paul','Simon');

Insert into Publisher values ( 1000,'Thomson' ), ( null, 'Morgan Kaufmann' ), ( null, 'O Reilly Media, Inc.' ), ( null, 'Arthur A. Levine Books' ), ( null, 'Scholastic Paperbacks' ),( null, 'Scribner' ),( null, 'Night Shade Books' ), ( null, 'Del Rey' ),( null, 'Wrox'); Insert into Category values ( 100, 'Comics') , ( null, 'Sci Fi' ), ( null, 'Mystery & Thrillers'), ( null, 'Computer' ), ( null, 'Fantasy' ), ( null, 'Sports' ), (null, 'Science');

Insert into book values ( '9151000001111','Sleep Walker', 560 , 300.00 , '1980',1002, 102,null ), ('9780545010221','Harry Potter and the Deathly Hallows',740 , 500 ,'2007',1003,104 , 10 ), ('9780439358071','Harry Potter and the Order of the Phoenix',870,500, '2004',1004,104,null ), ('9780439136358','Harry Potter and the Prisoner of Azkaban',448,300, '1999',1003,104,15 ), ('9781416584087','Just After Sunset',384, 300, '2008',1005,102,2), ('9781597801430','The Living Dead',490, 500, '2008', 1006,102,8), ('9780596527082','MySQL Cookbook' , 975,850 ,'2007',1002,103,3), ('9780345477446','Star Wars :Luke Skywalker and the Shadows of Mindor',387,300, '2008',1007,101 ,14);

Insert into book_author values ( '9151000001111', 200004), ('9780545010221',200008), ('9780439358071',200008), ('9780439136358',200008), ('9781416584087',200004), ('9781597801430',200004 ), ('9780596527082',200009),('9780596527082',200006), ('9780345477446',200010);