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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:55:41+00:00 2026-05-27T16:55:41+00:00

I am trying to create one to many relation using one table. Is this

  • 0

I am trying to create one to many relation using one table.
Is this possible?

create table user(id int primary key auto_increment not null,                                                                                                                      
created_by int default null                                                                                                                                                        
)ENGINE=INNODB;                                                                                                                                                                    

alter table user add foreign key (created_by) references user(id) ON DELETE SET NULL ON UPDATE CASCADE;                                                                            

insert into user (id) VALUES(1);                                                                                                                                                   
insert into user (id, created_by) VALUES (2,1);   

Now when I delete user with id=1 the value of created_by automaticaly change to NULL as I expected.

But when I change id of the user with id=1 I get this error

mysql> update user set id=2 where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`jrt`.`user`, CONSTRAINT `user_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `user` (`id`) ON DELETE SET NULL ON UPDATE CASCADE)

How can I fix this? Affter this update I want the column created_by to be cascade changed.

  • 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-27T16:55:41+00:00Added an answer on May 27, 2026 at 4:55 pm

    Keys aren’t meant to be changed, they are meant to be ‘identifying’. So user with id 2 is logically a different user with user with id 1. The moment you create a unique user, it’s primary key should be the same for the lifetime of that user.

    So this is really a design issue. You need to question why you want to change the id for a particular user. It might be that what you want is both a fixed identifying key AND another identifier (which isn’t the key and exists solely on the user table) which you can change.

    [Updated with more information]
    Here’s a resource (there are many available online) on the fundamentals of relational database design. http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx

    The relevant section is “Tables, Uniqueness and Keys”.

    Fabian Pascal, in his book SQL and Relational Basics, notes that the
    decision should be based upon the principles of minimality (choose the
    fewest columns necessary), stability (choose a key that seldom
    changes), and simplicity/familiarity (choose a key that is both simple
    and familiar to users).

    Personally, I would go further on stability; try to choose a key that never changes. For example, “email” would be a bad choice of key for a user as user’s can change their email. If you choose a key, such as an internally generated number or perhaps a unique identifier from the personnel system, then you don’t ever have to worry about it changing and migrating this change to other tables.

    Note: there may be cases where as a “one off” you need to change a primary key. This is best done with a few manual SQL statements (delete the first user and create an identical second user with a different key). It shouldn’t be part of the database design though, which the automated nature of a cascaded update implies.

    SEE ALSO: When to use "ON UPDATE CASCADE"

    ALSO NOTE: http://forums.mysql.com/read.php?136,391782,391782

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

Sidebar

Related Questions

Possible Duplicate: MySQL Relationships I am trying to create a one to many relationship
I'm using Entity Framework 4.1 and trying to create a one-to-many relationship. In my
i am trying to create a Radio button using Cakephp like the one the
I'm trying to create a table with Listview and one of the fields I'm
this one is really easy. I'm trying to create a Regular Expression that will
I'm trying to create some sort of many-to-one association. The basic premise is a
I'm trying to create a discriminator column. This column would hold one of the
Using SQLAlchemy , I have a one to many relation with two tables -
I am trying to create a program where that only one user can be
This is what I'm trying to do: I have 2 tables... CREATE TABLE `parent`

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.