Is a foreign key MUST have a UNIQUE or PK column as a reference?

First I tried, from what I saw Yes it must be Unique or PK. Secondly, I am pretty sure that I read somewhere where there are PK or unique. Thirdly, I checked oradocs and other pages, the thing ends, I owe an answer

Constraint foreign key (also called a constraint referential integrity) designates a column as a foreign key and establishes a relationship between this foreign key and a primary or unique key specified, called the referenced key.


This implies, but does not explicitly specify that it MUST be a PK or a SINGLE.


Basically I want a simple yes/no answer just to give me some peace of mind. I don't want to waste any time, but changes to the oracle from time to time, and I'd rather have a confirmation from someone with more knowledge/experience.


Thank you!

Yes.

This same document you are referencing tells you what are the rules for the creation of a foreign key.

Data integrity

Foreign key constraints

Whenever two tables have one or more common columns, Oracle database can apply the relationship between the two tables with a foreign key, also called a referential integrity constraint constraint. The constraint requires that for each value in the column on which the constraint is defined, the value in the other specified that another table and column must match. An example of a referential integrity rule is that an employee may work for only one Department.

Table 5-2 lists the terms associated with referential integrity constraints.

Table 5-2 referential integrity constraint conditions

Term Definition

Foreign key

The column or set of columns included in the constraint definition that refers to a key that is referenced. For example, the department_id column employees is a foreign key that refers to the department_id column of departments .

Foreign keys can be described as several columns. However, a composite foreign key must refer to a primary or unique key with the same number of columns and the same types of data.

The value of the foreign keys can be either the primary or unique key value referenced or be null. If any column of a composite foreign key is null, then the portions not null the key do not match any corresponding part of a parent key.

Referenced key

The unique key or the primary key of the table referenced by a foreign key. For example, the department_id column departments is the key referenced to the department_id column of employees .

Table dependent or child

The table containing the foreign key. This table is based on the values present in the primary or unique key referenced. For example, the employees table is a child of departments .

Referenced or the parents table

The table that is referenced by the foreign key of the child table. It is the key to this table reference that determines whether specific inserts or updates are allowed in the child table. For example, the departments table is a parent of employees .

The first three terms defined in the table EXPLICITLY State references to the 'core' or 'unique' keys to the parent.

The doc also refers to the Application Developer Guide:

Maintaining the integrity of the data in Database Applications

Foreign key references a primary key by default

If the list of columns is not included in the REFERENCES option when you set a FOREIGN KEY constraint (simple or composite column), then Oracle database assumes that you want to reference the primary key of the specified table. You can also explicitly specify the columns to reference the table parent in parentheses. Oracle database checks automatically to check this list of column refers to a primary or unique to the parent table key. If it isn't, an informative error is returned.

That the entire paragraph is as explicitly as it gets.

No list of columns? Oracle then checks for a primary key.

There is a list of columns? Then Oracle 'control to verify this list of column made reference to a primary or unique key'...

Tags: Database

Similar Questions

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

  • foreign key mapped to a unique constraint

    OK, here's the question

    create a parent of the table (col1, col2 char char);
    alter table add constraint pk_parent key (col1) parent primary;
    alter table add constraint uk_parent unique(col1,col2) parent;

    Insert into parent values ('A', '7');

    * 1 row created.*

    create the child table (col1, col2 char char);
    ALTER table child add constraint fk_child_parent foreign key(col1,col2) references parent(col1,col2);

    insert into values('B','4') of the child;

    insert into the child values('B','4')
    ***
    ERROR on line 1:
    ORA-02291: integrity constraint (WILDGOD. FK_CHILD_PARENT) violated - parent key
    not found

    That's what I expect... but

    insert into values('B',) of the child;

    * 1 row created.*


    why he let me do, I realize its because col2 is NULL, but it should not yet check for col1 in the parent?

    Please clarify, thanks in advance.

    Documented behavior.

    See here:
    http://download.Oracle.com/docs/CD/E11882_01/AppDev.112/e10471/adfns_constraints.htm#ADFNS273

    -Mark

  • another computer on the network is using the same IP address that your computer uses. Each computer on a network must have a unique IP address. To resolve this problem, run Network Diagnostics

    another computer on the network is using the same IP address that your computer uses.

    Hi johnnyredd


    1. is connected to a domain or a server computer?
    2. what operating system is installed on the computer?

     

    You can refer to the link below and check if the information provided will help you-

    http://Windows.Microsoft.com/en-us/Windows7/get-help-with-there-is-an-IP-address-conflict-message

     

    Check out the link below to identify the operating system installed.
    http://Windows.Microsoft.com/en-us/Windows7/help/which-version-of-the-Windows-operating-system-am-i-running

     

    I hope this helps.

  • Details of the foreign key constraints

    Why foreign key constraints is not ongoing validation in case of null values?
    For example, in the table EMP and DEPT table, EMP. References DEPTNO Dept. DEPTNO.
    But I am able to insert rows into the EMP table that contains nulls for EMP. DEPTNO.

    Prasath N wrote:
    Why foreign key constraints is not ongoing validation in case of null values?
    For example, in the table EMP and DEPT table, EMP. References DEPTNO Dept. DEPTNO.
    But I am able to insert rows into the EMP table that contains nulls for EMP. DEPTNO.

    I guess he's saying essentially that a foreign key must have a parent key or can be an orphan. But may not be a key that is not listed in the parent.

    And also NULL is neither equal to anything, nor does not correspond to something.

    But still, you can put a NOT NULL constraint if you do not want your foreign key to contain orphaned records.

  • foreign key index

    I read a book on the index, he said
    You should almost always index foreign keys because they are frequently used in joins. In addition, if you intend to delete or update unique or primary keys on the parent table, you should index the foreign keys to improve the locking of child records.
    what I don't understand, I should create index on the foreign key in parent-child table or table column or both?

    Thank you

    Hello

    806540 wrote:
    I read a book on the index, he said

    You should almost always index foreign keys because they are frequently used in joins. In addition, if you intend to delete or update unique or primary keys on the parent table, you should index the foreign keys to improve the locking of child records.
    

    what I don't understand, I should create index on the foreign key in parent-child table or table column or both?

    They mean that you need to create an index in the child table, that is the table that contains the foreign key constraint.

    In order to have a foreign key constraint, it must have a primary key constraint, or a unique constraint on the column, or columns referenced in the parent table. Either type (primary key or unique) automatically creates an index, so there is nothing more you need to do in the parent table.

  • Drop partition without disabling the foreign key

    Hi all

    I have a table parent and child.

    Parent table

    create the table parent_1
    (identification number,
    create_date date,
    Constraint parent_1_pk001 PRIMARY KEY (id))
    PARTITION OF RANGE (create_date)
    INTERVAL (NUMTODSINTERVAL(1,'DAY'))
    (PARTITION parent_1_part VALUES LESS THAN (January 1, 2010 '));

    Child table
    create the table child_1
    (identification number,
    create_date date,
    Constraint child_1_fk001 FOREIGN KEY (id)
    REFERENCES parent_1 (id))
    PARTITION OF RANGE (create_date)
    INTERVAL (NUMTODSINTERVAL(1,'DAY'))
    (PARTITION create_date_part VALUES LESS THAN (January 1, 2010 '));


    I have problems to delete partition.
    Parent_1
    1 JULY 26, 12
    2 JULY 26, 12

    Child_1
    1 JULY 26, 12

    ALTER drop partition table CHILD_1 SYS_P274;
    table altered CHILD_1.

    ON THE PARENT PARTITION A FALL
    ALTER drop partition table parent_1 SYS_P273;

    Error report:
    SQL error: ORA-02266: unique/primary keys in table referenced by foreign keys enabled
    02266 00000 - "unique/primary keys in table referenced by foreign keys enabled.
    * Cause: An attempt was made to truncate a single table or
    primary keys referenced by foreign keys in another table.
    Other unauthorized trades are a drop/truncate partition a
    partitioned table or an ALTER TABLE EXCHANGE PARTITION.
    * Action: Before performing the operations described above, the table, disable the
    constraints foreign key in other tables. You can see what
    constraints are making reference to a table by issuing the following
    command:
    SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'tabnam ';


    PLEASE KNOW IF THERE IS A WAY TO THE PARENT PARTITION DROP WITHOUT TOGGLE FOREIGN CONSTRAINTS


    Thank you
    SQL Error: ORA-02266: unique/primary keys in table referenced by enabled foreign keys
    02266. 00000 - "unique/primary keys in table referenced by enabled foreign keys"
    *Cause: An attempt was made to truncate a table with unique or
    primary keys referenced by foreign keys enabled in another table.
    Other operations not allowed are dropping/truncating a partition of a
    partitioned table or an ALTER TABLE EXCHANGE PARTITION.
    *Action: Before performing the above operations the table, disable the
    foreign key constraints in other tables. 
    

    You can do until you disable the foreign key constraint

    http://jonathanlewis.WordPress.com/2006/12/10/drop-parent-partition/

    Hope this helps

    Mohamed Houri
    www.hourim.WordPress.com

  • Foreign key problem

    Hi all...
    I'm new to this field of database... I have a small question about the foreign key

    I have two table name as ACCT_MSTR column ACCT_NO
    & FD_MSTR another FD_SER_NO column.

    Now, I create a table called ACCT_FD_CUST_DTLS
    having the column ACCT_FD_NO, CUST_NO.

    Now, I want to do a foreign key relationship are provided with
    acct_mstr & fd_mstr on a single column of acct_fd_cust_dtls (acct_fd_no)
    That is to say.
    foreign key: -.
    ACCT_FD_CUST_DTLS. ACCT_FD_NO-> ACCT_MSTR. ACCT_NO
    OR
    ACCT_FD_CUST_DTLS. ACCT_FD_NO-> FD_MSTR. FD_SER_NO

    How can I make this relationship?
    Please help me by providing the syntax

    Thanks in advance

    user11207304 wrote:
    @ Sissi
    No, this isn't like that
    values are either present in ACCT_MSTR. ACCT_NO or the FD_MSTR. FD_SER_NO.
    It is not necessary to present the two table.

    Referential constraints do not work like that. If you build a relationship, it must be respected.

    If the value is not found in one of the parents (in this case), there will be an exception.

    IMO, the solution to your problem is not a referential constraint, but a trigger.

  • Adding a foreign key when adding a column

    Hi all

    How to add a column with constriant foreign key

    I have a table TEST (pk number nm)
    and TEST1 (PK nm1)

    TEST1, I need to add a column nm2 which referred to TEST (nm), using the unique ALTER command


    Please help in this.

    Hello

    Please check the documentation for it, it's all there:
    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/clauses002.htm#CJAGIICD

    ALTER TABLE test1 ADD (
     nm2 NUMBER
     CONSTRAINT nm2_fk REFERENCES test(nm)
    );
    
  • Foreign keys (null)

    Your primary key is not null.
    If the foreign key also have the value null is not*, since it is based on a PK?

    Clive_S wrote:
    Your primary key is not null.
    If the foreign key also have the value null is not*, since it is based on a PK?

    It's all business requirements. If your requirement of bussiness stats this column must not be zero then do else do not.
    It is not mandatory to have a foreign key to null.

    For example:
    A new employee to join the Organization, which the Department (foreign key) has not yet been set.

    Concerning
    Anurag

  • Question on foreign key

    Hello

    I have a question about foreign key. When we create an index on a foreign key index name are displayed in the column index_name dba_constraints DD.

    But the name of the index associated with the primary key is displayed.

    Why so much?

    Thanks in advance.

    Unique or Primary Key cannot guarantee the uniqueness of a column with an Index on that column. That's why Oracle will create an Index by default if there is no such thing as an index on that column all to utter those two constraints. If an index is always associated with these two constraints.

    But no foreign key guarantees uniqueness on a column. And an Index on a foreign key is not required. So even if you create an index on a foreign key column that index is not associated with the foreign key.

  • foreign keys different files randomly

    Hello dear members,

    Here is my table

    MY_TABLE
    --------------------
    PRIMARY_KEY FOREIGN_KEY
    1A
    1 b
    3A
    4 B
    5 B
    6 B
    8 m
    8 C
    9 C
    10 D

    I want to fetch random files N where these records N foreign keys must be different.

    Example outouts:
    N = 2
    1
    4

    N = 3
    1
    7
    9


    I'm sure there is a quick and logical way to achieve this goal, but I couldn't find that either by myself or through google.
    Thanks for your replies.

    brkmhr wrote:
    Hello

    Are you sure that's the answer? Because I'm not!

    Stew of solution is really COOL, but your sentence unfortunately does not show satisfaction.

    If you want to try its solution with a slight change, you exactly what you need:

    with mydata AS
    (
       SELECT 1  pk, 'A' fk FROM DUAL UNION ALL
       SELECT 2  pk, 'A' fk FROM DUAL UNION ALL
       SELECT 3  pk, 'A' fk FROM DUAL UNION ALL
       SELECT 4  pk, 'B' fk FROM DUAL UNION ALL
       SELECT 5  pk, 'B' fk FROM DUAL UNION ALL
       SELECT 6  pk, 'B' fk FROM DUAL UNION ALL
       SELECT 7  pk, 'B' fk FROM DUAL UNION ALL
       SELECT 8  pk, 'C' fk FROM DUAL UNION ALL
       SELECT 9  pk, 'C' fk FROM DUAL UNION ALL
       SELECT 10 pk, 'D' fk FROM DUAL
    )
    SELECT pk
      FROM (  SELECT a.*
                   , ROW_NUMBER () OVER (PARTITION BY fk ORDER BY DBMS_RANDOM.random) rn
                FROM mydata a
            ORDER BY fk)
     WHERE rn = 1
       AND ROWNUM <= 3  -- this is your N
    ;
    

    Simply replace the number in the last line of the n you want.

    Next time try the solution before giving these answers.

    Kind regards.
    Al

  • Rename the foreign key constraint

    Hello

    I have table tbl_a with a column a_col1 SEO tbl_b.b_col1

    I want to create the name of the foreign key as FK_A_COL1 constraint which is FK_ < COLUMN NAME OF the CHILD >.

    Foreign naming key standards does not have the use of the name of the child under the name column.

    Is there any other solution/script to achieve this?

    Thank you.
    fkeys = model.getFKIndexAssociationSet().toArray();
    for (var i = 0; i;//also can use fk.getTable().getAbbreviation()fkname = "FK_"+fk.getTable().getName();cols = fk.getColumns();for( var k = 0;k
    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
    
  • Can we create a foreign key by using built in functions?

    Hi all

    I'm in the need to create a foreign key referring to an expression of a column in another table.

    For example, I try below and I got some errors.

    CREATE TABLE TABLE_A

    (

    student_id VARCHAR2 (30),

    student_name VARCHAR2 (100),

    CONSTRAINT fk_A FOREIGN KEY (student_id) REFERENCES TABLE_B (UPPER (column_name))

    );

    Please suggest if it is possible that a foreign key could be made in an expression or not?

    2761969 wrote:

    Hi all

    I'm in the need to create a foreign key referring to an expression of a column in another table.

    For example, I try below and I got some errors.

    CREATE TABLE TABLE_A

    (

    student_id VARCHAR2 (30),

    student_name VARCHAR2 (100),

    CONSTRAINT fk_A FOREIGN KEY (student_id) REFERENCES TABLE_B (UPPER (column_name))

    );

    Please suggest if it is possible that a foreign key could be made in an expression or not?

    No - it isn't.

    You can, however, create a VIRTUAL column in the parent table, and then create a foreign key that references.

  • correct handling of foreign keys in unique constraints for operations ON DELETE SET NULL

    During my troubleshooting at a client site, I came across an interesting setup where I would like to ask your opinion.


    Consider two tables ADTB and TBLB.

    ADTB:

    INT COLA1

    COLA2 INT

    TBLB:

    COLB1 INT

    COLA2 is COLB1 as a foreign key ON DELETE SET NULL value.

    COLA1 and COLA2 are combined into a UNIQUE for the BATTERY constraint.

    I have two records of TBLB:

    (1)

    (2)

    and two DRUM recordings:

    (1, 1)

    (2, 2)

    so far, all the constraints are valid.

    During my research, I learned that I can not put COLA2 null in ADTB for two lines at the same time:

    TBLA UPDATE set COLA2 = NULL where COLA1 = 1;

    -> OK

    TBLA UPDATE set COLA2 = NULL where COLA1 = 2;

    -> VIOLATED UNIQUE CONSTRAINT

    Unfortunately, it's something my application needs, anyway. In order to get this resolved, but I try to understand, what would happen if I:

    REMOVE THE TBLB;

    ?

    It should set COLA2 in ADTB "Null" on the two lines, which is not allowed due to the UNIQUE CONSTRAINT.

    Can someone shed light on this issue? Thank you.


    Yours,

    Ronny

    Unfortunately, as you say, you can't not NULL values in such a unique constraint.  It would be the same as if you had a composite primary key and try to allow one of the columns will be null in several rows with the same value in the other column.  NULL is treated as an "unknown" value, it is possible that it could be the same value (it may or may not be, but Oracle does not know), so it must assume that it might be and that's why treat it as if there is the potential for a duplicate... so it's not unique.

    In short, do not allow null values in your unique constraints.

    The default is in the drawing, not in the way that Oracle is the treatment.  Fix the design if you do not have NULL values.

    In terms of relational database design, it seems that you are trying to transform the 1:M relations M:M relations, and which requires an intermediate table if you have M:1 and 1:M between 3 tables instead.

Maybe you are looking for