A foreign key can reference a unique non-primary key?

Hello everyone, I have 2 tables: [books] with the fields (id, title, author, isbn) and another table called [issue Book] with the fields (id, borrower_id, borrow_date, book_isbn)...
Now the field (book_isbn) in the [book number] table of authorities (isbn) in the table [books], which is a single field, but not a primary key.

My questions:

1 - is this OK for a foreign key referencing a unique key but no primary in another table?

2 is it normal that the foreign key field to have another name as the field it references in another table?

Your help is appreciated indeed...

Yes CF can refer to Unique key, but you must specify the Unique key column when the creation of FK

and YES FK may have a different name, but for your convenience, we use the same column name

Here is an example

create table mymaster
(val_id number unique);

create table mydetail
(det_id number,
 val_id_fk number references mymaster(val_id));

Please mark correct / useful if it is

Bangoura
[My Oracle Blog | http://baigsorcl.blogspot.com/]

Tags: Database

Similar Questions

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

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

  • Unique or foreign key? Help

    Hi, this is my little project. Here is the code:

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    create table person
    (id_utente number (5) Primary Key,)
    name varchar2 (40),
    sexo varchar2 (1) check (sex = ' or sex = 'F').
    Morada varchar2 (60),
    date of data_nascimento,
    contacto number (10),
    numero_BI number (9)
    );

    create table Serviço
    (id_servico number (5) Primary Key,)
    Description varchar2 (40),
    Valor number (5),
    date of data_inicio,
    date of data_fim,
    check (data_fim > = data_inicio)
    );

    create the table modalidade
    (id_modalidade number (5) Primary Key,)
    Description varchar2 (40),
    Valor number (5)
    );

    create table quarto
    (id_quarto number (5) Primary Key,)
    Description varchar2 (40)
    );

    create the table inscricao_servico
    (id_inscricao_servico number (5) Primary Key,)
    id_servico number (5) references Serviço (id_servico).
    id_utente number (5) references user (id_utente),.
    date of data_pagamento
    );

    create the table inscricao_mod
    (id_inscricao_mod number (5) Primary Key,)
    id_modalidade number (5) references modalidade (id_modalidade).
    id_utente number (5) references user (id_utente),.
    date of data_inscricao,
    date of data_cessacao,
    check (data_cessacao > data_inscricao)
    );

    create table voy
    (id_estadia number (5) Primary Key,)
    id_utente number (5) references user (id_utente),.
    id_quarto number (5) quarto references (id_quarto).
    date of data_entrada,
    date of data_saida,
    check (data_saida > data_entrada)
    );

    create the table pag_mensalidade
    (id_pag_mensalidade number (5) Primary Key,)
    id_inscricao_mod number (5) references inscricao_mod (id_inscricao_mod).
    mes_mensalidade number (6).
    date of data_pagamento,
    single (mes_mensalidade)
    );

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    insert into person values (1, 'Manel', am', 'Rua large', to_date('80.02.02','yy.mm.dd'), 123456789, 687654321);
    insert into person values (2, "Artur", am', 'Rua pequeña', to_date ('81.03.04', 'yy.mm.dd'), 223456789, 587654321);
    insert into person values (3, 'Cardoso', ', 'Rua estreita', to_date ('82.04.05', 'yy.mm.dd'), 323456789, 487654321);
    insert into person values (4, 'Abílio', ', 'Rua larga', to_date ('79.07.06', 'yy.mm.dd'), 423456789, 787654321);
    insert into person values (5, 'Antunes', ', 'Rua nova', to_date ('79.06.01', 'yy.mm.dd'), 423557982, 387654822);

    insert into modalidade values (1, 'Total', 800);
    insert into modalidade values (2, ' Diurno ate 18' 600 ');
    insert into modalidade values (3, ' Diurno ate 22', 700 ');

    Insert in quarto values (1, '412');
    Insert in quarto values (2, '413');
    Insert in quarto values (3, '414');
    insert into values of quarto (4, '415');
    Insert in quarto values (5, '416');

    insert into Serviço values (1, ' Excursao a Fatima ", 150, to_date ('11.05.13', 'yy.mm.dd'), to_date ('11.05.13', 'yy.mm.dd'));
    insert into Serviço values (2, 'Ida ao cinéma', 10, to_date ('11.08.22', 'yy.mm.dd'), to_date ('11.08.22', 'yy.mm.dd'));
    insert into Serviço values (3, 'Apoio domiciliario', 100, to_date ('11.07.01', 'yy.mm.dd'), to_date ('11.07.31', 'yy.mm.dd'));

    insert into voy values (1, 1, 1, to_date ('11.05.05', 'yy.mm.dd'), null);
    insert into voy values (2, 2, 2, to_date ('10.01.02', 'yy.mm.dd'), null);
    insert into voy values (3, 3, 3, to_date ('09.12.15', 'yy.mm.dd'), to_date ('10.02.25', 'yy.mm.dd'));

    insert into inscricao_mod values (1, 1, 1, to_date ('09.12.15', 'yy.mm.dd'), null);
    insert into inscricao_mod values (2, 2, 2, to_date ('11.11.11', 'yy.mm.dd'), null);
    insert into inscricao_mod values (3, 1, 3, to_date ('10.04.10', 'yy.mm.dd'), to_date ('11.09.21', 'yy.mm.dd'));

    insert into inscricao_servico values (1, 1, 1, null);
    insert into inscricao_servico values (2, 1, 2, to_date ('11.10.01', 'yy.mm.dd'));
    insert into inscricao_servico values (3, 2, 4, null);
    insert into inscricao_servico values (3, 4, 5, to_date ('10.12.12', 'yy.mm.dd'));

    insert into pag_mensalidade values (1, 1, 201110, to_date ('11.10.23', 'yy.mm.dd'));
    insert into pag_mensalidade values (2, 2, 201111, to_date ('11.11.27', 'yy.mm.dd'));
    insert into pag_mensalidade values (3, 3, 201112, NULL);
    insert into pag_mensalidade values (1, 4, 201111, NULL);

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Select * from user;
    Select * from voy;
    Select * from inscricao_mod;
    Select * from inscricao_servico;
    Select * from modalidade;
    Select * from pag_mensalidade;
    Select * from quarto;
    Select * from Serviço.

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    drop table voy;
    drop table inscricao_servico;
    drop table quarto;
    drop table pag_mensalidade;
    drop table inscricao_mod;
    drop table modalidade;
    drop table Serviço;
    drop table person;

    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    OK, I have a little problem with table PAG_MENSALIDADE (meaning: payment of monthly instalments). This is the table that stores information about the payment of monthly payments, associated with a subscription (INSCRICAO_MOD) of a category (MODALIDADE) that has a price (MODALIDADE. VALOR).

    Select * from pag_mensalidade;

    Now, my problem is:
    It is an example of inserting data in this table:


    insert into pag_mensalidade values (1, 1, 201110, to_date ('11.10.23', 'yy.mm.dd'));

    the 3rd field - mes_mensalidade - we want of the month to pay. If I do this:

    insert into pag_mensalidade values (1, 2, 201110, to_date ('11.10.26', 'yy.mm.dd'));

    the system accepts, but I was really doing customer with subscription nº1 (2nd field, id_inscricao_mod = 1) pay for October 2011 twice (3rd field, 201110).
    And I don't want to do because customers must pay one month specific only once.

    How can I do this? Using the unique key? Foreign key? And how?

    Thank you

    Chiapa

    Hello Chiapa.

    I would use a trigger for this:

    CREATE OR REPLACE TRIGGER trigger_mensalidade
    BEFORE INSERT ON pag_mensalidade
    FOR EACH ROW
    DECLARE
    total NUMBER;
    
    BEGIN
     SELECT COUNT(1)
     INTO total
     FROM pag_mensalidade
     WHERE id_inscricao_mod = :new.id_inscricao_mod and
     substr(mes_mensalidade,5,2) = substr(:new.mes_mensalidade,5,2);
    
     IF total > 0 THEN
       RAISE_APPLICATION_ERROR(-20001,'Clients can only pay a specific month once.');
     END IF;
    END trigger_mensalidade;
    /
    
  • 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:

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

  • PARTITION BY REFERENCE: ORA-02266 when several relatives of foreign key

    Bubble my problem down to a simple example, imagine providers of satellite TV, their programming packages, their clients and customers linking to packages.  Shouldn't be able to drop all data from a provider in one shot?

    CREATE TABLE suppliers

    (provider_name VARCHAR2 (20) NOT NULL)

    CONSTRAINT providers_pk PRIMARY KEY (nom_de_fournisseur)

    )

    PARTITION BY LIST (provider_name) (PARTITION provider__dflt VALUES (DEFAULT))

    /

    CREATE TABLE programming

    (programming_name VARCHAR2 (50) NOT NULL)

    , provider_name VARCHAR2 (20) NOT NULL

    CONSTRAINT programming_pk PRIMARY KEY (programming_name)

    CONSTRAINT programming_fk1 FOREIGN KEY (nom_de_fournisseur)

    REFERENCES (provider_name) providers

    )

    PARTITION BY REFERENCE (programming_fk1)

    /

    Subscribers of CREATE TABLE

    (subscriber_name VARCHAR2 (50) NOT NULL)

    , provider_name VARCHAR2 (20) NOT NULL

    CONSTRAINT subscribers_pk PRIMARY KEY (subscriber_name)

    CONSTRAINT subscribers_fk1 FOREIGN KEY (nom_de_fournisseur)

    REFERENCES (provider_name) providers

    )

    PARTITION BY REFERENCE (subscribers_fk1)

    /

    CREATE TABLE subscriber_programming

    (subscriber_name VARCHAR2 (50) NOT NULL)

    , programming_name VARCHAR2 (20) NOT NULL

    CONSTRAINT Subscriber_programming_pk

    PRIMARY KEY (subscriber_name, programming_name)

    CONSTRAINT subscriber_programming_fk1 FOREIGN KEY (subscriber_name)

    Subscribers of REFERENCES (subscriber_name)

    CONSTRAINT subscriber_programming_fk2 FOREIGN KEY (programming_name)

    Programming REFERENCES (programming_name)

    )

    PARTITION BY REFERENCE (subscriber_programming_fk1)

    /

    ALTER TABLE SPLIT PARTITION provider__dflt values ("dishnetwork") suppliers

    IN (provider_dishnetwork PARTITION, PARTITION provider__dflt)

    UPDATE INDEX

    /

    INSERT INTO suppliers (provider_name)

    VALUES ("dishnetwork")

    /

    INSERT INTO subscribers (subscriber_name, provider_name)

    VALUES ('john doe', "dishnetwork")

    /

    INSERT INTO programming (programming_name, provider_name)

    VALUES ('top200","dishnetwork")

    /

    ALTER TABLE DROP PARTITION provider_dishnetwork UPDATE INDEX providers

    /

    I have same INSERT in subscriber_programming; the above is sufficient to cause the latter to draw "ORA-02266: unique/primary keys in table referenced by foreign keys enabled". ""  Version information:

    -Oracle Database 11 g Enterprise Edition Release 11.2.0.4.0 - 64 bit Production

    -With options for partitioning, Real Application Clusters, Automatic Storage Management, OLAP, Data Mining and Real Application Testing

    -Linux 2.6.32 - 279.11.1.el6.x86_64 #1 SMP Sat Sep 22 07:10:26 EDT 2012 x86_64 x86_64 x86_64 GNU/Linux

    This simple demo is simply representative of course.  Our number of real paintings ~ 60, with hundreds of millions of records in a large number of tables, a tree FK covering several levels of depth, without the partition littered throughout key.  Given that the volume of data, we try hard to avoid executing DELETE, which is why I explore PARTITION migration BY reference.  The problem is not unlike the already-fixed Bug 9329773, but it is slightly different - especially that this variant is not resolved.  If there is no way that such a facility would never fly, you have any suggestions that would not involve changes to application code?  (For example, adding a partition to the tables key column kids won't fly, but I might be able to sell disabling constraints to developers.)

    After much head striking, I found the trick, by https://jonathanlewis.wordpress.com/2006/12/10/drop-parent-partition/ .  The difference of Appendix A is bold: the NOVALIDATE on subscriber_programming_fk2 and the EXCHANGE/CREATE/DROP of programming_e.

    CREATE TABLE suppliers

    (provider_name VARCHAR2 (20) NOT NULL)

    CONSTRAINT providers_pk PRIMARY KEY (nom_de_fournisseur)

    )

    PARTITION BY LIST (provider_name) (PARTITION provider__dflt VALUES (DEFAULT))

    /

    CREATE TABLE programming

    (programming_name VARCHAR2 (50) NOT NULL)

    , provider_name VARCHAR2 (20) NOT NULL

    CONSTRAINT programming_pk PRIMARY KEY (programming_name)

    CONSTRAINT programming_fk1 FOREIGN KEY (nom_de_fournisseur)

    REFERENCES (provider_name) providers

    )

    PARTITION BY REFERENCE (programming_fk1)

    /

    Subscribers of CREATE TABLE

    (subscriber_name VARCHAR2 (50) NOT NULL)

    , provider_name VARCHAR2 (20) NOT NULL

    CONSTRAINT subscribers_pk PRIMARY KEY (subscriber_name)

    CONSTRAINT subscribers_fk1 FOREIGN KEY (nom_de_fournisseur)

    REFERENCES (provider_name) providers

    )

    PARTITION BY REFERENCE (subscribers_fk1)

    /

    CREATE TABLE subscriber_programming

    (subscriber_name VARCHAR2 (50) NOT NULL)

    , programming_name VARCHAR2 (20) NOT NULL

    CONSTRAINT Subscriber_programming_pk

    PRIMARY KEY (subscriber_name, programming_name)

    CONSTRAINT subscriber_programming_fk1 FOREIGN KEY (subscriber_name)

    Subscribers of REFERENCES (subscriber_name)

    CONSTRAINT subscriber_programming_fk2 FOREIGN KEY (programming_name)

    Programming (programming_name) NOVALIDATE REFERENCES

    )

    PARTITION BY REFERENCE (subscriber_programming_fk1)

    /

    ALTER TABLE SPLIT PARTITION provider__dflt values ("dishnetwork") suppliers

    IN (provider_dishnetwork PARTITION, PARTITION provider__dflt)

    UPDATE INDEX

    /

    INSERT INTO suppliers (provider_name)

    VALUES ("dishnetwork")

    /

    INSERT INTO subscribers (subscriber_name, provider_name)

    VALUES ('john doe', "dishnetwork")

    /

    INSERT INTO programming (programming_name, provider_name)

    VALUES ('top200","dishnetwork")

    /

    INSERT INTO subscriber_programming (subscriber_name, programming_name)

    VALUES ('john doe', "top200")

    /

    CREATE TABLE programming_e AS

    SELECT * PROGRAMMING WHERE 0 = 1

    /

    ALTER TABLE programming

    Provider_dishnetwork WITH TABLE programming_e SWAP PARTITION

    /

    DROP TABLE programming_e

    /

    ALTER TABLE DROP PARTITION provider_dishnetwork UPDATE INDEX providers

    /

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

  • Question: Is foreign key references to a different column in the same table

    Hello

    I'm not grasp this concept. Would you be able to provide the logic how and why you need to create a foreign key that references a column of the same table?

    For example, referencing the schema, SupervisorNo @ here is a FOREIGN KEY to the "EmpNo" column in the same table.

    EMPLOYEE (EmpNo, title, Fname, Lname, phone, Email, date of birth, sex, salary, HireDate, OutNo, SupervisorNo @)


    Why would you want to do this and how can it be used? If you are filling out the data in the table, and this constraint is enabled, you will not be able to load whatever it is because the "EmpNo" column will be empty.


    Any input you can provide me with this would be greatly appreciated. Thank you!



    His name

    Self-referential integrity constraints

    Lets say that we have the table emp with emp_no and mgr_no columns.

    Now all managers should also be an employee too. How can I check this when inserting data into this table? I applied auto integrity constraint. It would help me to make sure no handler (doesn't mean no mgr_no) must be registered if this Manager is not an employee of this organization. Which means, I won't be able to enter any mgr_no in the column until and unless that the same employee also is an employee (emp_no from the same table).

    Check the link as well below.

    Data integrity

    Thank you

    Ishan

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

  • 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

  • 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

  • How to set the condition of rule to check the Primary Key, Foreign Key and Unique

    I use Oracle 10.2
    Could someone tell me please how to set the rule condition to check the primary key, foreign key constraint of uniqueness?

    Hello

    Primary key, unique constraints and foreign key are better applied using the native functionality of database constraint. If you like the rules to check if a key exists at the time of the evaluation, you can use a function defined by the user in a rule condition and this function can ask to turn the table keys.

    Hope this helps,
    -Aravind.

  • Unique key and foreign key in the view of materialzied

    Hello world
    I have one main site and 4 materialized view sites.
    In my main site, I did a main group that contains my tables participating in replication.
    I have a table that has a primary key, foreign key constraint unique.
    In my first materialized view site I did group for my opinion

    BEGIN
    DBMS_REPCAT. () CREATE_MVIEW_REPGROUP
    gname = > "hr_repg"
    Master = > "orc1.com"
    propagation_mode = > 'ASYNCHRONOUS');
    END;
    /

    BEGIN
    DBMS_REFRESH. DO)
    name = > 'mviewadmin.hr_refg ',.
    list = > ",
    next_date = > SYSDATE,.
    interval = > 'SYSDATE + 1/144',.
    implicit_destroy = > FALSE,
    rollback_seg = > ",
    push_deferred_rpc = > TRUE,
    refresh_after_errors = > FALSE);
    END;
    /

    Then I created the materialized view

    HR.test CREATE MATERIALIZED VIEW
    COOL OFF QUICKLY WITH A PRIMARY FOR THE UPDATE KEY
    AS SELECT * FROM [email protected]
    /

    BEGIN
    DBMS_REPCAT. () CREATE_MVIEW_REPOBJECT
    gname = > "hr_repg"
    sName = > 'hr ',.
    oname = > 'test ',.
    Type = > "SNAPSHOT."
    min_communication = > TRUE);
    END;
    /

    BEGIN
    DBMS_REFRESH. ADD)
    name = > 'mviewadmin.hr_refg ',.
    list = > "hr_test.test"
    Lax = > TRUE);
    END;
    /

    When I check the test (materialized view) table in the site of the materialized view to a primary key, but it has no foreign key forced unique having the base table and because my test materialized view can be changed I need to have these 2 constraint on my my materialized view. You have an idea why these 2 constraint were not created on my materialized view? And what should I do to have these two constraints.

    Thanks in advance

    On MView is created automatically only PK. Other constraints (check, not null, unique or FK) should be created manulay. Indexes also.
    But be careful with the FK.
    On ORACLE8, I found lines that are inserted to updatable mview back in deleted updating process, then inserted (downloaded on the main site). When FK is with on delete cascade that the lines of the child tables (mviews are removed too). And when the child mview is updated before mview parent you found the child missing lines.
    When CF is on delete restrict that you refresh the process fails.

  • Can I remove Logical columns that I will not use in the layer of presentation as the id or foreign key columns?

    Hello

    Can I remove Logical columns at the level of the business layer that I'm not going to use the layer of presentation as the ID or foreign key columns? It will not affect the relations to the business layer?

    Kind regards

    Nestor

    Hi Nestor,.

    It won't unless they are used as keys, columns sort order, the ID column or the likes. But you can leave them in the MDB layer and expose just in the presentation layer - just in case you need it later. It is not use all the resources or generate overhead. More: Developer of the RPD should be used to work with loads of columns

Maybe you are looking for