Source: system analysis and design methods, by Jeffrey L Whitten et al.,. McGraw
-Hill/Irwin, ISBN 0072932619. Money are kept by boxes buried in the ground in ...
Lecture 8 SQL (I)
Money are kept by boxes buried in the ground in the backyard. 1
Source: system analysis and design methods, by Jeffrey L Whitten et al., McGraw-Hill/Irwin, ISBN 0072932619
2
Introduction to SQL database management systems (DBMS)
Database vs. Files • File – a collection of similar records.
• Database is the bank for data. • Use SQL language as the interface for storing & retrieving data. • Many database products exist.
– Files are unrelated to each other except in the code of an application program. – Data storage is built around the applications that use the files. – Data are retrieved through file-reading of format only known to applications using the data file.
– Commercial: Oracle Database 12c, IBM db2, Microsoft SQL server, … – Free & Open source: Oracle MySQL, MariaDB, PostgreSQL, SQLite, …
• Database – a collection of interrelated files – Records in one file (or table) are physically related to records in another file (or table). – Applications are built around the integrated database – Data are retrieved through a standardized interface or format
Source: system analysis and design methods, by Jeffrey L Whitten et al., McGraw-Hill/Irwin, ISBN 0072932619
Money are kept in the bank
• One DBMS manages many databases, one database contains many tables, one table contains many rows & columns. 3
4
Assignment #4
Assignment #4
Due: 11/19/2013
Due: 11/19/2013
• This assignment continues the development of your e-business software system. – We have done requirement specification and software estimation • In this assignment, you are required to design and document your system using UML diagrams for software systems and ER diagrams for data modeling. • The ER model is created for discovering business rules. The model then turns into your physical data model, and then becomes the blueprint for implementing the database (tables and fields). • ER model is often further developed into class models.
• Please complete this assignment with software products such as Microsoft Visio (or others, no hand-drawings, pls.). The following diagrams are required for each of the software (2-tier and 3-tier) to be developed: – – – – – –
Use case diagrams (you have done it, hopefully) Class diagrams Deployment diagrams A sequence diagram for online ordering used parts. A sequence diagram for online posting of used parts to be sold. A state diagram for shopping carts in the system.
• ER model should be the same for both 2-tier and 3-tier systems. • Please do both UML diagrams for your core systems, plus two more functions in your high-priority function list.
• UML diagrams are used to design the system. They are then discussed to discover issues before actual implementation. 5
6
1
Assignment #4 Due: 11/19/2013 • Enumerate all business rules from your ER model, and make sure the generated business rules are reasonable, logical, and fulfills your business needs. • The class diagram should focus on online ordering & posting, and it should be somehow extended from E-R model.
SQL (I)
7
8
Basic SQL Operations - Outline • • • •
Introduction to SQL • SQL
Database Creation & Connection Table & Index Creation Changing/Deleting Objects Basic Data Manipulation
– Structured Query Language – an ANSI standard computer language for accessing and manipulating databases. – Retrieve, insert, delete, update data against a database – CRUD – Creation, Retrieval, Updating, and Deletion.
– Data insertion – Data retrieval – Data deletion and updating
• Not a complete language like Java, C++, … – SQL is a sub-language of about 30 statements – Usually embedded in other languages or tool for database access – Portable across operating systems – Somewhat portable among DBMS vendors
• Advanced Select
9
10
Source: MIT 1.264 lecture notes
Table & Index Creation
Database Creation & Connection • Create a new database
• Create a table – CREATE TABLE tableName ( field1 datatype [(length)] [NULL, NOT NULL], field2 datatype [(length)] [NULL, NOT NULL], …); – DESCRIBE tableName; (MySQL)
– CREATE DATABASE databaseName;
• Use a database – USE databaseName;
• Create an index • Show available databases; (MySQL)
– CREATE [UNIQUE] INDEX indexName ON tableName (columnName)
– SHOW DATABASES;
11
12
2
Advanced Table Creation
Index • Why use index?
CREATE TABLE tableName ( field1 datatype [(length)] [NULL, NOT NULL] [PRIMARY KEY] [UNIQUE] [DEFAULT …], [CHECK …] …); – – – –
– Fast data retrieval and sorting – Unique index ensures the values of the indexed column are unique
• Why not use index?
PRIMARY KEY: specify the column is used as a primary key UNIQUE: the values in the column should be unique DEFAULT: specify the default value if not defined CHECK: constrains the value of the column
– Performance penalty on data insertion, deletion, update
• When? – On columns with frequent retrievals – On columns used to join other tables (to be covered) – On columns accessed in sorted orders
• CHECK (title_id LIKE ‘[A-Z][A-Z][0-9][0-9][0-9][0-9]’)
13
14
Changing and Deleting Objects • Change table definition
Data Insertion • Inserting data
– ALTER TABLE tableName [ADD fieldName datatype …][DROP fieldName]
– INSERT INTO tableName [(col1, col2, …)] VALUES (val1 [, val2, …]); – Not all data attributes (field values) need to present – DBMS will use default values for absent data fields
• Delete a table
• Show data in a table
– DROP TABLE tableName
– SELECT * FROM tableName
• Delete a database – DROP DATABASE databaseName
• Demonstration with MS-SQL through SQL Server Management Studio Express.
• Delete an index – DROP INDEX tableName.indexName – DROP INDEX indexName on tableName (MySQL) 15
16
SQL syntax
Reviews insert insert insert insert insert insert
• Free form, case insensitive Create database dbname; Create table newTable ( f1 int primary key, f2 int ); alter table newTable add f3 int; alter table newTable drop column f2; drop table newTable; drop database dbname;
17
into into into into into into
newTable values (1, 3); newTable values (2, 4); newTable(f1) values (3); newTable (f1, f3) values (4, 3); newTable (f3, f1) values (3, 4); newTable (f3) values (5);
18
3
Data Deletion and Updating
Examples • Find the average sale
• Delete data
– SELECT AVG(Amt) FROM Orders;
– DELETE FROM tableName WHERE [criteria]
• Find the average sale for a customer
– DELETE FROM newTable WHERE f1 < 3;
– SELECT AVG(Amt) FROM Orders WHERE Cust = 211;
• Add an office
• Updating data – UPDATE tableName SET fieldName=newValue [, fieldName1=newValue1, …]; (NOTE! This will update ALL records in the table, use WHERE to limit records to be updated.) – UPDATE tableName SET […] WHERE [criteria] – UPDATE newTable SET f3 = 5; – UPDATE newTable SET f3 = f3 * 2 WHERE f1 < 3;
– INSERT INTO Offices (OfficeNbr, City, Region, Target, Sales) VALUES (‘55’, ‘Dallas’, ‘West’, 200000, 0);
• Delete a customer – DELETE FROM Customers WHERE Company = ‘Connor Co’;
• Raise a credit limit – UPDATE CustomersSET CreditLimit = 75000 WHERE Company = ‘AmaratungaEnterprise’;
19
20
1. selection_list
Advanced SELECT
• SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …]
• SELECT is the real heart of SQL.[1] • SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …] [WHERE criteria] [GROUP BY columnName [, …]] [HAVING criteria] [ORDER BY {columnName | select_list_number}] [DESC] • SQL is a free-form language. However, the order of these clauses has to be in the order shown above to be syntax correct.
– selection_list contains column names to be picked. The order of column names can be arranged in any order as you pleased. – selection_list can also be used to specify new table display labels – selection_list can do computations: (),*/,+-, functions – selection_list can also be specified by using tableName.columnName; 21
[1]
22
The Practical SQL Handbook, Judith et al. Addison-Wesley, ISBN 0201447878
2. FROM clause
3. WHERE clause
• SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …] – tableName can be given an alias, which is important when we’re dealing with joins (to be discussed)
• SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …] [WHERE criteria] – Criteria can have • • • • • •
Comparison operators (=, , =, ) Combinations or logical negations (AND, OR, NOT) Lists (IN, NOT IN) Ranges (BETWEEN and NOT BETWEEN) Unknown values (IS NULL and IS NOT NULL) Character matches (LIKE and NOT LIKE) – %: matching any length of characters – _: matching one character
23
24
4
4. ORDER BY clause
5. GROUP BY clause
• SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …] [WHERE criteria] [GROUP BY columnName [, …]] [HAVING critera] [ORDER BY {columnName | select_list_number}] [DESC]
• SELECT [ALL | DISTINCT] selection_list FROM {tableName | viewName} [, …] [GROUP BY columnName [, …]] [HAVING criteria] – Aggregate using the entire table • SUM(exp), AVG(exp), COUNT(exp), COUNT(*), MAX(exp), MIN(exp) • SUM(DISTINCT exp), AVG(DISTINCT exp), COUNT(DISTINCT exp)
– Column name or select_list_number – DESC suggested the sorted list should be in descending order – The list can have multiple column names
– Aggregate with GROUP BY – WHERE criteria that is a pre-filtering criteria to select records before aggregation; while HAVING criteria is post-filtering criteria to select aggregated records after aggregation. 25
26
Examples
Example
Aggregation of the entire table
Grouping • Find the total amount of product purchased by each customer
• Summation of sales amount
– SELECT cust, SUM(amt), COUNT(cust), SUM(qty) FROM orders GROUP BY cust;
– SELECT SUM(amt) AS total FROM orders;
• Count the number of orders • Find the number of distinct products purchased by each customer
– SELECT COUNT(cust) FROM orders;
– SELECT cust, COUNT(DISTINCT prod) FROM orders GROUP BY cust;
• NULLs are not counted! – SELECT COUNT(qty), COUNT(*) FROM orders;
• Find the distinct products purchased by each customer – SELECT cust, prod FROM orders GROUP BY cust, prod;
• Various aggregated functions applied – SELECT MAX(amt), MIN(amt), SUM(amt), AVG(amt) FROM orders WHERE prod LIKE '%CRANE%';
• Find distinct customers of each product purchased – SELECT prod, cust FROM orders GROUP BY prod, cust;
• Find distinct customers – SELECT COUNT(DISTINCT cust), COUNT(cust) FROM orders; 27
Example
28
Microsoft SQL Server Express
Having • Find total amount of purchase of each customer
• Home
– SELECT cust, SUM(amt) AS total FROM orders GROUP BY cust;
– http://www.microsoft.com/sqlserver/en/us/default.aspx
• Find out who is our VIP
• Express version download
– SELECT cust, SUM(amt) AS total FROM orders GROUP BY cust HAVING total > 2000000; (Invalid in T-SQL) – SELECT cust, SUM(amt) AS total FROM orders GROUP BY cust HAVING SUM(amt) > 2000000;
– Microsoft® SQL Server® 2012 Express with Tools – Express with Tools (with LocalDB) Includes the database engine and SQL Server Management Studio Express) – ENU\x64\SQLEXPRWT_x64_ENU.exe (~1GB)
• Compare the following SQL statement – SELECT cust, SUM(amt) AS total FROM orders WHERE amt > 2000000 GROUP BY cust; 29
30
5