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 6003735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:09:49+00:00 2026-05-23T01:09:49+00:00

My question is two fold, firstly, is it possible to create a foreign key,in

  • 0

My question is two fold,

  1. firstly, is it possible to create a foreign key,in mysql, from a referencing table to a column in the referenced table that is not a candidate key ? I tried it using SQLYOG schema designer and it created it strangely. Just want to confirm with others out there, before assuming it is a bug maybe with sqlyog or mysql actually allows it.

example :

table1 columns :

ssn : primary key

name : non-candidate key (names can repeat)

table2 columns

id2 : primary key

name_referencing : foreign key to name in table1 (is this foreign key possible??)

2.If the above case is possible, what happens when ‘on delete cascade’ happens. That is, if there are same values (in various rows) of the referenced column, does the deletion of the child(in referencing) happen only on the deletion of the last value(of the repeated values) in the referenced table ?

  • 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-23T01:09:49+00:00Added an answer on May 23, 2026 at 1:09 am
    -- Create the tables
    (anthony@localhost) [test]> create table foo (a int primary key, b int not null, index(b)) engine=innodb;
    Query OK, 0 rows affected (0.33 sec)
    
    create table bar (b int not null, constraint b_exists foreign key (b) references foo(b) on delete cascade) engine=innodb;
    Query OK, 0 rows affected (0.40 sec)
    

    So, MySQL actually allows this situation. Weird. Oracle and PostgreSQL will not (both raise errors), and I don’t believe the SQL standard allows it (but haven’t checked, so could be wrong there). Let’s see how it handles it:

    -- Fill foo
    (anthony@localhost) [test]> insert into foo values (1,1);
    Query OK, 1 row affected (0.11 sec)
    
    (anthony@localhost) [test]> insert into foo values (2,1);
    Query OK, 1 row affected (0.07 sec)
    
    -- Check foreign key works:
    (anthony@localhost) [test]> insert into bar values (1);
    Query OK, 1 row affected (0.13 sec)
    
    (anthony@localhost) [test]> insert into bar values (2);
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`bar`, CONSTRAINT `b_exists` FOREIGN KEY (`b`) REFERENCES `foo` (`b`) ON DELETE CASCADE)
    
    -- Delete
    
    (anthony@localhost) [test]> delete from foo  where a = 1;
    Query OK, 1 row affected (0.09 sec)
    
    (anthony@localhost) [test]> select * from bar;
    Empty set (0.00 sec)
    

    So, MySQL deletes the row from the referencing table. At least in 5.1.49.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my question is about two possible ways to access data: (My question is about
Ok so this question is two fold. I am creating a bill reminder app
Two question on Bluetooth development on Android: Is there a way to enable Bluetooth
I have two question to put forward: I was very interested, even intrigued by
The question has two parts, one of which I already have the answer for.
I've been asking a question or two over the past few days of working
Question I have two compilers on my hardware C++ and C89 I'm thinking about
Two Part Question: What kind of actions does SQL Server process in RAM? Of
Two random question as I try to integerate Paypal IPN into my Codeigniter based
Background This question is in two parts. I have a one-way WCF operation hosted

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.