Yet Another Oracle Certified Master Candidate

This is a record of my attempt to become certified as an Oracle Certified Master (OCM).

Help

 

Search

Go
 

Featured Links

 

Calendar

Previous January 2012 Next
SMTWTFS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
 

My Communities

 

Recently Updated Blogs

here and there
1 hr 36 mins ago

M A M Home Services
5 hrs 8 mins ago

Country Communicator
5 hrs 44 mins ago

Stories to Life
7 hrs 28 mins ago

Aus22
8 hrs 10 mins ago

view more

 

Blog Rating  (8 votes)    Rate this Blog  * Needs improving** Below average*** Good**** Recommended***** Excellent

< Previous Post | Homepage | Next Post >

 

Almost Solved Fred's Problem

Sunday 27 April, 2008 - 23:33 by Douglas in Default

views (272) | rating ooooo (0 votes)

Now to reproduce Fred's problem with Primary Key Puzzle. First, create the table without the primary key constraint.

CREATE TABLE pvssession (
  msisdn varchar2(20) not null,
  creationdate date not null,
  lastaccess date not null);

Next create a virtual index.

CREATE UNIQUE INDEX pk_pvssession_msisdn
  ON pvssession(msisdn)
  NOSEGMENT;

This has to be a UNIQUE index because it support a primary key constraint. The NOSEGMENT keyword makes this a virtual index.

Now to add the constraint:

ALTER TABLE pvssession
  ADD CONSTRAINT pk_pvssession_msisdn
  PRIMARY KEY (msisdn)
  USING INDEX;

The important thing here is to have the name of the constraint matching the unique index created previously.

USER_CONSTRAINTS shows:

select constraint_name,constraint_type,search_condition,index_name from user_constraints;

CONSTRAINT_NAMECONSEARCH_CONDITIONINDEX_NAME
SYS_C005395C"LASTACCESS" IS NOT NULL 
SYS_C005394C"CREATIONDATE" IS NOT NULL 
SYS_C005393C"MSISDN" IS NOT NULL 
PK_PVSSESSION_MSISDNP PK_PVSSESSION_MSISDN

Now the index name is not similar to what Fred reported. However, the following query returns no rows:

select * from user_indexes;

Inserted some test data:

insert into pvssession (msisdn,creationdate,lastaccess) values ('HOWDYDOODY', sysdate, sysdate );
commit;
insert into pvssession select * from pvssession;
commit;

MSISDNCREATIONDATELASTACCESS
HOWDYDOODY27-APR-0827-APR-08
HOWDYDOODY27-APR-0827-APR-08

What is happening is that Oracle has no way of enforcing a primary key constraint because the index does not physically exist. Since it does not know that the row with the same value of MSISDN already exists, Oracle allows the row to be inserted.

As for the mystery of the different index name, I can suggest that the constraint was created along with the index.

-- Remove the primary key constraint
ALTER TABLE pvssession DROP CONSTRAINT pk_pvssession_msisdn;
DROP INDEX pk_pvssession_pk;
-- Reove all data in the table
TRUNCATE TABLE pvssession;
-- Recreate the constraint
ALTER TABLE pvssession
  ADD CONSTRAINT pk_pvssession_msisdn
  PRIMARY KEY (msisdn)
  USING INDEX NOSEGMENT;

And I ended up at the same place I was before. I give up.

Permalink | Rate post * Needs improving** Below average*** Good**** Recommended***** Excellent

 

Leave a Comment

This blog accepts comments from BigBlog members only.

You need to log in to use your BigBlog Identity. Once you have logged in, we'll bring you back to this page to leave your comment.