Вы находитесь на странице: 1из 25

Guide to Oracle 10g

Chapter 2:
Creating and Modifying
Database Tables (DDL)

1
Database Objects
 An Oracle database consists of multiple
user accounts
 Each user account owns database
objects
 Tables
 Views
 Stored programs
 Etc.

2
Database Queries
 Query: command to perform
operation on database object
 Create
 Modify
 View
 Delete
 Structured Query Language (SQL)
 Standard query language for
relational databases
3
SQL Command Types
 Data Definition Language (DDL)
 Used to create and modify the structure of
database objects
 Data Manipulation Language (DML)
 Used to insert, update, delete, and view
database data

4
DDL Commands
 Used to create and modify the structure
of database objects
 CREATE
 ALTER
 DROP
 DDL commands execute as soon as
they are issued, and do not need to be
explicitly saved

5
User Accounts
 Each Oracle database user has a user
schema
 Area in the database where the user’s
database objects are stored
 Identified by a unique username and
protected by a password
 Each user schema is granted specific
privileges
6
Creating New User Accounts

 Done by DBA
 Syntax:
CREATE USER username IDENTIFIED BY
password;

7
Oracle Naming Standard
 Oracle database objects must adhere to
the Oracle Naming Standard
 1 to 30 characters long
 Must begin with a character
 Can contain characters, numbers, and the
symbols $, _, and #

8
Defining Database Tables
 To create a table, you must
specify:
 Table name
 Field names
 Field data types
 Field sizes
 Constraints

9
Creating a Database Table
 Syntax:
CREATE TABLE table_name
( fieldname1 datatype,
fieldname2 datatype, …);
 Example:
CREATE TABLE my_students
( s_id NUMBER(6),
s_name VARCHAR2(30),
s_dob DATE,
s_class CHAR(2));

10
Oracle Character Data Types

 VARCHAR2
 CHAR
columnname VARCHAR2(max_size) columnname CHAR(max_size)
 Variable-length character strings  Fixed-length character data
 Max_size can be between 1 and  Max_size can be between 1 and
4,000 characters 2000 characters
 Must specify the size
 Max_size is optional. Default is 1.
 Adds trailing blank spaces to pad
 No trailing blank spaces are width
added  If more than max_size data is
 If more than max_size data is inserted, an error occurs.
inserted, an error occurs.  Example declaration:
 Example declaration: student_gender CHAR(2)
student_name VARCHAR2(30)

11
Number Data Type
 NUMBER
 stores negative, positive, fixed, and floating point
numbers values between 10-130 and 10126
 General declaration format:
variable_name NUMBER(precision, scale)
 Number type (integer, fixed point, floating point) specified by
precision and scale
 Precision: total number of digits on either side of the decimal
point. It does not include the decimal point itself or any commas
or any formatting symbols.
 Scale: number of digits to right of decimal point

12
Date Data Type
 DATE  If no time value is
 Stores dates from 1/1/4712 given when a new
BC to 12/31/4712 AD
date is inserted,
 Stores both a date and time
component default value is
 Default date format: 12:00:00 AM
DD-MON-YY HH:MI:SS AM  If no date value is
 example: 05-JUN-03 given when a new
12:00:00 AM time is inserted,
default date is first
 Sample declaration: day of current month
s_dob DATE

13
Large Object (LOB) Data Types

;Ex: f_image BLOB


14
Constraint Names
 Constraint naming convention:
tablename_fieldname_constraintID
 Constraint ID values:
 Primary key: pk
 Foreign key: fk
 Check condition: cc
 Not NULL: nn
 Unique: uk
 Example constraint name:
my_students_s_id_pk
15
Primary Key Constraints
 Table-level
 Defining a primary key:
CONSTRAINT constraint_name PRIMARY KEY

 Example:
s_id NUMBER(6)
CONSTRAINT student_s_id_pk PRIMARY KEY

16
Primary Key Constraints
 Can be defined when field is declared

17
Composite Primary Keys
 Syntax:
CONSTRAINT constraint_name
PRIMARY KEY (field1, field2)
 Must be defined after fields that compose
key are defined

18
Foreign Key Constraints
 Can be defined when field is declared

19
Types of Value Constraints
 Check condition: restricts to specific values
 Example: s_gender (M or F)
CONSTRAINT my_students_s_gender_cc
CHECK (s_gender = ‘M’) OR (s_gender = ‘F’)
 Not NULL: specifies that a field cannot be
NULL
 Example:
CONSTRAINT my_students_s_dob_nn
NOT NULL
20
Types of Value Constraints
 Default: specifies a default value that is inserted
automatically
 Example:
s_state CHAR(2) DEFAULT ‘WI’
 Unique
 Table constraint
 Specifies that a non-primary key field must have a
unique value
CONSTRAINT consultant_c_email_uk UNIQUE (c_email)

21
Viewing Table Information
 Viewing a table’s structure
DESCRIBE table_name;

22
Modifying Tables
 Unrestricted actions
1. Renaming tables
 GF: Rename Old_tableName To
new_tableName;
 Ex
 Rename Faculty To New_Faculty

23
Modifying Tables
ALTER TABLE T_name Action

Rename Add Modify Drop E/D

F_old_name (Field_name Column Enable


(Field_name Field_name Const_name
To DataType
F_new_name TYPE )
[Constraint])
Constraint Disable
Const_name Const_name
Constraint
Cons_specif

New_one Increase_size Decrease_size


24
Deleting Tables
 Syntax to delete table if no table fields
are referenced as foreign keys:
DROP TABLE tablename;

 Syntax to delete table and constraints if


table contains fields that are referenced
as foreign keys:
DROP TABLE tablename CASCADE CONSTRAINTS;

25

Вам также может понравиться