Indexes in Oracle An Introduction: Please Check Speaker Notes For Additional Information
Indexes in Oracle An Introduction: Please Check Speaker Notes For Additional Information
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
Table created.
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)
8 rows selected.
Constraints SQL> clear columns
columns cleared
SQL> SELECT * FROM new_first_pay;
8 rows selected.
1 row created.
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.
ENABLED
ENABLED
Constraints
Table after three records/rows inserted.
1 row created.
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
9 rows selected.
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.
Table altered.
Table created.
Index created.
Index created.
Index created.
Shows the indexes that
were created.
Table altered.
Table created.
Table altered.