tags:

views:

49

answers:

6

Hi,

I have 2 tables

T_Foo
foo_id
fooHeader

T_FooBodys
foo_id
foobody

foo_id are primary key for both of their respective tables. In second table, foo_id is foreign key to first table. I generate the PK for both table values using a sequence and try to make insert - once into FooHeader and twice into FooBody.

MY code crashes on the second insert into t_FooBodys in the loop from error "ORA-00001: unique constraint (USERID.FooBodys_PK) violated"

So I have 2 questions:

1) What is the fundamental difference between MSSQL Server and Oracle here? This worked fine in SQL Server! I had one-to-one/many relationships all the time in there

2) What is the simplest way to fix this besides adding another key and essentially ending the shared primary key concept?

Thanks much

A: 

If foo_id is the primary key in the T_FooBodys table, then it must be unique. It sounds like you want foo_id to be a foreign key in the T_FooBodys. I would think SQL Server has the same constraint - that a primary key must be unique - that's basically the definition of a primary key.

Can you add a new PK to the Bodys table? And leave foo_id as the PK in the Header table and a foreign key in the Bodys table?

Ed Schembor
+2  A: 

foo_id are primary key for both of their respective tables

You cannot have duplicate entries for a PK with either MS SQL Server or Oracle, so you have made a mistake somewhere. You may have set up the FK relationship in SQL Server, but there is no way that you set foo_id in T_FooBodys as a PK and it still allowed duplicate entries.

RedFilter
*scratch head*... hummm.. I guess I'm thinking of 1->1 and not 1->N with shared PK. Duh... *brainfart*
dferraro
A: 
  1. I don't believe that it worked as you describe in MS SQL Server. If you had a primary key constraint on T_FooBodys.foo_id, then you could insert only one row with a given value.

  2. To make T_FooBodys accept multiple rows for a given value of foo_id, you should add another column and make a two-column primary key over foo_id with that new column. That will allow you have distinct values in the second column, preserving uniqueness.

    CREATE TABLE T_FooBodys (
      foo_id INTEGER NOT NULL,
      foo_body_id INTEGER NOT NULL,
      foobody TEXT,
      PRIMARY KEY (foo_id, foo_body_id),
      FOREIGN KEY (foo_id) REFERENCES T_Foo (foo_id)
    );
    
Bill Karwin
A: 

To answer given problem i will say :

  1. I don't know about MSSQL, but Oracle is strict to foreign key and primary key. To add some foreign key, you need to be sure that that id already exists in primary table. If that primary key is not exist, you will get ORA-xxx Constraint Violated. Similar to foreign key, the primary key constraint needs its value to be unique. When something's not unique is inserted as primary key, you will get ORA-xxx Primary Key Violated. I'm not sure whether MSSQL is as strict as Oracle or not. But in MySQL, this kind of situation can be avoided by using some engine which is not "too strict" (in foreign key constraint).
  2. To handle this, you can do several things. First, do not mix primary key and foreign key in same field. It helps when you separate them. Just let the primary key increases every time you insert and make sure that foreign key(in another field) doesn't break any constraint. Second, you can maintain this "multi-function field" concept by using only one field. But, in order to do that, you need to remove the primary key constraint from this multi-function field. Hence produce a table with no primary key. I am not really sure about this, since i don't have any access to Oracle database any longer. Someone please confirm this.
  3. After all, to use 1-1 relationship is like providing proof that you incorrectly design your table structure. Because, as you all may know, 1-1 relationship can be simplified into single table.

Hope it helps

jancrot
A: 

You should change the table T_FOOBODYS in the following way:

/*Create table*/
create table T_FOOBODYS  
(  
  FOOBODY_ID NUMBER(10) not null,  
  FOO_ID     NUMBER(10) not null,  
  FOOBODY    VARCHAR2(512)  
);

/* Create/Recreate primary, unique and foreign key constraints */
alter table FOOBODY  
  add constraint FBPK primary key (FOOBODY_ID)  
  ENABLE;  
alter table FOOBODY  
  add constraint FBFK foreign key (FOO_ID)  
  references T_FOO(FOO_ID);

Here, FOOBODY_ID stands for your primary key for your T_FOOBODYS table, and FOO_ID is a reference constraint to T_FOO.

be here now
A: 

RedFilter answered the question as to why you're getting the error. I'm wondering why the 2 tables are separate: it looks like there's a 1-to-1 relationship, and both header and body should be in the main table. The only reason not to do this would be if there was a performance consideration.

orbfish