Download presentation
Presentation is loading. Please wait.
1
CS311 Database Management system
Lecture 9 CS311 Database Management system Somchai Thangsathityangkul
2
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.
3
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;
4
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.
5
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’;
6
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’;
7
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;
8
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.
9
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.
10
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;
11
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.
12
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;
13
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.
14
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;
15
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;
16
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;
17
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 );
18
Create a script file Now , save file as c:/pc.sql
In mySQL console type command : source c:/pc.sql
19
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
20
The Entities information should have :
Authors first name , last name, 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
21
Relational Schema:
22
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 );
23
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 ) );
24
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 ) );
25
Insert into Author values( ,'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');
26
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');
27
Insert into book values ( ' ','Sleep Walker', 560 , , '1980',1002, 102,null ), (' ','Harry Potter and the Deathly Hallows',740 , 500 ,'2007',1003,104 , 10 ), (' ','Harry Potter and the Order of the Phoenix',870,500, '2004',1004,104,null ), (' ','Harry Potter and the Prisoner of Azkaban',448,300, '1999',1003,104,15 ), (' ','Just After Sunset',384, 300, '2008',1005,102,2), (' ','The Living Dead',490, 500, '2008', 1006,102,8), (' ','MySQL Cookbook' , 975,850 ,'2007',1002,103,3), (' ','Star Wars :Luke Skywalker and the Shadows of Mindor',387,300, '2008',1007,101 ,14);
28
Insert into book_author
values ( ' ', ), (' ',200008), (' ',200008), (' ',200008), (' ',200004), (' ', ), (' ',200009),(' ',200006), (' ',200010);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.