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

  • SEARCH
  • Home
  • 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 1097233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:24:07+00:00 2026-05-17T00:24:07+00:00

Can the same column have primary key & foreign key constraint to another column?

  • 0

Can the same column have primary key & foreign key constraint to another column?

Table1: ID - Primary column, foreign key constraint for Table2 ID
Table2: ID - Primary column, Name 

Will this be an issue if i try to delete table1 data?

Delete from table1 where ID=1000;

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-17T00:24:08+00:00Added an answer on May 17, 2026 at 12:24 am

    There should be no problem with that. Consider the following example:

    CREATE TABLE table2 (
       id int PRIMARY KEY,
       name varchar(20)
    ) ENGINE=INNODB;
    
    CREATE TABLE table1 (
       id int PRIMARY KEY, 
       t2_id int, 
       FOREIGN KEY (t2_id) REFERENCES table2 (id)
    ) ENGINE=INNODB;
    
    INSERT INTO table2 VALUES (1, 'First Row');
    INSERT INTO table2 VALUES (2, 'Second Row');
    
    INSERT INTO table1 VALUES (1, 1);
    INSERT INTO table1 VALUES (2, 1);
    INSERT INTO table1 VALUES (3, 1);
    INSERT INTO table1 VALUES (4, 2);
    

    The tables now contain:

    SELECT * FROM table1;
    +----+-------+
    | id | t2_id |
    +----+-------+
    |  1 |     1 |
    |  2 |     1 |
    |  3 |     1 |
    |  4 |     2 |
    +----+-------+
    4 rows in set (0.00 sec)
    
    SELECT * FROM table2;
    +----+------------+
    | id | name       |
    +----+------------+
    |  1 | First Row  |
    |  2 | Second Row |
    +----+------------+
    2 rows in set (0.00 sec)
    

    Now we can successfully delete rows like this:

    DELETE FROM table1 WHERE id = 1;
    Query OK, 1 row affected (0.00 sec)
    
    DELETE FROM table1 WHERE t2_id = 2;
    Query OK, 1 row affected (0.00 sec)
    

    However we won’t be able to delete the following:

    DELETE FROM table2 WHERE id = 1;
    ERROR 1451 (23000): A foreign key constraint fails
    

    If we had defined the foreign key on table1 with the CASCADE option, we would have been able to delete the parent, and all the children would get deleted automatically:

    CREATE TABLE table2 (
       id int PRIMARY KEY,
       name varchar(20)
    ) ENGINE=INNODB;
    
    CREATE TABLE table1 (
       id int PRIMARY KEY, 
       t2_id int, 
       FOREIGN KEY (t2_id) REFERENCES table2 (id) ON DELETE CASCADE
    ) ENGINE=INNODB;
    
    INSERT INTO table2 VALUES (1, 'First Row');
    INSERT INTO table2 VALUES (2, 'Second Row');
    
    INSERT INTO table1 VALUES (1, 1);
    INSERT INTO table1 VALUES (2, 1);
    INSERT INTO table1 VALUES (3, 1);
    INSERT INTO table1 VALUES (4, 2);
    

    If we were to repeat the previously failed DELETE, the children rows in table1 will be deleted as well as the parent row in table2:

    DELETE FROM table2 WHERE id = 1;
    Query OK, 1 row affected (0.00 sec)
    
    SELECT * FROM table1;
    +----+-------+
    | id | t2_id |
    +----+-------+
    |  4 |     2 |
    +----+-------+
    1 row in set (0.00 sec)
    
    SELECT * FROM table2;
    +----+------------+
    | id | name       |
    +----+------------+
    |  2 | Second Row |
    +----+------------+
    1 row in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table, let's call it Users . This table has primary key
I have two data tables which have a primary key column in common but
I have a table with a clustered primary key index on a uniqueidentifier column.
I have read many posts about how to specify the foreign key name in
if i have a table like this tbl_attributes --------------- id_attrib (auto incremented, primary key)
I have a two column table with a primary key (int) and a unique
I have a 4 column table, the primary key is a composite of id,
I have create this little sample Table Person. The Id is a primary key
I have two columns say Main and Sub . (they can be of same
How can I create a completely new column with the same value for each

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.