Professional Documents
Culture Documents
Oracle ch3
Oracle ch3
Objectives
Become acquainted with Structured Query Language
(SQL)
Identify the table name and structure
Create a new table using the CREATE TABLE command
Use a subquery to create a new table
Add a column to an existing table
Modify the definition of a column in an existing table
Delete a column from an existing table
Mark a column as unused and then delete it at a later time
Rename a table
Truncate a table
Drop a table
Dr. Chen, Oracle Database System (Oracle)
Tables
Views
Stored programs, etc.
Creating a Table
Table name
Field names
Field data types
Field sizes
Constraints
restrictions on the data values that a field can store
1. From 1 to 30 characters
2. Only alphanumeric characters, and special
characters ($ , _, #)
3. Must begin with a letter and can not contain blank
spaces or hyphens
4. Must be unique and No reserved words are allowed
10
11
Built-in
provided by the system
Library
User-defined
built by users
Dr. Chen, Oracle Database System (Oracle)
12
Character
VARCHAR2
CHAR
Numeric
NUMBER
DATE
OTHERS:
LOB, BLOB, LONG, RAW, LONG RAW
13
1. VARCHAR2
Stores variable-length character data up
to a maximum of 4,000 characters
Values in different records can have a
different number of characters
fieldname VARCHAR2(maximum_size)
(e.g.) emp_name VARCHAR2(20);
an instance: Jason Chen
14
15
Character Subtypes
Examples:
VARCHAR2(5)
Smith or Smi
CHAR(5)
Smith or Smi
LONG
Smith...
16
s_Last CHAR(15);
17
18
19
3.89
2.7569
3.2
Dr. Chen, Oracle Database System (Oracle)
20
TO_DATE (
TO_DATE (
TO_DATE (
, MM/DD/YY)
, DD-MON-YYYY)
, HH:MI AM)
21
Table 3-2
22
Table Creation
Figure 3-1
Defining Columns
Column definition list must be enclosed in
parentheses
Datatype must be specified for each column
Maximum of 1,000 columns
Dr. Chen, Oracle Database System (Oracle)
23
Amfirst
Amlast
AmeDate
Amsal
Amcomm
Region
VARCHAR2(4)
VARCHAR2(12)
VARCHAR2(12)
DATE
NUMBER(8,2)
NUMBER(7,2)
CHAR(2)
NOT NULL);
GROUP WORK
Complete the
CREATE command
manually!
24
Amfirst
Amlast
AmeDate
Amsal
Amcomm
Region
VARCHAR2(4)
VARCHAR2(12)
VARCHAR2(12)
DATE
NUMBER(8,2)
NUMBER(7,2)
CHAR(2)
25
26
27
Figure 3-2
28
Figure 3-4
29
Figure 3-8
30
Figure 3-7
31
32
33
Figure 3-18 The ALTER TABLE MODIFY command to increase the column width
Dr. Chen, Oracle Database System (Oracle)
34
Modification Guidelines
Column must be as wide as the data it
already contains
If a NUMBER column already contains
data, size cannot be decreased
Adding or changing default data does not
affect existing data
35
36
Dr. Chen, Oracle Database System (Oracle)
36
37
AUTHOR
AuthorID
LName
VARCHAR2(4) VARCHAR2(10)
FName
VARCHAR2(10)
LNAME
---------SMITH
JONES
AUSTIN
MARTINEZ
KZOCHSKY
PORTER
ADAMS
BAKER
PETERSON
WHITE
WHITE
FNAME
---------SAM
JANICE
JAMES
SHEILA
TAMARA
LISA
JUAN
JACK
TINA
WILLIAM
LISA
AUTH
---R100
F100
W110
LNAME
---------ROBINSON
FIELDS
WILKINSON
FNAME
---------ROBERT
OSCAR
ANTHONY
14 rows selected.
Dr. Chen, Oracle Database System (Oracle)
38
AUTHOR
AuthorID
LName
VARCHAR2(4) VARCHAR2(10)
FName
VARCHAR2(10)
Show: test_UNUSED.sql
and test_UNUSED.doc
39
AUTHOR
AuthorID
LName
VARCHAR2(4) VARCHAR2(10)
FName
VARCHAR2(10)
40
41
42
43
End of chapter 3
44