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: Example: ALTER TABLE table_name MODIFY COLUMN column_name datatype; 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: [] = optional SELECT "column1" [,"column2",etc] FROM "tablename" [WHERE "condition"]; Example 1: (we like to see did we get from dealer A?) SELECT article, price 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 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 * ); 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 * WHERE price < 5); This would create a new table called MiniShop based on the shop table.
STEP-4: DELETE a table; SYNTAX 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 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" WHERE dealer = A ; STEP-7: Use SQL function COUNT to get define the size of data from a table SYNTAX 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" 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" 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" 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 * 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 as well as re-describe the business situation. STEP B: Do steps 1-10 in the Tutorial # 02 above using your own data from Project 1. Do all examples in each step, you may need to use different values for a clause(since your data is different) but you should keep the same structure of the clause. For example, in the tutorial we have WHERE price > 5 AND price < 10 but if you have no data in that range, then change it into this WHERE price > 30 AND price < 90. So that it would NOT return nothing (i.e. must return something). Write down in your PROJECT REPORT the SQL commands and include the screenshots for every step in the same order and format used above. IF the SQL command changes the contents of the table THEN make sure to show the table contents BEFORE and AFTER (using SQL select). Your report must have a separate title page with your name and a table of contents pointing to the page # of each step. Please read report SAMPLE, use report TEMPLATE and watch project VIDEOs (all posted on our WEBSITE) for more details. What to submit? Report file in Word, OpenOffice, or PDF format. Where to submit? At Blackboard, under PROJECTS and click on Project # 2 and then upload your file via Browse My Computer. Deadline: June 28 th (hard deadline, late work will not be graded! )