Home » SQL & PL/SQL » SQL & PL/SQL » Foreign key
Foreign key [message #681814] Wed, 02 September 2020 04:54 Go to next message
ttornado
Messages: 5
Registered: September 2020
Junior Member
Hi,

I have a doubt: having to create two tables
TABLE1 (A,B,C) AND TABLE2(E,F,G,H), is it possible to define for the TABLE2 (two columns G,H of table2 having for foreign key the same column C of talbe1)?
CREATE TABLE TABLE2
(
E ...,
F ...,
G ...INT,
H ... INT,
FOREIGN KEY (G, H)
REFERENCES TABLE1 (C)
);

Thanks a lot

TT
Re: Foreign key [message #681815 is a reply to message #681814] Wed, 02 September 2020 05:16 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.
Please read the OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Indent the code, use code tags and align the columns in result.

Also always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.

Yes, it is possible.
Database SQL Reference

Re: Foreign key [message #681816 is a reply to message #681815] Wed, 02 September 2020 05:31 Go to previous messageGo to next message
ttornado
Messages: 5
Registered: September 2020
Junior Member
Michel Cadot wrote on Wed, 02 September 2020 05:16

Welcome to the forum.
Please read the OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Indent the code, use code tags and align the columns in result.

Also always post your Oracle version, with 4 decimals (query v$version), as often solution depends on it.

Yes, it is possible.
Database SQL Reference

Hi Michel Cadot,
I'm sorry! I'll follow your directions for my next post.
Thank you for your reply
Re: Foreign key [message #681817 is a reply to message #681815] Wed, 02 September 2020 05:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

For instance:
CREATE TABLE TABLE2
(
  E ...,
  F ...,
  G ...,
  H ...,
  FOREIGN KEY (G) REFERENCES TABLE1 (C),
  FOREIGN KEY (H) REFERENCES TABLE1 (C)
);

[Updated on: Wed, 02 September 2020 05:49]

Report message to a moderator

Re: Foreign key [message #681818 is a reply to message #681817] Wed, 02 September 2020 05:53 Go to previous messageGo to next message
ttornado
Messages: 5
Registered: September 2020
Junior Member
Michel Cadot wrote on Wed, 02 September 2020 05:48

For instance:
CREATE TABLE TABLE2
(
  E ...,
  F ...,
  G ...,
  H ...,
  FOREIGN KEY (G) REFERENCES TABLE1 (C),
  FOREIGN KEY (H) REFERENCES TABLE1 (C)
);
Very precious. thank you so much.
Re: Foreign key [message #681821 is a reply to message #681814] Wed, 02 September 2020 07:33 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
You ask "is it possible"?

What would be the cost of trying it for yourself? Never be afraid to test. You'll learn more, learn better, and quite possibly get your answer faster than waiting on a forum participant that lives 12 time zones away from you.
Re: Foreign key [message #681826 is a reply to message #681821] Wed, 02 September 2020 13:42 Go to previous messageGo to next message
ttornado
Messages: 5
Registered: September 2020
Junior Member
EdStevens wrote on Wed, 02 September 2020 07:33
You ask "is it possible"?

What would be the cost of trying it for yourself? Never be afraid to test. You'll learn more, learn better, and quite possibly get your answer faster than waiting on a forum participant that lives 12 time zones away from you.
Thank you so much.
Re: Foreign key [message #681832 is a reply to message #681826] Wed, 02 September 2020 15:31 Go to previous messageGo to next message
Littlefoot
Messages: 21806
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Though, ttornado & Michel share the same time zone. Me too. You, Ed, don't. Are you feeling lonely? /forum/fa/917/0/
Re: Foreign key [message #681838 is a reply to message #681814] Thu, 03 September 2020 11:15 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
ttornado wrote on Wed, 02 September 2020 04:54
Hi,

I have a doubt: having to create two tables
TABLE1 (A,B,C) AND TABLE2(E,F,G,H), is it possible to define for the TABLE2 (two columns G,H of table2 having for foreign key the same column C of talbe1)?
CREATE TABLE TABLE2
(
E ...,
F ...,
G ...INT,
H ... INT,
FOREIGN KEY (G, H)
REFERENCES TABLE1 (C)
);

Thanks a lot

TT
Is it possible?
What happened when you tried it for yourself?
Re: Foreign key [message #681843 is a reply to message #681838] Thu, 03 September 2020 13:18 Go to previous messageGo to next message
ttornado
Messages: 5
Registered: September 2020
Junior Member
EdStevens wrote on Thu, 03 September 2020 18:15
ttornado wrote on Wed, 02 September 2020 04:54
Hi,

I have a doubt: having to create two tables
TABLE1 (A,B,C) AND TABLE2(E,F,G,H), is it possible to define for the TABLE2 (two columns G,H of table2 having for foreign key the same column C of talbe1)?
CREATE TABLE TABLE2
(
E ...,
F ...,
G ...INT,
H ... INT,
FOREIGN KEY (G, H)
REFERENCES TABLE1 (C)
);

Thanks a lot

TT
Is it possible?
What happened when you tried it for yourself?
Hi,
I tried it and got an error. But trying with
CONSTRAINT FK_TAB2_TAB1_G FOREIGN KEY(G) REFERENCES TABLE1 (C),
CONSTRAINT FK_TAB2_TAB1_H FOREIGN KEY(H) REFERENCES TABLE1 (C)
is OK
Thank you

TT
Re: Foreign key [message #681867 is a reply to message #681832] Fri, 04 September 2020 12:56 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Littlefoot wrote on Wed, 02 September 2020 15:31
Though, ttornado & Michel share the same time zone. Me too. You, Ed, don't. Are you feeling lonely? /foru/forum/fa/917/0/
"Social distancing"!

Smile
Previous Topic: to_timestamp_tz
Next Topic: outer query returning more than one row
Goto Forum:
  


Current Time: Thu Mar 28 16:39:15 CDT 2024