01-Using DDL Statements To Create and Manage Tables
01-Using DDL Statements To Create and Manage Tables
01-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, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 3
Database Objects
Object
Table View Sequence
Description
Basic unit of storage; composed of rows Logically represents subsets of data from one or more tables Generates numeric values
Index
Synonym
10 - 4
Naming Rules
Table names and column names: Must begin with a letter Must be 130 characters long Must contain only AZ, az, 09, _, $, and # Must not duplicate the name of another object owned by the same user Must not be an Oracle serverreserved word
10 - 5
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 6
A storage area
CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]);
You specify:
Table name Column name, column data type, and column size
10 - 7
10 - 8
DEFAULT Option
Specify a default value for a column during an insert.
... hire_date DATE DEFAULT SYSDATE, ...
Literal values, expressions, or SQL functions are legal values. Another columns name or a pseudocolumn are illegal values. The default data type must match the column data type.
CREATE TABLE hire_dates (id NUMBER(8), hire_date DATE DEFAULT SYSDATE);
10 - 9
Creating Tables
Create the table:
CREATE TABLE dept (deptno dname loc create_date
10 - 10
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 11
Data Types
Data Type
CHAR(size) NUMBER(p,s) DATE LONG CLOB RAW and LONG RAW BLOB BFILE ROWID
Description
Fixed-length character data Variable-length numeric data Date and time values Variable-length character data (up to 2 GB) Character data (up to 4 GB) Raw binary data Binary data (up to 4 GB) Binary data stored in an external file (up to 4 GB) A base-64 number system representing the unique address of a row in its table
10 - 12
Description
Date with fractional seconds Stored as an interval of years and months Stored as an interval of days, hours, minutes, and seconds
10 - 13
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 14
Including Constraints
Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid:
NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK
10 - 15
Constraint Guidelines
You can name a constraint, or the Oracle server generates a name by using the SYS_Cn format.
Define a constraint at the column or table level. View a constraint in the data dictionary.
10 - 16
Defining Constraints
Syntax:
CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint], ... [table_constraint][,...]);
10 - 17
Defining Constraints
Example of a column-level constraint:
CREATE TABLE employees( employee_id NUMBER(6) CONSTRAINT emp_emp_id_pk PRIMARY KEY, first_name VARCHAR2(20), ...);
10 - 18
NOT NULL constraint (Primary Key enforces NOT NULL constraint.) NOT NULL constraint Absence of NOT NULL constraint (Any row can contain a null value for this column.)
10 - 19
UNIQUE Constraint
EMPLOYEES
UNIQUE constraint
INSERT INTO
10 - 20
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 - 21
DEPARTMENTS
PRIMARY KEY
INSERT INTO
DEPARTMENTS
EMPLOYEES
FOREIGN KEY
INSERT INTO
10 - 23
10 - 24
10 - 25
CHECK Constraint
Defines a condition that each row must satisfy The following expressions are not allowed:
References to CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns Calls to SYSDATE, UID, USER, and USERENV functions
Queries that refer to other values in other rows
..., salary NUMBER(2) CONSTRAINT emp_salary_min CHECK (salary > 0),...
10 - 26
10 - 27
Violating Constraints
10 - 28
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 - 29
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 30
Match the number of specified columns to the number of subquery columns. Define columns with column names and default values.
10 - 31
DESCRIBE dept80
10 - 32
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 33
10 - 34
Read-Only Tables
Use the ALTER TABLE syntax to put a table into the read-only mode: Prevents DDL or DML changes during table maintenance Change it 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 - 35
Lesson Agenda
Database objects
Naming rules
Data types Overview of constraints: NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK constraints Creating a table using a subquery ALTER TABLE
Read-only tables
10 - 36
Dropping a Table
Moves a table to the recycle bin Removes the table and all its data entirely if the PURGE clause is specified Invalidates dependent objects and removes object privileges on the table
DROP TABLE dept80;
10 - 37
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 - 38
10 - 39