Basic SQL: ITCS 201 Web Programming Part II
Basic SQL: ITCS 201 Web Programming Part II
Basic SQL: ITCS 201 Web Programming Part II
INSERT
UPDATE DELETE SELECT
Page 2
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
Page 3
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SQL Categories
Two broad type
1. Data Definition Language (DDL) NOT Study in this course
Page 4
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Description
Insert row(s) into a table
Modify an attributes value in one or more tables rows Delete one or more rows from a table Select attributes from rows in one or more tables
WHERE
GROUP BY HAVING ORDER BY
Page 5
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Commands
Comparison Operators =,<,>,<=,=>,<> Logical Operators AND/OR/NOT Special Operators IS NULL LIKE
Description
Used in conditional expression
Check whether an attribute value is null Check whether an attribute value matches a given string pattern
DISTINCT
Aggregate Functions COUNT
Returns the number of rows with non-null values for a given column
MIN
MAX SUM AVG
Page 6
INSERT Command
Use INSERT command to enter data into a table
Basic syntax:
INSERT INTO tablename VALUES( value1, value2, , valueN)
Page 7
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
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');
Page 9 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
UPDATE Command
Use UPDATE command to modify data in a table
Basic syntax:
UPDATE tablename SET col1 = expression1, col2 = expression2, colN = expressionN [WHERE conditionList]
Note: Be careful about WHERE clause, there could be a huge problem if you forget to insert WHERE condition
UPDATE inventory SET dateAcquired = 2010-08-22, comments = go go go WHERE inventoryID = 57;
Page 10 ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
DELETE Command
Use DELETE command to delete a table row
Basic syntax:
DELETE FROM tablename [WHERE conditionList]
Note: Be careful about WHERE clause, there could be a huge problem if you forget to insert WHERE condition
DELETE FROM inventory WHERE inventoryID = 57;
Page 11
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
SELECT Command
Use SELECT command to query partial table contents by placing conditions we want specified in WHERE clause (optional)
Basic Syntax:
SELECT columnList FROM tablename [ WHERE conditionList ]
The columnList specifies the selected attribute we want, if we want all attributes in that table, simply use *
SELECT * FROM employee
Page 12
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 13
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 14
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 15
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 16
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 17
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 18
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 19
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 21
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 22
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 23
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 26
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 27
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 28
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University
Page 29
ITCS201 Web Programming, The Faculty of Information and Communication Technology, Mahidol University