GOALScs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote02.pdf · 2018. 5. 22. · Project 02’s...
Transcript of GOALScs.newpaltz.edu/~phamh/sdb/dbs/sdb-ProjectNote02.pdf · 2018. 5. 22. · Project 02’s...
PROJECT Note # 02: “Database Management Systems” by Hanh Pham
GOALS
Most databases in the world are SQL-based DBMS. Using data and managing DBMS efficiently and effectively can help companies save a lot of time and money. It requires a deep knowledge of SQL. In this project students will learn:
how to manipulate data types and tables
how to retrieve and filter data from DBMS using advanced SQL statements with clauses
Advanced MySQL commands: This project continues with the data used in project # 01. Let’s recall that a small company, which sells notebooks to schools, hires you to manage their database. In project 1 we created a table named shop. After a series of changes the table had the following contents: DATA:
STEP-1: CHANGE the data type of a COLUMN in a table: SYNTAX/RULE/FORMAT:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
Example:
ALTER TABLE shop
MODIFY COLUMN article INT(2);
After that the table shop will look like this:
STEP-2: GET/Retrieve particular data using a keyword (with WHERE … = "keyword";) SYNTAX/RULE/FORMAT: SELECT "column1" [,"column2",etc]
FROM "tablename"
[WHERE "condition"];
[] = optional
Example 1: (we like to see did we get from dealer A ?)
SELECT article, price
FROM shop
WHERE dealer ='A';
After that the output of this query (doesn’t mean the contents of the table shop) will look like this:
Example 2: (we like to see the kinds of notebooks which cost between 5-10 and who are their dealers ?)
SELECT article, dealer
FROM shop
WHERE price > 5 AND price < 10;
After that the output of this query (doesn’t mean the contents of the table shop) will look like this:
STEP-3: Make a COPY of a table;
SYNTAX
The syntax for copying all of the columns of a table into another table is:
CREATE TABLE new_table
AS (SELECT * FROM old_table);
Example 1: (make an exact copy or clone a table)
CREATE TABLE NewShop
AS (SELECT * FROM shop);
This would create a new table called NewShop which is an exact copy of the shop table.
Example 2: (CREATE a new table USING a part of an EXISTING TABLE)
CREATE TABLE MiniShop
AS (SELECT * FROM shop
WHERE price < 5);
This would create a new table called MiniShop based on the shop table.
STEP-4: DELETE a table;
SYNTAX
The syntax for the deleting a table is:
DROP TABLE table_name;
table_name is the name of the table to remove from the database.
EXAMPLE
DROP TABLE NewShop;
STEP-5: Filter and sort data from a table using ORDER BY clause
SYNTAX
SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ ASC | DESC ];
EXAMPLE (SORTING IN DESCENDING ORDER)
SELECT dealer
FROM shop
WHERE price > 5
ORDER BY dealer DESC;
STEP-6: Use SQL functions to get MAX, MIN, AVE … based on data from a table
SYNTAX
SELECT MAX(expression)
FROM tables
WHERE conditions;
EXAMPLE
You might wish to know the maximum price for a notebook.
SELECT MAX(price) AS "Most Expensive Notebook"
FROM shop
WHERE dealer = ‘A’;
STEP-7: Use SQL function COUNT to get define the size of data from a table
SYNTAX
The syntax for the SQL COUNT function is:
SELECT COUNT(expression)
FROM tables
WHERE conditions;
EXAMPLE
The simplest way to use the SQL COUNT function would be to return a single field that returns the COUNT of something. For example, you might wish to know how many notebooks cost more than 3.
SELECT COUNT(*) AS "Number of Notebooks"
FROM shop
WHERE price > 3;
STEP-8: Using DISTINCT clause
EXAMPLE
You may want to know the number of unique dealers who sell at least one notebook with price less than 10.
SELECT COUNT(DISTINCT dealer) AS "Unique dealers"
FROM shop
WHERE price < 12;
STEP-9: Using GROUP BY clause
EXAMPLE
You may want to know the number of articles (kinds of notebooks) each dealer supplies ?
SELECT dealer, COUNT(*) AS "Number of articles"
FROM shop
GROUP BY dealer;
STEP-10: Use HAVING clause
The HAVING clause enables you to filter which group results appear in the final results.
Syntax:
The HAVING clause must follow the GROUP BY clause in a query and must also precede the ORDER BY clause if used. The following is the syntax of the SELECT statement, including the HAVING clause:
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
Example:
What if we need to display notebooks sorted/ordered by dealers’ name, but only (filter) for those with price more than 4.
SELECT *
FROM shop
HAVING price > 4
ORDER BY dealer;
This would produce the following result:
Project 02’s ASSIGNMENTS
STEP A: Make sure to continue with the data you’ve got after step-10 in Project 1. Show that data in the beginning of your report again. STEP B: Do steps 1-10 above to manipulate the tables and retrieve data from your database. Write down in your PROJECT REPORT the SQL commands and include the screenshots for every step in the same order and format used above. Your report must have a separate title page with your name and a table of contents pointing to the page # of each step. What to submit ?
Report file in Word, OpenOffice, or PDF format.
Where to submit ?
+ At the submission page of our class WEBSITE
+ ALSO at Blackboard, under “PROJECTS” and click on “Project # 1” and then upload your file
via “Browse My Computer”.
Deadline: see the SCHEDULE on the class WEBSITE