Problem in defining Foreign key

Asked

Viewed 42 times

1

inserir a descrição da imagem aqui

I’m learning now database. I’m not getting why not let me set team_id as Foreign key.

  • Daina, from what I read in the documentation, it seems to me that your Team table has no primary key defined. What seems strange to me, that Pleo print of your ER model, your Team table, does not have a Team_id column but a Id column.

  • Pablo thanks was really this, but I was not managing to define team_id as FK of the table Attendance and Person, was giving the error that I show in the photo, but I managed to define team_id in the table LEAGUE as Primary and Foreign will be by in the table LEAGUE was already as Primary?

  • Very likely yes, at the time q vc defined q the link between the League table and Team, and the primary key is in the League table, vc created a 1 to N ratio between League and Team, but at the time q vc set the PK in Team, you will have a 1-to-1 ratio between League and Team tables. All you have to do is take care of which one of you has the primary key when it comes to defining FK. For example if you set q a Referencing Column, is the Team table Id, it means that the record will have to exist first in the Team table, and then you enter it into the League table. If I’m wrong, correct me.

1 answer

0


What you lack is to set the Id column in your Team table as the primary key.

To add a primary key to a table, you can use the command ALTER TABLE.

Your syntax is:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n);

Or by SQL Developer, right-click on the desired table and seal the menu Edit. inserir a descrição da imagem aqui

Select the item Primary Key inserir a descrição da imagem aqui

Select your primary key inserir a descrição da imagem aqui

And finally click OK.


References:

Oracle / PLSQL: Primary Keys. Available in: https://www.techonthenet.com/oracle/primary_keys.php. Accessed: 25 Nov. 2017.

Getting Started with Oracle SQL Developer. Available in: http://www.oracle.com/ocom/groups/public/@otn/Documents/webcontent/257055.htm. Accessed: 25 Nov. 2017.

  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - Of Revision

  • I think it’s better now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.