A table with two foreign keys

Hi all

I need to create a table (mytable) filled from 2 different tables, for example, table1 and table2

When the rows of the table (mytable) comes from the first table (table1) columns in mytable (foreign key to the second table) are nulled, when they come (foreign key) column in the second table (table2)
the first table) are cancelled as well.

In terms of performance, architecture is valid, in other words, create a foreign key 2 on mytable and consequently, (the composite foreign key in this table are null according to whether they belong to the first or the second table)?

Thanks in advance for any help

I hope to clear

As promised,

mhouri.world > create table p as select * from all_users;

Table created.

mhouri.world > alter table p add constraint p_pk primary key (user_id);

Table altered.

mhouri.world > create table c (user_id references p, name varchar2(30));

Table created.

mhouri.world > select min(user_id),max(user_id) from p;

MIN(USER_ID) MAX(USER_ID)
------------ ------------
           0           72                                                       

mhouri.world > insert into c (user_id, name) values (0, 'test without index');

1 row created.

Thanks to the autonomous operation I simulates the effect of two users working on the same parent/child table

(a) first of all, without any index covering the FK on the child table

(b) second with a function based index on the FK column (note the desc clause in the definition of the index)

(c) and finally with an index b-tree on the FK columns

case (a)

mhouri.world > declare
  2  pragma autonomous_transaction;
  3  begin
  4  delete from p where user_id = 72;
  5  commit;
  6  end;
  7  /
declare
*
ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource
ORA-06512: at line 4 

mhouri.world > rollback;

Rollback complete.

case (b)

mhouri.world > create index fbi_c_fk on c (user_id desc);

Index created.

mhouri.world > insert into c (user_id, name) values (0, 'test with fbi index');

1 row created.

mhouri.world > declare
  2  pragma autonomous_transaction;
  3  begin
  4  delete from p where user_id = 72;
  5  commit;
  6  end;
  7  /
declare
*
ERROR at line 1:
ORA-00060: deadlock detected while waiting for resource
ORA-06512: at line 4 

mhouri.world > rollback;

Rollback complete.

case (c)

mhouri.world > create index btree_c_fk on c (user_id);

Index created.

mhouri.world > insert into c (user_id, name) values (0, 'test with b-tree index');

1 row created.

mhouri.world > declare
  2  pragma autonomous_transaction;
  3  begin
  4  delete from p where user_id = 72;
  5  commit;
  6  end;
  7  /

PL/SQL procedure successfully completed.

Only the index b-tree covered with success the threat of the deadlock in key foreign unidexed

Best regards

Mohamed Houri

Tags: Database

Similar Questions

  • ORA-02449: unique/primary keys in table referenced by foreign keys

    SQL > create table empinformation
    () 2
    primary key pk_empinformation number (6) 3 forced mobileno
    4 address varchar (100),
    5 salary number (10),
    6 personalid varchar (10) constraints fk_employees_section references employee (emp_id));

    Table created.


    SQL > drop table empinformation;

    ORA-02449: unique/primary keys in table referenced by foreign keys

    solution

    This error happens when the foreign key of a table is referenced by the primary key of the other table.

    If you want to remove the table had refernce key then, you must
    need to remove this table with the foreign key is referenced.

    or

    SQL > drop table EMPLOYEE CASCADE CONSTRAINTS;
    Deleted table.

    This will remove the table parent without droping the child table.

    Published by: Ritesh Singh October 3, 2011 14:04

  • Two foreign keys of a table in a single column

    Hi gurus

    I wonder that can we add two foreign keys of a table on a single column, I think the answer is, but what should be the reason behind this?

    Appreciate if someone there explain to me...

    Concerning

    Muzz

    Hello Muzz,

    Perhaps the example of human Chen reveals a design error.
    If a student has a 'teacher_id' "teacher" FK and FK for "emp" becaue each teacher is an EMP, I guess that the correct design would be to have only the FK of 'teacher' and to have another pointing to the 'emp' table in the 'teacher' tabe FK

    But we can imagine comical situations...
    CREATE TABLE PEOPLE (id NUMBER of KEY PRIMARY, name VARCHAR2 (20 CHAR),...);
    CREATE TABLE SCIENTIST (id people KEY PRIMARY NUMBER (id) REFERENCES, discipline VARCHAR2 (20 CHAR),...);
    CREATE TABLE POLICEMAN (id REFERENCES to KEY PRIMARY NUMBER (id), hire_date people DATE,...);

    First table = all.
    2nd: a subset of the first, with people having the profile of 'scientific '...
    3rd: a subset of the first, with people being a policeman.
    It might be people first table and in none of the 2nd and 3rd, 1st and 2nd, 1st and 3rd only or in the 3 tables.

    Then we can have a fourth table of 'something' referring to a person who must be a scientist and a police officer.

    Here is a sqlplus session illustrating; the last piece: I try inserting several lines in xxxx, only the last is accepted.

    SQL > CREATE TABLE PEOPLE (id NUMBER of the PRIMARY KEY, name VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE SCIENTIST (people of REFERENCES of KEY PRIMARY NUMBER id (id), discipline VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE POLICEMAN (people of REFERENCES of KEY PRIMARY NUMBER id (id), DATE hire_date);
    Table created.

    SQL > CREATE TABLE XXXX (id PRIMARY KEY NUMBER, people_id NUMBER, any other VARCHAR2 (30 CHAR));
    Table created.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxsci FOREIGN KEY (people_id) scientific xxxx REFERENCES (id);
    Modified table.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxpol FOREIGN KEY (people_id) police xxxx REFERENCES (id);
    Modified table.

    SQL > INSERT INTO person VALUES (100, 'John');
    1 line of creation.

    SQL > INSERT INTO person VALUES (120, 'Mary');
    1 line of creation.

    SQL > INSERT INTO person VALUES (103, 'Tom');
    1 line of creation.

    SQL > INSERT INTO person VALUES (123, "Bruno");
    1 line of creation.

    SQL > INSERT INTO VALUES of scientific (120, 'Chemistry');
    1 line of creation.

    SQL > INSERT INTO scientific VALUES (123, 'Mathematics');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (103, DATE ' 2001-04-01');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (123, DATE ' 1998-07-01');
    1 line of creation.

    SQL > INSERT INTO xxxx VALUES (1, 456, "nothing");
    ERROR on line 1:
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 100, "only to people");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 120, "only the learned");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 103, "only police officer");
    ORA-02291: integrity constraint (SYS. FK_XXSCI) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 123, 'ok');
    1 line of creation.

    Best regards

    Bruno Vroman.

  • Two foreign keys

    Hi, I would like to create a table with two strangers with the same name.

    I have a first table named ' * registry * ' with a column named "* id_registry *" PK, and another called 'friends' table with two columns "* id_res *" and "* id_friends." Now I would like to id_res and id_friends should be CF and have the same id_registry PK as references.
    I tried this way:
    ALTER TABLE friends add friends COSTRAINT: FK REFERENCES FOREIGN KEY (id_res, id_friends) anagrafica (id_registry); but I got an error like this: ORA-02256.

    Is possible to do?

    You must create two foreign keys separated like that.

    ALTER TABLE friends ADD CONSTRAINT friends_FK1 FOREIGN KEY (id_res) REFERENCES anagrafica (id_registry);
    
    ALTER TABLE friends ADD CONSTRAINT friends_FK2 FOREIGN KEY (id_friends)  REFERENCES anagrafica (id_registry);
    
  • SQL statement error INSERT The conflicted with the FOREIGN KEY constraint

    I recently installed a reporting for vmware vsphere software, but I get a SQL error. I opened a request for assistance with vmware, but so far they have not come up with a solution. The error is caused by: com.microsoft.sqlserver.jdbc.SQLServerException: instruction INSERT The conflicted with the FOREIGN KEY constraint 'FK_CB_VSM_NETWORK_VC_ID '. The conflict occurred in database 'VCChargebackVCC02', table "dbo." " CB_VSM_SERVER', column 'VC_ID '. I don't know a lot about SQL, so I'm lost in the extent of troubleshooting is concerned. If anyone has any ideas I'd love to hear them.

    SQL questions are better posed on Technet.  They are better equipped to manage the

    http://social.technet.Microsoft.com/forums/en-us/categories/

  • ORA-02266: permit to unique/primary keys in table referenced by foreign keys

    Hello
    I'm trying to delete data from a table by dropping a partition. I've identified all the child tables by running the following command.

    Select "select count (*) from ' |" table_name |' where employee_id = 100; »
    of dba_constraints
    where constraint_type = 'R '.
    and r_constraint_name in
    (select constraint_name from dba_constraints
    where constraint_type in ('P', 'U') and table_name =' EMPLOYEE);


    "SELECTCOUNT (*) OF | TABLE_NAME | "WHEREEMPLOYEE_ID_ID = 100; »
    -----------------------------------------------------------------------------------------------
    Select count (*) in the PT_ORDERS where employee_id = 100;
    Select count (*) in the PT_DEP where employee_id = 100;
    Select count (*) in the PT_SKILLSET where employee_id = 100;

    I dropped the score for number 100 in all child tables. The count (*) select returns 0 rows for each of the foregoing.

    When I try to run the command on the EMPLOYEE table, below I get ' ORA-02266: unique/primary keys in table referenced by foreign keys enabled.

    Drop partition ALTER table EMPLOYEE EMP_ID_100;

    I don't see why I am unable to give up this partition now because it is data child present in any of the referenced tables. Any suggestions or help on this would be greatly appreciated.

    Thank you.

    RGS,
    Rob

    You must first disable foreign key constraints and delete the partition. Deletion of lines or a fall in childs partitions do not work in this case
    as you have the overall dependence:


    SQL > create table scott.t (x int primary key, int y)
    2 partition by (list (y)
    3 values p_1 (1) partition, partition values p_2 (2))
    4.

    Table created.

    SQL > create table scott.t_c (x int references scott.t (x), int y)
    2 partition by (list (y)
    3 values p_1 (1) partition, partition values p_2 (2))
    4.

    Table created.

    SQL > insert into scott.t values (1,1)
    3 N

    1 line of creation.

    SQL > insert into scott.t values (2,2)
    3 N

    1 line of creation.

    SQL > insert into scott.t_c values (1,1)
    3 N

    1 line of creation.

    SQL > insert into scott.t_c values (2,2)
    3 N

    1 line of creation.

    SQL > commit;

    Validation complete.

    SQL > alter table scott.t_c drop partition p_2.

    Modified table.

    SQL > alter table scott.t drop partition p_2.
    ALTER table drop partition p_2 scott.t
    *
    ERROR on line 1:
    ORA-02266: permit to unique/primary keys in table referenced by foreign keys

    SQL > select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'P '.
    3 and table_name = 't';

    CONSTRAINT_NAME
    ------------------------------
    SYS_C0011058

    SQL > select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'R '.
    3 and r_constraint_name = "SYS_C0011058";

    CONSTRAINT_NAME
    ------------------------------
    SYS_C0011059

    SQL > alter the constraint to disable scott.t_c table SYS_C0011059;

    Modified table.

    SQL > alter table scott.t drop partition p_2.

    Modified table.

    SQL > alter table scott.t_c enable novalidate constraint SYS_C0011059;

    Modified table.

    I guess you should consider options such as partitioning Referencial (with some restrictions).

    Best wishes
    Dmitry.

  • Two foreign keys to a primary key

    I can't display information to two foreign keys in my table. My purpose is to display the name and surname of a project manager and account manager. I have a "Jobs" table that is linked to a table 'employees '.

    Jobs
    ProjectMgrID
    AcctMgrID

    Employees
    EmployeeID
    LastName
    FirstName

    The ProjectMgrID (fk) and the AcctMgrID (fk) are related to the EmployeeID (pk). When I create a query, I do not know how to display the first name and the name of the fields for both. It only allows me to add them once and when I add both times, it just displays the same information twice.

    Help, please!

    Welcome to the forum!

    Looks like you will have to join EMPLOYEES twice.

    Something like the following:

    SELECT  e1.firstname
    ,       e1.lastname
    ,       e2.firstname
    ,       e2.lastname
    FROM    jobs
    JOIN    employees e1    ON e1.employeeid = jobs.projectmgrid
    JOIN    employees e2    ON e2.employeeid = jobs.acctmgrid
    ;
    
  • How to create a table with two unique columns

    How to create a table with two unique columns. I use the following syntax below and I get the error message such unique or primary key already exists.

    create the table COPQ_WORKCELL_GOAL
    (
    Type_id varchar2 (4) NOT NULL UNIQUE,
    Tyle_Location varchar2 (30) NOT NULL UNIQUE,
    Type_Description varchar2 (20).
    KEY elementary SCHOOL (Type_id)
    );

    Use 1 or the other column type_id;

    create table COPQ_WORKCELL_GOAL
    (
      Type_id varchar2(4),
      Tyle_Location varchar2(30) NOT NULL UNIQUE,
      Type_Description varchar2(20),
      PRIMARY KEY (Type_id)
    );
    
    Table created
    drop table COPQ_WORKCELL_GOAL;
    
    Table dropped
    create table COPQ_WORKCELL_GOAL
    (
      Type_id varchar2(4) primary key,
      Tyle_Location varchar2(30) NOT NULL UNIQUE,
      Type_Description varchar2(20)
    );
    
    Table created
    
  • adobe products come with two product keys?

    adobe products come with two product keys (serial number)? in this way, you can use the product on 2 separate devices?

    Here is the link to the forum with details about the license delegating.

    http://forums.Adobe.com/message/4379859

    Sanjay.

  • Fill a table with two columns using a custom bean

    Hello

    Can you provide me or give me a link to an example of populating a table (with two columns) with a custom bean?

    Thank you

    TSPS

    Hello..
    I'm Jules Destrooper is what you want

    http://download.Oracle.com/docs/CD/E18941_01/tutorials/jdtut_11r2_36/jdtut_11r2_36.html

    Hopes, will help you

  • Two constraints of primary key of a table with two different constraint name

    Hi guys

    I am creating the table below

    Table name: TITLE_COPY

    Column name Copy_id Title_id Status

    Key type

    PKPK, FK
    UniqueNN, UNN, UNN
    CheckAvailable, destroyed, leased, reserved
    Table Ref FKTitle
    FK Ref ColTitle_id
    Data typeNumberNumberVarchar2
    Length101015

    The SQL statement below can create it

    CREATE TABLE title_copy (copy_id NUMBER (10),)

    title_id CONSTRAINT NUMBER (10) title_copy_title_id_fk title (title_id) REFERENCES.

    primary key constraint title_copy_title_id_pk title_id (copy_id)

    status VARCHAR2 (15) NOT NULL CONSTRAINT status_ck CHECK (status IN ('AVAILABLE', 'DESTROYED', 'RENTAL', 'RESERVED'))

    );

    Two primary keys (title_id, copy_id) are created with the same name of constraint (title_copy_title_id_pk) (-> table-level constraint). What happens if I want to have two different names of the primary key constraint, say title_copy_title_id_pk for the title_id column and title_copy_copy_id_pk for the copy_id column?

    I can do that with a column-level constraint? Something like below (complains about the additional primary key)

    CREATE TABLE title_copy (copy_id NUMBER (10) CONSTRAINT title_copy_copy_id_pk PRIMARY KEY,)

    constraint title_copy_title_id_pk primary key (title_id).

    title_id CONSTRAINT NUMBER (10) title_copy_title_id_fk title (title_id) REFERENCES.

    status VARCHAR2 (15) NOT NULL CONSTRAINT status_ck CHECK (status IN ('AVAILABLE', 'DESTROYED', 'RENTAL', 'RESERVED'))

    );

    In summary, I can define two primary keys with the name of different constraint in a column-level constraint?

    There are no two primary keys. There is a primary key made up of two columns - also called a composite key.

    A table can have at least a primary key. You can, if you wish, create a unique constraint on a different set of columns.

  • Join sorted on 2 tables, not a foreign key

    Hello

    I have two tables:

    BookAuthor
    -------------
    number surrogateid
    AuthorName varchar
    number bookid

    Books
    -------------------
    number bookid
    BookTitle varchar
    other columns

    I want to make some kind of query to join on the two tables where the query arguments are: rangefirst and: rangelast for 2 authornames, where I want to catch all bookauthors authorname between implicit, sorted, and then join the table books to fetch the booktitle and bookid and the authorname, and authornames which are the same also sort the product join by booktitle to the Books table.

    I hope it's possible!

    NOTE: there no foreign key relationship, so if a line of BookAuthor bookid value that does not exist in the table of books, I don't want a line into the product.

    And what kind of evidence do I need on each table.

    Thank you
    Andy

    You need clues if your table is large enough and you select only a few records in the table.

    If the create index case bookid on tables.

    Once the data are joined, you have no clue to the order.

    G.

  • Large partition table with the primary key referenced by several child tables

    I tested this procedure and it works perfectly. Although when children tables refer to the key column, after execution of dbms_redefinition.finish_redef_table they all are referring to table exercised. In test I deal forced FK droped on tables children and recreated them. What could be the best way to do this in this scenario?

    Thanks for all comments/opinions!

    Are you sure? You should end up with the original FK constraint (disabled and renamed to have a prefix of $$ TMP) pointing to the staging table (which is of course the famous original table), and allowed a new constraint with the correct name, pointing to the new version of the table. so all you need to do to bring order to the top is to remove the constraint of renamed.

    This running-in of the SCOTT schema clearly indicates:

    create the table deptint in select * from dept where 1 = 2;

    exec dbms_redefinition.start_redef_table (user, 'dept', 'deptint')

    n number of var

    exec dbms_redefinition.copy_table_dependents (user, "dept', 'deptint', DBMS_REDEFINITION.") CONS_ORIG_PARAMS, TRUE, TRUE, TRUE, TRUE,: n)

    Print n

    exec dbms_redefinition.finish_redef_table (user, 'dept', 'deptint')

    Select CONSTRAINT_NAME, CONSTRAINT_TYPE, TABLE_NAME, user_constraints STATE;

  • Main table with two tables of children as part of the ADF

    Hello

    I'm trying to implement single master with two tables in detail using oracle adf framework of Jdev 11.1.1.4.0. I am able to only master / details using the link display but unable to reach the details i.e., nested block child master with nested blocks.

    I created query view object based on the master and two poll objects of the base seen as details. Then, I created two master of first child link and another link for master of the second child. Even in this case in my data controls I see two different components which is incorrect.

    Please let me know how to create a data control for an example below:
    Fruit [MASTER]
    -Details of Fruits such as the table of the adf
    -Apples [FIRST CHILD]
    -Details of the apples as adf table
    -Orange [SECOND CHILD]
    -Details of the Oranges as adf table

    Kind regards
    Amar.

    You need two viewLinks
    Fruit-> apples
    Fruit-> Oranges

    Then, in the data model, you choose the entry of fruit that has:
    Fruit
    |---> Apples

    You stand on the fruit and you shuttle Oranges to be below left to right.

    If you use the HR schema by default, you will see this type of relationship for employees:
    https://blogs.Oracle.com/Shay/entry/master_with_two_details_on_the

  • How to associate an index with a foreign key constraint

    I understand how to associate a primary key constraint or a Unique key constraint index...
    Yet I don't see how to associate the index of a foreign key constraint. Is this possible?

    Thank you
    Bob Larsen

    Hi Bob,

    In Data Modeler, physical model for Oracle primary and unique keys dialog boxes offer an Index field using, which is used to generate the 'using_index_clause' in the DDL for primary and Unique key constraints.
    However the Oracle Database DDL does not have the "using_index_clause" for Foreign Key constraints, thus Data Modeler does not provide this feature for foreign keys.

    So, you will need to create a separate Index that uses the same columns as the foreign key (using the index page of the table properties dialog box in the relational model).

    David

Maybe you are looking for