MySQL Forums
Forum List  »  Newbie

Re: Reseting Auto Increment for Indexes?
Posted by: Sébastien F.
Date: May 28, 2022 02:54AM

You should use FOREIGN KEY with CASCADE capabilities:

CREATE TABLE parents (
    id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    ...
);

CREATE TABLE children (
    id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    ...
    parent_id BIGINT UNSIGNED,
    CONSTRAINT FOREIGN KEY (parent_id) REFERENCES parents (id) ON UPDATE CASCADE ON DELETE RESTRICT
)

So, when you update parents.id, children.parent_id will be updated automatically.

Then you can do following to specify next parents.id:

ALTER TABLE parents AUTO_INCREMENT = {{ NEXT_ID }}

Options: ReplyQuote


Subject
Written By
Posted
Re: Reseting Auto Increment for Indexes?
May 28, 2022 02:54AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.