Creating and Modifying Database Tables: A Guide To Oracle9i 1
Creating and Modifying Database Tables: A Guide To Oracle9i 1
Database Tables
Chapter 2
A Guide to Oracle9i 1
Objectives
A Guide to Oracle9i 2
Objectives
A Guide to Oracle9i 3
Introduction to SQL
A Guide to Oracle9i 4
Oracle9i User Accounts
A Guide to Oracle9i 5
Defining Oracle9i Database Tables
• To create a new table specify:
– Table name
– Name of each data field
– Data type and size of each data field
• Constraints: restrictions on the data values that a field can store
• Oracle Naming Standard:
– Series of rules Oracle Corporation established for naming all
database objects
– Objects must be from 1 to 30 characters long
– Can contain letters, numbers, and the special symbols $, _, and #
– Must begin with a character
A Guide to Oracle9i 6
Creating a Table
A Guide to Oracle9i 7
Oracle9i Data Types
A Guide to Oracle9i 8
Character Data Types
• 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)
A Guide to Oracle9i 9
Character Data Types
• CHAR
– Fixed-length character data up to a maximum size
of 2,000 characters
– Data values for different records all have the same
number of characters
– DBMS adds trailing blank spaces to the end of the
entry to make the entry fill the maximum_size
value
– Data longer than maximum_size causes an error
– fieldname CHAR[(maximum_size)]
A Guide to Oracle9i 10
Character Data Types
A Guide to Oracle9i 11
Number Data Types
A Guide to Oracle9i 12
Date and Time Data Types
• DATE
– Dates from December 31, 4712 BC to December 31, 4712
AD
– Default format DD-MON-YY
– Default time format HH:MI:SS A.M.
– fieldname DATE
• TIMESTAMP
– Similar to DATE but stores fractional seconds
– fieldname TIMESTAMP (fractional_seconds_precision)
A Guide to Oracle9i 13
Date and Time Data Types
• INTERVAL YEAR TO MONTH
– Time interval, expressed in years and months
– +02-11 specifies a positive time interval of 2 years and 11 months
– fieldname INTERVAL YEAR[(year_precision)] TO MONTH
• INTERVAL DAY TO SECOND
– Time interval, expressed in days, hours, minutes, and seconds
– -04 03:20:32.00: 4 days, 3 hours, 20 minutes, and 32 seconds
– fieldname INTERVAL DAY[(leading_precision)] TO
SECOND[(fractional_seconds_precision)]
A Guide to Oracle9i 14
Large Object (LOB) Data Types
A Guide to Oracle9i 15
Constraints
• Rules that restrict the data values that you can enter into a field in a
database table
• Table constraint: restricts the data value with respect to all other
values in the table
• Field constraint: limits the value that can be placed in a specific field,
irrespective of values that exist in other table records
A Guide to Oracle9i 16
Oracle Constraint Naming
Convention
• tablename_ fieldname_constraintID
A Guide to Oracle9i 17
Integrity Constraints
A Guide to Oracle9i 18
Value Constraints
• Check conditions: field value must be a specific value or fall
within a range of values
A Guide to Oracle9i 19
Creating Database Tables Using
SQL*Plus
• Type SQL commands at the SQL prompt
A Guide to Oracle9i 20
Log On to SQL*Plus
A Guide to Oracle9i 21
SQL*Plus Program Window
A Guide to Oracle9i 22
Create a Table
A Guide to Oracle9i 23
Table Creation Sequence
A Guide to Oracle9i 24
Using Notepad
A Guide to Oracle9i 25
Errors
A Guide to Oracle9i 26
Exiting SQL*Plus
A Guide to Oracle9i 27
Create Table with Foreign Key
Constraint
A Guide to Oracle9i 28
Viewing Information About Tables
A Guide to Oracle9i 29
Viewing Tables in the Database
A Guide to Oracle9i 30
Viewing Constraints on One Table
A Guide to Oracle9i 31
Modifying and Deleting Database
Tables
• Modify existing database tables by:
– Changing the name of a table
– Adding new columns
– Deleting columns that are no longer needed
– Changing the data type or maximum size of an existing
column
• Unrestricted action: some specifications can always
be modified
• Restricted action: specifications modified only in
certain situations
A Guide to Oracle9i 32
Unrestricted Action
A Guide to Oracle9i 33
Restricted Actions
A Guide to Oracle9i 34
Deleting and Renaming Tables
• To delete:
– Drop table [tablename]
– Use with caution
– To delete foreign key constraints, add “cascade constraints”
• To rename:
– Rename old_tablename to new_tablename
– DBMS automatically transfers to the new table integrity
constraints, indexes, and privileges that referenced the old
table.
A Guide to Oracle9i 35
Adding Fields to Existing Tables
• To add a field:
– ALTER TABLE tablename ADD(fieldname
data_declaration constraints);
A Guide to Oracle9i 36
Modifying Existing Field Data
Definitions
• Can only change datatype to compatible data
type (i.e. varchar2 to char)
A Guide to Oracle9i 37
Deleting a Field
A Guide to Oracle9i 38
Adding and Deleting Constraints
A Guide to Oracle9i 39
Enabling and Disabling Constraints
A Guide to Oracle9i 40
Summary
A Guide to Oracle9i 41
Summary
A Guide to Oracle9i 42