PRIMARY KEY-FOREIGN KEY 관계 찾기(MASTER TABLE CONSTRAINT 정보 찾는 SQL)

제품 : ORACLE SERVER
작성날짜 : 2003-06-19
PRIMARY KEY-FOREIGN KEY 관계 찾기
=================================
PURPOSE
이 자료는 MASTER TABLE CONSTRAINT 정보를 찾는 SQL이다.
Explanation
SCOTT의 EMP table의 Foreign Key와 부모 제약 조건을 찾으려면
다음의 질의문을 사용하여 찾을 수 있다.
SQL> alter table dept add constraint dept_pk primary key (deptno);
Table altered.
SQL> alter table emp add constraint emp_dept_fk foreign key(deptno)
references dept(deptno);
Table altered.
SQL> select c.constraint_name as "foreign key",
p.constraint_name as "referenced key",
p.constraint_type,
p.owner,
p.table_name
from dba_constraints c, dba_constraints p
where c.owner = 'SCOTT'
and c.table_name = 'EMP'
and c.constraint_type = 'R'
and c.r_owner = p.owner
and c.r_constraint_name = p.constraint_name;
foreign key referenced key C OWNER TABLE_NAME
EMP_DEPT_FK DEPT_PK P SCOTT DEPT
Example
none
Reference Documents
<Note:1010844.6>

I don't have intimate knowledge of SQL Server, but to me, your code seems reasonable. I guess there is some "fine point" (a bug?) to using self referenceing foreign keys in SQL Server. It doesn't seem plausible that the error originates with the driver, unless it's a very intelligent kind of driver.
A "Gordian Knot" kind of solution to your problem is to ask whether you really need the foreign key constraint in the db schema. Is the table used by something other than your EJB? Anyway, putting logic responsible for the correct functioning of your EJB into the db schema is often a bad practice, as it makes the code harder to understand, maintain, port etc.

Similar Messages

  • Please give the query to find out primary key in table in Sql plus

    Dear friends,
    Please give me the query to find out the primary key in Sql plus.

    hi
    SQL> DESC user_constraints
    Name                                                                                                  
    OWNER                                                                                                 
    CONSTRAINT_NAME                                                                                       
    CONSTRAINT_TYPE                                                                                       
    TABLE_NAME                                                                                            
    SEARCH_CONDITION                                                                                      
    R_OWNER                                                                                               
    R_CONSTRAINT_NAME                                                                                     
    DELETE_RULE                                                                                           
    STATUS                                                                                                
    DEFERRABLE                                                                                            
    DEFERRED                                                                                              
    VALIDATED                                                                                             
    GENERATED                                                                                             
    BAD                                                                                                   
    RELY                                                                                                  
    LAST_CHANGE                                                                                           
    INDEX_OWNER                                                                                           
    INDEX_NAME                                                                                            
    INVALID                                                                                               
    VIEW_RELATED                                                                                          
    SQL> SELECT constraint_name,table_name,r_constraint_name,status
      2  FROM user_constraints WHERE constraint_type='P';
    CONSTRAINT_NAME                TABLE_NAME                     R_CONSTRAINT_NAME              STATUS
    SYS_C003141                    CUSTOMERS                                                     ENABLED
    PK_DEPT                        DEPT                                                          ENABLED
    SYS_C003139                    SALESREPS                                                     ENABLEDKhurram

  • PRIMARY KEY(MASTER)를 REFERENCE하는 FOREIGN KEY 찾는 SQL

    제품 : ORACLE SERVER
    작성날짜 : 2004-03-12
    PRIMARY KEY(MASTER)를 REFERENCE하는 FOREIGN KEY 찾는 SQL
    ========================================================
    $sqlplus sys/manager(SYS user 로 login 함)
    sql> select c.name constraint_name
    from dba_objects a,
    cdef$ b,
    con$ c
    where a.object_name = 'TABLE_NAME'
    and a.object_id = b.robj#
    and b.con# = c.con#
    /

    Hello again,
    May be I was not clear enough.
    Scenario 1: We use the master-detail form as is with the default oolbar. In this case, the user can insert the detail records one by one without needing the primary-foreign key value since this is handled by default.
    Once we save the form (commit_form), I use the pre-insert trigger to get the master block primary key generated from the sequence and since the detail block key is copied from this value, both are saved correctly and it is the end of the story.
    Scenario 2: As explained in the initial post, the user will populate the detail records one by one by clicking on the -INSERT DETAIL- button and hence has a window where he can insert the detail info and then be brought back to the master-detail form every time he enters a new detail record.
    The problem here is that I can't generate the primary key for the master block since the client has the following requirement:
    The user can always change his mind and not complete, meaning save the form, his process
    As such, the key should be generated in the last step before Commit.

  • ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    Hi,
    I am trying to delete data from a table by dropping a partition. I have identified all the child tables by running the following command.
    select 'select count(*) from '||table_name||' where employee_id = 100;'
    from 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(*)FROM'||TABLE_NAME||'WHEREEMPLOYEE_ID_ID=100;'
    select count(*) from PT_ORDERS where employee_id = 100;
    select count(*) from PT_DEP where employee_id = 100;
    select count(*) from PT_SKILLSET where employee_id = 100;
    I dropped the partition for employee_id 100 in all of the child tables. The select count(*) returns 0 rows for each of the above.
    When I try to run the below command on the EMPLOYEE table, I get 'ORA-02266: unique/primary keys in table referenced by enabled foreign keys'.
    alter table EMPLOYEE drop partition EMP_ID_100;
    I cant see why I am unable to drop this partition now as there is now child data in any of the referenced tables. Any suggestions or help on this would be greatly appreciated.
    Thanks.
    Rgs,
    Rob

    You should disable foreign key constraints first and drop partition. Deletion of rows or dropping partitions in childs don't work in this case
    as you have the global dependency:
    <PRE>
    SQL> create table scott.t (x int primary key, y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> create table scott.t_c (x int references scott.t(x), y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> insert into scott.t values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t values(2,2)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(2,2)
    2 /
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> alter table scott.t_c drop partition p_2;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    alter table scott.t drop partition p_2
    ERROR at line 1:
    ORA-02266: unique/primary keys in table referenced by enabled 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 table scott.t_c disable constraint SYS_C0011059;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    Table altered.
    SQL> alter table scott.t_c enable novalidate constraint SYS_C0011059;
    Table altered.
    </PRE>
    I guess you should consider such option as Referencial partitioning (with some restrictions).
    Best wishes,
    Dmitry.

  • Add a primary key to table.

    Hello everybody,
    I want to add a primary key to a table but it doesn't work.
    select count(*), count(id) from tableA;
    COUNT(*) COUNT(ID)
    830447 830447
    You see: The number of ids is equal to the number of rows. The ID is not marked as unique but it should be unique. I try to set id as the primary key:
    alter table tableA add constraint "pk_constraint" PRIMARY KEY (id);
    But I a get an error message: ORA-02437: cannot validate (pk_constraint) - primary key violated.
    What is the problem?
    I use Oracle 11g.

    user9206958 wrote:
    Hello everybody,
    I want to add a primary key to a table but it doesn't work.
    select count(*), count(id) from tableA;
    COUNT(*) COUNT(ID)
    830447 830447
    You see: The number of ids is equal to the number of rows. The ID is not marked as unique but it should be unique. I try to set id as the primary key:
    alter table tableA add constraint "pk_constraint" PRIMARY KEY (id);
    But I a get an error message: ORA-02437: cannot validate (pk_constraint) - primary key violated.
    What is the problem?
    I use Oracle 11g.Revisit your query... 11g can help much in this sql.... select count(*), count(id) from tableA;... to get what your desired output should be.
    Edited by: Z.K. on Mar 30, 2010 1:15 PM

  • Populate choice component  with   2 primary key  in table

    There are 2 primary key in table. When I create my uix with jheadstart, the UIXs don't populate choice component. How can I create automatically populate choice component, with 2 PK in table?

    A choice component can only have one value attribute. So if you have a composite primary key in the table you use to populate the choice, it will not work correctly, because based on this single attribute the choice cannot render the correct row as selected.
    Steven Davelaar,
    JHeadstart Team.

  • TIPS(9):PARENT-CHILD(FOREIGN KEY) 관계를 갖는 MASTER TABLE의 PRIMARY KEY 확인.

    제품 : SQL*PLUS
    작성날짜 : 1996-10-21
    TIPS(9) : PARENT-CHILD관계를 갖는 TABLE의
    PRIMARY key, FOREIGN key의 COLUMN 명과 POSITION 확인
    ==============================================================
    ** Name : Show_Position.Sql
    ** Usage : @Show_Positions Parent_Table Child_Table
    ** Description : Shows Primary And Foreign Key Positions
    ** WARNING : 이 문장은 해당 Table의 Constraint생성시 Naming
    ** Convention을 따른 경우에 적용되도록 되어 있다.
    SET VERIFY OFF
    CLEAR BREAK
    BREAK ON CONSTRAINT_NAME ON TABLES
    SELECT SUBSTR(CONSTRAINT_NAME,1,27) CONSTRAINT_NAME,
    SUBSTR(TABLE_NAME,1,15) TABLES,
    SUBSTR(COLUMN_NAME,1,15) COL_NAME,
    SUBSTR(POSITION,1,3) POSITION,
    SUBSTR(OWNER,1,7) OWNER
    FROM USER_CONS_COLUMNS
    WHERE TABLE_NAME = UPPER('&1')
    AND CONSTRAINT_NAME LIKE 'PK%'
    UNION
    SELECT SUBSTR(CONSTRAINT_NAME,1,27) CONSTRAINT_NAME,
    SUBSTR(TABLE_NAME,1,15) TABLES,
    SUBSTR(COLUMN_NAME,1,25) COL_NAME,
    SUBSTR(POSITION,1,3) POSITION,
    SUBSTR(OWNER,1,7) OWNER
    FROM USER_CONS_COLUMNS
    WHERE TABLE_NAME = UPPER('&2')
    AND CONSTRAINT_NAME LIKE 'FK%'
    ORDER BY 1 DESC , 4 ASC;
    < 실행 예 >
    SQL> @SHOW_POSITIONS EMP_SERVICE EMP_SERVICE_LOG
    CONSTRAINT_NAME TABLES COL_NAME POS
    PK_EMP_SERVICE EMP_SERVICE EMP_ID 1
    CUST_ID 2
    FK_EMP_SERVICE_LOG_EC EMP_SERVICE_LOG EMP_ID 1
    CUST_ID 2

    제품 : SQL*PLUS
    작성날짜 : 1996-10-21
    TIPS(9) : PARENT-CHILD관계를 갖는 TABLE의
    PRIMARY key, FOREIGN key의 COLUMN 명과 POSITION 확인
    ==============================================================
    ** Name : Show_Position.Sql
    ** Usage : @Show_Positions Parent_Table Child_Table
    ** Description : Shows Primary And Foreign Key Positions
    ** WARNING : 이 문장은 해당 Table의 Constraint생성시 Naming
    ** Convention을 따른 경우에 적용되도록 되어 있다.
    SET VERIFY OFF
    CLEAR BREAK
    BREAK ON CONSTRAINT_NAME ON TABLES
    SELECT SUBSTR(CONSTRAINT_NAME,1,27) CONSTRAINT_NAME,
    SUBSTR(TABLE_NAME,1,15) TABLES,
    SUBSTR(COLUMN_NAME,1,15) COL_NAME,
    SUBSTR(POSITION,1,3) POSITION,
    SUBSTR(OWNER,1,7) OWNER
    FROM USER_CONS_COLUMNS
    WHERE TABLE_NAME = UPPER('&1')
    AND CONSTRAINT_NAME LIKE 'PK%'
    UNION
    SELECT SUBSTR(CONSTRAINT_NAME,1,27) CONSTRAINT_NAME,
    SUBSTR(TABLE_NAME,1,15) TABLES,
    SUBSTR(COLUMN_NAME,1,25) COL_NAME,
    SUBSTR(POSITION,1,3) POSITION,
    SUBSTR(OWNER,1,7) OWNER
    FROM USER_CONS_COLUMNS
    WHERE TABLE_NAME = UPPER('&2')
    AND CONSTRAINT_NAME LIKE 'FK%'
    ORDER BY 1 DESC , 4 ASC;
    < 실행 예 >
    SQL> @SHOW_POSITIONS EMP_SERVICE EMP_SERVICE_LOG
    CONSTRAINT_NAME TABLES COL_NAME POS
    PK_EMP_SERVICE EMP_SERVICE EMP_ID 1
    CUST_ID 2
    FK_EMP_SERVICE_LOG_EC EMP_SERVICE_LOG EMP_ID 1
    CUST_ID 2

  • Master-detail with auto-generated primary key(master)

    Hello,
    I have the following master-detail setup:
    Master - block A primary key is: codeA
    Detail - block B
    codeA is genrated only at commit time and is obtained from a sequence.
    User needs to add detail records. This is easy to do since he has to click on the insert icon on the toolbar. At commit time, use commit_form.
    Prob: the requirements as asked by the client is to add a button (which we will label INSERT DETAIL). When the user click on the button, it opens a window where he can enter the detail info then click on save. This should bring him back to the master-detail form and the record in the detail block is inserted.
    What is the problem here ? since the detail block has the foreign key tied to the master primary key, the window-form (with the call_form) that we opened for the detail entry won't be able to save since at this point we still have not assigned a value for the primary key (and hence a value for the foreign key).
    Possible Suggestions:
    -Use a temp database table to hold the detail record from the second form and use it to transfer values to the detail record in the master-detail
    -Use a global record group
    -We can't use parameter list to pass back these values.
    So my question is:
    What would be the most efficient way to have an insert button on the detail block that would allow the user to have a pop up where he can insert his values and then be brought back to the master-detail.
    Thanks.

    Hello again,
    May be I was not clear enough.
    Scenario 1: We use the master-detail form as is with the default oolbar. In this case, the user can insert the detail records one by one without needing the primary-foreign key value since this is handled by default.
    Once we save the form (commit_form), I use the pre-insert trigger to get the master block primary key generated from the sequence and since the detail block key is copied from this value, both are saved correctly and it is the end of the story.
    Scenario 2: As explained in the initial post, the user will populate the detail records one by one by clicking on the -INSERT DETAIL- button and hence has a window where he can insert the detail info and then be brought back to the master-detail form every time he enters a new detail record.
    The problem here is that I can't generate the primary key for the master block since the client has the following requirement:
    The user can always change his mind and not complete, meaning save the form, his process
    As such, the key should be generated in the last step before Commit.

  • How to add primary key for table with existing data?

    The table is occupied data already. There was no primary key before, so for every column, there are some duplicate values.
    I want to add a new column, which should be of the datatype integer, and can automatically increment, starting from 001. I tried this in Oracle SQL Developer, but it says "ORA-01758: table must be empty to add mandatory (NOT NULL) column". How can I do it? Thanks!

    Hello, don't call the column ID, that could be an ID relating to anything. Perhaps be a little more specific like emp_id .
    Then to update it:
    UPDATE Employee emp
      SET emp_id = (
      SELECT row_num
        FROM (
      SELECT ROWID,
                 ROW_NUMBER() OVER (ORDER BY ROWID) row_num
        FROM Employee emp2) emp2
      WHERE emp.ROWID = emp2.ROWID);That will assign a new EMP_ID for each, beginning at 1.
    Then:
    ALTER TABLE Employee MODIFY emp_id NOT NULL;
    ALTER TABLE Employee ADD CONSTRAINT EMP_PK PRIMARY KEY (EMP_ID);Then create a sequence for future population of the Employee table:
    CREATE SEQUENCE emp_seq START WITH <maximum value of emp_id + 1>;

  • Primary key for table

    I have a table Gr_order which was populated ages ago and now it need to be populated
    with some more look up data.
    "Orderid" is primary key for the table but question is how do I insert "orderid" in insert statement in procedure. I could not find any sequence name. There are 1230
    records which need to be inserted.
    Desc Gr_order
    Orderid  Number Primary key
    Desc     Varchar2
    INSERT INTO gr_order (orderid,desc) 
    VALUES (???, upper(each_pom.desc);Thanks
    sandy

    You find the code that inserts into the table and see if it uses the sequence ;-) . A sequence is not "attached" to a particular table - it is up to the inserting statement to decide if a sequence is to be used.
    You might want to check if the table has a trigger - some developers like to emulate MS-sql server behaviour by having a trigger on the table that selects from a sequence. If it has such a trigger, then you just do your inserts without specifying order_id. But if the table does not have such a trigger, then you cannot know if the table was originally populated using a sequence or not (unless you can find the insert statements somewhere.)

  • How to create tabular form whithout primary key in table

    Hi All,
    I have requirement to create a tabular form but the problem is, the table which I am using in the application don't have any Primary key, I am using only one table can't change any thing in table.(i.e can't add any row in table,no change in data model)
    How can I create tabular form when primary key is not available.
    looking forward for all of your quick responce.
    Thanks in advance.
    Dikshit Kumar Nidhi

    You can create a view like
    select a.rowid id, a.*
    from table a
    and create a tabular form on this view. You can use
    the new column ID as Primary key.Did you try to actually do this? I did at
    http://htmldb.oracle.com/pls/otn/f?p=24317:159
    And when I change something and click Submit, I get an error
    Error in mru internal routine: ORA-20001: Error in MRU: row= 1, ORA-01733: virtual column not allowed here, update "VIKASA"."NO_PK_VW" set "ID" = :b1, "I" = :b2, "J" = :b3 where "ID" = :p_pk_col
    Thanks

  • Help with setting primary key in table

    Hi,
    I have created a table in Oracle and populated it with information, but I need a primary key (like an autonumber in Access) to identify each record. How can I do this for an existing table?
    Thanks so much for any help you can provide!

    Hello,
    I am assuming you have created the sequence and trigger i posted in my earlier post, here run this and if this answers your question mark post answered and reward points for helpful posts.
    INSERT INTO customer1 (FIRST_NAME,
                           LAST_NAME,
                           TITLE,
                           ADDRESS1,
                           ADDRESS2,
                           CITY,
                           STATE,
                           ZIP,
                           PHONE,
                           EXTENSION,
                           FAX,
                           WORK_PHONE,
                           WORK_EXTENSION,
                           MOBILE_PHONE,
                           EMERGENCY_PHONE,
                           PHONE_NOTES,
                           EMAIL)
       SELECT   FIRST_NAME,
                LAST_NAME,
                TITLE,
                ADDRESS1,
                ADDRESS2,
                CITY,
                STATE,
                ZIP,
                PHONE,
                EXTENSION,
                FAX,
                WORK_PHONE,
                WORK_EXTENSION,
                MOBILE_PHONE,
                EMERGENCY_PHONE,
                PHONE_NOTES,
                EMAIL
         FROM   CUSTOMER;
         COMMIT;Regards

  • Issues while changing primary key in table

    Hi
    I have one table. In that two fields are primary key. I want to change the second PK as FK only. But when i am changing this field as FK. Its showing one error 'Primary Key Change not permitted for value Table ZCAUSECATMASTER' . How to avoid this error.
    Please help me.

    Hi.....
    Remove that primary key for the second field and assign your foregin key..table to that filed.....
    So when you give entries in that second filed it will be validated with its foregin key table......its nothing but check table...
    what all values in the foregin table only can given.....
    regards
    raja

  • Inner join-select -primary key in table issue

    Hi ,
            Iam using FEBKO(header) and FEBEP(item) in inner join  select .But the datas fetching by this selct in not correct.The analysis is the is no common primary fields in the both table.
    Question 1-> Can i use inner join without common primary key in the both tables, weather it possible to make a select without common primary key in both table. Please kindly let me know.
    Question 2-> What is the other possible way to give the selct for both table(better performance)
    Regards,
    Veera

    Hi,
    When you use INNER JOIN in this case, link your tables based on KUKEY and ESNUM fields, bcoz there can be many items under a single header. So this will work for you, even from the performance point of view.
    Hope this is helpful to you. If you need further information, revert back.
    Reward all the helpful answers.
    Regards
    Nagaraj T

  • Database Copy can't copy primary key for tables

    When I use Tools->Database Copy Wizard to copy my existing schema (for example: azteca_KSMMS) to a brand new schema (azteca), I find the primary key for some of the tables can't be copied, also the views seem not valid under the new schema (azteca). Please give me your comments about that. Thanks for your help.
    Kevin

    For example I have a table called workorder, it has a primary key with name (CW_PKEY_519, on WORKORDERID column) from the source schema (azteca_KSMMS). After I database copy to new schema azteca, the primary key lost. The data of workorder and other indexes have been copied to azteca schema.
    Kevin

Maybe you are looking for