Foreign key, check force

Hello

This is the structure of two two table.

Untitled.png

DDL:

CREATE TABLE TEST_PARENT
  (
    PARENT_KEY NUMBER (22),
    PARENT_ID NUMBER (22),
    PARENT_NAME VARCHAR2 (255 CHAR),
    EFFECTIVE_START_DATE DATE DEFAULT sysdate NOT NULL ,
    EFFECTIVE_END_DATE   DATE
  ) ;
ALTER TABLE TEST_PARENT ADD CONSTRAINT PK_TEST_PARENT PRIMARY KEY ( PARENT_KEY ) ;


CREATE TABLE TEST_CHANNEL
  (


    CHANNEL_KEY NUMBER (22),
    CHANNEL_ID NUMBER (22),
    CHANNEL_NAME VARCHAR2 (255 CHAR),
    EFFECTIVE_START_DATE DATE DEFAULT sysdate NOT NULL ,
    EFFECTIVE_END_DATE   DATE 
  ) ;
ALTER TABLE TEST_CHANNEL ADD CONSTRAINT PK_TEST_CHANNEL PRIMARY KEY ( CHANNEL_KEY ) ;

Logic:

Parent_id is always fixed to a Parent. But a Parent can have different attributes (parent_name here). Where the attributes are changing it will end the bu previous records put a value to effective_end_date and it will create a new line. New line will have the PARENT_KEY new PARENT_NAME with new effective_start_date and null in effective_end_date.

Same logic applies to the CHILD table as well.

What is the best way to put a reference to the type of the foreign key in the CHILD table that in case, will ensure that a child cannot exist without a parent now. I can't take PARENT_KEY as a foreign key to the same parent can change the PARENT_KEY and a child must always point to the parent ASSET (EFFECTIVE_END_DATE as null) folder.

Thanks in advance.

I would enrich your datamodel by tables of the object/version as:

CREATE TABLE TEST_PARENT_OT
(
NUMBER OF PARENT_KEY (22) NOT NULL
) ;

ALTER TABLE TEST_PARENT_OT ADD CONSTRAINT PK_TEST_PARENT_OT PRIMARY KEY (PARENT_KEY);

CREATE TABLE TEST_PARENT_VT
(
NUMBER OF PARENT_KEY (22) NOT NULL,
NUMBER of VERSION_ID (22) not null,
PARENT_NAME VARCHAR2 (255 CHAR),
EFFECTIVE_START_DATE DATE default sysdate NOT NULL,
DATE OF EFFECTIVE_END_DATE
) ;

ALTER TABLE TEST_PARENT_VT ADD CONSTRAINT PK_TEST_PARENT_VT PRIMARY KEY (PARENT_KEY, VERSION_ID);
ALTER TABLE TEST_PARENT_VT add CONSTRAINT FK_PARENT_VT_PARENT_OT foreign key (parent_key) refers to test_parent_ot (parent_key);

CREATE TABLE TEST_CHANNEL_OT
(
NUMBER OF CHANNEL_KEY (22) NOT NULL
) ;

ALTER TABLE TEST_CHANNEL_OT ADD CONSTRAINT PK_TEST_CHANNEL_OT PRIMARY KEY (CHANNEL_KEY);

CREATE TABLE TEST_CHANNEL_VT
(
NUMBER OF CHANNEL_KEY (22) NOT NULL,
NUMBER OF VERSION_ID (22) NOT NULL,
PARENT_KEY NUMBER (22) NOT NULL,
CHANNEL_NAME VARCHAR2 (255 CHAR),
EFFECTIVE_START_DATE DATE default sysdate NOT NULL,
DATE OF EFFECTIVE_END_DATE
) ;

ALTER TABLE TEST_CHANNEL_VT ADD CONSTRAINT PK_TEST_CHANNEL_VT PRIMARY KEY (CHANNEL_KEY, VERSION_ID);
ALTER TABLE TEST_CHANNEL_VT add CONSTRAINT FK_CHANNEL_VT_CHANNEL_OT FOREIGN KEY (channel_key) REFERENCES TEST_CHANNEL_OT (channel_key);
ALTER TABLE TEST_CHANNEL_VT add CONSTRAINT FK_CHANNEL_VT_PARENT_OT FOREIGN KEY (PARENT_key) REFERENCES test_parent_ot (parrent_key);

Using this you will not have to reallocate your child of the lines when a new version of parent is created and you will be able to have the independent channel versions of the parent versions (so no need to change all the children when the parents ends).

the construction of views of parents and the channels which only show active lines.

The problem you may have is to be sure to have for each point in time an active version and so I would not implement of an effective_end date, but inherit the effective end of the effective next to (which is a little more work but you will help gapelessness if necessary)

HTH

Tags: Database

Similar Questions

  • Foreign key check - possible where clause

    I want to create a foreign key referential constraint, but I want to do this only for certain data.

    example 2 tables. With 5 rows T1, t1 is to reference col1 in t1, but where the only col2 = "a". If I have all the data of reference I get an error creating the audit as t1.col1 is not unique for all data and is not a PK.
    create table t1
       (col1 varchar(1),
       col2 varchar(1))
       
       create table t2
       (col1 varchar(1),
       col3 varchar(1))
       
       insert into t1 values ('a','a');
       
       insert into t1 values ('b','a');
       
       insert into t1 values ('c','a');
       
       insert into t1 values ('d','b');
       
       insert into t1 values ('d','b');
    
    commit;
    
    alter table t2
      add constraint FK_t1_col1 foreign key (col1)
      references t2(col1);
    
    ORA-02270: no matching unique or primary key for this column-list
    This is an example of a small subset of a much larger table where col1 is not the PK and I can't create a unique index on it as his unique not on its own.

    I could create a materialized view but would rather not if possible. Y at - it solution or method on t1 to create the repository control with a where clause clause in order to do something like the following pseudocode
    alter table t2
      add constraint FK_t1_col1 foreign key (col1)
      references t2(col1)   
        where col2 = 'a' ;

    961469 wrote:
    I want to create a foreign key referential constraint, but I want to do this only for certain data.

    example 2 tables. With 5 rows T1, t1 is to reference col1 in t1, but where the only col2 = "a". If I have all the data of reference I get an error creating the audit as t1.col1 is not unique for all data and is not a PK.

    You don't have to have a primary key as a reference point, it must be unique, and it may be a virtual column. If you are running 11g is easy:

    drop table t2;
    drop table t1;
    
    create table t1 (
         code_type     varchar2(10)     not null,
         code_number     number(10)     not null,
         a_code          generated always as
                        (case code_type when 'A' then code_number else null end)
    );
    
    alter table t1 add constraint t1_uka unique (a_code);
    
    create table t2(
         id1     number not null references t1(a_code)
    );
    
    insert into t1 (code_type, code_number) values('A',1);
    
    -- next row is not a duplicate
    
    insert into t1 (code_type, code_number) values('B',1);
    
    insert into t1 (code_type, code_number) values('B',2);
    
    -- next row succeeds
    
    insert into t2 values(1);
    
    -- next row fails
    
    insert into t2 values(2);
    
    commit;
    

    Concerning
    Jonathan Lewis

  • How to make a primary foreign key?

    I'm working on my data model and I can't understand how a foreign key to be a primary foreign key... I'll make a primary foreign key for this specific table 2...
    I tried to do the foreign key checked as the primary_UID... but this isn't the solution... I want to show in the relational model indicated as "PF" or the primary foreign key

    Anyone know? Thanks in advance

    Hi Delos,

    What version of SQL Developer Data Modeler do you use?
    For me, it works in 3.1.1.703 and 3.1.2.704.
    Can you provide more details.
    For the logic model you can use the Unique identifier dialog box and add links to the list of used objects.

    Philippe

  • foreign key 'generate' strange behavior check box, any idea?

    Summary: in the properties of the table, the foreign keys, the box generate behaves badly, and I don't understand why or how I got there.

    Oracle Data Modeler 4.0.2.840 8.1 X 64 Windows, jdk1.7.0_55

    the behavior is:

    (1) add the foreign key (two different more to see strange behavior)

    (2) uncheck 'generate' on a foreign key

    (3) apply, Ok

    (4) ask 'Preview DDL', a foreign key constraint is generated, SO FAR SO GOOD, I'm happy

    5-now click on the button 'generate the DDL' for any design = > * * TWO foreign keys are generated?

    (6) even more strange: sometimes the 'generate' check itself after step 5, most of the time is not

    (7) at the beginning this has occurred for a couple of foreign key 'uncheckees', now it almost always happens.

    The relational model was built by importing the SQL scripts already exist (try to migrate the Data Modeler). No too complex ~ 115 tables, 12 times.

    I think it's a bug, but I can't find any reference on the web, maybe I do something wrong?

    Hi Michel,.

    The generation is different depending on whether or not the relevant storage design is open.

    If the design of the storage is not open, the option "Generate DDL" on the foreign key is taken into account (for an overview of the DDL or full generation of DDL).  For an entire generation of DDL, the foreign key will initially not selected phase in the generation of the DDL if Options the option "Generate DDL" on the foreign key has been disabled.  (This change in the phase of the generation of DDL Options will also cause the option "Generate DDL" on the foreign key update.)

    If the design of the storage is open, the option 'generate DDL"on the foreign key has no effect (in a Preview of DDL or full DDL generation).

    Also in a generation full of DDL, an object will be initially marked as not selected if it has been disabled in the previous generation of DDL.

    I hope that this helps to explain the behavior that you find.

    David

  • How to check where the table field has been used as a foreign key in the database

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

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

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

    Check this box

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

    Type = 'R' means referential integrity constraint type.

  • 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

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

  • can associate US foreign key index?

    Hello
    I've searched and can't find the answer to the foregoing;
    I have a foreign key table constraint; I added an index on that column
    However when I ask the all_constraints under index_name to this foreign constraint, there is nothing. only when I PK/UK I deal see index associated with them;
    then oracle will still associate my index finger with the FK column forced? or I need excplicity pair with the foreign key column? If so, how?
    THX
    Rgds

    When you do "I added an index on that column;" you "associate explicitly" a column with an index.
    You can check this with

    select *
    from all_ind_columns
    

    But there is no direct relationship between a FK constraint and index

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

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

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

  • Oracle cannot validate the composite foreign key when NULLs comes into play

    Hi all

    When a composite foreign key is used in a schema. Oracle does not check the necessary rules correctly, as indicated below.

    create the table parent_tab
    (
    x int,
    int y,
    z int,
    primary key for constraint p_key (x, y)
    );

    create the table child_tab
    (
    x int,
    int y,
    z int,
    Constraint f_key foreign key (x, y) refers to parent_tab (x, y)
    );

    insert into child_tab (x, y, z) values (1, NULL, 4);
    commit;

    Select count(x|| y) from parent_tab;

    0


    Select count(x|| y) from child_tab;

    1

    Tom Kyte says the following for an explanation.

    The database is unable to validate a foreign key when it is partially null. To assert the rule MATCH FULL for composite foreign keys, you add a constraint to your table:

    The constraint will ensure that either

    • All columns have NULL value in the foreign key, or
    • None of the columns are set to NULL in the foreign key

    As long as this constraint is in place, your foreign key will work as you probably think that it should.

    However, it does not explain WHY? I mean why Oracle is unable to validate the foreign key when it is partially NULL?

    Concerning

    John Spencer wrote:

    He thinks logically.  You have a composite PK of two columns, so 1, 1 is a valid key, as these are the 1, 2 and 1, 3 and 1.42.  In the child table, you have a line 1, the null value.

    What parent should have the child?

    I wish that it would be logical about this. Unfortunately, Oracle applies different rules in different cases. For example, parent of the value 1, NULL value and 1 child, NULL is not the same as NULL is equal or not equal to null. At the same time, one constraint unique index / unique Treaty, 1, NULL, equal to another 1, NULL NULL treatment as being equal to NULL and don't let several lines with 1, the NULL value.

    SY.

  • Exclude foreign keys from the relational model comparison

    Hello!

    Is it possible to exclude foreign keys for comparison when comparing relational models?

    It's the same and when inporting dictionary of data in the relational model.

    Thank you very much for the help!

    Kind regards

    Dimitar Angelov

    Hi Dimitar,

    Yes it is possible to do using "generate DDL' flag - there is an option in the dialog box"compare models"- tab" Options > compare Options '-C' is a checkbox "use generate DDL settings to filter objects.

    You must make a DDL generation with open physical model that will be used - in the DDL generation options you can deselect items you don't want to have in comparison. You can save as a configuration of generation of DDL or simply do it before the comparison.

    After comparing models of dialog, you can check "Use generate DDL settings to filter objects" box to check (and probably to load a configuration of generation of DDL) and use the buton "refresh trees."

    You can use this method to filter the tables, constraints PK and UK, index, constraints FK and the views.

    Philippe

  • 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

  • logical foreign key

    Hi all

    I'm sorry to bring the similar question. .
    I am able to create the logical foreign key for table logic with A new (A.2 = B.2) (2 is calculated logical column).
    However there are still some physical join foreign key with the old constitution (A1 = B. 1). When I deploy this RPD, it generates sql with (A1 = B. 1) join condition.

    Then, I removed this RPD physical foreign key join and received a warning "logical dimension B has a source which don't happen with any fact table" in the consistency check.

    Always after the deployment of the Dre, I get the error message [nQSError: 14025] indeed no table exists at the level of detail requested: in the report.

    I'm missing something. The logical foreign key has join condition correct that he is still not on the physical join of riding.

    Thank you for going through this question,

    http://Tinypic.com/r/2qiwmrr/6

    Based on your comments, I am looking at the documentation:
    I found a Note: a logical key to a fact table should consist of key columns that join the tables of attribute. Logical foreign key joins may be necessary if the Oracle's BI server should be used as an ODBC data source for some queries and third party reporting tools.

    I need to see why ODBC, and unlike the foreign logic key join is not the substitution of the physical layer to join.

    BTW: Since you want to go with a join defined using logical column, you can use the same expression in physical join using the expression builder?

    It should work.

    EX: T43770. Department_id = case when T43764. Department_id > 10 then T43764. Department_id 0 otherwise end

    Published by: Srini VIEREN on 18 January 2013 16:52

Maybe you are looking for