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

Similar Messages

  • Two foreign keys to one primary key

    I'm having trouble displaying information for two foreign keys in my table. My goal is to display the first and last name of both a Project Manager and Account Manager. I have a "Jobs" table linked to an "Employees" table.
    Jobs
    ProjectMgrID
    AcctMgrID
    Employees
    EmployeeID
    LastName
    FirstName
    Both ProjectMgrID(fk) and AcctMgrID(fk) are linked to EmployeeID(pk). When I build a query, I am not sure how to display both the FirstName and LastName fields for both. It only allows me to add them once, and when I add them twice it just displays the same information twice.
    Please help!

    Welcome to the forum!
    It looks like you'll need to join to 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
    ;

  • How can I see all references to primary key

    How can I make a query for my primary key to see the detail foreign keys and see which key is not available?

    select a.table_name, a.constraint_name, b.table_name as Ref_Table, b.constraint_name as Ref_Constraint
    From ALL_CONSTRAINTS a, ALL_CONSTRAINTS b
    where a.r_constraint_name = b.constraint_name
    and a.constraint_type = 'R'
    and b.table_name = 'TABLE_NAME';
    OR, more exactly:
    select a.table_name, a.constraint_name, b.table_name as Ref_Table, b.constraint_name as Ref_Constraint
    From ALL_CONSTRAINTS a, ALL_CONSTRAINTS b
    where a.r_constraint_name = b.constraint_name
    and a.constraint_type = 'R'
    and b.constraint_type = 'P'
    and b.table_name = 'TABLE_NAME';
    null

  • 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

  • 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

  • How shall we do validation for Unique Key and Multiple Primary Key?

    Hi,
    I have table created From EO in which one column is checked as Unique.
    How to do validation for column checked as Unique.
    I know how to do validation for column checked as primary key.
    Below is sample code for primary key validation
    if (getRvSize() != null)
    throw new OAAttrValException(OAException.TYP_ENTITY_OBJECT,
    getEntityDef().getFullName(), // EO name
    getPrimaryKey(), // EO PK
    "RvSize", // Attribute Name
    value, // Attribute value
    "AK", // Message product short name
    "FWK_TBX_T_EMP_ID_NO_UPDATE"); // Message name
    if (value != null)
    OADBTransaction transaction = getOADBTransaction();
    Object[] rvKey = {value};
    EntityDefImpl rvDefinition = xxczVAGCSRVSizingEOImpl.getDefinitionObject();
    xxczVAGCSRVSizingEOImpl rv =
    (xxczVAGCSRVSizingEOImpl)rvDefinition.findByPrimaryKey(transaction, new Key(rvKey));
    if (rv != null)
    throw new OAAttrValException(OAException.TYP_ENTITY_OBJECT,
    getEntityDef().getFullName(), // EO name
    getPrimaryKey(), // EO PK
    "RvSize", // Attribute Name
    value, // Attribute value
    "AK", // Message product short name
    "FWK_TBX_T_EMP_ID_UNIQUE"); // Message name
    What changes need to be done for above code in order to do the validation for Unique Key.
    I have one more Question
    How shall we do the Validation for Multiple Primary Key in a table?
    - Mithun

    1. If you just validate on one attribute like your unique key, then put your logic in the set<Your AttributeName) method
    2. If you want to do the cross validation ( like validating multiple attributes) then put your logic in the validateEntrity Method
    How to do that?
    1. Create a Validation View object.
    2. Associate your VVO to the VAM
    3. Create entity expert.
    4. Have method in entity expert for your validation (you would be calling AM and then VO execute the query and do the validation.
    5. You would be calling the Entity experty method from your EO either setMethods or validateEntity.
    I have given just the high level points.
    Hope this helps.
    Thanks,
    RK

  • Partition key on non primary key column

    I have scenario where the table has defined non primary key column as "partition key".
    I was wondering how it will affect the purge/archive process if the index and table are using different column (they are not aligned)?
    What should be the solution to this scenario?
    Can I use the non primary key column as partition key? Is this affect purging/archiving or switching -IN or OUT partitions?
    Here is the article which Im following but didn't get the precise answer:
    "When the indexes and the table use the same partitioning function and columns in the same order, the table and index
    are aligned." 
    https://msdn.microsoft.com/en-US/library/ms345146%28v=SQL.90%29.aspx?f=255&MSPPError=-2147217396#sql2k5parti_topic9
    Look at "Index
    Partitioning" and "Defining
    the Partitioning Key" section.
    ZK

    If it non aligned, you cannot do switching in or out.  to switch, you should have aligned partitons.
    Hope it Helps!!

  • EJB3: One-to-one relationships and auto-generation of primary key values...

    Dear Forum,
    I'm relatively new to JEE, and while I've so far enjoyed the convenience of EJB3 for most things, it seems that I have a corner case that can not be handled by the entity manager.
    First, here's a quick mockup of my entities:
        Gallery
                |
                -- [1] -------[1] ----> Author
                |
                -- [1] -------[M] ---> Photo
                                                  |
                                                  -- [1] ------ [1] -> PhotoImage
                                                                                     |
                                                                                     -- [1] ------ [1] -> IPTCData
                                                                                     |
                                                                                     -- [1] ------ [1] -> EXIFDataI also have a remote, stateless session bean that does nothing more than persist()/merge()/remove() and locate (through find() and a few custom queries) my Gallery and Photo instances.
    Now, the abridged version of my problem is this: when I persist a Photo, it naturally gets its primary key generated (I used @Id and @GeneratedValue). However, for some reason none of the other dependent entities inherit Photo's primary key value. In my client, I create a new Gallery locally and then persist it. I then create new Photo, PhotoImage, IPTCData and EXIFData instances. I add the IPTCData and EXIFData instances to PhotoImage (there are PhotoImage methods for adding those); then I add the PhotoImage instance to Photo (again, Photo has a setter for adding a PhotoImage). Then, I persist the Photo.
    --- Aside: ------------
    I've set up my relationships using annotations like @OneToMany (Gallery references a Collection of Photos), and @OneToOne (Photo's primary key references PhotoImage's primary key; PhotoImage's primary key references the primary keys of IPTCData and EXIFData). I've also added @JoinColumn annotations where necessary.
    Finally, I get the Gallery instance's Photo collection (public Collection<Photo> getPhotos()), I add the Photo to the collection, and I add the Photo collection BACK to the Gallery (public void setPhotos(Collection<Photo>)).
    At this point, the Gallery should have all of my entities contained inside of itself. However, when I persist the Gallery at this point, all of the entities show up in the database, but none of the primary keys get inherited. Photo's primary key gets generated automatically, of course, but the rest have zero (0) as their PK values. I had wanted, for example, IPTCData to get the value of Photo.id.
    I had assumed that the entity manager would persist everything for me provided that I told it, through my annotations, about which entities relate to which. Does persistence only work in the direction of retrieval? That is, does the entity manager only do its thing when fetching (versus setting) data when there is more than 1 object in the graph? I can't believe that it can't, otherwise we'd all be ditching it and going back to straight SQL to do our work.
    Thus, I HAVE to be doing something wrong, or else omitting to do something right.
    I've made other incarnations of the above setup, and none of them have worked perfectly. I've even set up foreign key constraints in the database, but I had my share of unresolvable problems there, too.
    So I guess my big question with all of this is: when you have a deeply nested graph of objects, can the entity manager figure everything out and persist all of the objects correctly and with the right values? If not, boo. If so, how does one accomplish this?
    I know that I could persist each and every entity separately in my client code, but I wanted to have a natural-looking way of piecing all of my entities together. More specifically, I could persist IPTCData explicitly; however, I want to be able to add everything to a Photo and only persist the Photo itself, with the idea that all of the objects and sub-objects would be persisted automagically.
    Is this possible? Or have I bought into a pipe dream with the guarantee of the proverbial magic bullet?
    Thanks to everyone in advance for his/her input.
    Regards,
    Michael

    Daniel,
    Late last night I found the solution to my problem. And you're right, it had everything to do with which side is wired to which. I got confused about which side to consider the "owning" side, since one can look at it from the perspective of the database or the application.
    Interestingly, the first 2 lines of your example code match my client code almost verbatim: First, I create a new Gallery (or else I get an existing one). I then create a new Photo and then add it to the Gallery's collection using add(). I then persist the Gallery. My session bean internally calls EntityManager.merge(gallery). How it differs from your example, though, is in the fact that, on an application level, a Photo has no knowledge whatsoever of a Gallery. Which is to say that my Photo class does not have a 'getGallery()' method. Somehow, Hibernate is able to figure it all out and still update Photo's 'parentIdRef' column in the database. My logs show that Hibernate issues a SQL UPDATE on the Photo after both a Gallery and a Photo have been created. It sets the Photo's 'parentIdRef' column to the primary key value of the owning Gallery.
    Getting back to the matter, my problem was not with being able to map a Photo to a Gallery; rather, I was unable to add a PhotoImage to a Photo and have the relationship get correctly mapped in the database. The relationship between a Photo and a Gallery is many-to-one: there can be many Photo objects inside of a Gallery. In the database, a Photo row contains a 'parentIdRef' column whose value is that of a Gallery's primary key. I used @OneToMany on the Gallery, since a Gallery is, from an application perspective, the "owning" side, even though it's only the Photo that knows who it's referring to since only IT contains the 'parentIdRef' column in the database.
    I tried setting up the relationship between a PhotoImage and a Photo in the same manner, with the PhotoImage having a 'parentIdRef' column and referring to a Photo's primary key. I had annotated the Photo's getPhotoImage() method with @OneToOne, along with an appropriate @JoinColumn.
    It was THIS part that I had backwards. With a @OneToMany relationship, the child refers back to the parent --- it can be no other way. However, with a @OneToOne relationship, the owner refers to the child.
    So, to make a long story short, all I had to do was create extra database columns for Photo. Those columns refer to the primary keys of PhotoImage, EXIFData and IPTCData. Concretely, for example, Photo.photoImageIdRef references PhotoImage.id. And so on and so forth.
    The only negative side-effect of this is that my database-level foreign key constraints are now broken, since Hibernate wants to persist PhotoImage, EXIFData and IPTCData before it persists Photo.
    I have to investigate more deeply the 'cascade=...' option of the @OneToOne annotation in order to find out it relates, if at all, to a database-level cascade.
    Anyway, thanks for the reply. It seems that you had a very good idea about what my problem was. I would have posted the fact that I had resolved the problem last night, except that it was too late and I was heading to bed. If you still want the Duke points, let me know. I don't currently know the rules about if, when and to whom I should issue points in cases like these when I've already solved the problem.
    Regards,
    Michael

  • FOREIGN KEY IS PRIMARY KEY

    Hi,
    Has anyone come across the following problem?
    I have two tables
    customer individual
    customer_id (PK) customer_id (PK)
    customer_id in individual is the primary key for that table. It is also a foreign key ( related back to customer).
    If I use JDeveloper to drag in the two tables ( into a class diagraqm), I have a problem.
    JDeveloper "sees" the Individual bean foreign key mapping and generates a getCustomer() method (which returns a handle to the appropriate customer bean).
    However as it "removes" the getCustomer_id() method(which returns the actual customer_id), the primary_key reference is dropped.
    When I try and compile, JDeveloper quite rightly complains with "where is the primary key".
    Can anyone help? Do I need to make it a rule that the foreign key is not also the primary key?
    thanks in advance,
    Kevin

    Hello,
    I am having the same problem attempting to use a foreign key as a primary key in an EJB 2.0 CMR running on WLS 6.1. If I do not set the foreign key portion of my composite key in the ejbCreate() method I get an error stating that the field cannot be null (and must be set), when I set the field in the ejbCreate I then get an Error in the ejbPostCreate stating that CMR fields cannot be set using the setXXX methods. The entity relations are as follows:
    SalesOrder <--------------------------------------->SalesOrderProduct
    SalesOrder = (orderid(PK)+orderstatus+salesrepid+contractdate+processdate)
    SalesOrderProduct = (orderid(PK/FK)+productid(PK)+qty+unitcost)
    The CMR relationship is bi-directional. The SalesOrder entity bean's ejbPostCreate() method
    invokes a helper method that attempts to create the salesOrder product entity as part of the salesorder create transaction and that's when I get the errors stated above.
    Have or anyone found out how to handle this kind of cmr using the Weblogic server (6.1) and ejb 2.0??
    Thanks,
    Darryl

  • Primary Key from foreign keys

    I have a table which has 3 foreign keys, two of which form the primary key.
    I have: ID_Product, ID_Sale, ID_Buy, and also an attribute which tells if its a buy or a sale.
    So, if a sale is made, the primary key should consist of ID_Product && ID_Sale, otherwise, it's ID_Product && ID_Buy.
    Is there a way to do this in Oracle, and if there is, can you point me to where I can read about this?
    thank you in advance

    tashe,
    This is the wrong forum for you question. Try posting here:
    PL/SQL
    I will say, however, that there are advantages to keeping your primary keys apart from your data - just populating them from a sequence. Also, you may be temped to enforce this via triggers, but read the following first:
    http://tkyte.blogspot.com/2008/05/another-of-day.html
    Not that you would have a mutating table problem, but the two table solution may help you. Really think the design through.
    Regards,
    Dan

  • How to set two attributes as  a primary key in database ?

    how to set two attributes as a primary key?
    Take COffeesbreak as an example ,
    let's suppose that there are cof_name ,sup_id,price and so on;
    the same cof_name may be suplied by more sup_ids,
    and one sup_id may suply more cof_names.
    so the Primary key should be set the cof_name and sup_id ,
    how to set ?
    (of course that i konw if I set a cof_id,the problem will be easy work out
    but now there are those like above)
    I set that as following:
    create table coffees (cof_name VARCHAR(32) PRIMARY KEY,sup_id INTEGER, PRIMARY KEY,PRICE INTEGER )
    THE database print error :cant add more primary keys!
    thanks in advance

    You can only use the PRIMARY KEY declaration on a column if it is the only Primary Key column.
    Use the PRIMARY KEY constraint statement instead.
    create table coffees (
      cof_name VARCHAR(32),
      sup_id INTEGER,  
      PRICE INTEGER,
      PRIMARY KEY ( cof_name ,sup_id )
    )Dave

  • Primary Key / Foreign Key relationship

    Hi Folks,
    I have a MASTER_TABLE which has master values . This table has two fields, STATUS & STATUS_TEXT,  Key field is STATUS.
    This is a check table for another table INSTANCE_TABLE which also has field STATUS; this is not a key field though.
    The requirement is: If I  attempt to delete a STATUS value from MASTER_TABLE, it should not allow me if there is any record in INSTANCE_TABLE which has the same STATUS value. Is there a way to establish such a relationship between the two tables?
    Thank you for the help!
    Sid

    Hi Friend,
    Only through table Maintaince only it can be acheived , by table relatio it can not be  .
    because as you know the  non primary key cannot be be  check table field  .means
    In Mater table STATUS is primary key so you are  making it as check table field to  Insance table  STATUS Field.
    But  in Instance table is not a primary key so , without primary key you will not be able to  do the foriegn key relation to the master table ,so that  it will not be check table becs of that they check will not happen for your  scenario  . to acheieve this you need to either make your STATUS field in Instance table also as primary key and  do the foerign key relationship with  Master table  .
    Or else  write an event in the table maintainance  .
    Regards,

  • 2 Primary Keys to 1 Foreign Key

    Can I connect 2 different Primary Key to 1 foreign Key as mentioned in Scenario 1? Is this a good design?
    Else suggest me which among the following scenarios hold good.
    Scenario 1:
    Global Employee (Global Employee ID)
    Local Employee (Local Employee ID)
    Dept_Global_Local Employee_Map (Employee ID, Global_Local Employee ID)Scenario 2:
    Global Employee (Global Employee ID)
    Local Employee (Local Employee ID)
    Dept_Global_Local Employee_Map (Employee ID, Global Employee ID, Local Employee ID)Scenario 3:
    Global Employee (Global Employee ID)
    Local Employee (Local Employee ID)
    Dept_Global_Employee_Map (Employee ID, Global Employee ID)
    Dept_Local_Employee_Map (Employee ID, Local Employee ID)

    A foreign key in child table refers Primary OR Unique key in Parent table.
    If you are defining 1 foreign key to 2 primary keys (of 2 different tables), there might be a chance of 1 to 1 relationship between 2 parent tables. So, defining 1 to 1 relationship among parent table is a good idea.
    Even though defining 1 FK to 2 PKs not possible in Oracle, defining such constraints is not suggestable in any OLTP DBs.

  • Primary key,Froign key , Unique key

    I want the answer of following questions.
    1) Primary key create cluster index or simple index?
    2) What is cluster index : I know only( cluster index stored record at physical level in sorted order) if there is another difference then tell me
    3) Can I set relation ship with unique key if it is specified with NOT NULL constraints in a table.
    4) Foreign key creates a index or not?

    Hello
    I want to be at least 6 inches taller, better looking and really good at playing the drums, but that's neither here nor there.
    A simple please never hurts.
    For answers to the first 2 I'd concentrate on the clusters section of the concepts guide as your understanding of what clusters are for and when they should be used appears to be lacking:
    http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96524/c11schem.htm#25479
    and also have a look at this asktom thread:
    http://asktom.oracle.com/pls/ask/f?p=4950:8:6996872588440689863::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:1308402202736
    The last two questions, again you can find the info you need in the concepts guide but
    3) Can I set relation ship with unique key if it is specified with NOT NULL constraints in a table.To set up a foreign key from one table to another, it needs to reference a primary key or a unique key in the target table, the NOT NULL constraint has very little to do with it. A very simple test will show this:
    SQL> create table dt_test(id number);
    Table created.
    SQL> create table dt_test_fk(id number);
    Table created.
    SQL> create table dt_test_fk(id number, pk_id number);
    Table created.
    SQL> alter table dt_test_fk add constraint fk_dt_test_fk FOREIGN KEY(pk_id) references dt_test(id);
    alter table dt_test_fk add constraint fk_dt_test_fk FOREIGN KEY(pk_id) references dt_test(id)
    ERROR at line 1:
    ORA-02270: no matching unique or primary key for this column-list
    SQL> alter table dt_test add constraint uk_dt_test UNIQUE (id);
    Table altered.
    SQL> alter table dt_test_fk add constraint fk_dt_test_fk FOREIGN KEY(pk_id) references dt_test(id);
    Table altered.
    SQL> alter table dt_test modify(id number not null);
    Table altered.
    4) Foreign key creates a index or not? Creating a foreign key on a column does NOT automatically create an index on that column.
    A primary key may create a new index. If an index is already available on the table and it can be used i.e the leading edge of the index holds the columns in the primary key, it will be used rather than creating a new one.
    HTH

  • Auto_increment issue faced with primary key

    create table test(
    snum int not null auto_increment,
    course_id varchar(15),
    primary key(course_id, snum))
    create table testt(
    course_id varchar(15),
    pre_id varchar(15),
    foreign key (course_id) references test(course_id),
    foreign key (pre_id) references test(course_id))
    the above two table test has a primary key (snum, course_id) . course_id, which is being referenced in below table testt as a foreign key.
    i have an issue when trying to create a table test wit primary key (course_id, snum) that Error code 1075, SQL state 42000: Incorrect table definition; there can be only one auto column and it must be defined as a key. but when place primary key(snum, course_id) it's working in this case i wasn't able to use course_id as foreign key for below table testt.
    could you please suggest me how to resolve this issue.

    The description of this forum is "Discussion of Oracle SQL and PL/SQL issues "
    MySQL Forums are at http://forums.mysql.com/
    Hemant K Chitale
    Edited by: Hemant K Chitale on Jan 11, 2011 2:56 PM

Maybe you are looking for