foreign key referencing the table in different schema

I create a foreign key for the table in the schema A a table to diagram B.

I do that by grant references (column name) on the name of the table to username;


is there any drawback/disadvantage in the creation of foreign keys referencing tables in another schema?

Not as such, no.

Generally, you want to check a second time when you are in this kind of situation that the two tables really belong in different schemas. It should be relatively rare to find a child table that belongs to a different schema than the parent. Sometimes, but it should be an exception, not the rule.

Justin

Tags: Database

Similar Questions

  • How do I change or add constr. foreign key to the table whose children or data lines

    Hello

    I have a table with the name of Department in which the DEPTNO column is a column of data and primary key in it for all columns. Now, I have a table EMP where DEPTNO column is not having any constraints, including constraint foreign key also. I now have data in the EMP table for all columns. I would now add the DEPTNO column foreign key constraint in the EMP table that references the column DEPTNO in DEPT table. Both the table have not all null values.

    I need to add the foreign key without removing the data in the EMP table.


    Please advice.

    Kind regards
    Alriq

    In addition, if you have pre-existing data that will never have a parent record but you want to ensure that any NEW data is consistent with the FK constraint you can always create it with the NOVALIDATE option:

    SQL> CREATE TABLE EMP_BKP AS SELECT * FROM SCOTT.EMP;
    
    Table created.
    
    SQL> CREATE TABLE DEPT_BKP AS SELECT * FROM SCOTT.DEPT;
    
    Table created.
    
    SQL> DELETE FROM DEPT_BKP WHERE DEPTNO=10;
    
    1 row deleted.
    
    SQL> ALTER TABLE DEPT_BKP ADD CONSTRAINT DEPT_BKP_PK PRIMARY KEY(DEPTNO);
    
    Table altered.
    
    SQL> ALTER TABLE EMP_BKP ADD CONSTRAINT DEPT_FK FOREIGN KEY(DEPTNO) REFERENCES DEPT_BKP(DEPTNO);
    ALTER TABLE EMP_BKP ADD CONSTRAINT DEPT_FK FOREIGN KEY(DEPTNO) REFERENCES DEPT_BKP(DEPTNO)
                                       *
    ERROR at line 1:
    ORA-02298: cannot validate (TEST_USER.DEPT_FK) - parent keys not found
    
    SQL> ALTER TABLE EMP_BKP ADD CONSTRAINT DEPT_FK FOREIGN KEY(DEPTNO) REFERENCES DEPT_BKP(DEPTNO) NOVALIDATE;
    
    Table altered.
    
    SQL> INSERT INTO EMP_BKP(EMPNO,ENAME,DEPTNO) VALUES(9999,'Test Emp',10);
    INSERT INTO EMP_BKP(EMPNO,ENAME,DEPTNO) VALUES(9999,'Test Emp',10)
    *
    ERROR at line 1:
    ORA-02291: integrity constraint (TEST_USER.DEPT_FK) violated - parent key not found
    
    SQL> SELECT * FROM EMP_BKP WHERE DEPTNO = 10 ORDER BY EMPNO;
    
         EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO
    ---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
          7782 CLARK      MANAGER         7839 06/09/1981 00:00:00       2450                    10
          7839 KING       PRESIDENT            11/17/1981 00:00:00       5000                    10
          7934 MILLER     CLERK           7782 01/23/1982 00:00:00       1300                    10
    
  • Questions about the creation of a foreign key on a table

    Hello @ll,

    during an update of the database, I lost a foreign key between two tables. The tables are called werteart and werteartarchiv_pt. Because of its size, werteartarchiv_pt is a partitioned table. The missing foreign key was a constraint on werteartarchiv_pt table referencing werteart.

    Some statistics on the size of the mentioned tables:
    werteart 22 MB
    werteartarchiv_pt 223 GB
    werteartarchiv_pt (Index) 243 GB

    I tried to create the foreign key again but it failed with the following error (apologies for the German error message):

    sqlplus ORA-00604: Fehler auf rekursiver SQL-ebony 1
    sqlplus ORA-01652: Temp-Segment kann nicht um 128 in Tablespace TEMPS00 judgements

    The statement that I used:

    ALTER table werteartarchiv_pt
    Add constraint werteartarchiv_pt_fk1
    foreign key (rail, werteartadresse, merkmale)
    Reference werteart (rail, werteartadresse, merkmale)
    on delete cascade
    at the delayed start can be delayed;

    So, the problem seems to be that Oracle needs a lot of temporary tablespace to generate foreign key and I do not know how and why.

    My questions now are, and I hope that someone is there who can answer all or part of it:

    (1) why Oracle should temporary tablespace to create the foreign key? The foreign key uses the same columns as the primary key.

    is 2 (a) possible to change the statement without using the temporary tablespace?

    2 (b) if it is not possible to avoid the use of temporary storage space, is there a formula how to calculate the necessary temporary tablespace?

    (3) is it possible to modify data in tables while the foreign key is created or is the whole table locked during the process?

    Any help or advice is appreciated.

    Kind regards
    Bjoern

    RollinHand wrote:
    My questions now are, and I hope that someone is there who can answer all or part of it:

    (1) why Oracle should temporary tablespace to create the foreign key? The foreign key uses the same columns as the primary key.

    Because it is data validation to ensure that the foreign key are violated. If you had specified NOVALIDATE ACTIVATE in its creation then the existing data in the table should be checked and the statement must fill out instantly (future data added is enabled by the constraint).

    http://download.Oracle.com/docs/CD/B28359_01/server.111/b28310/general005.htm

    Search for "Enable Novalidate constraint State"

  • 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.

  • Renaming of the foreign keys in the relational model

    Hello


    I'm new to the Data Modeler, we use v 3.0.0.66.5 and Oracle 11 g, and I am trying to build a logic and a relational model for a new application.

    We always call our primary keys as the ID, this causes me a problem with my names of foreign keys in the relational model, as they show as ID #. Is it possible to add the abbreviated of the table of the foreign key?

    Thanks in advance

    Sue

    Yes it is.

    Take a look at the tools > preferences, select Data Modeler > naming Standard > model, you can specify the standard you want to use for your foreign key. You can apply this designation to existing models and all new created FK uses the same model. To apply the standard to existing templates, select the relational model in the browser, right-click to display the context menu and select the menu "apply Naming standards... ».

    Sue

  • 'For' loop with a different number of iterations. Second, the auto-indexation of the tables with different sizes is done. It can affect the performance of the Vi?

    Hello

    I have a loop 'for' which can take different number of iterations according to the number of measures that the user wants to do.

    Inside this loop, I'm auto-indexation four different 1 d arrays. This means that the size of the tables will be different in the different phases of the execution of the program (the size will equal the number of measures).

    My question is: the auto-indexation of the tables with different sizes will affect the performance of the program? I think it slows down my Vi...

    Thank you very much.

    My first thought is that the compiler to the LabVIEW actually removes the Matlab node because the outputs are not used.  Once you son upward, LabVIEW must then call Matlab and wait for it to run.  I know from experience, the call of Matlab to run the script is SLOW.  I also recommend to do the math in native LabVIEW.

  • How can I use statistics for all the tables in a schema in SQL Developer? and how long will it take on average?

    Hello

    How can I use statistics for all the tables in a schema in SQL Developer? and how long will it take on average?

    Thank you

    Jay.

    Select the connection and right-click on it and select schema statistics collection

  • First database with Oracle 12 c and Entity Framework 5: no foreign key in the EMD Associations

    We have an updated database implemented in Oracle 12 c R1. When attempting to reverse engineer the EMD of the DB using Visual Studio 2013 with ODAC 12 c Release 2 and Oracle developer Tools support all we get are entities with no association, despite multiple foreign keys defined in the database. There, of course, no navigation property in both entities. No error or warning messages are displayed and activate us the option "include foreign keys in the model columns.

    Does anyone have any idea why this might happen?

    If you use files PDB (pluggable database), there is a known bug (17474322) to generate an EDM with foreign keys in a PDB file. This is probably the question you are running in.

  • primary key for the table

    I have a Gr_order table that was settled long ago, and now he needs to be filled
    with some more search data.

    "Orderid" is a primary key for the table, but the question is how can I insert "orderid" in the insert statement in the procedure. I couldn't find any name sequence. There are 1230
    documents that must be inserted.
    Desc Gr_order
    Orderid  Number Primary key
    Desc     Varchar2
    INSERT INTO gr_order (orderid,desc)  
    VALUES (???, upper(each_pom.desc);
    Thank you
    Sandy

    You can find the code that inserts into the table and see if it uses the sequence ;-). A sequence is not 'attached' to a particular table - this is the insert statement to determine whether a sequence should be used.

    You can check if the table has a trigger - some developers like to emulate the behavior of MS - sql server by having a trigger on the table that selects in a sequence. If it has such a release, then you just do your insertions without specifying order_id. But if the table does not have such a release, then you cannot know if the table was originally filled using a sequence or not (unless you can find the instructions for inserting somewhere).

  • Access to the content of the tables of a schema into another schema

    Hello

    I have my doubts... Suppose I have a user called DEMO and it has tables. now I have another user called DEMO1

    my doubt is. How can I get the DEMO user tables in DEMO1. If updating the table DEMO1 which should reflect

    DEMO user. Help me...

    Dear friend,

    You can consult the tables of a schema in another schema using the following steps.

    (1) you must grant privileges on table demo Demo1, here's the statement to do so.

    Grant Select, update on the table table_name to Demo1

    (2) Create in Demo1 for the table_name demo, the suite is about education to do.

    SYNONYM to CREATE or REPLACE table_name for Demo.table_name;

    (3) do the Update statement on the table table_name in Demo1. Updated these results reflected in demo when you post the update statement in Demo1.

    Hope that gives you an idea.

    Kind regards
    Ravi Kumar Ankarapu.

  • READ the TABLE of another schema

    Hello
    pls help me.

    I want to read the table of another schema that is declared in the application of the apex.

    SELECT * FROM DBA_ROLE_PRIVS;
    (SELECT * FROM sys.) DBA_ROLE_PRIVS: auch bad)

    The owner of this table is SYS.

    Error: The Table is unknown or cannot be found.

    my real schema is GCs.

    How is the select statement for this case for reading in the table (or tableview) of another schema?
    many and many thanks for your help.
    TL

    The SYS schema is a special schema in Oracle. You must explicitly grant the user rights by selecting tables and views sys.

  • Primary key and foreign key between the different schemas

    Hi all;

    SCOTT user have a 'rank' of table-> it contains the primary key-> users tablespace

    The U1 user have a 'emp' table-> it contains the foreign key-> test tablespace

    U1 > select constraint_name, constraint_type, r_owner, owner, r_constraint_name of all_constraints where table_name = 'EMP ';

    OWNER CONSTRAINT_NAME C R_OWNER R_CONSTRAINT_NAME

    ------------ ------------------------------ - -------------------------- ----------------------- -----------------------------------------------------

    U1              EMP_EMPLEV_C12_FK R SCOTT GRADE_GRL_C1_PK

    If I have connection U1, no possibility to find the name of the primary key without connection scott table?

    Thank you

    Hello

    So, you want to know what picture of the SCOTT schema has the constraint that is named "GRADE_GRL_C1_PK." Isn't it?

    If so,.

    SELECT table_name

    Of all_constraints

    Owner WHERE = 'SCOTT '.

    AND the argument constraint_name = 'GRADE_GRL_C1_PK. '

    ;

  • How can I make sure that changes in a primary key (in the parent table) would also appear directly in the FOREIGN KEY in the child table?

    Forgive my question. I am very new to Oracle.

    How can I make sure that changes in the key primary supplier_id (concerning the supplier table) would also appear directly in the FOREIGN KEY (supplier_id) in the products table?

    Is that not all the primary key and FOREIGN KEY on?

    My paintings:

    I created 2 tables and connect to apply in the data base referential integrity, as I learned.

    CREATE TABLE - parent provider

    (the numeric (10) of supplier_id not null,)

    supplier_name varchar2 (50) not null,

    Contact_Name varchar2 (50).

    CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)

    );

    CREATE TABLE - child products

    (the numeric (10) of product_id not null,)

    supplier_id numeric (10) not null,

    CONSTRAINT fk_supplier

    FOREIGN KEY (supplier_id)

    REFERENCES beg (supplier_id)

    );

    I inserted the following text:

    INSERT INTO provider

    (supplier_id, supplier_name, contact_name)

    VALUES

    (5000, 'Apple', 'first name');

    I expect that the supplier_id (5000) to the provider of the table also appears in the products table under key supplier_id having the same value which is 5000. But this does not happen.

    How to get there?

    Thanks in advance!

    Hello

    What is a foreign key in Oracle?

    A foreign key is a way to ensure referential integrity in your Oracle database. A foreign key means that the values of a table must appear also in another table.

    Ok!??

    What is now the right way to implement referential integrity in your Oracle database that the values of a table must also be included in another table?

    A foreign key referential integrity indeed enfore in ensuring that the value in the child table must have a corresponding parent key (otherwise you will encounter an error, as evidenced by "SomeoneElse"). However, it will never automatically insert a row in the other table.

    If you are looking for a solution that automatically inserts a record in the other table, maybe you should go for triggers:

    See:

  • Delete data table Parent and child data automatically delete or the value null in the foreign key of the child table.

    Oracle 10g

    Hi gurus

    I test_mst table that has a primary key test_mst.test_id, I detail table test_dtl which has test_dtl.test_id of foreign key, now I have that I want to use the delete statement in test_mst then it should also remove the constraint of her child or null in test_dtl.test_id, can again do in oracle 10g?

    Thanks in advance

    Hi Mit.

    example of cascade

    create table bill(
      bill_id number not null primary key
    )
    ;
    create table bill_item(
      bill_item_id number not null primary key,
      bill_id number references bill(bill_id) on delete cascade
    )
    ;
    insert into bill values (1)
    ;
    insert into bill_item values (1, 1)
    ;
    delete bill
    ;
    select * from bill_item
    ;
    drop table bill_item purge
    ;
    drop table bill purge
    ;
    
    Table BILL created.
    Table BILL_ITEM created.
    1 row inserted.
    1 row inserted.
    1 row deleted.
    
    no rows selected
    
    Table BILL_ITEM dropped.
    Table BILL dropped.
    

    exemplary null

    create table employer(
      employer_id number not null primary key
    )
    ;
    create table employee(
      employee_id number not null primary key,
      employer_id number references employer(employer_id) on delete set null
    )
    ;
    insert into employer values (1)
    ;
    insert into employee values (1, 1)
    ;
    delete employer
    ;
    select * from employee
    ;
    drop table employee purge
    ;
    drop table employer purge
    ;
    
    Table EMPLOYER created.
    Table EMPLOYEE created.
    1 row inserted.
    1 row inserted.
    1 row deleted.
    
                                EMPLOYEE_ID                             EMPLOYER_ID
    --------------------------------------- ---------------------------------------
                                          1                                       
    
    Table EMPLOYEE dropped.
    Table EMPLOYER dropped.
    
  • How to check where the table field has been used as a foreign key in the database

    Hi I have a field in my table Office I had office_code field, this field has been used in the tables of diffirent as foreign key is a sql I can wirte to see all the tables that have used this field as a foreign key

    Edited by: adf009 09/05/2013 10:37

    Edited by: adf009 09/05/2013 10:38

    Check this box

    SELECT * FROM user_constraints WHERE table_name='EMP' and CONSTRAINT_TYPE='R';
    

    Type = 'R' means referential integrity constraint type.

Maybe you are looking for

  • After downloading 20 FF, I can't use the private browsing.

    HelloI can't use the incognito since download 20 FF. I have Win7/64-bit Home Premium edition. I checked the help articles, but this go to the "Firefox Button" at the top of the window. I do not have this button. I would be grateful for advice, which

  • When the Carplay will have its own forum?

    Hello, I wonder when Apple will have a forum dedicated to Carplay. Anyone know?

  • Save the file text at different rates

    Hi all I'm kinda new to LabView, so perhaps this question is trivial, but gives me a lot of pain. I want to permanently save a string given at two different rates; in the example provided these rates are every 500 ms and ms 50, in two different (one

  • HP Officejet Pro 8630: HP Officejet Pro 8630 printing problem

    Hello I have a printer HP OfficeJet Pro 8630 with two different computers connected on it. I have no problem printing from the computer that I use every day, but when I try to print from the other office, nothing happens. The second recognizes the pr

  • [W520] [Dev] Qt Designer will not start

    Hello! I finally received my W520 last Friday and my first impression is really good. Unfortunately I have a problem to start Qt Designer on it so if any C++ developer is around and not a clue, don't hesitate. I use the Qt libs and the plugin for MS