Professional Documents
Culture Documents
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 1
Objectives
Applied
Given the design for a data structure, write the DDL statements to
create the tables, constraints, indexes, and sequences that are required.
Write a script that includes all of the DDL statements for creating the
tables of a data structure.
Use Oracle SQL Developer to work with the columns, data,
constraints, indexes, and sequences for a table.
Knowledge
Describe how each of these types of constraints restricts the values that
can be stored in a table: not null, unique, primary key, foreign key, and
check.
Describe the difference between a column-level constraint and a table-
level constraint.
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 2
Objectives (continued)
Knowledge
Describe the use of an index.
Describe the use of a sequence.
Describe the use of a script for creating the tables of a data structure.
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 3
The syntax of the CREATE TABLE statement
CREATE TABLE [schema_name.]table_name
(
column_name_1 data_type [column_attributes]
[, column_name_2 data_type [column_attributes]]...
[, table_level_constraints]
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 4
A statement that creates a table
without column attributes
CREATE TABLE vendors
(
vendor_id NUMBER,
vendor_name VARCHAR2(50)
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 5
Another statement that creates a table
with column attributes
CREATE TABLE invoices
(
invoice_id NUMBER NOT NULL UNIQUE,
vendor_id NUMBER NOT NULL,
invoice_number VARCHAR2(50) NOT NULL,
invoice_date DATE DEFAULT SYSDATE,
invoice_total NUMBER(9,2) NOT NULL,
payment_total NUMBER(9,2) DEFAULT 0
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 6
The syntax of a column-level
primary key constraint
[CONSTRAINT constraint_name] PRIMARY KEY
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 7
A table with column-level constraints
CREATE TABLE vendors
(
vendor_id NUMBER PRIMARY KEY,
vendor_name VARCHAR2(50) NOT NULL UNIQUE
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 8
A table with table-level constraints
CREATE TABLE vendors
(
vendor_id NUMBER,
vendor_name VARCHAR2(50) NOT NULL,
CONSTRAINT vendors_pk PRIMARY KEY (vendor_id),
CONSTRAINT vendor_name_uq UNIQUE (vendor_name)
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 9
Terms to know
Constraint
Column-level constraint
Table-level constraint
Not null constraint
Unique constraint
Primary key constraint
Foreign key constraint
Check constraint
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 10
The syntax of a column-level
foreign key constraint
[CONSTRAINT constraint_name]
REFERENCES table_name (column_name)
[ON DELETE {CASCADE|SET NULL}]
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 11
A table with a column-level foreign key constraint
CREATE TABLE invoices
(
invoice_id NUMBER PRIMARY KEY,
vendor_id NUMBER REFERENCES vendors (vendor_id),
invoice_number VARCHAR2(50) NOT NULL UNIQUE
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 12
A table with a table-level foreign key constraint
CREATE TABLE invoices
(
invoice_id NUMBER NOT NULL,
vendor_id NUMBER NOT NULL,
invoice_number VARCHAR2(50) NOT NULL UNIQUE,
CONSTRAINT invoices_pk
PRIMARY KEY (invoice_id),
CONSTRAINT invoices_fk_vendors
FOREIGN KEY (vendor_id)
REFERENCES vendors (vendor_id)
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 13
An INSERT statement that fails
because a related row doesn’t exist
INSERT INTO invoices
VALUES (1, 1, '1')
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 14
A constraint that uses the ON DELETE clause
CONSTRAINT invoices_fk_vendors
FOREIGN KEY (vendor_id) REFERENCES vendors (vendor_id)
ON DELETE CASCADE
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 15
The syntax of a check constraint
[CONSTRAINT constraint_name] CHECK (condition)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 16
A statement with table-level check constraints
CREATE TABLE invoices
(
invoice_id NUMBER PRIMARY KEY,
invoice_total NUMBER(9,2) NOT NULL,
payment_total NUMBER(9,2) DEFAULT 0,
CONSTRAINT invoices_ck CHECK (invoice_total >= 0
AND payment_total >= 0)
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 17
An INSERT statement that fails
due to a check constraint
INSERT INTO invoices
VALUES (1, 99.99, -10)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 18
The syntax for modifying the columns of a table
ALTER TABLE [schema_name.]table_name
{
ADD column_name data_type [column_attributes] |
DROP COLUMN column_name |
MODIFY column_name data_type [column_attributes]
}
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 19
A statement that adds a new column
ALTER TABLE vendors
ADD last_transaction_date DATE;
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 20
A statement that changes the length of a column
ALTER TABLE vendors
MODIFY vendor_name VARCHAR2(100);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 21
A statement that changes a default value
ALTER TABLE vendors
MODIFY vendor_name DEFAULT 'New Vendor';
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 22
Warning
You should never alter a table or other database object in a
production database without consulting the DBA.
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 23
The syntax for modifying the constraints of a table
ALTER TABLE table_name
{
ADD CONSTRAINT constraint_name
constraint_definition [DISABLE] |
DROP CONSTRAINT constraint_name |
ENABLE [NOVALIDATE] constraint_name |
DISABLE constraint_name
}
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 24
A statement that adds a new check constraint
ALTER TABLE invoices
ADD CONSTRAINT invoice_total_ck
CHECK (invoice_total >= 0);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 25
A statement that enables a constraint
for new values only
ALTER TABLE invoices
ENABLE NOVALIDATE CONSTRAINT invoice_total_ck;
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 26
A statement that adds a foreign key constraint
ALTER TABLE invoices
ADD CONSTRAINT invoices_fk_vendors
FOREIGN KEY (vendor_id) REFERENCES vendors (vendor_id);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 27
How Oracle handles new constraints
By default, Oracle verifies that existing data satisfies a new
constraint.
If that’s not what you want, you can add a disabled constraint.
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 28
A statement that renames a table
RENAME vendors TO vendor
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 29
The syntax of the CREATE INDEX statement
CREATE [UNIQUE] INDEX index_name
ON table_name (column_name_1 [ASC|DESC]
[, column_name_2 [ASC|DESC]]...)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 30
A statement that creates a unique index
CREATE UNIQUE INDEX vendors_vendor_phone_ix
ON vendors (vendor_phone);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 31
A statement that creates a function-based index
CREATE INDEX vendors_vendor_name_upper_ix
ON vendors (UPPER(vendor_name));
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 32
How to enable function-based indexes
CONNECT system/system;
ALTER SYSTEM SET QUERY_REWRITE_ENABLED=TRUE;
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 33
The syntax of the CREATE SEQUENCE statement
CREATE SEQUENCE sequence_name
[START WITH starting_integer]
[INCREMENT BY increment_integer]
[{MAXVALUE maximum_integer | NOMAXVALUE}]
[{MINVALUE minimum_integer | NOMINVALUE}]
[{CYCLE|NOCYCLE}]
[{CACHE cache_size|NOCACHE}]
[{ORDER|NOORDER}]
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 34
A statement that creates a sequence
CREATE SEQUENCE vendor_id_seq
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 35
Using the NEXTVAL pseudo column
INSERT INTO vendors
VALUES (vendor_id_seq.NEXTVAL, 'Acme Co.',
'123 Main St.', NULL,
'Fresno', 'CA', '93711', '(800) 221-5528',
'Wiley' , 'Coyote');
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 36
The syntax of the ALTER SEQUENCE statement
ALTER SEQUENCE sequence_name
[sequence_attributes]
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 37
The script that creates the AP schema
CONNECT ap/ap;
-- Use an anonymous PL/SQL script to drop
-- all tables and sequences in the current schema and
-- suppress any error messages that may be displayed
-- if these objects don't exist
BEGIN
EXECUTE IMMEDIATE 'DROP SEQUENCE vendor_id_seq';
EXECUTE IMMEDIATE 'DROP SEQUENCE invoice_id_seq';
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 38
The script that creates the AP schema (continued)
CREATE TABLE general_ledger_accounts
(
account_number NUMBER NOT NULL,
account_description VARCHAR2(50) NOT NULL,
CONSTRAINT gl_accounts_pk
PRIMARY KEY (account_number),
CONSTRAINT gl_account_description_uq
UNIQUE (account_description)
);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 39
The script that creates the AP schema (continued)
CREATE TABLE terms
(
terms_id NUMBER NOT NULL,
terms_description VARCHAR2(50) NOT NULL,
terms_due_days NUMBER NOT NULL,
CONSTRAINT terms_pk
PRIMARY KEY (terms_id)
);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 40
The script that creates the AP schema (continued)
CREATE TABLE vendors
(
vendor_id NUMBER NOT NULL,
vendor_name VARCHAR2(50) NOT NULL,
vendor_address1 VARCHAR2(50),
vendor_address2 VARCHAR2(50),
vendor_city VARCHAR2(50) NOT NULL,
vendor_state CHAR(2) NOT NULL,
vendor_zip_code VARCHAR2(20) NOT NULL,
vendor_phone VARCHAR2(50),
vendor_contact_last_name VARCHAR2(50),
vendor_contact_first_name VARCHAR2(50),
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 41
The script that creates the AP schema (continued)
default_terms_id NUMBER NOT NULL,
default_account_number NUMBER NOT NULL,
CONSTRAINT vendors_pk
PRIMARY KEY (vendor_id),
CONSTRAINT vendors_vendor_name_uq
UNIQUE (vendor_name),
CONSTRAINT vendors_fk_terms
FOREIGN KEY (default_terms_id)
REFERENCES terms (terms_id),
CONSTRAINT vendors_fk_accounts
FOREIGN KEY (default_account_number)
REFERENCES general_ledger_accounts (account_number)
);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 42
The script that creates the AP schema (continued)
CREATE TABLE invoices
(
invoice_id NUMBER NOT NULL,
vendor_id NUMBER NOT NULL,
invoice_number VARCHAR2(50) NOT NULL,
invoice_date DATE NOT NULL,
invoice_total NUMBER(9,2) NOT NULL,
payment_total NUMBER(9,2) DEFAULT 0,
credit_total NUMBER(9,2) DEFAULT 0,
terms_id NUMBER NOT NULL,
invoice_due_date DATE NOT NULL,
payment_date DATE,
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 43
The script that creates the AP schema (continued)
CONSTRAINT invoices_pk
PRIMARY KEY (invoice_id),
CONSTRAINT invoices_fk_vendors
FOREIGN KEY (vendor_id)
REFERENCES vendors (vendor_id),
CONSTRAINT invoices_fk_terms
FOREIGN KEY (terms_id)
REFERENCES terms (terms_id)
);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 44
The script that creates the AP schema (continued)
CREATE TABLE invoice_line_items
(
invoice_id NUMBER NOT NULL,
invoice_sequence NUMBER NOT NULL,
account_number NUMBER NOT NULL,
line_item_amt NUMBER(9,2) NOT NULL,
line_item_description VARCHAR2(100) NOT NULL,
CONSTRAINT line_items_pk
PRIMARY KEY (invoice_id, invoice_sequence),
CONSTRAINT line_items_fk_invoices
FOREIGN KEY (invoice_id)
REFERENCES invoices (invoice_id),
CONSTRAINT line_items_fk_acounts
FOREIGN KEY (account_number)
REFERENCES general_ledger_accounts (account_number)
);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 45
The script that creates the AP schema (continued)
-- Create the indexes
CREATE INDEX vendors_terms_id_ix
ON vendors (default_terms_id);
CREATE INDEX vendors_account_number_ix
ON vendors (default_account_number);
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 46
The script that creates the AP schema (continued)
-- Create the sequences
CREATE SEQUENCE vendor_id_seq
START WITH 124;
CREATE SEQUENCE invoice_id_seq
START WITH 115;
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 47
Notes for creating tables in a script
You must create the tables that don’t have foreign keys first.
When you drop tables, you start by dropping the last table that was
created and then work back to the first table that was created.
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 48
How to use a sequence as the default value
(12c and later)
CREATE TABLE vendors
(
vendor_id NUMBER
DEFAULT vendor_id_seq.NEXTVAL PRIMARY KEY,
vendor_name VARCHAR2(50) NOT NULL UNIQUE
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 49
A simple GENERATED clause
(12c and later)
CREATE TABLE vendors
(
vendor_id NUMBER
GENERATED AS IDENTITY PRIMARY KEY,
vendor_name VARCHAR2(50) NOT NULL UNIQUE
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 50
A more complex GENERATED clause
(12c and later)
CREATE TABLE vendors
(
vendor_id NUMBER GENERATED BY DEFAULT
AS IDENTITY ( START WITH 124) PRIMARY KEY,
vendor_name VARCHAR2(50) NOT NULL UNIQUE
)
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 51
Two statements that use the generated ID value
(12c and later)
The DEFAULT keyword
INSERT INTO vendors
VALUES (DEFAULT, 'default test');
A column list
INSERT INTO vendors (vendor_name)
VALUES ('column list test');
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 52
The column definitions for the Invoices table
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 53
The constraints for the Invoices table
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 54
The indexes for the AP schema
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 55
The sequences for the AP schema
Murach’s SQL and PL/SQL, C10 © 2014, Mike Murach & Associates, Inc. Slide 56