Date post: | 05-Apr-2018 |
Category: |
Documents |
Upload: | keziavalenni |
View: | 218 times |
Download: | 0 times |
of 29
8/2/2019 12_Basic_SQL
1/29
Basic SQL
ITCS 201 Web Programming Part II
8/2/2019 12_Basic_SQL
2/29
Page 2
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Outline for Today
What is SQL?
SQL Category
Basic DML Commands
INSERT
UPDATE
DELETE
SELECT
8/2/2019 12_Basic_SQL
3/29
Page 3
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
What is SQL?
SQL Structured Query Language
Language that defines commands for user to
Create database and table structure
Basic data management
Query useful information
Not case-sensitive
Non procedural language : users specify what must be done, NOT
how it is to be done
8/2/2019 12_Basic_SQL
4/29
Page 4
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SQL Categories
Two broad type1. Data Definition Language (DDL) NOT Study in this course
Commands used to create database objects including
Database
Table/Attributes
Keys etc.
2. Data Manipulation Language (DML)
Commands used to insert, update, delete, and retrieve datawithin tables.
8/2/2019 12_Basic_SQL
5/29
Page 5 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Basic DML Commands
Commands DescriptionINSERT Insert row(s) into a table
UPDATE Modify an attributes value in one or more tables rows
DELETE Delete one or more rows from a table
SELECTSelect attributes from rows in one or more tables
WHERE Restrict the selection of rows based on condition expression
GROUP BY Group the selected rows based on one or more attributes
HAVING Restrict the selection of group rows based on a condition
ORDER BY Order the selected rows based on one or more attributes
8/2/2019 12_Basic_SQL
6/29
Page 6 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Commands Description
Comparison Operators
=,,, Used in conditional expression
Logical Operators
AND/OR/NOT Used in conditional expression
Special Operators
IS NULL Check whether an attribute value is null
LIKE Check whether an attribute value matches a given string patternDISTINCT Limit values to unique values
Aggregate Functions
COUNT Returns the number of rows with non-null values for a givencolumn
MIN Return the minimum attribute value found in a given columnMAX Return the maximum attribute value found in a given column
SUM Return the sum of all values for a given column
AVG Return the average of all values for a given column
8/2/2019 12_Basic_SQL
7/29
Page 7 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
INSERT Command
Use INSERT command to enter data into a tableBasic syntax:
Note: Be careful with data type and allow-null attribute
INSERT INTO tablenameVALUES( value1, value2, , valueN)
INSERT INTO computers VALUES( '37', Lenovo Y550');
8/2/2019 12_Basic_SQL
8/29
Page 8 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
INSERT Command (cont.)
Special case of INSERT1. Insert rows with null attributes
If we do not know some values of attribute, we have to leavethem as null
To enter a null, use following syntax
Note: You can insert NULL only the allow-null attribute
INSERT INTO tablenameVALUES(value1,NULL,,valueN)
INSERT INTO inventoryVALUES( '57', '2010-04-23', 37', 2', NULL);
8/2/2019 12_Basic_SQL
9/29
Page 9 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
INSERT Command (cont.)
2. Insert rows with only required attributes Rather than declaring each attribute as NULL, indicate just
only the required value
To insert only some require attribute (e.g. col1, col2, ,
colN), use following syntax
INSERT INTO tablename(col1,col2,...,colN)VALUES(value1,value2,,valueN)
INSERT INTO inventory(inventoryID, dateAcquired, computerID, employeeID)
VALUES( '57', '2010-04-23', 37', 2');
8/2/2019 12_Basic_SQL
10/29
Page 10 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
UPDATE Command
Use UPDATE command to modify data in a tableBasic syntax:
Note: Be careful about WHERE clause, there could be a hugeproblem if you forget to insert WHERE condition
UPDATE tablenameSET col1 = expression1,
col2 = expression2,
colN = expressionN[WHERE conditionList]
UPDATE inventorySET dateAcquired= 2010-08-22,comments = go go go
WHERE inventoryID = 57;
8/2/2019 12_Basic_SQL
11/29
Page 11 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
DELETE Command
Use DELETE command to delete a table rowBasic syntax:
Note: Be careful about WHERE clause, there could be a hugeproblem if you forget to insert WHERE condition
DELETE FROMtablename[WHERE conditionList]
DELETE FROMinventory
WHERE inventoryID = 57;
8/2/2019 12_Basic_SQL
12/29
Page 12 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command
Use SELECT command to query partial table contents by placingconditions we want specified in WHERE clause (optional)
Basic Syntax:
The columnList specifies the selected attribute we want, if we wantall attributes in that table, simply use *
SELECT columnList
FROMtablename[ WHERE conditionList ]
SELECT *FROM employee
SELECT employeeID, firstNameFROM employee
8/2/2019 12_Basic_SQL
13/29
Page 13 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command (cont.)
Using a conditional expression in SELECT command
SELECT *FROM employees
WHERE lastName = 'Mouse'
SELECT *FROM employees
WHERE employeesID = 3
8/2/2019 12_Basic_SQL
14/29
Page 14 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Logical Operators
Use Logical Operatorsin SELECT command to connect multipleconditional expressions
AND / OR / NOT
SELECT *
FROM employeesWHERE lastName = 'Mouse'AND firstName = 'Mickey';
SELECT *FROM employees
WHERE (lastName = 'Mouse'ORlastName = 'Jetson')AND NOT(
firstName ='Mickey');
8/2/2019 12_Basic_SQL
15/29
Page 15 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Comparison Operators
Use Comparison Operatorsin SELECT command =,,,
SELECT *FROM employees
WHERE employeeID >10
SELECT *FROM employees
WHERE employeeID >10
AND lastName Jetson
8/2/2019 12_Basic_SQL
16/29
Page 16 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Special Operators: IS NULL
Use Special Operatorsin SELECT command1. IS NULL Operator
Use to check for a null attribute value
SELECT inventoryID, commentsFROM inventory
WHERE comments IS NULL
SELECT inventoryID, comments
FROM inventoryWHERE inventoryID >50AND comments IS NOT NULL
8/2/2019 12_Basic_SQL
17/29
Page 17 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Special Operators: LIKE
2. LIKE Operator Use with wildcards to find pattern within string attribute
% means any and all following character are eligibleP% include Paul, Peter, PatPa% include Paul, Pat, Parrot
_ means any one character may be substituted13_11 include 13A11, 13411_a_ include Rat, Van , Tap
8/2/2019 12_Basic_SQL
18/29
Page 18 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Special Operators: LIKE
2. LIKE Operator (cont)SELECT *FROM `computers`
WHERE computerDescriptionLIKE 'DELL%'
SELECT *FROM `employees`
WHERE lastName LIKE '_o_'
8/2/2019 12_Basic_SQL
19/29
Page 19 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Special Operators: DISTINCT
3. DISTINCT Operator Use DISTINCT operator to produce a list of unique attribute
values
SELECT DISTINCT computerIDFROM inventory
8/2/2019 12_Basic_SQL
20/29
Page 20 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Aggregate Function: COUNT
Use Aggregate Functionsto perform mathematical summary inSELECT command
1. COUNT Function
Use COUNT function to count the number of non-null values
of an attributeSELECT COUNT( * )FROM inventory
WHERE computerID =1
SELECT COUNT( comments )FROM inventory
WHERE 1
(there are totally 26 rows)
8/2/2019 12_Basic_SQL
21/29
Page 21 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Aggregate Function: MIN/ MAX
2. MIN/MAX Function Use MIN function to find the lowest value of a given attribute
Use MAX function to find the highest value of a givenattribute
SELECT MIN( computerID )FROM computers
SELECT MAX( computerID )FROM computers
8/2/2019 12_Basic_SQL
22/29
Page 22 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Aggregate Function: SUM
3. SUM Function Use SUM function to compute a total sum for any specific
attribute
SELECT SUM(computerID)FROM inventory
8/2/2019 12_Basic_SQL
23/29
8/2/2019 12_Basic_SQL
24/29
Page 24 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command GROUP BY
Use GROUP BY when you have other attribute columns combinedwith aggregate functions in SELECT command
Basic Syntax:
SELECT columnList
FROMtablename[ WHERE conditionList ][ GROUP BY columnList ]
SELECT employeeID,
COUNT( computerID )FROM `inventory`GROUP BY employeeID
8/2/2019 12_Basic_SQL
25/29
Page 25 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command -- HAVING
Use HAVING clause when you want to specify condition ofGROUP BY clause in SELECT command
Basic Syntax:
SELECT columnListFROMtablename[ WHERE conditionList ][ GROUP BY columnList ][ HAVING conditionList ]
SELECT employeeID,
COUNT( computerID )FROM `inventory`GROUP BY employeeIDHAVING count(computerID)>1
8/2/2019 12_Basic_SQL
26/29
Page 26 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command ORDER BY
Use ORDER BY in SELECT command to sort values in a givenattribute
Basic Syntax:
SELECT columnList
FROMtablename[ WHERE conditionList ][ GROUP BY columnList ][ HAVING conditionList ][ ORDER BY columnList [ASC | DESC]]
8/2/2019 12_Basic_SQL
27/29
Page 27 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command ORDER BY
SELECT inventoryID,
employeeID,comments
FROM `inventory`WHERE inventoryID >40ORDER BY employeeID
SELECT inventoryID,
employeeID,comments
FROM `inventory`WHERE inventoryID >40ORDER BY employeeID DESC
8/2/2019 12_Basic_SQL
28/29
Page 28 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Joining Tables
Perform when data are retrieved from more than one tables
Normally compose of an equality comparison (=) of FK and PK ofrelated tables.
SELECT *FROMinventory, computers
WHERE inventoryID >40AND inventory.computerID = computers.computerID
8/2/2019 12_Basic_SQL
29/29
Page 29 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command Join Tables : Alias
Use Aliasto identify the source table from which data are takenwhen join is performed
SELECT e.firstName, e.lastName,COUNT( * )AS 'number of computer'
FROM inventoryAS i, employeesAS eWHERE e.employeeID = i.employeeIDGROUP BY e.firstName, e.lastNameHAVING COUNT( computerID ) >1ORDER BY COUNT( * ) DESC