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

Tags: Database

Similar Questions

  • 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

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

  • How can I change my mood Message in Skype for the Web?

    How can I change my mood message in Skype for the Web?

    People of good news: you can now change your mood message in Skype for the Web

    Just click on the picture at the top left of your profile, and then click the small icon of pencil next to the message of change in activity.

  • When I click on a file, it brings up a search screen. How can I change back so that it opens just the file?

    When I click on a file, it brings up a search screen.  How can I change back so that it opens just the file?

    Open the box and run by pressing the Windows logo and the R keys together then type;

    regsvr32 /i shell32.dll click OK

  • How can I change my task bar background color and the background color of site Web

    How can I change my task bar background color and the background color of site Web

    It is white now and he won't let me see the search fields

    Hello

    Method 1: I suggest you to refer the link below which will help you change the color of your frames of windows, the start menu, the taskbar, and more.

    http://Windows.Microsoft.com/en-us/Windows7/change-the-colors-on-your-computer

    Change the colors on your computer: http://windows.microsoft.com/en-US/windows7/change-colors-on-your-computer.

    Method 2: Try the following steps, which would help you to change the background color of your Web site.

    (a) open Internet Explorer.

    (b) on the Tools menu, click Internet Options, and then click the general tab.

    (c) click on colors under appearance.

    (d) uncheck the box that says use Windows colors.

    (e) then click on background and change the color you prefer.

    General Internet Explorer Settings change: http://windows.microsoft.com/en-US/windows7/Change-Internet-Explorer-General-settings

    Hope this information is useful.

  • How can I change which hard drive Adobe installs on the.

    How can I change which hard drive Adobe installs on the. It does give me an option. Install on 'C' and I love it on drive 'E '. Can someone help me please.

    Change for Applications installation location

    With the application of CC open, select the gear icon in the upper right, and then choose Preferences.  In the preferences of the screen select creative cloud and then look for the drop-down list installation location

  • I no longer live in the Middle East. How can I change my new place of residence in the USA? I do not need to deal with exchanges of currencies.

    I no longer live in the Middle East. How can I change my new place of residence in the USA? I do not need to deal with exchanges of currencies.

    Hello

    Please see using document https://helpx.adobe.com/x-productkb/policy-pricing/change-country-associated-with-adobe-id .html

    In order to change the country on your Adobe account.

    or you can sign up for the new e-mail address on www.adobe.com

    Hope this will help.

  • Hello world. How can I change 2000 channels of unique text in the layout ID to another 2000 located in the Excel table?

    Hello world. How can I change 2000 channels of unique text in the layout ID to another 2000 located in the Excel table?

    Hello

    1 click using the Multi-Find/Change to our clever friend Marthino da Gloria, version 2.0.

  • 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

  • How to add the primary key for the table with the existing data?

    The table is already busy data. There was no primary key before, so for each column, there are some duplicate values.

    I want to add a new column, which should be of the integer data type and can automatically incremented, from 001. I tried with Oracle SQL Developer, but it says "ORA-01758: table must be empty to add mandatory (NOT NULL) column. How can I do? Thank you!

    Hello

    Look for the [ALTER TABLE | http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_3001.htm#sthref4803] command to find out how to add a column (step (1)) and a (step (3)) constraint to an existing table.

    For the step (2):

    CREATE SEQUENCE  employee_id_seq
    START WITH  1
    ;
    
    UPDATE  employee
    SET     id  = employee_id_seq.NEXTVAL;
    

    When you create a sequence, START WITH 1 is the default value, so that the line is not really necessary above... I've included just to show how you could start with any number you have chosen.

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

  • 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

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

  • Unable to add a new column to the table of the LCD? Option, just usually add it.

    I have a form of LCD with a table in it. Im trying to simply add a new column to the table. The option is there, but the column appear just after clicking Add the column to the left. I selected a column, right click the space > insert > column to the left. But the habit intsert. The object table properties tab shows lines and colum numbers, but they are grey.

    See screenshots:

    http://www.emermed.NET/staging/forums...

    http://www.emermed.NET/staging/forums...

    It is a dynamic array, where a new line can be inserted using a button. Is that the dynamic nature of the question? ID hate to cancel all the flow dynamics and programming just to add a column, and then reapply them all.

    Thank you!

    Hello

    You do not add the columns in the object > palette (screenshot 2) of the Table. I suspect that the problem is that it is not enough on the page (in the content area) to add the column. In the screenshot 1, if you look at the width of the column highlighted from the space to the right of the table, you will see that a column can be added due to restrictions of space.

    Reduce the width of the column highlighted (temporarily), and then add a column. Once added, you can resize the columns to match the width of the page.

    Hope that helps,

    Niall

Maybe you are looking for