Fdocuments - in - Lecture 1 SQL DDL DML
Fdocuments - in - Lecture 1 SQL DDL DML
DCO11310
Database Systems and Design
By Rose Chang
Outline of Lecture
Lecture 2 2
SQL - Background
Lecture 2 3
What is a Table?
A table is like a file that stores records with
a 2-Dimension Structure)
… … … …
Lecture 2 4
SQL
Components of language
Schema definition, data retrieval, data
modification, constraints, views, authorization,
etc.
DDL = Data Definition Language
DML = Data Manipulation Language
DCL = Data Control Language
Lecture 2 5
SQL Statements
Lecture 2 7
Oracle Internet Platform
Clients Any mail
Any browser client Any FTP client
System management
Development tools
Internet applications
SQL
SQL
Business logic Presentation and
and data business logic
PL/SQL
PL/SQL
Application
Databases servers
Java
Java
Network services
Lecture 2
SQL and iSQL*Plus Interaction
SQL statements
iSQL*Plus Oracle
Internet server
Browser
Formatted report
Client
Lecture 2 9
SQL Statements vs.
iSQL*Plus Commands
SQL iSQL*Plus
• A language • An environment
• ANSI standard
• Oracle proprietary
• Keywords can be
• Keyword cannot be abbreviated
abbreviated • Commands do not allow
• Statements manipulate manipulation of values in
data and table the database
definitions in the • Runs on a browser
database • Centrally loaded, does not
have to be implemented on
each machine
SQL iSQL*Plus
statements commands
Lecture 2 10
Oracle Naming Rules
Lecture 2 11
Question
Lecture 2 12
SQL Data Types I
Lecture 2 13
SQL Data Types II
Lecture 2 14
Data Type List
Data Type Description
VARCHAR2(size) Variable-length character data
CHAR(size) Fixed-length character data
NUMBER(p,s) Variable-length numeric data
DATE Date and time values
LONG Variable-length character data
up to 2 gigabytes
CLOB Character data up to 4
gigabytes
RAW and LONG RAW Raw binary data
BLOB Binary data up to 4 gigabytes
BFILE Binary data stored in an external
file; up to 4 gigabytes
ROWID A 64 base number system representing
the unique address of a row in its table.
Lecture 2 15
Creating and Deleting Tables
Lecture 2 16
The ALTER TABLE Statement
ALTER
ALTER TABLE
TABLE department
department
ADD
ADD (job_id
(job_id VARCHAR2(9));
VARCHAR2(9));
Table altered.
Table altered.
ALTER
ALTER TABLE
TABLE department
department
MODIFY
MODIFY (last_name
(last_name VARCHAR2(30));
VARCHAR2(30));
Table
Table altered.
altered.
ALTER
ALTER TABLE
TABLE department
department
DROP
DROP COLUMN
COLUMN job_id;
job_id;
Table
Table altered.
altered.
Lecture 2 18
Required Fields
A null value is unavailable, unassigned,
unknown, or inapplicable, is not the same as
zero or a blank space
CREATE TABLE department
(departmentno NUMBER(2)NOT NULL,
dname VARCHAR2(14),
loc VARCHAR2(13));
Table created.
INSERT
INSERT INTO
INTO department
department
VALUES
VALUES (100,
(100, 'Finance',
'Finance', NULL);
NULL);
Lecture 2 19
Default Values
Lecture 2 20
Changing the Name of an
Object
To change the name of a table, view,
sequence, or synonym, you execute the
RENAME statement.
RENAME department TO detail_department;
Table renamed.
Lecture 2 21
Truncating a Table
TRUNCATE
TABLE detail_department;
Table truncated.
You cannot roll back row removal when using
TRUNCATE.
Alternatively, you can remove rows by using the
DELETE statement.
Lecture 2 22
The INSERT Statement Syntax
Lecture 2 23
Inserting New Rows
Lecture 2 24
Inserting Rows with Null
Values
Implicit method: Omit the column from the
column list.
INSERT INTO department (department_id,
department_name )
VALUES (30, 'Purchasing');
1 row created.
Lecture 2 26
Updating Rows in a Table
DELETE
DELETE [FROM]
[FROM] table
table
[WHERE
[WHERE condition];
condition];
Lecture 2 28
Deleting Rows from a Table
Table 1 Table 1
Join
Table 1 Table 2
Lecture 2 30
Basic SELECT Statement
SELECT
SELECT *|{[DISTINCT]
*|{[DISTINCT] column|expression
column|expression [alias],...}
[alias],...}
FROM
FROM table
table
[WHERE
[WHERE condition(s)]
condition(s)]
[ORDER
[ORDER BY
BY {column,
{column, expr,
expr, alias}
alias} [ASC|DESC]];
[ASC|DESC]];
Lecture 2 31
Selecting All Columns
SELECT *
FROM department;
Lecture 2 32
Selecting Specific Columns
Projection
Lecture 2 33
Selecting Specific Rows
Selection
Lecture 2 34
Writing SQL Statements
Lecture 2 35
Arithmetic Expressions
+ Add
- Subtract
* Multiply
/ Divide
Lecture 2 36
Using Arithmetic Operators
Lecture 2 37
Operator Precedence
Lecture 2 39
Using Parentheses
Lecture 2 40
Null Values
in Arithmetic Expressions
Arithmetic expressions containing a null value
evaluate to null
SELECT last_name, 12*salary*commission_pct
FROM employees;
Lecture 2 41
Defining a Column Alias
A column alias:
Renames a column heading
Is useful with calculations
Immediately follows the column name - there can
also be the optional AS keyword between the
column name and alias
Requires double quotation marks if it contains
spaces or special characters or is case sensitive
Lecture 2 42
Using Column Aliases
SELECT last_name AS name, commission_pct AS comm
FROM employees;
…
Lecture 2 43
Concatenation Operator
A concatenation operator:
Concatenates columns or character strings to
other columns
Is represented by two vertical bars (||)
Creates a resultant column that is a character
expression
Lecture 2 44
Using the Concatenation
Operator
Lecture 2 45
Literal Character Strings
Lecture 2 46
Using Literal Character Strings
Lecture 2 47
Duplicate Rows
…
Lecture 2 48
Eliminating Duplicate Rows
Lecture 2 49
Limiting the Rows Selected
Lecture 2 50
Character Strings and Dates
Lecture 2 51
Comparison Conditions
Operator Meaning
= Equal to
Lecture 2 52
Using Comparison Conditions
Lecture 2 53
Other Comparison Conditions
Operator Meaning
Lecture 2 54
Using the BETWEEN
Condition
Use the BETWEEN condition to display
rows based on a range of values.
SELECT last_name, salary
FROM employees
WHERE salary BETWEEN 2500 AND 3500;
Lecture 2 55
Using the IN Condition
Lecture 2 56
Using the LIKE Condition
Lecture 2 59
Logical Conditions
Operator Meaning
Lecture 2 60
Using the AND Operator
Lecture 2 61
Using the OR Operator
OR requires either condition to be true.
SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary >= 10000
OR job_id LIKE '%MAN%';
Lecture 2 62
Using the NOT Operator
Lecture 2 63
Rules of Precedence
Lecture 2 65
Rules of Precedence
Lecture 2 66
ORDER BY Clause
Sort rows with the ORDER BY clause
ASC: ascending order, default
DESC: descending order
SELECT last_name, job_id, department_id, hire_date
FROM employees
ORDER BY hire_date ;
…
Lecture 2 67
Sorting in Descending Order
Lecture 2 68
Sorting by Column Alias
Lecture 2 69
Sorting by Multiple Columns
The order of ORDER BY list is the order of sort
You can sort by a column that is not in the SELECT list
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;
…
Lecture 2 70
Q&A
SELECT
SELECT *|{[DISTINCT]
*|{[DISTINCT] column|expression
column|expression [alias],...}
[alias],...}
FROM
FROM table
table
[WHERE
[WHERE condition(s)]
condition(s)]
[ORDER
[ORDER BY
BY {column,
{column, expr,
expr, alias}
alias} [ASC|DESC]];
[ASC|DESC]];
Lecture 2 71