Lab 10
Lab 10
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 2
Database Objects
Object Description
Table Basic unit of storage; composed of rows
View Logically represents subsets of data from one or
more tables
Sequence Generates numeric values
Index Improves the performance of some queries
10 - 3
Naming Rules
10 - 4
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 5
CREATE TABLE
Statement
• You must have:
– CREATE TABLE
privilege
– A storage
CREATE area
TABLE [schema.]table
(column datatype [DEFAULT expr][, ...]);
• You specify:
– Table name
– Column name, column data type, and column size
10 - 6
Referencing Another User’s Tables
USERA USERB
SELECT * SELECT *
FROM FROM
userB.employe userA.employe
es; es;
10 - 7
DEFAULT
Option
• Specify a default value for a column during an insert.
10 - 8
Creating Tables
10 - 9
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 10
Data Types
10 - 11
Datetime Data Types
10 - 14
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 15
Including Constraints
10 - 16
Constraint Guidelines
10 - 17
Defining Constraints
• Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
[column_constraint],
...
[table_constraint][,...]);
• Column-level constraint syntax:
column [CONSTRAINT constraint_name] constraint_type,
• Table-level constraint syntax:
column,...
[CONSTRAINT constraint_name] constraint_type
(column, ...),
10 - 18
Defining Constraints
10 - 19
NOT NULL
Constraint
Ensures that null values are not permitted for the column:
…
NOT NULL constraint Absence of NOT NULL
(Primary Key enforces NOT NULL constraint (Any row can
NOT NULL constraint contain a null value for
constraint.) this column.)
10 - 20
UNIQUE
Constraint
UNIQUE
EMPLOYEES 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
PRIMARY KEY
Constraint
Not allowed
(50 already exists)
10 - 23
FOREIGN KEY
Constraint
DEPARTMENTS
PRIMARY
KEY
…
EMPLOYEES
FOREIGN
KEY
…
INSERT INTO Not allowed
(9 does not
exist)
Allowed
10 - 24
FOREIGN KEY
Constraint
Defined at either the table level or the column level:
10 - 25
FOREIGN KEY
Constraint:
• Keywords
FOREIGN KEY: Defines the column in the child table at
the table-constraint level
• REFERENCES: Identifies the table and column in the parent
table
• ON DELETE CASCADE: Deletes the dependent rows in
the child table when a row in the parent table is deleted
• ON DELETE SET NULL: Converts dependent foreign
key values to null
10 - 26
CHECK
Constraint
• Defines a condition that each row must satisfy
10 - 27
CREATE TABLE:
CREATE TABLE Example
employees
( employee_id NUMBER(6)
CONSTRAINT emp_employee_id PRIMARY KEY
, first_name VARCHAR2(20)
, last_name VARCHAR2(25)
CONSTRAINT emp_last_name_nn NOT NULL
, email VARCHAR2(25)
CONSTRAINT emp_email_ NOT NULL
nn UNIQUE
CONSTRAINT emp_email_uk
, phone_number DATE
VARCHAR2(20)
, hire_date emp_hire_date_nn NOT NULL
CONSTRAINT VARCHAR2(10)
, job_id emp_job_nn NOT NULL
CONSTRAINT NUMBER(8,2)
, salary emp_sala CHECK (salary>0)
, commission_pct
CONSTRAINT NUMBER(2,2)
ry_ck
, manager_id NUMBER(6)
CONSTRAINT emp_manager_fk REFERENCES
employees (employee_id)
, department_id NUMBER(4)
CONSTRAINT emp_dept_fk
departments REFERENCES
(department_id));
10 - 28
Violating Constraints
UPDATE employees
SET department_id = 55
WHERE department_id =
110;
10 - 29
Violating Constraints
10 - 30
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 31
Creating a Table
Using a Subquery
• Create a table and insert rows by combining the CREATE
TABLE statement and the AS subquery option.
CREATE TABLE table
[(column, column...)]
AS subquery;
• Match the number of specified columns to the number of
subquery columns.
• Define columns with column names and default values.
10 - 32
Creating a Table
Using a Subquery
DESCRIBE dept80
10 - 33
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 34
ALTER TABLE
Statement
Use the ALTER TABLE statement to:
• Add a new column
• Modify an existing column definition
• Define a default value for the new column
• Drop a column
• Change table to read-only status
10 - 35
ALTER TABLE
Statement
Use the ALTER TABLE statement to:
• To add column
ALTER TABLE table_name ADD (col_name1 DATATYPE CONSTRAINT [,
col_name2 DATATYPE CONSTRAINT, …] );
• To modify column
ALTER TABLE table_name MODIFY (col_name1 DATATYPE CONSTRAINT [,
col_name2 DATATYPE CONSTRAINT] );
• To drop column
ALTER TABLE table_name DROP (col_name1 [, col_name2, …]);
10 - 35
Read-Only Tables
10 - 36
Lesson Agenda
• Database objects
– Naming rules
• CREATE TABLE statement:
– Access another user’s tables
– DEFAULT option
• 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
• DROP TABLE statement
10 - 37
Dropping a Table
10 - 38
Thank you