Home » SQL & PL/SQL » SQL & PL/SQL » Trouble with FK and PK ref from another table
Trouble with FK and PK ref from another table [message #234382] Tue, 01 May 2007 06:22 Go to next message
Power Penguin
Messages: 33
Registered: April 2007
Location: Australia
Member
I have a table and that is were my Fk and Pk are from. From what I understand If I have a fk referencing another table i can make the pk of my current table an attribute from the table i referenced as the fk.

The table im referencing is one called code which is where my pk and fk came from but I cant get my code to work. Is there an obvious error I have missed?

CREATE TABLE Videos_P (Title varchar (25), Description varchar (15), Rating varchar (3), Length number (1),
CONSTRAINT PK_VIDEOS_CODE PRIMARY KEY (Code),
CONSTRAINT FK_ITEM_CODE_V FOREIGN KEY (Code) REFERENCES Item (Code));



Thanks
Re: Trouble with FK and PK ref from another table [message #234383 is a reply to message #234382] Tue, 01 May 2007 06:26 Go to previous messageGo to next message
Michel Cadot
Messages: 64098
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
CONSTRAINT PK_VIDEOS_CODE PRIMARY KEY (Code),


You don't have a column named "code" in your table, so it can't be its primary key.

Regards
Michel
Re: Trouble with FK and PK ref from another table [message #234384 is a reply to message #234383] Tue, 01 May 2007 06:30 Go to previous messageGo to next message
Power Penguin
Messages: 33
Registered: April 2007
Location: Australia
Member
I thought if it was a primary key in the table the foreign key is referenced to its ok?
Re: Trouble with FK and PK ref from another table [message #234390 is a reply to message #234384] Tue, 01 May 2007 06:50 Go to previous messageGo to next message
Michel Cadot
Messages: 64098
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
No, the column must be in the table even if it references another column.

Regards
Michel
Re: Trouble with FK and PK ref from another table [message #234393 is a reply to message #234390] Tue, 01 May 2007 07:08 Go to previous messageGo to next message
Power Penguin
Messages: 33
Registered: April 2007
Location: Australia
Member
Does that mean if I have 'code' in the current table and reference it as a pk and reference code in another table as a fk will will it join (i know its not the right word) the two together so if i enter say 'penguin' in one it will put it in the other.


Thanks
Re: Trouble with FK and PK ref from another table [message #234411 is a reply to message #234393] Tue, 01 May 2007 09:34 Go to previous messageGo to next message
Michel Cadot
Messages: 64098
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
The definition of a primary key in the relational theory is a list a columns of the table that uniquely defined a row.

The definition of a foreign key is a list of columns of a table that references a list of columns defined as a primary or unique key in another or the same table.

There is nothing with automatic filling. You fill them as you want but the data must satisfy the relations you gave.

Regards
Michel
Re: Trouble with FK and PK ref from another table [message #234436 is a reply to message #234382] Tue, 01 May 2007 10:22 Go to previous messageGo to next message
Bill B
Messages: 1482
Registered: December 2004
Senior Member
A foreign key is simply a constraint so that you may NOT insert a row into a child table that contains a value that is not in the parent table. For example if your column code in your child contains the value XYZ and the parent table has no row that contains XYZ in heir code column, you will be blocked from inserting the row into the child table.
Re: Trouble with FK and PK ref from another table [message #234510 is a reply to message #234436] Tue, 01 May 2007 19:48 Go to previous message
Power Penguin
Messages: 33
Registered: April 2007
Location: Australia
Member
Thanks for taking the time to explain as I am only new at this. I really appreciate it. Razz
Previous Topic: How do I sum a value? (merged)
Next Topic: Problem in Connect By
Goto Forum:
  


Current Time: Fri Dec 02 12:27:16 CST 2016

Total time taken to generate the page: 0.09729 seconds