You are on page 1of 56

Chapter 10

How to create tables,


indexes, and sequences

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]
)

Common column attributes


 NOT NULL
 UNIQUE
 DEFAULT

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)
)

A statement that creates a table


with column attributes
CREATE TABLE vendors
(
vendor_id NUMBER NOT NULL UNIQUE,
vendor_name VARCHAR2(50) NOT NULL UNIQUE
)

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

The syntax of a table-level


primary key constraint
[CONSTRAINT constraint_name]
PRIMARY KEY (column_name_1 [, column_name_2]...)

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
)

A table with named column-level constraints


CREATE TABLE vendors
(
vendor_id NUMBER
CONSTRAINT vendors_pk PRIMARY KEY,
vendor_name VARCHAR2(50)
CONSTRAINT vendor_name_nn NOT NULL
CONSTRAINT vendor_name_un 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)
)

A table with a two-column primary key constraint


CREATE TABLE invoice_line_items
(
invoice_id NUMBER NOT NULL,
invoice_sequence NUMBER NOT NULL,
line_item_description VARCHAR2(100) NOT NULL,
CONSTRAINT line_items_pk
PRIMARY KEY (invoice_id, invoice_sequence)
)

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}]

The syntax of a table-level


foreign key constraint
[CONSTRAINT constraint_name]
FOREIGN KEY (column_name_1 [, column_name_2]...)
REFERENCES table_name (column_name_1
[, column_name_2]...)
[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')

The response from the system


SQL Error: ORA-02291: integrity constraint
(EX.INVOICES_FK_VENDORS) violated - parent key not found
*Cause: A foreign key value has no matching
primary key value.
*Action: Delete the foreign key or add a matching
primary key.

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)

A statement with check constraints


CREATE TABLE invoices
(
invoice_id NUMBER PRIMARY KEY,
invoice_total NUMBER(9,2) NOT NULL
CHECK (invoice_total >= 0),
payment_total NUMBER(9,2) DEFAULT 0
CHECK (payment_total >= 0)
)

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)

The response from the system


SQL Error: ORA-02290: check constraint (EX.INVOICES_CK)
violated 02290. 00000 - "check constraint (%s.%s)
violated"
*Cause: The values being inserted do not satisfy the
named check
*Action: do not insert values that violate the
constraint.

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;

A statement that drops a column


ALTER TABLE vendors
DROP COLUMN last_transaction_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);

A statement that changes the type of a column


ALTER TABLE vendors
MODIFY vendor_name CHAR(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';

A statement that fails because it would lose data


ALTER TABLE vendors
MODIFY vendor_name VARCHAR2(10);

The response from the system


SQL Error: ORA-01441: cannot decrease column
length because some value is too big

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);

A statement that drops a check constraint


ALTER TABLE invoices
DROP CONSTRAINT invoice_total_ck;

A statement that adds a disabled constraint


ALTER TABLE invoices
ADD CONSTRAINT invoice_total_ck
CHECK (invoice_total >= 1) DISABLE;

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;

A statement that disables a constraint


ALTER TABLE invoices
DISABLE 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);

A statement that adds a unique constraint


ALTER TABLE vendors
ADD CONSTRAINT vendors_vendor_name_uq
UNIQUE (vendor_name);

A statement that adds a not null constraint


ALTER TABLE vendors
MODIFY vendor_name
CONSTRAINT vendors_vendor_name_nn NOT NULL;

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

A statement that deletes all data from a table


TRUNCATE TABLE vendor

A statement that deletes a table


from the current schema
DROP TABLE vendor

A statement that qualifies the table to be deleted


DROP TABLE ex.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]]...)

A statement that creates an index


based on a single column
CREATE INDEX invoices_vendor_id_ix
ON invoices (vendor_id);

A statement that creates an index


based on two columns
CREATE INDEX invoices_vendor_id_inv_no_ix
ON invoices (vendor_id, invoice_number);

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);

A statement that creates an index


that’s sorted in descending order
CREATE INDEX invoices_invoice_total_ix
ON invoices (invoice_total DESC);

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));

Another statement for a function-based index


CREATE INDEX invoices_balance_due_ix
ON invoices
(invoice_total - payment_total - credit_total DESC);

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;

A statement that drops an index


DROP INDEX vendors_vendor_state_ix

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

A statement that specifies a starting integer


CREATE SEQUENCE vendor_id_seq
START WITH 124

A statement that specifies all parameters


CREATE SEQUENCE test_seq
START WITH 100 INCREMENT BY 10
MINVALUE 0 MAXVALUE 1000000
CYCLE CACHE 10 ORDER;

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');

Using the CURRVAL pseudo column


SELECT vendor_id_seq.CURRVAL from dual;

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]

A statement that alters a sequence


ALTER SEQUENCE test_seq
INCREMENT BY 9
MINVALUE 99 MAXVALUE 999999
NOCYCLE CACHE 9 NOORDER;

A statement that drops a sequence


DROP SEQUENCE test_seq;

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';

EXECUTE IMMEDIATE 'DROP TABLE invoice_archive';


EXECUTE IMMEDIATE 'DROP TABLE invoice_line_items';
EXECUTE IMMEDIATE 'DROP TABLE invoices';
EXECUTE IMMEDIATE 'DROP TABLE vendor_contacts';
EXECUTE IMMEDIATE 'DROP TABLE vendors';
EXECUTE IMMEDIATE 'DROP TABLE terms';
EXECUTE IMMEDIATE 'DROP TABLE general_ledger_accounts';
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('');
END;
/

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);

CREATE INDEX invoices_vendor_id_ix


ON invoices (vendor_id);
CREATE INDEX invoices_terms_id_ix
ON invoices (terms_id);
CREATE INDEX line_items_account_number_ix
ON invoice_line_items (account_number);
CREATE INDEX invoices_invoice_date_ix
ON invoices (invoice_date DESC);

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

You might also like