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.

Tags: Database

Similar Questions

  • 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

  • Constraint primary key turn off - turn on problem solution

    Hello
    recently I truncate a table. during the truncation process I face with problem and solve below the track.

    SQL > TRUNCATE TABLE USER_STATE;
    TRUNCATE TABLE USER_STATE
    *
    ERROR on line 1:
    ORA-02266: permit to unique/primary keys in table referenced by foreign keys

    Then I find the related CONSTRAINT on the table according to the truncate table ORA-02266


    SQL > ALTER TABLE USER_STATE_PARAMETER disable the CONSTRAINT FK1_USR_SES_ST_PAR;

    Modified table.

    SQL > ALTER TABLE USER_STATE disable the CONSTRAINT SYS_C0030849;

    Modified table.

    Then successfully truncate the table as below

    SQL > TRUNCATE TABLE USER_STATE;

    Table truncated.

    then I activate the CONSTRAINT

    SQL > ALTER TABLE USER_STATE allow CONSTRAINT SYS_C0030849;

    Modified table.

    SQL > ALTER TABLE USER_STATE_PARAMETER allow FK1_USR_SES_ST_PAR of CONSTRAINT;

    Modified table.


    -My Question is, after you disable the CONSTRAINT, I did the truncate operation, and then enable the CONSTRAINT. I do who will trust me to do not face any problem in the database of the DML OPERATION? I also found there is no CONSTRAINT to disable in my database.

    >
    I do
    >
    Nope - you would not enable foreign key constraints, if the tables had given them that reference the you truncated.

    So if the constraints are enabled and valid, you will have no problem.

    You can (and should) always test certain simple dml to confirm there is no problem.

  • 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

    /

  • Oracle 10.2.0.3.0 - impdp - unusable index exists on unique/primary...

    Hi all

    Oracle 10.2.0.3.0 - during impdp the following error appears:

    Object type SCHEMA_EXPORT/TABLE/CONSTRAINT/treatment
    ORA-39083: THAT CONSTRAINT Type could not create object error:
    ORA-14063: unusable Index exists on constraint unique/primary key
    Because sql is:
    ALTER TABLE 'MY_SCHEMA '. "' MY_TABLE ' ADD CONSTRAINT PRIMARY KEY 'MY_TABLE_PK' ('ID')
    USING INDEX PCTFREE, INITRANS 10 2 MAXTRANS 255
    STORAGE (INITIAL 1048576 THEN 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 DEFAULT USER_TABLES)
    TABLESPACE "MY_TBS".

    Question:

    Tree index does not contain the dump file, and impdp should simply create index as it appears in the metadata information.

    So, what's the point to clear this error?

    921817 wrote:

    So, what's the point impdp tells his State? It can be hundreds of unusable indexes, why it is not only rebuild?

    Because Impdb cannot create an object that is already there and can't make it valid.

    Aman...

  • Accompanied by primary key to a non-unique index?

    Met a weird situation today. A utility, we set up which allows analysts to restore the data in their paintings, started failed when it attempted to drop an index. The index supported a primary key. Makes sense. But our script was supposed to be only an attempt to drop/recreate the nonunique indexes. It turns out that supported on the primary key index was not unique and to the best of my knowledge has emerged as follows:
    SQL> create table junk (f number(1));
    
    Table created.
    
    SQL> create index junk_ix on junk(f);
    
    Index created.
    
    SQL> select UNIQUENESS from DBA_INDEXES where index_name = 'JUNK_IX';
    
    UNIQUENES
    ---------
    NONUNIQUE
    
    SQL> alter table forbesc.junk add constraint junk_pk primary key (f) using index junk_ix;
    
    Table altered.
    
    SQL> select UNIQUENESS from DBA_INDEXES where index_name = 'JUNK_IX';
    
    UNIQUENES
    ---------
    NONUNIQUE
    
    SQL> insert into junk values (1);
    
    1 row created.
    
    SQL> insert into junk values (1);
    insert into junk values (1)
    *
    ERROR at line 1:
    ORA-00001: unique constraint (FORBESC.JUNK_PK) violated
    
    
    SQL> select index_name from dba_constraints where constraint_name = 'JUNK_PK';
    
    INDEX_NAME
    ------------------------------
    JUNK_IX
    What I can't understand, is how an index is not unique is to apply oneness. I thought it was the key to this process. I thought that perhaps an index with the 'SYS_123456' has be created, perhaps, but I couldn't find a:
    SQL> select object_name, object_type from dba_objects order by created desc;
    
    
    OBJECT_NAME   OBJECT_TYPE
    -------------------------------------------     
    JUNK_IX     INDEX
    JUNK     TABLE
    ...
    How the uniqueness is get applied in this case? It comes in Oracle 11.1.0.7

    Thank you
    -= Chuck

    It has always been like that. Oracle can and will use a non-unique index to apply a constraint to PK, the existing index just needs to have the columns PK as the leader or the columns in the index:

    SQL> create table t (id number, id1 number, descr varchar2(10));
    
    Table created.
    
    SQL> create index t_ids on t(id, id1);
    
    Index created.
    
    SQL> select index_name from user_indexes
      2  where table_name = 'T';
    
    INDEX_NAME
    ------------------------------
    T_IDS
    
    SQL> alter table t add constraint t_pk
      2  primary key (id);
    
    Table altered.
    
    SQL> select index_name from user_indexes
      2  where table_name = 'T';
    
    INDEX_NAME
    ------------------------------
    T_IDS
    
    SQL> insert into t values (1, 1, 'One');
    
    1 row created.
    
    SQL> insert into t values (1, 2, 'Two');
    insert into t values (1, 2, 'Two')
    *
    ERROR at line 1:
    ORA-00001: unique constraint (OPS$ORACLE.T_PK) violated
    

    John

  • What is the difference between primary key and unique indexes with forced not null?

    Primary key is = unique index + not null?

    The short answer is Yes.

    However, even if the primary key, applying both uniquness and not null, there is a notion of "special".

    You can only have one primary key in tables, but you can have multiple unique indexes and constraints not null.

    See: https://asktom.oracle.com/pls/asktom/f?p=100:11:0:P11_QUESTION_ID:8743855576462

  • When I change a column is a primary key the associated non-unique index to become unique?

    So basically I already tried this and it shows me that the associated index is not unique.

    create table employees2 in select * from employees;

    create index emp_idx on employees2 (employee_id);

    ALTER employees2 table add primary key (employe_id) using index emp_idx;

    Select * from user_indexes where index-name = "EMP_IDX";

    I was wondering if I right assuming that when you change a column to a primary key or unique while using a given index that does not have the respective index become unique.

    The textbooks I use are sometimes a little hard to understand because of the wording, also, I want to just ask someone with a little more experience than me.

    Thank you.

    your test did give the correct answer: the index is not unique if it serves to bear a unique or primary key constraint. Indeed, it is one of the benefits of the use of no unique indexes in support of UK/PK constraints (since it allows to set the unusable index before to make bulk loads; and, of course, they have also some disadvantages - for example, they need an additional logical reading to reach a line). Richard Foote explains the details in https://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ (and other items).

  • Why we cannot create more than one primary key on a table. Why we create several unique key on a table. Please explain if anyone have details of this.

    Why we cannot create more than one primary key on a table. Why we create several unique key on a table. Please explain if anyone have details of this.

    «a primary key has semantic meaning, it is to be immutable (never change of value), unique and not null.»

    a unique constraint is simply "at any time, these values are unique - they can change and they can be null.

    You use a unique when constraint

    (a) you do not already have a primary key for a table can have only one
    (b) you allow NULL values in attributes
    "(c) to allow you to update the values in the attributes.

    https://asktom.Oracle.com/pls/Apex/f?p=100:11:0:P11_QUESTION_ID:5541352100346689891

  • Engineering problems the surrogate with Unique key primary key

    SDDM 3.3.0.747 with 2 problems (at least so far).  I hope that the problem comes from this recruit SDDM and I forgot a setting. PROBLEM 1 I don't want to start a religious debate on substitution vs natural keys but I'm having a problem correctly both the logic model of engineering.  I'm a rookie when it comes to SDDM but have many years of experience with the Designer. By default, only I wish I had a natural UID (UK) and a surrogate mother based on a primary key UID (PK) which is used for foreign keys.  The problem I have with engineering is I can successfully engineer the substitute PK, engineer of the FK with the PK, but cannot get the unique key to contain surrogate keys in the child table.  If I check the identification property in relationships, the PK columns and the UK are included in the child PK and the United Kingdom contains no columns. The installation program, I've defined two reference entities, PROBABILITY and GRAVITY with unique keys natural defined.  I also have a child entity RISK_ASSESMENT with links to entities of the PROBABILITY and SEVERITY and both have the "use surrogate keys:": check box selected.  The unique key of the entity RISK_ASSESMENT includes links to PROBILITY and GRAVITY.  None of the entities have a PK or replacement of the defined keys and they all have the checkbox "Create a surrogate key" checked.  In addition the following preferences are defined: Data Modeling/model/logic NO checked - use and the first Unique key as primary key value NOT checked - name - Keep under the name of the from attribute Checked - entity carrier to create controlled key - use surrogate PROBLEM 2 key relationship when the foreign key columns are engineered I want names have a prefix 'FK_"but they don't.  Models are defined as follows: Data Modeler/assignment of Standard/Templates Foreign Key name: FK_ {children} {parent} foreign key column: engineer FK_ {Ref column} relational model/General checked Options - translation name Marcus Bacon apply

    Once more, I designed without removing anything and still no FK to the United Kingdom of the child.

    There is a synchronization problem in DM 4.0 EA1. This will be fixed for the production version.

    Philippe

  • Potential problems for tables without primary keys and unique keys

    GoldenGate 11.2.1.0.3/Solaris 10
    DB: Oracle for Oracle (Source and target is 11.2.0.3)
    Topology: unidirectional


    In our one-way configuration GG, little of the tables being replicated is not a primary key or a Unique key.

    Last week when we have implemented GG for the test, we received warnings for these table below.
    GGSCI > add trandata WMHS_UD.crtn_dtl
    
    2013-01-12 11:34:33  WARNING OGG-00869  No unique key is defined for table 'CRTN_DTL'. All viable columns will be used to represent the key, but may not guarantee uniqueness.  KEYCOLS may be used to define the key.
    
    Logging of supplemental redo data enabled for table WMHS_UD.crtn_dtl.
    Replication seems to work very well for these tables.

    Googling, I think that there may be performance degradation when you replicate tables without PK or the United Kingdom.

    But are there other potential problems such as data of a certain kind not replicated to the lack of PK/UK?

    It really depends on the data.

    By default, GG is combining all columns as a virtual primary key but don't no conflict control by default. So when you can be sure that the record you insert into the table is unique, then it will work.
    BUT as soon as you insert the same record, which is already inserted, then you will encounter problems.

    Let me show what happens when you use an initial charge because it makes it easier to describe:
    We start at 10:00 the capture for a table. Now, you insert a record at 10:00 in the tables. When you now start an initial charge to 10.02, then check you have inserted in the database to 10.01 will be repeated two times. During the IPL as the initial charge is made to 10.02 and it includes data of 10.01 AND it will be replicated again through the process of capture/replicate.

  • Difference of path between primary key and a Unique Index

    Hi all

    Is there a specific way the oracle optimizer to treat differently the Primary key and Unique index?

    Oracle Version
    SQL> select * from v$version;
    
    BANNER
    --------------------------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
    PL/SQL Release 11.2.0.3.0 - Production
    CORE    11.2.0.3.0      Production
    TNS for IBM/AIX RISC System/6000: Version 11.2.0.3.0 - Production
    NLSRTL Version 11.2.0.3.0 - Production
    
    SQL> 
    Sample data test for Index Normal
    SQL> create table t_test_tab(col1 number, col2 number, col3 varchar2(12));
    
    Table created.
    
    SQL> create sequence seq_t_test_tab start with 1 increment by 1 ;
    
    Sequence created.
    
    SQL>  insert into t_test_tab select seq_t_test_tab.nextval, round(dbms_random.value(1,999)) , 'B'||round(dbms_random.value(1,50))||'A' from dual connect by level < 100000;
    
    99999 rows created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> exec dbms_stats.gather_table_stats(USER_OWNER','T_TEST_TAB',cascade => true);
    
    PL/SQL procedure successfully completed.
    
    SQL> select col1 from t_test_tab;
    
    99999 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1565504962
    
    --------------------------------------------------------------------------------
    | Id  | Operation         | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |            | 99999 |   488K|    74   (3)| 00:00:01 |
    |   1 |  TABLE ACCESS FULL| T_TEST_TAB | 99999 |   488K|    74   (3)| 00:00:01 |
    --------------------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
           6915  consistent gets
            259  physical reads
              0  redo size
        1829388  bytes sent via SQL*Net to client
          73850  bytes received via SQL*Net from client
           6668  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
          99999  rows processed
    
    SQL> create index idx_t_test_tab on t_test_tab(col1);
    
    Index created.
    
    SQL> exec dbms_stats.gather_table_stats('USER_OWNER','T_TEST_TAB',cascade => true); 
    
    PL/SQL procedure successfully completed.
    
    SQL> select col1 from t_test_tab;
    
    99999 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1565504962
    
    --------------------------------------------------------------------------------
    | Id  | Operation         | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
    --------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |            | 99999 |   488K|    74   (3)| 00:00:01 |
    |   1 |  TABLE ACCESS FULL| T_TEST_TAB | 99999 |   488K|    74   (3)| 00:00:01 |
    --------------------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
           6915  consistent gets
              0  physical reads
              0  redo size
        1829388  bytes sent via SQL*Net to client
          73850  bytes received via SQL*Net from client
           6668  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
          99999  rows processed
    
    SQL> 
    Examples of test when using primary key data
    SQL> create table t_test_tab1(col1 number, col2 number, col3 varchar2(12));
    
    Table created.
    
    SQL> create sequence seq_t_test_tab1 start with 1 increment by 1 ;
    
    Sequence created.
    
    SQL> insert into t_test_tab1 select seq_t_test_tab1.nextval, round(dbms_random.value(1,999)) , 'B'||round(dbms_random.value(1,50))||'A' from dual connect by level < 100000;
     
    99999 rows created.
    
    SQL> commit;
    
    Commit complete.
    
    SQL> exec dbms_stats.gather_table_stats('USER_OWNER','T_TEST_TAB1',cascade => true);
    
    PL/SQL procedure successfully completed.
    
    SQL> select col1 from t_test_tab1;
    
    99999 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 1727568366
    
    ---------------------------------------------------------------------------------
    | Id  | Operation         | Name        | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT  |             | 99999 |   488K|    74   (3)| 00:00:01 |
    |   1 |  TABLE ACCESS FULL| T_TEST_TAB1 | 99999 |   488K|    74   (3)| 00:00:01 |
    ---------------------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
           6915  consistent gets
              0  physical reads
              0  redo size
        1829388  bytes sent via SQL*Net to client
          73850  bytes received via SQL*Net from client
           6668  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
          99999  rows processed
    
    SQL> alter table t_test_tab1 add constraint pk_t_test_tab1 primary key (col1);
    
    Table altered.
    
    SQL> exec dbms_stats.gather_table_stats('USER_OWNER','T_TEST_TAB1',cascade => true); 
    
    PL/SQL procedure successfully completed.
    
    SQL> select col1 from t_test_tab1;
    
    99999 rows selected.
    
    
    Execution Plan
    ----------------------------------------------------------
    Plan hash value: 2995826579
    
    ---------------------------------------------------------------------------------------
    | Id  | Operation            | Name           | Rows  | Bytes | Cost (%CPU)| Time     |
    ---------------------------------------------------------------------------------------
    |   0 | SELECT STATEMENT     |                | 99999 |   488K|    59   (2)| 00:00:01 |
    |   1 |  INDEX FAST FULL SCAN| PK_T_TEST_TAB1 | 99999 |   488K|    59   (2)| 00:00:01 |
    ---------------------------------------------------------------------------------------
    
    
    Statistics
    ----------------------------------------------------------
              1  recursive calls
              0  db block gets
           6867  consistent gets
              0  physical reads
              0  redo size
        1829388  bytes sent via SQL*Net to client
          73850  bytes received via SQL*Net from client
           6668  SQL*Net roundtrips to/from client
              0  sorts (memory)
              0  sorts (disk)
          99999  rows processed
    
    SQL> 
    If you see here the same even as the statistics were gathered,
    * In the 1st table T_TEST_TAB, table always use FULL table access after creating indexes.
    * And in the 2nd table T_TEST_TAB1, table uses PRIMARY KEY as expected.

    Any comments?

    Kind regards
    BPat

    >
    * In the 1st table T_TEST_TAB, table always use FULL table access after creating indexes.
    * And in the 2nd table T_TEST_TAB1, table uses PRIMARY KEY as expected.
    >
    Yes - for the first table a full table scan will be used as the currently selected column is nullable and indexes do not include null values.

    The index can be used for the second query, since all the data (first column) is available between the index and there may be no NULL values because of the primary key. If you check constraints, you find that the there is now a CHECK constraint to ensure that the first column cannot be null.

    For a full and interesting discussion see the explanation of this and a related issue on the question I ask in this thread
    What SYS tables (not seen) contain the value NULL spec /not/ column definition? and my response he posted: 23 April 2012 09:02

    I ask the question is based on a question here which is similar to yours
    Columns becoming nullable after a fall of primary key?

  • Primary key and unique key not Null

    Hello gurus,

    Asked in an interview about the difference between primary and unique keys.
    I talked Unique keys can accept null values, but they are unique through should have the table. So, the next question asked me was "a Unique key NOT NULL can be treated as the primary key?

    Tricky question! :)
    I said yes! that it meets the requirement to be a primary key for this particular table, but since this isn't really a primary key, that it can't be covered in the foreign keys.

    The interviewer wanted just a Yes or no.

    Can someone please put some of this?

    Thanks in advance!

    Two things.
    1. unique can also be referenced as a foreign key. If your statement "but because this isn't really a primary key may not be seized key foreign." is not true.
    2. primary key and Unique key are different (for example you can have more than one UK in a table but PK's alone) and to know the difference, read some RDBMS concepts.

    I'm curious to know what is the outcome of your interview? ;)

  • A composite primary key needs of the Unique ID column or not?

    Hello dear,
    I have a table that's the primary key is a key that is composed of two columns and there are like 5 columns of information. You think I'm going to use two columns as the primary key of this table or create a unique ID column.
    Example:
    Employees table:
    Services of table:

    The table of the problem is the Department managers:
    Now, I'll add employee id and service id in this table and Manager info etc... (info does not exist in the employee table), should I use as primary or create the id column in this table?

    Thank you very much.

    You must use the new ID as primary (alter table add constraint primary key,...) you cannot use foreign key as primary :)

  • 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/]

Maybe you are looking for