Mysql 自动递增主键并同时更新其在其他表中的引用

Mysql 自动递增主键并同时更新其在其他表中的引用,mysql,sql,Mysql,Sql,我有一个主学生表和多个其他表,其中包含学生id引用。我需要更改主学生表中的学生id(主键),方法是 ALTER TABLE `student` AUTO_INCREMENT = 72805; 并替换所有表(包括主学生表)中以72805开头的所有学生id引用。有没有办法做到这一点 学生表 student_id student_name 1 Sarah 2 John 3 Peter class_id s

我有一个主学生表和多个其他表,其中包含学生id引用。我需要更改主学生表中的
学生id
(主键),方法是

ALTER TABLE `student` AUTO_INCREMENT = 72805;
并替换所有表(包括主学生表)中以
72805
开头的所有学生id引用。有没有办法做到这一点

学生表

student_id   student_name
    1           Sarah
    2           John
    3           Peter
class_id    student_id
        340         2
        432         1
        127         3
boook_id    student_id
    105         1
    106         1
    205         2
    207         2
    217         3
课程表

student_id   student_name
    1           Sarah
    2           John
    3           Peter
class_id    student_id
        340         2
        432         1
        127         3
boook_id    student_id
    105         1
    106         1
    205         2
    207         2
    217         3
活动

activity_id     student_id
    105             1
    106             1
    205             2
    207             2
    217             3
图书表

student_id   student_name
    1           Sarah
    2           John
    3           Peter
class_id    student_id
        340         2
        432         1
        127         3
boook_id    student_id
    105         1
    106         1
    205         2
    207         2
    217         3

您需要删除类、活动和书籍上的外键约束,并使用on UPDATE CASCADE子句重新创建它们

SHOW CREATE TABLE class;
SHOW CREATE TABLE activity;
SHOW CREATE TABLE books;
这些将显示外键约束的名称。您需要在
学生id
上输入外键约束的名称

ALTER TABLE class DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`) ON UPDATE CASCADE;
ALTER TABLE activity DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`) ON UPDATE CASCADE;
ALTER TABLE books DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`) ON UPDATE CASCADE;
这样做会告诉MySQL,如果更新student表上的student_id以更新引用表中的值。取决于您的业务逻辑,取决于您是否保留on UPDATE CASCADE子句。我从未亲自使用过它,因为你很少更新主键。 如果没有外键,那么应该删除drop子句并保留add子句

该结构现在可以更新您的学生id,并将其级联到其他表中

UPDATE student SET student_id = student_id + 72804; -- Minus 1 because the values already in here start at 1, not 0.
您不希望将auto_increment设置为72805,因为您希望第一条记录从这里开始,现在有一行占用该值。 此查询将获得您现在应该将自动增量值设置为的值

SELECT MAX(student_id) + 1 FROM student;
现在运行ALTERTABLE语句并将auto_增量设置为从上述查询中收到的值

ALTER TABLE student AUTO_INCREMENT = [Value you receive from the SELECT MAX()].
完成后,我将删除并重新创建外键,而无需更新级联

ALTER TABLE class DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`);
ALTER TABLE activity DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`);
ALTER TABLE books DROP FOREIGN KEY [enter the foreign key name here], ADD CONSTRAINT [enter foreign key name here] FOREIGN KEY (`student_id`) REFERENCES student (`student_id`);

这些键是否在更新级联时使用了
!非常感谢。