Two foreign keys to a primary key

I can't display information to two foreign keys in my table. My purpose is to display the name and surname of a project manager and account manager. I have a "Jobs" table that is linked to a table 'employees '.

Jobs
ProjectMgrID
AcctMgrID

Employees
EmployeeID
LastName
FirstName

The ProjectMgrID (fk) and the AcctMgrID (fk) are related to the EmployeeID (pk). When I create a query, I do not know how to display the first name and the name of the fields for both. It only allows me to add them once and when I add both times, it just displays the same information twice.

Help, please!

Welcome to the forum!

Looks like you will have to join EMPLOYEES twice.

Something like the following:

SELECT  e1.firstname
,       e1.lastname
,       e2.firstname
,       e2.lastname
FROM    jobs
JOIN    employees e1    ON e1.employeeid = jobs.projectmgrid
JOIN    employees e2    ON e2.employeeid = jobs.acctmgrid
;

Tags: Database

Similar Questions

  • Two foreign keys of a table in a single column

    Hi gurus

    I wonder that can we add two foreign keys of a table on a single column, I think the answer is, but what should be the reason behind this?

    Appreciate if someone there explain to me...

    Concerning

    Muzz

    Hello Muzz,

    Perhaps the example of human Chen reveals a design error.
    If a student has a 'teacher_id' "teacher" FK and FK for "emp" becaue each teacher is an EMP, I guess that the correct design would be to have only the FK of 'teacher' and to have another pointing to the 'emp' table in the 'teacher' tabe FK

    But we can imagine comical situations...
    CREATE TABLE PEOPLE (id NUMBER of KEY PRIMARY, name VARCHAR2 (20 CHAR),...);
    CREATE TABLE SCIENTIST (id people KEY PRIMARY NUMBER (id) REFERENCES, discipline VARCHAR2 (20 CHAR),...);
    CREATE TABLE POLICEMAN (id REFERENCES to KEY PRIMARY NUMBER (id), hire_date people DATE,...);

    First table = all.
    2nd: a subset of the first, with people having the profile of 'scientific '...
    3rd: a subset of the first, with people being a policeman.
    It might be people first table and in none of the 2nd and 3rd, 1st and 2nd, 1st and 3rd only or in the 3 tables.

    Then we can have a fourth table of 'something' referring to a person who must be a scientist and a police officer.

    Here is a sqlplus session illustrating; the last piece: I try inserting several lines in xxxx, only the last is accepted.

    SQL > CREATE TABLE PEOPLE (id NUMBER of the PRIMARY KEY, name VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE SCIENTIST (people of REFERENCES of KEY PRIMARY NUMBER id (id), discipline VARCHAR2 (20 CHAR));
    Table created.

    SQL > CREATE TABLE POLICEMAN (people of REFERENCES of KEY PRIMARY NUMBER id (id), DATE hire_date);
    Table created.

    SQL > CREATE TABLE XXXX (id PRIMARY KEY NUMBER, people_id NUMBER, any other VARCHAR2 (30 CHAR));
    Table created.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxsci FOREIGN KEY (people_id) scientific xxxx REFERENCES (id);
    Modified table.

    SQL > ALTER TABLE ADD CONSTRAiNT fk_xxpol FOREIGN KEY (people_id) police xxxx REFERENCES (id);
    Modified table.

    SQL > INSERT INTO person VALUES (100, 'John');
    1 line of creation.

    SQL > INSERT INTO person VALUES (120, 'Mary');
    1 line of creation.

    SQL > INSERT INTO person VALUES (103, 'Tom');
    1 line of creation.

    SQL > INSERT INTO person VALUES (123, "Bruno");
    1 line of creation.

    SQL > INSERT INTO VALUES of scientific (120, 'Chemistry');
    1 line of creation.

    SQL > INSERT INTO scientific VALUES (123, 'Mathematics');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (103, DATE ' 2001-04-01');
    1 line of creation.

    SQL > INSERT INTO VALUES of policeman (123, DATE ' 1998-07-01');
    1 line of creation.

    SQL > INSERT INTO xxxx VALUES (1, 456, "nothing");
    ERROR on line 1:
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 100, "only to people");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 120, "only the learned");
    ORA-02291: integrity constraint (SYS. FK_XXPOL) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 103, "only police officer");
    ORA-02291: integrity constraint (SYS. FK_XXSCI) violated - key parent not found

    SQL > INSERT INTO xxxx VALUES (1, 123, 'ok');
    1 line of creation.

    Best regards

    Bruno Vroman.

  • Two foreign keys

    Hi, I would like to create a table with two strangers with the same name.

    I have a first table named ' * registry * ' with a column named "* id_registry *" PK, and another called 'friends' table with two columns "* id_res *" and "* id_friends." Now I would like to id_res and id_friends should be CF and have the same id_registry PK as references.
    I tried this way:
    ALTER TABLE friends add friends COSTRAINT: FK REFERENCES FOREIGN KEY (id_res, id_friends) anagrafica (id_registry); but I got an error like this: ORA-02256.

    Is possible to do?

    You must create two foreign keys separated like that.

    ALTER TABLE friends ADD CONSTRAINT friends_FK1 FOREIGN KEY (id_res) REFERENCES anagrafica (id_registry);
    
    ALTER TABLE friends ADD CONSTRAINT friends_FK2 FOREIGN KEY (id_friends)  REFERENCES anagrafica (id_registry);
    
  • A table with two foreign keys

    Hi all

    I need to create a table (mytable) filled from 2 different tables, for example, table1 and table2

    When the rows of the table (mytable) comes from the first table (table1) columns in mytable (foreign key to the second table) are nulled, when they come (foreign key) column in the second table (table2)
    the first table) are cancelled as well.

    In terms of performance, architecture is valid, in other words, create a foreign key 2 on mytable and consequently, (the composite foreign key in this table are null according to whether they belong to the first or the second table)?

    Thanks in advance for any help

    I hope to clear

    As promised,

    mhouri.world > create table p as select * from all_users;
    
    Table created.
    
    mhouri.world > alter table p add constraint p_pk primary key (user_id);
    
    Table altered.
    
    mhouri.world > create table c (user_id references p, name varchar2(30));
    
    Table created.
    
    mhouri.world > select min(user_id),max(user_id) from p;
    
    MIN(USER_ID) MAX(USER_ID)
    ------------ ------------
               0           72                                                       
    
    mhouri.world > insert into c (user_id, name) values (0, 'test without index');
    
    1 row created.
    

    Thanks to the autonomous operation I simulates the effect of two users working on the same parent/child table

    (a) first of all, without any index covering the FK on the child table

    (b) second with a function based index on the FK column (note the desc clause in the definition of the index)

    (c) and finally with an index b-tree on the FK columns

    case (a)

    mhouri.world > declare
      2  pragma autonomous_transaction;
      3  begin
      4  delete from p where user_id = 72;
      5  commit;
      6  end;
      7  /
    declare
    *
    ERROR at line 1:
    ORA-00060: deadlock detected while waiting for resource
    ORA-06512: at line 4 
    
    mhouri.world > rollback;
    
    Rollback complete.
    

    case (b)

    mhouri.world > create index fbi_c_fk on c (user_id desc);
    
    Index created.
    
    mhouri.world > insert into c (user_id, name) values (0, 'test with fbi index');
    
    1 row created.
    
    mhouri.world > declare
      2  pragma autonomous_transaction;
      3  begin
      4  delete from p where user_id = 72;
      5  commit;
      6  end;
      7  /
    declare
    *
    ERROR at line 1:
    ORA-00060: deadlock detected while waiting for resource
    ORA-06512: at line 4 
    
    mhouri.world > rollback;
    
    Rollback complete.
    

    case (c)

    mhouri.world > create index btree_c_fk on c (user_id);
    
    Index created.
    
    mhouri.world > insert into c (user_id, name) values (0, 'test with b-tree index');
    
    1 row created.
    
    mhouri.world > declare
      2  pragma autonomous_transaction;
      3  begin
      4  delete from p where user_id = 72;
      5  commit;
      6  end;
      7  /
    
    PL/SQL procedure successfully completed.
    

    Only the index b-tree covered with success the threat of the deadlock in key foreign unidexed

    Best regards

    Mohamed Houri

  • 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 join two tables to retrieve the data from the columns in table two. Tables have primary and foreign key relationships

    Hello

    I want to join the two tables to retrieve the data from the columns of the two table passing parameters to the join query. Tables have primary and foreign key relationships

    Details of the table

    Alert-1 - AlertCode (FK), AlerID (PK)

    2 AlertCode-AlertDefinition-(PK)

    Help, please


    ----------

    Hi Vincent,.

    I think that you have not worked on adf 12.1.3.  In adf 12.1.3 you don't have to explicitly create the association. When you create the EO to your table, Association xxxxFkAssoc, will be created by ADF12.1.3 for you automatically. Please try this and do not answer anything... You can also follow the links below. I solved the problem by using the following link

    Oracle ADF Guide step by step - Oracle ADF tutorial: creating a relationship of the master / detail using Oracle ADF

    ---

  • ODI is not able to detect the primary/foreign keys to XML - the user has no privilege or object not found

    Hi guys,.

    Im trying to load an XML with the address of two entities and employee as below. The topology of reverse engineering, everything works well. Even able to display the xml data in ODI, but when I try to load the data of these two entities join by the schema of the primary keys and foreign keys which created odi engineering process reverses for xml, im im getting the error below.  IM able to load an entity data, error occurs only when I use the join odi creates internally to identify employees for xml and address components

    XML file:

    <? XML version = "1.0" encoding = "UTF-8"? >

    < EMP >

    < Empsch >

    < employee >

    < EmployeeID 12345 > < / EmployeeID >

    < original > t < / initials >

    John < name > < / LastName >

    DOE < FirstName > < / name >

    < / employee >

    < address >

    < > 12345 as WorkPhone < / as WorkPhone >

    < WorkAddress > test 234 < / WorkAddress >

    < / address >

    < / Empsch >

    < / EMP >

    Topology: jdbc:snps:xml?f=C:/Temp/RR/Empsch.xml & s = Empsch & re = EMP & dod = true & nobu = false

    Error message:

    -5501: 42501: java.sql.SQLException: user has no privilege or object not found: EMPSCH. EMPSCHPK

    java.sql.SQLException: user has no privilege or object not found: EMPSCH. EMPSCHPK

    at org.hsqldb.jdbc.Util.sqlException (unknown Source)

    for org.hsqldb.jdbc.JDBCPreparedStatement. < init >(Unknown Source)

    at org.hsqldb.jdbc.JDBCConnection.prepareStatement (unknown Source)

    at com.sunopsis.jdbc.driver.xml.SnpsXmlConnection.prepareStatement(SnpsXmlConnection.java:1232)

    at sun.reflect.GeneratedMethodAccessor65.invoke (unknown Source)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

    at java.lang.reflect.Method.invoke(Method.java:597)

    to oracle.odi.core.datasource.dwgobject.support.OnConnectOnDisconnectDataSourceAdapter$ OnDisconnectCommandExecutionHandler.invoke (OnConnectOnDisconnectDataSourceAdapter.java:200)

    to $Proxy2.prepareStatement (Unknown Source)

    at oracle.odi.runtime.agent.execution.sql.SQLCommand.doInitializeStatement(SQLCommand.java:83)

    at oracle.odi.runtime.agent.execution.sql.SQLCommand.getStatement(SQLCommand.java:117)

    at oracle.odi.runtime.agent.execution.sql.SQLCommand.getStatement(SQLCommand.java:111)

    at oracle.odi.runtime.agent.execution.sql.SQLDataProvider.readData(SQLDataProvider.java:81)

    at oracle.odi.runtime.agent.execution.sql.SQLDataProvider.readData(SQLDataProvider.java:1)

    at oracle.odi.runtime.agent.execution.DataMovementTaskExecutionHandler.handleTask(DataMovementTaskExecutionHandler.java:70)

    at com.sunopsis.dwg.dbobj.SnpSessTaskSql.processTask(SnpSessTaskSql.java:2913)

    at com.sunopsis.dwg.dbobj.SnpSessTaskSql.treatTask(SnpSessTaskSql.java:2625)

    at com.sunopsis.dwg.dbobj.SnpSessStep.treatAttachedTasks(SnpSessStep.java:577)

    at com.sunopsis.dwg.dbobj.SnpSessStep.treatSessStep(SnpSessStep.java:468)

    at com.sunopsis.dwg.dbobj.SnpSession.treatSession(SnpSession.java:2128)

    to oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor$ 2.doAction(StartSessRequestProcessor.java:366)

    at oracle.odi.core.persistence.dwgobject.DwgObjectTemplate.execute(DwgObjectTemplate.java:216)

    at oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor.doProcessStartSessTask(StartSessRequestProcessor.java:300)

    to oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor.access$ 0 (StartSessRequestProcessor.java:292)

    to oracle.odi.runtime.agent.processor.impl.StartSessRequestProcessor$ StartSessTask.doExecute (StartSessRequestProcessor.java:855)

    at oracle.odi.runtime.agent.processor.task.AgentTask.execute(AgentTask.java:126)

    to oracle.odi.runtime.agent.support.DefaultAgentTaskExecutor$ 2.run(DefaultAgentTaskExecutor.java:82)

    at java.lang.Thread.run(Thread.java:662)

    Caused by: org.hsqldb.HsqlException: the user has no privilege or object not found: EMPSCH. EMPSCHPK

    at org.hsqldb.error.Error.error (unknown Source)

    at org.hsqldb.ExpressionColumn.checkColumnsResolved (unknown Source)

    at org.hsqldb.QueryExpression.resolve (unknown Source)

    at org.hsqldb.ParserDQL.compileCursorSpecification (unknown Source)

    at org.hsqldb.ParserCommand.compilePart (unknown Source)

    at org.hsqldb.ParserCommand.compileStatement (unknown Source)

    at org.hsqldb.Session.compileStatement (unknown Source)

    at org.hsqldb.StatementManager.compile (unknown Source)

    at org.hsqldb.Session.execute (unknown Source)

    ... more than 27

    Please advice

    Thank you

    Fabien

    It is clear from the content of the xml file, you have given here. In this xml file, you have four type of complex. Two of them are employed and address. However the employee doesnot has nothing to do with the address that you have not added the relationship. That is why its failure. This isn't the fault of the ODI.

    Also I suggest not to use self-generated by ODI dtd that you might face problem in the future. For example, the address of XML type has 8 attributes, and 4 of them are not mandatory. This means that each of your xml file may have attributes between 4 and 8.  That's where ODI auto generated DTD fails.

    ComplexType element XML schema

    Thank you

    Chantal

  • How did the foreign key that is recognized by the primary key

    Hi, when I have two user tables and userLevel and the primary key of the user is the user name and I add a field to userLevel userID is only to make the userID field in userLevel automatically the foreign key of the table? Or do you switch the engine of MyISAM tables to InnoDB to make relational links. Can someone please explain this in English clear to me, because I just can't get my head around this.

    In the case of a query such as SELECT statement, we talked, being a key actually means that the field is indexed so that the database can quickly look up.  Thus, in phpMyAdmin, you set the field to be indexed by the visualization of the structure of the table and clicking the icon index (the one with the Lightning) for the field in the Action section.

    If you are looking for integrity refrential between tables we also talked about and using the InnoDB engine, you can click the link 'Show the relational' in the table structure page of phpMyAdmin and define foreign key constraints.

  • How can I make sure that changes in a primary key (in the parent table) would also appear directly in the FOREIGN KEY in the child table?

    Forgive my question. I am very new to Oracle.

    How can I make sure that changes in the key primary supplier_id (concerning the supplier table) would also appear directly in the FOREIGN KEY (supplier_id) in the products table?

    Is that not all the primary key and FOREIGN KEY on?

    My paintings:

    I created 2 tables and connect to apply in the data base referential integrity, as I learned.

    CREATE TABLE - parent provider

    (the numeric (10) of supplier_id not null,)

    supplier_name varchar2 (50) not null,

    Contact_Name varchar2 (50).

    CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)

    );

    CREATE TABLE - child products

    (the numeric (10) of product_id not null,)

    supplier_id numeric (10) not null,

    CONSTRAINT fk_supplier

    FOREIGN KEY (supplier_id)

    REFERENCES beg (supplier_id)

    );

    I inserted the following text:

    INSERT INTO provider

    (supplier_id, supplier_name, contact_name)

    VALUES

    (5000, 'Apple', 'first name');

    I expect that the supplier_id (5000) to the provider of the table also appears in the products table under key supplier_id having the same value which is 5000. But this does not happen.

    How to get there?

    Thanks in advance!

    Hello

    What is a foreign key in Oracle?

    A foreign key is a way to ensure referential integrity in your Oracle database. A foreign key means that the values of a table must appear also in another table.

    Ok!??

    What is now the right way to implement referential integrity in your Oracle database that the values of a table must also be included in another table?

    A foreign key referential integrity indeed enfore in ensuring that the value in the child table must have a corresponding parent key (otherwise you will encounter an error, as evidenced by "SomeoneElse"). However, it will never automatically insert a row in the other table.

    If you are looking for a solution that automatically inserts a record in the other table, maybe you should go for triggers:

    See:

  • Primary key and foreign key between the different schemas

    Hi all;

    SCOTT user have a 'rank' of table-> it contains the primary key-> users tablespace

    The U1 user have a 'emp' table-> it contains the foreign key-> test tablespace

    U1 > select constraint_name, constraint_type, r_owner, owner, r_constraint_name of all_constraints where table_name = 'EMP ';

    OWNER CONSTRAINT_NAME C R_OWNER R_CONSTRAINT_NAME

    ------------ ------------------------------ - -------------------------- ----------------------- -----------------------------------------------------

    U1              EMP_EMPLEV_C12_FK R SCOTT GRADE_GRL_C1_PK

    If I have connection U1, no possibility to find the name of the primary key without connection scott table?

    Thank you

    Hello

    So, you want to know what picture of the SCOTT schema has the constraint that is named "GRADE_GRL_C1_PK." Isn't it?

    If so,.

    SELECT table_name

    Of all_constraints

    Owner WHERE = 'SCOTT '.

    AND the argument constraint_name = 'GRADE_GRL_C1_PK. '

    ;

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

  • 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

  • Primary key and foreign key

    Hi all

    I'm confused. If I create a primary key of table constraints, it willl create a unique index by default. If I create a foreign key, it also creates an index? of any kind?


    Thank you very much

    Kinz

    Laughing out loud. foreign key does not have a unique index on the table.

  • Creation of primary keys, foreign keys and indexes

    Hello

    I created 60 new tables based on 60 existing tables.

    The 60 current tables are named FY11_ACC1, FY11_ACC2, FY11_SALES1, FY11_SALES2..., but all of the 60 tables start with FY11. I created the same structured tables but with names like FY12_ACC1, FY12_ACC2, FY12_SALES1, FY12_SALES2 FY12...

    I used the below SQL and generated SQL code.

    Select 'create table ' | Replace (table_name, 11, 12).
    ' in select * from '. table_name | "where 1 = 0'.
    from user_tables;

    But this did not generate the PRIMARY KEYS, FOREIGN KEYS and INDEXES. How can I automatically or with similar like SQL above? _

    Thanks for your time and your help.







    How can I create primary keys, foreign keys and indexes for all

    ssk1974 wrote:
    Can you please provide me with more information or examples.

    Something like:

    SET SERVEROUTPUT ON FORMAT WORD
    DECLARE
        h   NUMBER;
        th  NUMBER;
        ddl CLOB;
    BEGIN
        h := DBMS_METADATA.OPEN('TABLE_EXPORT');
        DBMS_METADATA.SET_FILTER(h,'SCHEMA','SCOTT');
        DBMS_METADATA.SET_FILTER(h,'NAME','DEPT');
        DBMS_METADATA.SET_FILTER(h,'EXCLUDE_PATH_EXPR','=''TABLE_DATA''');
        DBMS_METADATA.SET_FILTER(h,'EXCLUDE_PATH_EXPR','=''STATISTICS''');
        th := DBMS_METADATA.ADD_TRANSFORM(h,'DDL');
        DBMS_METADATA.SET_TRANSFORM_PARAM(th,'PRETTY',false);
        DBMS_METADATA.SET_TRANSFORM_PARAM(th,'SQLTERMINATOR',true);
        LOOP
          ddl := DBMS_METADATA.FETCH_CLOB(h);
          EXIT WHEN DDL IS NULL;
          DBMS_OUTPUT.PUT_LINE (ddl);
        END LOOP;
        DBMS_METADATA.CLOSE(h);
    END;
    /
    
      CREATE TABLE "SCOTT"."DEPT" ("DEPTNO" NUMBER(2,0), "DNAME" VARCHAR2(14),
    "LOC" VARCHAR2(13),  CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO") USING INDEX
    PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS  STORAGE(INITIAL 65536
    NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1
    FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE
    DEFAULT) TABLESPACE "USERS"  ENABLE) SEGMENT CREATION IMMEDIATE PCTFREE 10
    PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT
    1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST
    GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
    TABLESPACE "USERS" ;
    
      CREATE UNIQUE INDEX "SCOTT"."PK_DEPT" ON "SCOTT"."DEPT" ("DEPTNO") PCTFREE 10
    INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS  STORAGE(INITIAL 65536 NEXT 1048576
    MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
    BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE
    "USERS" ;
    
      ALTER TABLE "SCOTT"."DEPT" ADD CONSTRAINT "PK_DEPT" PRIMARY KEY ("DEPTNO")
    USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
    STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
    PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE
    DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "USERS"  ENABLE;
    
    PL/SQL procedure successfully completed.
    
    SQL>  
    

    SY.

    Published by: Solomon Yakobson January 5, 2012 16:22

  • 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

Maybe you are looking for

  • Satellite A505-s6970: Toshiba Service Station does not work on Windows 7

    Hey guys I need your help, I installed windows 7 ultimate on my toshiba Satellite A505 s6970 and I downloaded all the drivers from http://www.csd.toshiba.com/ and I installed all. When I open the Device Manager, 3 devices are not installed (Base-2 sy

  • HP ENVY 15-J003LA MAX MSATA SSD?

    I would like to know what is the maximum capacity that supports msata laptop HP ENVY 15 - J003LA. Another question I have is about the following limitation: SSD drive: Support for SSD drive 24-GB mSATA (not available on computer models with memory sy

  • ReadyCLOUD Admin

    I have a ReadyNAS 204 6.5.1 Can this device be adminitrated mthe ReadyCloud? When I click on the admin page, I get a message from the local administrator.

  • copies of the icons when attaching to move

    When move icons on the desktop, it creates another shortcut basically and he calls a copy. I had up to seven copies of the same thing on the screen. [Moved from comments]

  • Vista - Error Code: 80070005 (cannot install SP1 KB936330)

    I get an error code 80070005 whenever I try to install Windows Vista Pack 1 (KB936330) why that I get this message and how do I solve the problem?