This webpage lists all the SQL
statements that a student is expected to understand after finishing the
E-learning material.
For each topic, we list some example statements, the
corresponding outputs, as well as the related E-learning materials (follow the
link).
The SQL tutorial prepared by W3School (http://www.w3schools.com/sql/)
is selected as the complementary material.
All the SQL statements included in this website are based on a simple database model, as shown in the following (Testing Data).
The sample code to create the tables and insert data can be downloaded here(create_code, insert_value_code).
|
When a database is created, it does not contain any relation.
We can use CREATE TABLE statement to create new relations, and DROP TABLE statement to delete those useless relations.
In case the structure of a relation has to be changed, ALTER TABLE statement can be used.
Each DBMS supports different data types.
Even for the same data type (like string), different DBMS may use different names and/or different format.
A summary of the common data types available in MySQL is listed at the end of this page.
A complete description of all the data types available in MySQL is provided here.
When create tables inside a database, be careful about the order.
Like in our example, table ORDER_t and table PRODUCT_t must be created before the creation of table ORDER_LINE_t.
The reason is ORDER_LINE_t table uses primary keys of PRODUCT_t and ORDER_t as foreign keys.SQL Query Browser provides a user-friendly interface to run SQL statement (A snapshot).
Data Type |
Description |
Examples |
INT |
An integer with value between and |
-1278, 87600, 14 |
NUMERIC |
A floating point number |
-897.23, 0.984 |
DECIMAL(w(,n)) |
A floating point number with w the display width and n the number of digits after the decimal point (Note:w and n are optional) |
DECIMAL(4,2): 89.12 |
CHAR(n) |
fixed-length non-unicode character data with n the fixed length |
|
VARCHAR(n) |
variable-length non-unicode data with n the maximal length |
|
DATE |
date in 'YYYY-MM-DD' (Note: the single quotation mark is compulsory) |
'1996-02-25' |
DATETIME |
date/time in ' |
' |
Want to insert data into a table -> INSERT Statement
Want to update the values of certain data in a table -> UPDATE Statement
Want to delete certain data from a table -> DELETE Statement
SELECT statement is used to retrieve data from the database based on the conditions specified in the SELECT statement.
There are six building blocks for SELECT statement, as shown in the following figure.
Among them, SELECT and FROM clauses are necessary to form one valid SELECT statement.
|
||||||||||||||
|
Customer_Id |
Customer_Name |
Order_Id |
1 |
Contemporary Casuals |
1001 |
1 |
Contemporary Casuals |
1010 |
2 |
Value Furniture |
1006 |
3 |
Home Furnishings |
1005 |
4 |
Eastern Furniture |
1009 |
5 |
Impressions |
1004 |
6 |
Furniture Gallery |
NULL |
7 |
Period Furniture |
NULL |
8 |
Calfornia Classics |
1002 |
9 |
M and H Casual Furniture |
NULL |
10 |
Seminole Interiors |
NULL |
11 |
American Euro Lifestyles |
1007 |
12 |
Battle Creek Furniture |
1008 |
13 |
Heritage Furnishings |
NULL |
14 |
Kaneohe Homes |
NULL |
15 |
Mountain Scenes |
1003 |
SQL Playbook: http://www.mysmu.edu/faculty/bhzheng/teaching/SQLPlayBook/index.htm