Solved

Oracle Primary key problem

Posted on 2010-11-16
8
563 Views
Last Modified: 2013-12-19
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')



 
0
Comment
Question by:littlegiraffe
  • 3
  • 3
  • 2
8 Comments
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 34144145
You need to drop your Existing Primary key and create Composite primary key on these two columns ANSWER_QUESTION_ID and ANSWER_ID to get it work.
0
 
LVL 4

Expert Comment

by:pinkuray
ID: 34144199
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

0
 
LVL 4

Accepted Solution

by:
pinkuray earned 250 total points
ID: 34144291
Yes using Composite primary key on 2 columns you cna also get rid of your errors as rrjegan17 said:



--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,
    PRIMARY KEY (ANSWER_QUESTION_ID ,ANSWER_ID)
  )
  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

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:littlegiraffe
ID: 34144307
Which approach is most advantageous and why?

How do I create a composite Primary Key?
0
 
LVL 4

Expert Comment

by:pinkuray
ID: 34144505
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.
0
 

Author Comment

by:littlegiraffe
ID: 34144581
oooops....I see how the create the composite key in pinkuray's code. I'll try that
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 34144640
>> 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..
0
 

Author Closing Comment

by:littlegiraffe
ID: 34146047
Thanks for your help, I am splitting the point between you.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now