0% found this document useful (0 votes)
33 views

Indexes in Oracle An Introduction: Please Check Speaker Notes For Additional Information

The document discusses indexes and constraints in Oracle databases. It provides examples of adding different types of constraints like primary keys, unique constraints, and not null constraints. Primary keys must be unique and not null. Attempts to insert duplicate primary keys or null primary keys will result in errors. Foreign keys and other constraints help enforce data integrity.

Uploaded by

Luyen Bui
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Indexes in Oracle An Introduction: Please Check Speaker Notes For Additional Information

The document discusses indexes and constraints in Oracle databases. It provides examples of adding different types of constraints like primary keys, unique constraints, and not null constraints. Primary keys must be unique and not null. Attempts to insert duplicate primary keys or null primary keys will result in errors. Foreign keys and other constraints help enforce data integrity.

Uploaded by

Luyen Bui
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Indexes in Oracle

An Introduction
Please check speaker notes for
additional information!
Indexes

Without an index, Oracle will do a full table search where each row will be
checked. With a large database searching an index first will save on access time.
In looking an indexes, we are going to look at the large concept of constraints.
Constraints protests data. The two major categories of constraints are integrity
constraints that refer to key fields and value constraints that deal with the data that
is being keyed in. Indexes as constraints make sure that the primary key is unique
and that that the connection between a primary key and a foreign key is valid.
Constraints can be on a column or a table. The developer has the option of
naming some or all of the constraints used.
Constraints:
Check
Foreign Key
Primary Key
Not Null
Unique
Constraints

SQL> CREATE TABLE new_first_pay


2 AS
3 SELECT * FROM first_pay;

Table created.

SQL> SELECT * FROM new_first_pay;

Pay Id # NAME JO STARTDATE Salary BONUS


---------- -------------------- -- --------- ------------ ---------
1111 Linda Costa CI 15-JAN-97 $45,000.00 1000
2222 John Davidson IN 25-SEP-92 $40,000.00 1500
3333 Susan Ash AP 05-FEB-00 $25,000.00 500
4444 Stephen York CM 03-JUL-97 $42,000.00 2000
5555 Richard Jones CI 30-OCT-92 $50,000.00 2000
6666 Joanne Brown IN 18-AUG-94 $48,000.00 2000
7777 Donald Brown CI 05-NOV-99 $45,000.00
8888 Paula Adams IN 12-DEC-98 $45,000.00 2000

8 rows selected. Requests a listing of all


constraints associated with the
SQL> SELECT * FROM USER_CONSTRAINTS table new_first_pay. At the
2 WHERE TABLE_NAME = 'NEW_FIRST_PAY'; current time, there are none so no
rows selected is displayed.
no rows selected
Constraints
The ALTER statement can be used to add constraints to a table. In this case, I am
adding the PRIMARY KEY. The primary key is defined here as pay_id. That
means from now on the pay_id field will be treated as the primary key. It cannot
be null and you cannot add duplicates. The name of the constraint is
prim_key_new_first_pay.

SQL> ALTER TABLE new_first_pay


2 ADD CONSTRAINT prim_key_new_first_pay PRIMARY KEY(pay_id);

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'NEW_FIRST_PAY’;


OWNER CONSTRAINT_NAME C
------------------------------ ------------------------------ -
TABLE_NAME
------------------------------
SEARCH_CONDITION
--------------------------------------------------------------------------------
R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS
------------------------------ ------------------------------ --------- --------
SCOTT PRIM_KEY_NEW_FIRST_PAY P
NEW_FIRST_PAY

ENABLED
Constraints
Because pay_id is the primary key it cannot contain null values
or duplicates. The NOT NULL message appears in the
description for primary keys.
SQL> DESC new_first_pay;
Name Null? Type
------------------------------- -------- ----
PAY_ID NOT NULL VARCHAR2(4)
NAME VARCHAR2(20)
JOBCODE CHAR(2)
STARTDATE DATE
SALARY NUMBER(9,2)
BONUS NUMBER(5)

SQL> SELECT * FROM new_first_pay;

Pay Id # NAME JO STARTDATE Salary BONUS


---------- -------------------- -- --------- ------------ ---------
1111 Linda Costa CI 15-JAN-97 $45,000.00 1000
2222 John Davidson IN 25-SEP-92 $40,000.00 1500
3333 Susan Ash AP 05-FEB-00 $25,000.00 500
4444 Stephen York CM 03-JUL-97 $42,000.00 2000
5555 Richard Jones CI 30-OCT-92 $50,000.00 2000
6666 Joanne Brown IN 18-AUG-94 $48,000.00 2000
7777 Donald Brown CI 05-NOV-99 $45,000.00
8888 Paula Adams IN 12-DEC-98 $45,000.00 2000

8 rows selected.
Constraints SQL> clear columns
columns cleared
SQL> SELECT * FROM new_first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS


---- -------------------- -- --------- --------- ---------
1111 Linda Costa CI 15-JAN-97 45000 1000
2222 John Davidson IN 25-SEP-92 40000 1500
3333 Susan Ash AP 05-FEB-00 25000 500
4444 Stephen York CM 03-JUL-97 42000 2000
5555 Richard Jones CI 30-OCT-92 50000 2000
6666 Joanne Brown IN 18-AUG-94 48000 2000
7777 Donald Brown CI 05-NOV-99 45000
8888 Paula Adams IN 12-DEC-98 45000 2000

8 rows selected.

In this insert, I tried to insert a new record with an existing


pay_id. The Insert was rejected because of a unique
constraint violation.

SQL> INSERT INTO new_first_pay (pay_id, name, jobcode, salary)


2 VALUES('3333','Joseph Souza','IN',35000);
INSERT INTO new_first_pay (pay_id, name, jobcode, salary)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.PRIM_KEY_NEW_FIRST_PAY) violated
Constraints

Primary key cannot be null. In these examples, I tried


two ways of inserting a new record with pay_id null.
Both got rejected.

SQL> INSERT INTO new_first_pay (pay_id, name, jobcode, salary)


2 VALUES (null, 'Jane Doe', 'CI', 40000);
INSERT INTO new_first_pay (pay_id, name, jobcode, salary)
*
ERROR at line 1:
ORA-01400: mandatory (NOT NULL) column is missing or NULL during insert

SQL> INSERT INTO new_first_pay (name, jobcode, salary)


2 VALUES('Jane Doe', 'CI', 40000);
INSERT INTO new_first_pay (name, jobcode, salary)
*
ERROR at line 1:
ORA-01400: mandatory (NOT NULL) column is missing or NULL during insert
Constraints

There is no problem entering Joseph Souza as


long as I use a unique pay_id.

SQL> INSERT INTO new_first_pay (pay_id, name, jobcode, salary)


2 VALUES ('9999','Joseph Souza', 'IN', 35000);

1 row created.

SQL> SELECT * FROM new_first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS


---- -------------------- -- --------- --------- ---------
1111 Linda Costa CI 15-JAN-97 45000 1000
2222 John Davidson IN 25-SEP-92 40000 1500
3333 Susan Ash AP 05-FEB-00 25000 500
4444 Stephen York CM 03-JUL-97 42000 2000
5555 Richard Jones CI 30-OCT-92 50000 2000
6666 Joanne Brown IN 18-AUG-94 48000 2000
7777 Donald Brown CI 05-NOV-99 45000
8888 Paula Adams IN 12-DEC-98 45000 2000
9999 Joseph Souza IN 35000

9 rows selected.
Constraints

I created a new table with this code. The jobcode is the primary
key and therefore must be unique and not null, the jobname must
be unique and the joblevel must be not null.

SQL> CREATE TABLE first_pay_dept


2 (jobcode CHAR(2) CONSTRAINT prim_key_first_pay_dept PRIMARY KEY,
3 jobname VARCHAR2(20) CONSTRAINT jobname_uk UNIQUE,
4 joblevel VARCHAR2(5) CONSTRAINT joblevel_nn NOT NULL);

SQL> DESC first_pay_dept;


Name Null? Type
------------------------------- -------- ----
JOBCODE NOT NULL CHAR(2)
JOBNAME VARCHAR2(20)
JOBLEVEL NOT NULL VARCHAR2(5)
Constraints

SQL> SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'FIRST_PAY_DEPT';

OWNER CONSTRAINT_NAME C TABLE_NAME


------------------------------ ------------------------------ - ---------------------
SEARCH_CONDITION
--------------------------------------------------------------------------------
R_OWNER R_CONSTRAINT_NAME DELETE_RU STATUS
------------------------------ ------------------------------ --------- --------
SCOTT JOBLEVEL_NN C FIRST_PAY_DEPT
JOBLEVEL IS NOT NULL
ENABLED

SCOTT PRIM_KEY_FIRST_PAY_DEPT P FIRST_PAY_DEPT

ENABLED

SCOTT JOBNAME_UK U FIRST_PAY_DEPT

ENABLED
Constraints
Table after three records/rows inserted.

SQL> SELECT * FROM first_pay_dept;


I then tried to alter the new_first_pay table to make
JO JOBNAME JOBLE jobcode a foreign key linked to the primary key in
-- -------------------- ----- first_pay_dept which is also jobcode. The alter did not
CI Computer Information 3 up succeed because AP was missing from first_pay_dept.
CM Communications 3 up I then inserted AP in first_pay_dept and tried the alter
IN Internet/Network 3 up
again. This time it was successful and the link was
established.
SQL> ALTER TABLE new_first_pay
2 ADD CONSTRAINT new_first_pay_fk FOREIGN KEY(jobcode) REFERENCES first_pay_dept;
ALTER TABLE new_first_pay
*
ERROR at line 1:
ORA-02298: cannot enable (SCOTT.NEW_FIRST_PAY_FK) - parent keys not found

SQL> INSERT INTO first_pay_dept


2 VALUES('AP','Accounts Payable','1 up');

1 row created.

SQL> ALTER TABLE new_first_pay


2 ADD CONSTRAINT new_first_pay_fk FOREIGN KEY(jobcode) REFERENCES first_pay_dept;

Table altered. The alter is to the new_first_pay table and is establishing the jobcode on that table as a
foreign key which references the first_pay_dept table. When the reference is
established, it will link to the primary key on the first_pay_dept table which is jobcode.
Constraints
I tried to put a check on salary that would require it to be > 25000. The problem is
that an existing record had a salary of 25000 so the alter was rejected.
SQL> ALTER TABLE new_first_pay
2 ADD CONSTRAINT new_first_pay_ch CHECK(salary > 25000 AND salary < 99000);
ADD CONSTRAINT new_first_pay_ch CHECK(salary > 25000 AND salary < 99000)
*
ERROR at line 2:
ORA-02293: cannot enable (SCOTT.NEW_FIRST_PAY_CH) - check constraint violated

SQL> SELECT * FROM new_first_pay;


I modified the check
PAY_ NAME JO STARTDATE SALARY BONUS to salary >= 25000
---- -------------------- -- --------- --------- --------- and since there were
1111 Linda Costa CI 15-JAN-97 45000 1000 no records that
2222 John Davidson IN 25-SEP-92 40000 1500 violated this check,
3333 Susan Ash AP 05-FEB-00 25000 500 the table was altered.
4444 Stephen York CM 03-JUL-97 42000 2000
5555 Richard Jones CI 30-OCT-92 50000 2000
6666 Joanne Brown IN 18-AUG-94 48000 2000
7777 Donald Brown CI 05-NOV-99 45000
8888 Paula Adams IN 12-DEC-98 45000 2000
9999 Joseph Souza IN 35000

9 rows selected.

SQL> ALTER TABLE new_first_pay


2 ADD CONSTRAINT new_first_pay_ch CHECK(salary >= 25000 AND salary <= 99000);

Table altered.
Constraints SQL> SELECT * FROM first_pay_dept;

JO JOBNAME JOBLE
There is no jobcode AA in first_pay_dept. This -- -------------------- -----
is a foreign key violation since no parent key CI Computer Information 3 up
was found. CM Communications 3 up
IN Internet/Network 3 up
AP Accounts Payable 1 up
SQL> INSERT INTO new_first_pay
2 VALUES('2345','John Doe', 'AA', '10-SEP-00', 35000, null);
VALUES('2345','John Doe', 'AA', '10-SEP-00', 35000, null)
*
ERROR at line 2:
ORA-02291: integrity constraint (SCOTT.NEW_FIRST_PAY_FK) violated - parent key not found

Remember, I set up this check to prevent salary from being out of range. In the example below, I
attempted to insert a record with a salary below 25000. It was rejected because the new_first_pay_ch
was violated.

SQL> ALTER TABLE new_first_pay


2 ADD CONSTRAINT new_first_pay_ch CHECK(salary >= 25000 AND salary <= 99000);

Table altered.

SQL> INSERT INTO new_first_pay


2 VALUES('3456','David Anders', 'AP','10-JAN-00',22000, 500);
VALUES('3456','David Anders', 'AP','10-JAN-00',22000, 500)
*
ERROR at line 2:
ORA-02290: check constraint (SCOTT.NEW_FIRST_PAY_CH) violated
Indexes

The second index is a combination of two


SQL> CREATE TABLE test_index
fields: last_name and first_name.
2 (idno VARCHAR2(3),
Together they comprise the index.
3 last_name VARCHAR2(20),
4 first_name VARCHAR2(20),
5 party_des VARCHAR2(1),
6 vote99 VARCHAR2(1));

Table created.

SQL> CREATE INDEX idno_index ON test_index(idno);

Index created.

SQL> CREATE INDEX name_index ON test_index(last_name,first_name);

Index created.

SQL> CREATE INDEX party_index ON test_index(party_des);

Index created.
Shows the indexes that
were created.

SQL> SELECT * FROM USER_INDEXES WHERE TABLE_NAME = 'TEST_INDEX';


SQL> DESC test_index;
Indexes
Name Null? Type
------------------------------- -------- ----
IDNO VARCHAR2(3)
LAST_NAME VARCHAR2(20)
FIRST_NAME VARCHAR2(20)
PARTY_DES VARCHAR2(1)
VOTE99 VARCHAR2(1)
SQL> ALTER TABLE test_index
2 ADD CONSTRAINT idno_pk_test_index PRIMARY KEY(idno);
ADD CONSTRAINT idno_pk_test_index PRIMARY KEY(idno)
*
ERROR at line 2:
ORA-02439: Nonunique index exists on unique/primary key constraint

I tried to add the primary key constraint with an index


SQL> DROP INDEX idno_index; on idno in place. I got the message above. I then
dropped the nonunique index and add the constrain of a
Index dropped. primary key. This time it was accepted.
SQL> ALTER TABLE test_index
2 ADD CONSTRAINT idno_pk_test_index PRIMARY KEY(idno);

Table altered.

SQL> DESC test_index;


Name Null? Type
------------------------------- -------- ----
IDNO NOT NULL VARCHAR2(3)
LAST_NAME VARCHAR2(20)
FIRST_NAME VARCHAR2(20)
PARTY_DES VARCHAR2(1)
VOTE99 VARCHAR2(1)
Indexes/constraints

SQL> CREATE TABLE test_index_again


2 (idno VARCHAR2(3),
3 last_name VARCHAR2(20),
4 first_name VARCHAR2(20),
5 party_des VARCHAR2(1), The primary key was created based on
6 vote99 VARCHAR2(1)); last_name, first_name).

Table created.

SQL> ALTER TABLE test_index_again


2 ADD CONSTRAINT name_pk_test_index_again PRIMARY KEY (last_name, first_name);

Table altered.

SQL> DESC test_index_again;


Name Null? Type
------------------------------- -------- ----
IDNO VARCHAR2(3)
LAST_NAME NOT NULL VARCHAR2(20)
FIRST_NAME NOT NULL VARCHAR2(20)
PARTY_DES VARCHAR2(1)
VOTE99 VARCHAR2(1)
Indexes/constraints
First I put to records/rows
SQL> SELECT * FROM test_index_again;
into the table. Then I added
a record with the same last
IDN LAST_NAME FIRST_NAME P V
name as an existing record.
--- -------------------- -------------------- - -
There was no problem.
111 Jones Susan R Y
222 Smith John D N

SQL> INSERT INTO test_index_again


2 VALUES ('333','Smith','Ann','R','N');

1 row created. I then tried to insert


SQL> SELECT * FROM test_index_again; a record with the
same last name and
IDN LAST_NAME FIRST_NAME P V the same first name
--- -------------------- -------------------- - - as an existing
111 Jones Susan R Y record. The insert
222 Smith John D N was rejected because
333 Smith Ann R N the unique constraint
was violated.
SQL> INSERT INTO test_index_again
2 VALUES ('444','Jones','Susan','R','N');
INSERT INTO test_index_again
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.NAME_PK_TEST_INDEX_AGAIN) violated

You might also like