Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indexes in Oracle An Introduction Please check speaker notes for additional information!

Similar presentations


Presentation on theme: "Indexes in Oracle An Introduction Please check speaker notes for additional information!"— Presentation transcript:

1 Indexes in Oracle An Introduction Please check speaker notes for additional information!

2 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

3 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. SQL> SELECT * FROM USER_CONSTRAINTS 2 WHERE TABLE_NAME = 'NEW_FIRST_PAY'; no rows selected Constraints Requests a listing of all constraints associated with the table new_first_pay. At the current time, there are none so no rows selected is displayed.

4 SQL> ALTER TABLE new_first_pay 2 ADD CONSTRAINT prim_key_new_first_pay PRIMARY KEY(pay_id); 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> 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

5 Constraints 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) 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. SQL> SELECT * FROM new_first_pay; 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.

6 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. Constraints 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 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.

7 Constraints 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 Primary key cannot be null. In these examples, I tried two ways of inserting a new record with pay_id null. Both got rejected.

8 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 There is no problem entering Joseph Souza as long as I use a unique pay_id.

9 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); 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> DESC first_pay_dept; Name Null? Type ------------------------------- -------- ---- JOBCODE NOT NULL CHAR(2) JOBNAME VARCHAR2(20) JOBLEVEL NOT NULL VARCHAR2(5)

10 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

11 SQL> SELECT * FROM first_pay_dept; JO JOBNAME JOBLE -- -------------------- ----- CI Computer Information 3 up CM Communications 3 up IN Internet/Network 3 up Table after three records/rows inserted. 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. I then tried to alter the new_first_pay table to make jobcode a foreign key linked to the primary key in first_pay_dept which is also jobcode. The alter did not succeed because AP was missing from first_pay_dept. I then inserted AP in first_pay_dept and tried the alter again. This time it was successful and the link was established. 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.

12 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; 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. SQL> ALTER TABLE new_first_pay 2 ADD CONSTRAINT new_first_pay_ch CHECK(salary >= 25000 AND salary <= 99000); Table altered. 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. I modified the check to salary >= 25000 and since there were no records that violated this check, the table was altered.

13 Constraints SQL> SELECT * FROM first_pay_dept; JO JOBNAME JOBLE -- -------------------- ----- CI Computer Information 3 up 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 There is no jobcode AA in first_pay_dept. This is a foreign key violation since no parent key was found. 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 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.

14 Indexes SQL> CREATE TABLE test_index 2 (idno VARCHAR2(3), 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. The second index is a combination of two fields: last_name and first_name. Together they comprise the index. SQL> SELECT * FROM USER_INDEXES WHERE TABLE_NAME = 'TEST_INDEX'; Shows the indexes that were created.

15 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 SQL> DROP INDEX idno_index; Index dropped. 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) SQL> DESC test_index; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(3) LAST_NAME VARCHAR2(20) FIRST_NAME VARCHAR2(20) PARTY_DES VARCHAR2(1) VOTE99 VARCHAR2(1) Indexes I tried to add the primary key constraint with an index on idno in place. I got the message above. I then dropped the nonunique index and add the constrain of a primary key. This time it was accepted.

16 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), 6 vote99 VARCHAR2(1)); Table created. SQL> ALTER TABLE test_index_again 2 ADD CONSTRAINT name_pk_test_index_again PRIMARY KEY (last_name, first_name); Table altered. Indexes/constraints 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) The primary key was created based on last_name, first_name).

17 Indexes/constraints SQL> SELECT * FROM test_index_again; IDN LAST_NAME FIRST_NAME P V --- -------------------- -------------------- - - 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. First I put to records/rows into the table. Then I added a record with the same last name as an existing record. There was no problem. SQL> SELECT * FROM test_index_again; IDN LAST_NAME FIRST_NAME P V --- -------------------- -------------------- - - 111 Jones Susan R Y 222 Smith John D N 333 Smith Ann R N 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 I then tried to insert a record with the same last name and the same first name as an existing record. The insert was rejected because the unique constraint was violated.


Download ppt "Indexes in Oracle An Introduction Please check speaker notes for additional information!"

Similar presentations


Ads by Google