Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:09:37+00:00 2026-05-16T16:09:37+00:00

If i use a ForeignKey in SQL, does it always have to use all

  • 0

If i use a ForeignKey in SQL, does it always have to use all the column of the table I reference from?

So eg.

Table1

subjectID
firstname
surname
email

Table2:

car
books
Foreignkey(SubjectID)

Can i only use one column as Foreignkey, or do I always have to get all the columns?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:09:37+00:00Added an answer on May 16, 2026 at 4:09 pm

    A foreign key has to reference a unique key, usually the primary key.

    So, if the parent table has a single column in its primary key that is the only column you need to use in the foreign key. If the parent table has a compound primary key (i.e. several columns) then you need all of those columns in the child table.

    This is one reason why people tend to avoid using compound primary keys in favour of surrogate keys and unique constraints.

    Here is a worked example (using Oracle but it works the same across all flavours of RDBMS). First we create a parent table with a single column primary key and reference it from the child table.

    SQL> create table t1 (id number not null, seqno number not null)
      2  /
    
    Table created.
    
    SQL> alter table t1 add constraint t1_pk primary key (id)
      2  /
    
    Table altered.
    
    SQL> create table t2 (id number not null, t1_id number not null, whatever varchar2(10) )
      2  /
    
    Table created.
    
    SQL> alter table t2 add constraint t2_t1_fk foreign key (t1_id)
      2             references t1 (id)
      3  /
    
    Table altered.
    
    SQL>
    

    Simple enough. But if we drop those keys and give T1 a compound primary keys things fall apart …

    SQL> alter table t2 drop constraint  t2_t1_fk
      2  /
    
    Table altered.
    
    SQL> alter table t1 drop constraint  t1_pk
      2  /
    
    Table altered.
    
    
    SQL> alter table t1 add constraint t1_pk primary key (id, seqno)
      2  /
    
    Table altered.
    
    SQL> alter table t2 add constraint t2_t1_fk foreign key (t1_id)
      2             references t1 (id)
      3  /
               references t1 (id)
                              *
    ERROR at line 2:
    ORA-02270: no matching unique or primary key for this column-list
    
    
    SQL> 
    

    We need to add a matching second column to the child table and include it in the foreign key definition:

    SQL> alter table t2 add t1_seqno number
      2  /
    
    Table altered.
    
    SQL> alter table t2 add constraint t2_t1_fk foreign key (t1_id, t1_seqno)
      2             references t1 (id, seqno)
      3  /
    
    Table altered.
    
    SQL>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While trying to use LINQ to SQL I encountered several problems. I have table
I have an SqlAlchemy table like so: table = sql.Table('treeItems', META, sql.Column('id', sql.Integer(), primary_key=True),
Much of my sql code is generated (from POD). Now i have issue where
I have table Animal. I want to return everything from this table + one
I have some things I need to use custom SQL for. Just to start,
I have table inheritance in my DB (where descendants reference base with foreign key)
Is it possible to use TastyPie to update a ForeignKey field to None ?
What is different between models.ForeignKey(Modelname, unique=True) and models.OneToOneField in Django? Where should I use
use Text::Table; my $tb = Text::Table->new(Planet,Radius\nkm,Density\ng/cm^3); $tb->load( [ Mercury,2360,3.7], [ Mercury,2360,3.7], [ Mercury,2360,3.7], );
I have a table (TableA) where the primary key exists as a foreign key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.