Using DDL Statements To Create and Manage Tables
Using DDL Statements To Create and Manage Tables
Using DDL Statements To Create and Manage Tables
Objectives
After completing this lesson, you should be able to do the
following:
Categorize the main database objects
Review the table structure
List the data types that are available for columns
Create a simple table
Explain how constraints are created at the time of table
creation
Describe how schema objects work
10 - 2
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 3
Database Objects
10 - 4
Object
Description
Table
View
Sequence
Index
Synonym
Naming Rules
Table names and column names must:
Begin with a letter
Be 130 characters long
Contain only AZ, az, 09, _, $, and #
Not duplicate the name of another object owned by the
same user
Not be an Oracle serverreserved word
10 - 5
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 6
You specify:
The table name
The column name, column data type, and column size
10 - 7
10 - 8
USERA
USERB
SELECT *
FROM userB.employees;
SELECT *
FROM userA.employees;
DEFAULT Option
10 - 9
Creating Tables
NUMBER(2),
VARCHAR2(14),
VARCHAR2(13),
DATE DEFAULT SYSDATE);
10 - 10
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 11
Data Types
Data Type
Description
NUMBER(p,s)
DATE
LONG
CLOB
BLOB
BFILE
ROWID
10 - 12
10 - 14
Data Type
Description
TIMESTAMP
INTERVAL YEAR TO
MONTH
INTERVAL DAY TO
SECOND
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 15
Including Constraints
10 - 16
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
Constraint Guidelines
10 - 17
Defining Constraints
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
10 - 18
Defining Constraints
10 - 19
UNIQUE Constraint
EMPLOYEES
UNIQUE constraint
INSERT INTO
Allowed
Not allowed: already exists
10 - 21
UNIQUE Constraint
Defined at either the table level or the column level:
CREATE TABLE employees(
employee_id
NUMBER(6),
last_name
VARCHAR2(25) NOT NULL,
email
VARCHAR2(25),
salary
NUMBER(8,2),
commission_pct
NUMBER(2,2),
hire_date
DATE NOT NULL,
...
CONSTRAINT emp_email_uk UNIQUE(email));
10 - 22
DEPARTMENTS
PRIMARY KEY
Not allowed
(null value)
INSERT INTO
Not allowed
(50 already exists)
10 - 23
DEPARTMENTS
EMPLOYEES
FOREIGN
KEY
INSERT INTO
10 - 24
Not allowed
(9 does not
exist)
Allowed
10 - 25
10 - 26
CHECK Constraint
10 - 27
10 - 28
Violating Constraints
UPDATE employees
SET
department_id = 55
WHERE department_id = 110;
10 - 29
Violating Constraints
You cannot delete a row that contains a primary key that is used
as a foreign key in another table.
DELETE FROM departments
WHERE department_id = 60;
10 - 30
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 31
10 - 32
DESCRIBE dept80
10 - 33
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 34
10 - 35
Read-Only Tables
You can use the ALTER TABLE syntax to:
Put a table into read-only mode, which prevents DDL or
DML changes during table maintenance
Put the table back into read/write mode
ALTER TABLE employees READ ONLY;
-- perform table maintenance and then
-- return table back to read/write mode
ALTER TABLE employees READ WRITE;
10 - 36
Lesson Agenda
Database objects
Naming rules
Data types
Overview of constraints: NOT NULL, UNIQUE, PRIMARY
KEY, FOREIGN KEY, CHECK constraints
Creating a table using a subquery
ALTER TABLE
Read-only tables
10 - 37
Dropping a Table
10 - 38
Quiz
You can use constraints to do the following:
1. Enforce rules on the data in a table whenever a row is
inserted, updated, or deleted.
2. Prevent the deletion of a table.
3. Prevent the creation of a table.
4. Prevent the creation of data in a table.
10 - 39
Summary
In this lesson, you should have learned how to use the CREATE
TABLE statement to create a table and include constraints:
Categorize the main database objects
Review the table structure
List the data types that are available for columns
Create a simple table
Explain how constraints are created at the time of table
creation
Describe how schema objects work
10 - 40
10 - 41