Home » SQL & PL/SQL » SQL & PL/SQL » Options for definitions of INDEXES used by CONSTRAINTS
Options for definitions of INDEXES used by CONSTRAINTS [message #642632] Tue, 15 September 2015 16:10 Go to next message
mjm22
Messages: 54
Registered: January 2010
Location: Singapore
Member
Hi,

I had an interesting conversation today regarding INDEX definition for PRIMARY KEY CONSTRAINTS. So, I am interested in determining if there is any difference between the below three options for control of specifying an index when adding a constraint..

I have a table
CREATE TABLE test_table AS (account_no NUMBER(10), account_category NUMBER(1), currency_code VARCHAR2(3))
/



On which I want to create a primary key on account_no and account_category, but I want to specify the index so I more control (tablespace, etc).
I can think of a few ways to do this:

1. Create Primary Key with a using index clause to specify the unique INDEX
ALTER TABLE test_table
  ADD CONSTRAINT pk_test_table
    PRIMARY KEY (account_no, account_category)
    USING INDEX (CREATE UNIQUE INDEX pk_test_table ON test_table (account_no, account_category);
/


2. Create Index and then Primary Key with a using index clause, naming the index
CREATE UNIQUE INDEX pk_test_table ON test_table
(account_no, account_category)
/

ALTER TABLE test_table
  ADD CONSTRAINT pk_test_table
    PRIMARY KEY (account_no, account_category)
    USING INDEX pk_test_table
/



3. Create Index and then Primary Key with a using index clause and let Oracle figure out index
CREATE UNIQUE INDEX pk_test_table ON test_table
(account_no, account_category)
/

ALTER TABLE test_table
  ADD CONSTRAINT pk_test_table
    PRIMARY KEY (account_no, account_category)
    USING INDEX
/


My question is, is there any fundamental difference in implementation between the above options? As I understand it, the final option is not so good, if there is more than one index on the table using the primary key columns then Oracle may not select the one I intend?

Other than that is there any benefit of using one method over the other?

Thanks in advance,

Mike
Re: Options for definitions of INDEXES used by CONSTRAINTS [message #642633 is a reply to message #642632] Tue, 15 September 2015 18:14 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
BTW - The INDEX that supports the PK constraint does not need to be a UNIQUE INDEX.

How does this fact impact your analysis & results?
Re: Options for definitions of INDEXES used by CONSTRAINTS [message #642640 is a reply to message #642632] Wed, 16 September 2015 01:56 Go to previous message
John Watson
Messages: 8928
Registered: January 2010
Location: Global Village
Senior Member
If you use a unique index, you can't defer the constraint.
Previous Topic: Dynamic SQL in cursor
Next Topic: Table patition and aautomate sub partition.
Goto Forum:
  


Current Time: Tue Apr 16 16:32:32 CDT 2024