Professional Documents
Culture Documents
Week8 SQL
Week8 SQL
Week8 SQL
2
CREATE DATABASE
• The CREATE DATABASE statement is used to
create a database.
• Syntax:
CREATE DATABASE dbname;
• Example:
CREATE DATABASE studentInformationSystem;
USE STATEMENT
• The USE statement is used to choose a
database.
• Syntax:
USE dbname;
• Example:
USE studentInformationSystem;
CREATE TABLE
• The CREATE TABLE statement is used to create a table in a
database.
• Tables are organized into rows and columns
• Table must have a name
• Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
An ERD example
matrix name
code
name credit
grade
Tables for ERD
STUDENT
matrix name
SUBJECT
code name credit
ENROL
matrix code grade
CREATE TABLE
• The CREATE TABLE statement is used to create a table in a
database.
• Tables are organized into rows and columns
• Table must have a name
• Syntax:
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
CREATE TABLE EXAMPLE
STUDENT
matrix name
16
Staff
17
Example: All Columns, All Rows
18
Example All Columns, All Rows
19
Example: Specific Columns, All Rows
20
Example: Specific Columns, All Rows
21
Example: Use of DISTINCT
SELECT propertyNo
FROM Viewing;
Viewing
22
Example: Use of DISTINCT
• Use DISTINCT to eliminate duplicates:
23
Example: Calculated Fields
24
Example: Calculated Fields
25
Example: Comparison Search Condition
26
Example: Range Search Condition
List all staff with a salary between 20000 and
30000
27
Example: Pattern Matching
28
Example: Single Column Ordering
29
Example: Single Column Ordering
30
SELECT Statement - Aggregates
31
Example: Use of MIN, MAX, AVG
32
Example: Subquery of equality
• The following SQL code will display certain
records from table student based on the
condition given:
SELECT *FROM student
WHERE matrix=113100;
OUTPUT:
matrix name age address Email
113100 Nurfatin binti 19 No. 8, Jalan 2/4, Taman fatin@usim.edu.my
Mohd Seri Serdang, 43300
Seri Kembangan,
Selangor
SQL DELETE STATEMENT
• The DELETE statement is used to delete rows
in a table.
• Syntax:
DELETE FROM table_name
WHERE some_column=some_value;
• Example:
DELETE FROM student
WHERE matrix=113100;
SQL DROP TABLE
• The DROP TABLE statement is used to delete
a table in a database.
• Syntax:
DROP TABLE table_name;
• Example:
DROP TABLE student;