Online syntax to create Foreign key with Alter Table statement

I am a pretty meticulous reader. That being said, from the 4 texts that I read to start to learn SQL, I do not remember and do more that probably not, find a syntax online for the creation of a CF with the statement alter table.

Just something I thought while I was reading.

Is there a such syntax?

And if so someone can give me direction on my code?

ALTER table emp4 change employee_id key foreign employees of references (employe_id);

It gives me the option error invalid alter table.

Any idea would be appreciated.

Thank you.

rp0428 wrote:

A foreign key is a constraint, you must use ADD CONSTRAINT.

Thare is more than one way to skin a cat:

SQL > create table tbl (id number, name varchar2 (10))
2.

Table created.

SQL > alter table tbl adds (foreign key references emp (empno))
2.

Modified table.

SQL > select constraint_name,.
2 constraint_type
3 from user_constraints
4 where table_name = 'TBL '.
5.

CONSTRAINT_NAME C
------------------------------ -
SYS_C0021920 R

SQL > drop table tbl purge
2.

Deleted table.

SQL > create table tbl (id number, name varchar2 (10))
2.

Table created.

SQL > alter table tbl change (foreign key references emp (empno))
2.

Modified table.

SQL > select constraint_name,.
2 constraint_type
3 from user_constraints
4 where table_name = 'TBL '.
5.

CONSTRAINT_NAME C
------------------------------ -
SYS_C0021921 R

SQL >

SY.

Tags: Database

Similar Questions

  • foreign key with the apps table

    Hello world

    I created a custom table and I want to create a foreign key with per_all_people_f.

    Code:
    ALTER TABLE xxsshr.xxsm_sshr_hr_approver
    ADD CONSTRAINT fk_person_id
    FOREIGN KEY (person_id)
    REFERENCES HR. PER_ALL_PEOPLE_F (person_id)

    However, I get ora-00942: table or view does not exist.

    We use 11.5.10 and g 10 dB

    Thanks for any help...

    Elmer

    The XXSSHR account will need reference and select the privileges (and maybe others) on the PER_ALL_PEOPLE_F table. The ALTER TABLE statement must be run under the account XXSSHR after the above privileges are granted.

    HTH
    Srini

  • Another syntax for creating primary key


    Hello


    I have a question about the right way to create primary keys.
    Earlier, we were on Oracle 9i and we have upgraded to Oracle 10 g. The question is when creating primary key is allowed to create the unique index and then to create the primary key constraint? or directly create the first key is better? or are there ways to both the same?


    for example:
    one way:


    SQL & gt; CONN u2/u2@db1
    Connected.
    SQL & gt; create table a (a1, b1 varchar2 number (2), the date c1);


    Table created.


    SQL & gt; create an index on a (a1) tablespace unique pk_a t1;


    The index is created.



    SQL & gt; ALTER table a Add (constraint primary key (a1) using index pk_a);


    Modified table.
    Another way:
    SQL & gt; CONN u1/u1@db1
    Connected.
    SQL & gt; create table a (a1, b1 varchar2 number (2), the date c1)
    2.


    Table created.


    SQL & gt;
    SQL & gt; ALTER table a Add (constraint primary key (a1) using index tablespace t1 pk_a)
    2.


    Modified table.
    Which of the two approach is better? or are they the same?


    Thank you
    Cedric

    Oracle will attempt to create the index to apply the constraint. Of course, it is certainly possible that the creation of the index will fail, but you would be just to move the point of failure of the CREATE INDEX statement to the ALTER TABLE statement, assuming that everything else is equal (that is, you create the index in the same tablespace, quotas and tablespace sizes are the same, permissions are not changed, etc.)

    It may be preferable in some contexts to create a non-unique index before the creation of the constraint and instruct Oracle to use the non-unique index to apply the constraint. If you decide in the future to make the primary key constraint can be delayed, for example, have a non-unique index allows you to make this change without having to (implicitly), drop and re-create the index. A separate CREATE INDEX statement also influences when you go to remove a constraint / index. If the index was created before the constraint, you can remove the constraint without affecting the index. If the index was created after the constraint, deleting the constraint implicitly removes the index. Normally, particularly with OLTP applications, this doesn't buy you much. Sometimes, however, there are ETL process benefiting from such things.

    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.

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

  • 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

  • missing closing parenthesis when creating foreign key

    Schema1.Table1 ALTER TABLE ADD (CONSTRAINT test_FK

    FOREIGN KEY (test)

    REFERENCES schema1.table2 (test)

    USING INDEX TABLESPACE indextb);

    With the HELP of INDEX TABLESPACE indextb)

    *

    ERROR on line 4:

    ORA-00907: lack of right parenthesis

    Please help, I tried several times. But I still get the same error

    Delete the TABLESPACE of INDEX clause to HELP.  It is not valid here.

    Create a foreign key does not implicitly create an index.  Since there is no index, there is no tablespace to worry.  As a general rule, you don't want to create an index to support the how foreign key constraint there a sense perhaps specify a tablespace.

    Justin

  • Create Foreign Key forced in Warehouse Builder

    Hello world

    Yet once I need your help, please.
    My question is: Warehouse Builder is a way to create relationships between the Dimension Tables and one fact Table?
    I give you a simple example:
    We have produced paintings, customer, sales. So product and customer become a dimension table and sales become a fact table. In this fact table, we store the product_table_key, the customer_table_key and the messure of sale itself.
    If we think of datamodeling aspects our relationship 1: n between product_dimension_table and fact_table and also a relationship 1: n between customer_dimension_table and fact_table.
    How can we create these reports in the Warehouse Builder?
    If we design the objects of SQL database, we could create the need forced like that:
    Fact_table ALTER TABLE ADD (REFERENCES of KEY FOREIGN (customer_dim_fk) customer (customer_table_key));

    Please let me know how we can treat them in Warehouse Builder. I don't want to create which restricts manually in SQL Developer later.
    Thank you very much!

    Kind regards
    David

    Hi David,

    You can create the foreign key in OWB.
    An example of comprime1 that is created or imported in OWB.
    Open the T1 table in the data editor.
    Click constraints
    Type a name for the constraint
    Set the foreign key type
    Choose the local column on which you want to create the FK
    Now goto reference tab and choose the table and column (PK) whereby the CF should have a reference.

    She's.
    Save and generate the table T1 script you will have something similar to the following

    ALTER TABLE '' ADD CONSTRAINT '' FOREIGN KEY ('')
    REFERENCES "" ("");

    You can do a lot of settings on this FK in Table T1---> configure---> foreign keys

    Thank you
    Fati

  • Control of foreign key with conditions

    Hello


    I describe my problem with an example.

    My child table is like this:
    code_child key
    2 p
    2 V
    3 R
    3 T
    4 E



    I need to check the foreign key (code_child) in 2 two different parents tables (code_parent1, code_parent2)

    Table 1 parent:
    _______________

    code_parent1
    --------
    S
    V

    Table 2 of the parent:
    _______________

    code_parent2
    --------
    R
    T
    E

    Maybe I should use forced complex but I don't understand how it works!

    Thank you

    Hello

    Simply create constraints (Type of Condition) and put a command like:
    (CT is my alias in the data target (child) store, must be the same inserted in the data store)

    There are
    (select 1
    of Parent_table_1 P1, P2 Parent_table_2
    where CT.code_child = P1.code_parent1
    and CT.code_child = P2.code_parent2)

    No sense?

  • 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
    
  • Syntax to create the view with 2 inline tables

    Hello

    If I run this SQL, I can compile a view without error:

    CREATE OR REPLACE FORCE VIEW APPS.XX_TEST
    (
        period_name
      , ytd
    )
    AS
    with tbl_data AS
    (
    SELECT '1314-03:OCT' period_name, -100 YTD FROM DUAL UNION ALL
    SELECT '1314-03:OCT' period_name, -1250 YTD FROM DUAL UNION ALL
    SELECT '1314-03:OCT' period_name, 10922 YTD FROM DUAL UNION ALL
    SELECT '1314-02:SEP' period_name, -100 YTD FROM DUAL UNION ALL
    SELECT '1314-01:AUG' period_name, 0 YTD FROM DUAL UNION ALL
    SELECT '1314-00:OBL' period_name, 0 YTD FROM DUAL UNION ALL
    SELECT '1213-14:CBL' period_name, -350 YTD FROM DUAL UNION ALL
    SELECT '1213-13:ADJ' period_name, -350 YTD FROM DUAL UNION ALL
    SELECT '1213-12:JUL' period_name, -100 YTD FROM DUAL
    )    SELECT  period_name
               , ytd
            FROM tbl_data;
    

    I need to create a view that includes the 2 tables inline (sorry if this is the incorrect terminology), using this SQL:

    CREATE OR REPLACE FORCE VIEW APPS.XX_TEST2
    (
        period_name
      , ytd
    )
    AS
    with tbl_data AS
    (
    SELECT '1314-03:OCT' period_name, -100 YTD FROM DUAL UNION ALL
    SELECT '1314-03:OCT' period_name, -1250 YTD FROM DUAL UNION ALL
    SELECT '1314-03:OCT' period_name, 10922 YTD FROM DUAL UNION ALL
    SELECT '1314-02:SEP' period_name, -100 YTD FROM DUAL UNION ALL
    SELECT '1314-01:AUG' period_name, 0 YTD FROM DUAL UNION ALL
    SELECT '1314-00:OBL' period_name, 0 YTD FROM DUAL UNION ALL
    SELECT '1213-14:CBL' period_name, -350 YTD FROM DUAL UNION ALL
    SELECT '1213-13:ADJ' period_name, -350 YTD FROM DUAL UNION ALL
    SELECT '1213-12:JUL' period_name, -100 YTD FROM DUAL
    ), got_analytics AS
        (
            SELECT  
                period_name,
                ytd,
                ROW_NUMBER () OVER (ORDER BY  period_name)  AS a_num,
                ROW_NUMBER () OVER (ORDER BY  period_name  DESC)  AS d_num,
                FIRST_VALUE (ytd) OVER (ORDER BY  period_name) - FIRST_VALUE (ytd) OVER (ORDER BY  period_name  DESC) AS dif
        FROM    tbl_data
        )
        SELECT  *
           FROM got_analytics;
    

    I get this message:

    ORA-24344: success with compilation error

    There are no other errors but to provide additional information. I am using TOAD.

    I was wondering if it is not possible to create a view in this way, or if I'm missing something?

    I had a peek here:

    ORA-24344: success with Compilation error

    I'm using TOAD, trying to SHOW the ERRORS immediately after the code above only returned 'No Errors'.

    I also looked here:

    ORA-24344: success with compilation error

    Ran the same code SQL/Plus, but "Show Errors" or "see the err" also returned "No. Errors."

    Any advice would be much appreciated.

    Thank you very much.

    Hello

    Try this

      CREATE OR REPLACE FORCE VIEW  APPS.XX_TEST2
        (
            period_name
          , ytd
          ,  a_num    -- because you select * you need all rows
          ,  d_num
          ,  dif
        )
        AS  ...
    

    concerning
    Kay

  • How to create a grid with the tables and Boolean values?

    As part of a larger project, I'm trying to create a Subvi, which will allow me to spend a "cursor" in a 3 x 3 matrix.

    It should be 2 Boolean inputs, one for move them down in the table and the other to move them to the right. Once reached the edge of the table, the "cursor" will start back at the beginning.

    I have a vague idea of a table to 2 dimensions with LED. Once 'down' has been selected, the corresponding light to (0,0), light (0.1). Goes the same for the button 'right '. When 'down' is pressed (0.2), the LED should turn off, and LED (0,0) must light up.

    I'm sure it has something to do with the initialization of the tables, but I can't seem to find the right way to do it.

    Any help is much appreciated. Thank you!

    Your array is always empty.  Look at the detailed help for the function page replace table subset.  He says that the function has no effect if the specified index refers to a location outside the table.

    What you see on the Panel is only a view and has nothing to do with what is in the table.

    Lynn

  • Create the form with a table expanding

    I am trying to create a sheet of fax for one of our customer service representatives.  She needs from time to time fax on a list of names, acct # s, etc. to our collection agency of abducting people. Because it's done on a basis as needed, I want to use a table to create a repeating line number as the number of accounts can vary.  I've looked everywhere but can't seem to find out how or even if it is possible.  I am using LiveCycle Designer ES 8.2.  Thank you!

    Give this one a go. The most common mistake when creating dynamic forms forget to save the form in a dynamic PDF.

    FOR INFO. Something is wrong with this form. I would no more than 1 MB.

    Steve

  • Foreign keys without ON DELETE CASCADE; Database design question

    It's the 3rd company, while I work, where I see that created foreign keys
    ON DELETE CASCADE
    clause.
    My colleague said that it is created without the ON DELETE CASCADE clause in order to complicate the deletion of data (records of children). Thus, for codes of the Purge and archive, before we delete the parent table we must remove from the child table first.

    Occasinaly, something is wrong and we will end up disabling FKs make them all and then purging, archiving, etc...

    So, is it not better to create foreign keys with the clause ON DELETE CASCADE, rather than having all these hassles?

    Experience, no matter what you think?

    Personally, I am firmly in the removal of the brand of hard data camp. In the vast majority of cases, trying to remove an active parent is an error if the user must be informed of what they are trying to do and must make a conscious effort to do so (in other words, remove first the children).

    John

  • creating tables with references to foreign keys that are not created tables

    I have several patterns to create and each diagram has several tables that reference the columns in each other as a foreign key. So I have to sort out them in the order. In other words, if table A has the foreign key of the Table B column can create B first and then A. are there - it was none I can create a first and then B, that would save a lot of time.

    Hello

    ORA - aff wrote:

    I have several patterns to create and each diagram has several tables that reference the columns in each other as a foreign key. So I have to sort out them in the order. In other words, if table A has the foreign key of the Table B column can create B first and then A. are there - it was none I can create a first and then B, that would save a lot of time.

    You can create A without the constraints of foreign key and then, once the B array has been created, use ALTER TABLE to add the constraints of foreigners has.

Maybe you are looking for

  • G6-1D70CA: G6-1d70ca sm bus controller

    I rebooted just Windows 7 on the new drive hard (an old failed). I have downloaded all drivers etc for this computer and install them, but for some reason any, he shows that the SM Bus controller in the car is not installed and I can't find him anywh

  • Can't see images on web pages

    Probably, I feel a bit stupid when I learn the answer, but here goes! Since the purchase of a new computer laptop on Vista, I find that a lot of web pages load with the little red cross in a square instead of the images. This only seems to happen wit

  • GPIB-USB-HP6675A

    Try to connect to a power supply of HP6675A using a GPIB-USB Interface Need examples of location and library program. Thank you!

  • Additional SATA SSD drive

    Hello I have a new HP Pavilion HPE h9-1100eb Phoenix-desktop pc and wonder if I can add an extra 256 GB SSD OCZ-Octane, and if so, how can I go about it? Thank you very much in advance. John. System information: REF.. H9-1100ebProduktnr. H1G71EA #AK6

  • na instal windows live kom niet in internet, altijd probleembericht

    squeeze altijd probleembericht en kom niet in hot mail en facebook