1 Reply Latest reply: Nov 26, 2012 7:05 AM by Jim Smith RSS

    Create Relationships between Tables

    976002
      Hello,

      i have a problem transfering my er model into sql developer
      i have 2 entities in my er model

      Nation
      -nation_id
      -name
      -flag

      and an Entity

      Player
      -player_id
      -name
      -birthday
      -gender
      -nation_id(fk)

      it is a 1:n relationship because a nation has several players

      now is the problem how to create this model with the SQL Developer

      I just created the 2 tables and now i dont know how to make the relationship, how to set the foreign key in the table player

      my first try was to rightklick on player --->constraint -->add foreign key
      the name should be nation_id_fk
      and the referencetable and referencecolumn is clear, but why do i need to add the column name of the existing table ?
      what do i need to choose ? i want a new column, the column "nation_id_fk" but there are just the choices nation_id , name , flag..

      thank you for you help!
        • 1. Re: Create Relationships between Tables
          Jim Smith
          The SQL syntax for adding a foreign key is

          alter table player
          add constraint nation_id_fk
          foreign key (nation_id)
          references nation( nation_id )

          This means that the column player.nation_id references the column nation.nation_id. You don't need a new column as the relevant columns already exist.
          In the add constraint dialog nation and nation_id are the referenced table and column.

          Note that nation.nation_id needs to be a primary key or a unique queue.

          Edited by: Jim Smith on Nov 26, 2012 1:04 PM