Link to home
Start Free TrialLog in
Avatar of littlegiraffe
littlegiraffe

asked on

Oracle Primary key problem

I am using the book "Guerilla Oracle" By Richard J Staron to learn some basic Oracle. in the book there is a script to create a table lke this:

 
  CREATE TABLE  ANSWERS    (
   ANSWER_QUESTION_ID                  NUMBER(6)  PRIMARY KEY REFERENCES
                                                                      QUESTIONS(QUESTION_ID),
   ANSWER_ID                                       VARCHAR2(2) NOT NULL,
   ANSWER                                             VARCHAR2(30) NOT NULL
                          )
   TABLESPACE TABLESPACE_FOR_WAIVERS

Open in new window

                         ;
Then the table is to be populated with the INSERTS below, which cannot work as the PRIMARY KEY VALUE is being repeated. Has anyone figured out how to work around this?  

INSERT INTO   ANSWERS   VALUES (1, 1, 'cassettes, cd-roms, game boy');
INSERT INTO   ANSWERS   VALUES (1, 2, 'color, lasers, thin paper');
INSERT INTO   ANSWERS   VALUES (1, 3, 'memory, storage, wireless');
INSERT INTO   ANSWERS   VALUES (1, 4, 'software, licensing, freeware');
INSERT INTO   ANSWERS   VALUES (2, 1, 'CERN');
INSERT INTO   ANSWERS   VALUES (2, 2, 'POP');
INSERT INTO   ANSWERS   VALUES (2, 3, 'USTP');
INSERT INTO   ANSWERS   VALUES (2, 4, 'USSM');
INSERT INTO   ANSWERS   VALUES (3, 1, '1 second');
INSERT INTO   ANSWERS   VALUES (3, 2, '2 seconds');
INSERT INTO   ANSWERS   VALUES (3, 3, '4 seconds');
INSERT INTO   ANSWERS   VALUES (3, 4, 

Open in new window

'8 seconds')



 
SOLUTION
Avatar of Raja Jegan R
Raja Jegan R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I think if you remove the primary key from  create table statement then you will be able to load the data sucessfully:

As the primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a null value. A table can have only one primary key.

So as you have the multiple repeted records in your data for which you are getting the error.
--Create  the table as like below:

CREATE TABLE ANSWERS
  (
    ANSWER_QUESTION_ID NUMBER(6)  REFERENCES QUESTIONS(QUESTION_ID),
    ANSWER_ID          VARCHAR2(2) NOT NULL,
    ANSWER             VARCHAR2(30) NOT NULL
  )
  TABLESPACE TABLESPACE_FOR_WAIVERS ;

-- Insert the data into table :
BEGIN 
INSERT INTO   ANSWERS   VALUES (1, 1, 'cassettes, cd-roms, game boy');
INSERT INTO   ANSWERS   VALUES (1, 2, 'color, lasers, thin paper');
INSERT INTO   ANSWERS   VALUES (1, 3, 'memory, storage, wireless');
INSERT INTO   ANSWERS   VALUES (1, 4, 'software, licensing, freeware');
INSERT INTO   ANSWERS   VALUES (2, 1, 'CERN');
INSERT INTO   ANSWERS   VALUES (2, 2, 'POP');
INSERT INTO   ANSWERS   VALUES (2, 3, 'USTP');
INSERT INTO   ANSWERS   VALUES (2, 4, 'USSM');
INSERT INTO   ANSWERS   VALUES (3, 1, '1 second');
INSERT INTO   ANSWERS   VALUES (3, 2, '2 seconds');
INSERT INTO   ANSWERS   VALUES (3, 3, '4 seconds');
INSERT INTO   ANSWERS   VALUES (3, 4, '5 seconds');
end;

--- check if you are able to select the data
select * from ANSWERS;

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of littlegiraffe
littlegiraffe

ASKER

Which approach is most advantageous and why?

How do I create a composite Primary Key?
composite Primary is better rather then removing the key as this can be used as refence further to make any relationship to other entitys.
In my last post I have show how you can create the composite Primary Key in your table.
You can run the attached code and check if you are able to see primary key created and also check for alll the records.
oooops....I see how the create the composite key in pinkuray's code. I'll try that
>> I see how the create the composite key in pinkuray's code

You can create Composite keys as mentioned in pinkuray's code or you can also use ALTER TABLE to create Primary keys as suggested in my first comment..
Thanks for your help, I am splitting the point between you.