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

Similar Messages

  • Two foreign keys reference on primary key

    There are two tables:
    1) table CALENDAR with primary key: cal_id
    2) table FACTS with some columns, two of them are dates: cal_id_start_process, cal_id_stop_process
    we want on physical layer create two foreign keys for cal_id_start_process and cal_id_stop_process columns reference on CALENDAR.cal_id
    When we create foreign key for cal_id_start_process: we choose this column and choose CALENDAR table and its primary key. But when we want to create foreign key for cal_id_stop_process - we cannot coose table CALENDAR again.
    Is this rigth, that only one column from table can reference on specified primary key in CALENDAR table?
    How to create two foreign key on one table primary key?

    More complex. In your example there'll be such SQL:
    select c.cal_id, f.cal_id_start, f.cal_id_stop
    from CALENDAR c, FACTS f
    where c.cal_id = f.cal_id_start and c.cal_id = f.cal_id_stop
    (It meens FACTS.cal_id_start = FACTS.cal_id_stop )
    In my case, I want for such join:
    select c1.cal_id, c2.cal_id, f.cal_id_start, f.cal_id_stop
    from CALENDAR c1, CALENDAR c2, FACTS f
    where c1.cal_id = f.cal_id_start and c2.cal_id = f.cal_id_stop
    Edited by: annylut on Dec 22, 2011 3:33 PM

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

  • Parent-Child hierarchy based on two-column key

    Hello
    Is it possible to create a parent-child hierarchy, if the primary key of the table consists of two columns?
    My table looks like:
    TRACE_ID | DIAG_ID | SUPER_DIAG_ID
    with TRACE_ID and DIAG_ID as PK.
    If I define only DIAG_ID as PK, I can add a logical dimension (PC-hierarchy) without problems.
    However when I also add TRACE_ID to my PK, I cannot select a member key in the new logical dimension window and therefore not create a logical dimension.
    Is this a limitation of OBIEE and I have to merge the two colums (which would be rather bad, as there are FK relations to DIAG_ID) or is there a solution?
    Regards
    Matthias

    Dear Gowtham  ,
    I am very well aware of the level based hierarchy available in BO .
    The issue that i have raised is all about the Parent Child Hierarchy which creates the recursive query.
    I.e Every Parent has a child and that child can be parent of some other . (See the original example for more illustration)

  • Parent - child relationship in a table

    Oracle Database 10g Express Edition Release 10.2.0.1.0 - Product
    I have the following table. The table contains a attribute called parentraid which specifies whether its a parent or a child. For the data given below, ra1,ra2,ra3 are parent and remaining are child with their respective parents specified in parentraid attribute.
    CREATE TABLE  "ADDRESEARCHAREA"
       (     "RAID" VARCHAR2(30) NOT NULL ENABLE,
         "RANAME" VARCHAR2(30),
         "RASTARTDATE" DATE,
         "RAENDDATE" DATE,
         "PARENTRAID" VARCHAR2(30),
         "RASTATUS" VARCHAR2(30),
          PRIMARY KEY ("RAID") ENABLE
    insert into addresearcharea values ('ra1','raname1',to_date('04/01/2012','mm/dd/yyyy'),'','','Active')
    insert into addresearcharea values ('ra2','raname2',to_date('04/01/2012','mm/dd/yyyy'),'','','Active')
    insert into addresearcharea values ('ra3','raname3',to_date('04/01/2012','mm/dd/yyyy'),'','','Active')
    insert into addresearcharea values ('ra4','raname4',to_date('04/01/2012','mm/dd/yyyy'),'','ra1','Active')
    insert into addresearcharea values ('ra5','raname5',to_date('04/01/2012','mm/dd/yyyy'),'','ra2','Active')I am looking for the following output,
    RAID RANAME PARENTRAID PARENTRANAME
    ra1 raname1 -  - 
    ra2 raname2 -  - 
    ra3 raname3 -  - 
    ra4 raname4 ra1 raname1 
    ra5 raname5 ra2 raname2  Please help

    Hi,
    Try this
    SQL> CREATE TABLE  "ADDRESEARCHAREA"
      2     (       "RAID" VARCHAR2(30) NOT NULL ENABLE,
      3     "RANAME" VARCHAR2(30),
      4     "RASTARTDATE" DATE,
      5     "RAENDDATE" DATE,
      6     "PARENTRAID" VARCHAR2(30),
      7     "RASTATUS" VARCHAR2(30),
      8      PRIMARY KEY ("RAID") ENABLE
      9     );
    Table created.
    SQL>
    SQL> insert into addresearcharea values ('ra1','raname1',to_date('04/01/2012','mm/dd/yyyy'),'','','Active');
    1 row created.
    SQL> insert into addresearcharea values ('ra2','raname2',to_date('04/01/2012','mm/dd/yyyy'),'','','Active');
    1 row created.
    SQL> insert into addresearcharea values ('ra3','raname3',to_date('04/01/2012','mm/dd/yyyy'),'','','Active');
    1 row created.
    SQL> insert into addresearcharea values ('ra4','raname4',to_date('04/01/2012','mm/dd/yyyy'),'','ra1','Active');
    1 row created.
    SQL> insert into addresearcharea values ('ra5','raname5',to_date('04/01/2012','mm/dd/yyyy'),'','ra2','Active');
    1 row created.
    SQL>
    SQL>
    SQL>
    SQL>
    SQL> select DISTINCT A.raid RAID,A.raname RANAME,A.parentraid PARENTRAID,
      2  case when A.parentraid=B.raid then A.raname else null end PARENT_NAME
      3  from ADDRESEARCHAREA A LEFT JOIN ADDRESEARCHAREA B ON (A.parentraid=B.raid) ORDER BY RAID ASC ;
    RAID RANAME  PARE PARENT_NAME
    ra1  raname1
    ra2  raname2
    ra3  raname3
    ra4  raname4 ra1  raname4
    ra5  raname5 ra2  raname5
    SQL>
    SQL>Edited by: Chanchal Wankhade on Oct 8, 2012 11:41 PM
    Edited by: Chanchal Wankhade on Oct 8, 2012 11:41 PM

  • Parent Child Relationship in the table to be queried.

    Hi guys,
    I have the below query.
    Table 1:
    Column_name Reference
    Value1 Value1
    Value2 Value1
    Table 2:
    Column_name Attributes(child Elements)
    Value1 Child1
    Value1 Child2
    Now my query is to retrieve records as below using both the tables.
    Value1
    Value1.Child1
    Value1.Child2
    Value2
    Value2.Child1
    Value2.Child2
    How can I get such ?
    Regards,
    SQL Developer.

    Hi,
    As Alex said, you should have tried by joining them.
    With Parent As (
    select 'Value1' p_col1,'Value1' P_col2 from dual union all
    select 'Value2','Value1' from dual),
    child as (
    select 'Value1' c_col1,'Child1' c_col2 from dual union all
    select 'Value1','Child2' from dual)
    -- End of sample data
    select p_col1,null c_col2 from parent
    union
    select p_col1,c_col2 from parent,child
    where p_col2=c_col1
    order by p_col1,c_col2 desc
    PRAZY@11gR1> /
    P_COL1 C_COL2
    Value1
    Value1 Child2
    Value1 Child1
    Value2
    Value2 Child2
    Value2 Child1
    6 rows selected.
    Elapsed: 00:00:00.03Regards,
    Prazy

  • Parent/child records from same table

    I want to create a query that is a union such that the 2nd resultset is based on the 1st resultset. I have a table that has parent/child records in the same table.
    Table: EVENTS
    EVENT_ID
    PARENT_EVENT_ID
    CREATED_DATE
    (other columns)
    if PARENT_EVENT_ID is null then it is a parent record, else it is a child record. I want to select all parent records then union them with all the associated child records...something like this:
    select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null -- All parents
    union
    select * from EVENTS where PARENT_EVENT_ID in (select EVENT_ID from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null) -- include any children of parents selected from above
    This works but it's kind of ugly, I want to avoid using the sub-select in the 2nd because it is a repeat of the 1st statement, is there a way to alias the first statement and just refer to it in the 2nd query?

    Hi,
    kev374 wrote:
    Thanks, one question...
    I did a test and it seems the child rows have to also satisfy the parent row's where clause, take this example:
    EVENT_ID|PARENT_EVENT_ID|CREATED_DATE
    2438 | (null) | April 9 2013
    2439 | 2438 | April 11 2013
    2440 | 2438 | April 11 2013
    select * from EVENTS where CREATED_DATE < sysdate - 9
    start with EVENT_ID = 2438
    connect by PARENT_EVENT_ID = prior EVENT_IDSo you've changed the condition about only wanting roots and their children, and now you want descendants at all levels.
    This pulls in record #2438 (per the sysdate - 9 condition) but 2439 and 2440 are not connected. Is there a way to supress the where clause evaluation for the child records? I just want to pull ALL child records associated with the parent and only want to do the date check on the parent.Since the roots (the only rows you want to exclude) have LEVEL=1, you can get the results you requested like this:
    WHERE   created_date  < SYSDATE - 9
    OR      LEVEL         > 1However, since you're not ruling out the grandchildren and great-grandchildren any more, why wouldn't you just say:
    SELECT  *
    FROM    events
    WHERE   created_date     < SYSDATE - 9
    OR      parent_event_id  IS NOT NULL;?
    CONNECT BY is slow. Don't use it if you don't need it.
    If you x-reference my original query:
    select * from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null -- All parents
    union
    select * from EVENTS where PARENT_EVENT_ID in (select EVENT_ID from EVENTS where CREATED_DATE < sysdate - 90 and PARENT_EVENT_ID is null) -- include any children of parents selected from above
    The 2nd select does not apply the created_date < sysdate - 90 on the children but rather pulls in all related children :)Sorry; my mistake. That's what happens when you don't post sample data, and desired results; people can't test their solutions and find mistakes like that.

  • Can the Primary Key also be Primary key in another table

    Hi all,
    I am working on a project. The project specification says:
    Project table contains details of projects and the project number *(proj_no)* is the key.
    project ( proj_no , proj_date, proj_desc, proj_type, proj_status, s_no, cust_no)
    purchase_order table contains details of the purchase orders for each project and the combination of project
    number and a purchase order number *(proj_no, po_no) form the key.*
    purchase_order ( proj_no , po_no, po_date)
    How can I have proj_no in the project table and in the purchase_order as primary keys?
    If I create a composite key in the purchase_order table making the ( proj_no, po_no) as primary key. How can I make the project_no in the project table a primary key too?
    Is there a solution for it?
    CREATE TABLE purchase_order(
    po_no NUMBER(5) NOT NULL,
    proj_no NUMBER(5) NOT NULL,
    po_date date,
    CONSTRAINT p_o_po_no_proj_no_pk PRIMARY KEY (po_no,proj_no));
    Thanks!!!

    Hi,
    I think I'd set the project no in the projects table a primary key, and project no in the purchase orders table a foreign key constraint to the projects table, combined with a unique, not null constraint in the table.
    That, in addition to a unique, not null constraint on the purchase orders column, and a composite primary key should fulfill the requirements.
    Best of luck!
    Johan

  • Getting error message when passing data into table: Primary Key

    Getting an error in a process that is feeding the invoice creation in SAP, violation of PRIMARY KEY constraint 'INV1_Primary', cannot insert duplicate key in object 'INV1'. I assume this is due to duplicate key values being passed into the table INV1, however I get this error even when I am passing unique record combinations of the key fields docentry and linenum. This is on 2005A SP01.
    Thanks

    Hi Peter,
    Could you provide a code sample of the routine that is causing the error please?
    It sounds like a data corruption issue (though it could possibly be a bug in the DI API). I recommend you speak to SAP support. There is a utility that can validate the document numbering but you'll need to speak to SAP support before you run this as it can have other negative effects, depending on the state of your data. They may ask for a copy of your database to test.
    Kind Regards,
    Owen

  • Attachment Table - Primary Key in CO

    How can I add dynamically the primary key VALUE to an attachment table in CO processRequest??
    Thanks!
    Edited by: user12190100 on Apr 12, 2010 1:43 PM

    Yes Fact Table can have a Primary key
    But in your case it looks like the Other Table which is storing Day to Day transaction should be made FACT , and the one you are calling FACT should be made Dimension..
    Well depends again, how you are analysing data and against which attributes
    Thanks
    Ashish

  • Updating a transparent table primary key

    Hi Guys,
                  I am fixing a system copy error, for that plan to update the table through a program.
    The field I am going to update one of the primary key field.
    Is that possible to do it.
    Thanks

    Hi,
      if you are going to insert a record it is possible.
    if you are going to modify the existing record it is not possible to update the primary key you can only change the fields which are not primary key.

  • Tables-Primary Key-Sequential read

    Hi Folks,
    Out of the following which imporves performace?
    1.Using all the primary keys of a table in the where clause of a select statement?
    2.Using any one or two (not all) primary keys of a table in the where clause of a select statement?
    Let me the know the same in the case of using an Secondary index.
    3.If we follow the second one,then it will go for a sequential read,how this sequential read mars the performance?
    4.How creating an index will affect the database as BASIS guys are not in favour to creating an index.
    Thanks,
    K.Kiran.

    1.Using all the primary keys of a table in the where clause of a select statement?
    2.Using any one or two (not all) primary keys of a table in the where clause of a select statement?
    Out of the above 2 first one will give more performance. Coming to primary key or Secondary indexses, anything.. it gives better performance if you give the key fileds in the order of DB declaration.
    I mean you are specifying some fields of primary key.. but not in the order .. i mean u have specified key1, key3, key4. It will give less performance than specifying only key1 and key2.
    in secondary indexes if you are not specifying all key completely that will take the key up to the order matches. i mean in key1, key3, key4 case.. it will consider only Key1.
    In Key1, Key2 case it will consider both.
    3.If we follow the second one,then it will go for a sequential read,how this sequential read mars the performance?
    4.How creating an index will affect the database as BASIS guys are not in favour to creating an index.
    Creating an secondary index will save the table contents in the format of starting with index fields in the DB. So number of indexes on the same table will need to craete more views in database. So leads to poor DB performance. i mean more space unnecesarily for a single table. That's why they will create indexes only for very frequently used fields on tables.

  • Odi-target table-primary key

    hi,
    is it compulsory to have a primary key constraint in the target table in odi.
    Can we have a target table without any primary key constraint?

    Hi,
    Yes u can have target table without PK.
    Only for IKM Incremental Update u need to define Update key (which may not be persist in back end). For IKM Control append u no need to have PK defined.
    Thanks,
    Guru

  • OMPLUS retrieve table primary key column

    I'm trying to retrieve the primary key column associated with a table via OMBPLUS.
    The following command successfully returns the PK name
    OMB+> OMBRETRIEVE TABLE 'ODS_REGION' GET PRIMARY_KEY
    REGN_PKWhen I add the GET COLUMNS clause (as per API documentation), I'm still only getting the pk name.
    OMB+> OMBRETRIEVE TABLE 'ODS_REGION' GET PRIMARY_KEY GET COLUMNS
    REGN_PKAny suggestions on what I might be doing wrong?

    Worked it out. Syntaxt needed is:
    OMBRETRIEVE TABLE 'ODS_REGION' PRIMARY_KEY 'REGN_PK' GET COLUMNS
    (no GET between table name and PRIMARY_KEY clause)

  • Large table, primary key constraint

    I have migrated a table from 8i to 9i that is over 300 million rows. I migrated the the table to a 9i database without constraints or indexes.
    I have successfully created a composite index of two columns, t1 varchar2(512), t2 varchar2(32). This index took nearly 16 hours to create.
    I am now trying to create a primary key based on that index with the following sql:
    alter table table1
    add constraint table1_t1_t2_pk primary key(t1,t2)
    using index table1_t1_t2_idx
    nologging
    This process has taken over 24 hours and is well into the second day. Studio reports it will take an additional 15 hours to create.
    My questions are these?
    1. Is my syntax okay?
    2. I thought that by creating a primary key on an existing index, that another index is not being created. I thought it would be faster this way. Why is it taking a lot longer to create then the index it is based upon?
    3. Is there a more efficient method (other than parallel query) to create this index/constraint on such a large table? What happens when I go production and need to recreate this index if I have a failure. I have never had to do this before. I can't be down for 48 hours to create an index. What other alternatives do I have?
    The table is partit[i]Long postings are being truncated to ~1 kB at this time.

    Is INDEX table1_t1_t2_idx UNIQUE? If it's not that might explain why building the primary key constraint takes longer.
    I think the USING INDEX clause with an existing index is intended mainly for different UNIQUE constraints to share the same index. In your situation I think you would be better off just building the primary key constraint.
    Cheers, APC

Maybe you are looking for

  • Remote Blob Storage for SharePoint 2013

    Hi, We have a Site Asset library in SharePoint 2013 for which we would like to connect it to the external storage or network shared drive. I read few articles related to RBS, most of them talks about using the same sql server hard drive as a external

  • Ipod 120 Classic no longer shows up in devices in iTunes

    I am using a 120gb classic and iTunes 8 Everything was fine until yesterday when my ipod stopped appearing in devices in itunes. It synchs fine and appears in windows explorer but is gone from itunes. Is there a simple fix? Thanks Jon

  • In need of help to replicate something and then modify it

    I need to recreate an image document that is a bit complex and I need to get some direct feedback regarding how exactly to do this. I'm new to Photoshop. Even though I've watched many of the Photoshop training videos I still need further help for thi

  • Camera raw (4.6) crashes when using straighten tool

    Hello, Since I upgraded my computer from Windows Vista to Windows 7 I have a problem when using the straighten tool in Camera raw. Sometimes the program works fine, but very often ACR unexpectely crashes. It doesn't matter how many photo's I have ope

  • SQL Performance tunning on RAC environment

    Hi, I am working on oracle 11.2.0.2.0 with 2 node RAC. We have a batch process with runs everday and comsumes most of the time in cluster waits. Could someone please suggest what SQL / PL SQL changes / Hints are available to tune this cluster waits..