如何在另一个表上的CREATE table或ALTER table上设置auto_INCREMENT?
我发现了这个问题,但没有解决我的问题:
How to Reset an MySQL AutoIncrement using a MAX value from another table?
我也试过这个:
CREATE table IF NOT EXISTS `table_name` ( `ID` mediumint(6) unsigned NOT NulL auto_INCREMENT,`columnOne` tinyint(1) NOT NulL,`columnTwo` int(12) NOT NulL,PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAulT CHARSET=utf8 auto_INCREMENT=(SELECT `auto_INCREMENT` FROM `informatION_SCHEMA`.`tableS` WHERE `table_SCHEMA` = 'database_name' AND `table_name` = 'another_table_name');
这个:
ALTER table `table_name` auto_INCREMENT=(SELECT `auto_INCREMENT` FROM `informatION_SCHEMA`.`tableS` WHERE `table_SCHEMA` = 'database_name' AND `table_name` = 'another_table_name');
这个:
CREATE table IF NOT EXISTS `table_name` ( `ID` mediumint(6) unsigned NOT NulL auto_INCREMENT,PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAulT CHARSET=utf8 auto_INCREMENT=(SELECT (MAX(`ID`)+1) FROM `another_table_name`);
还有这个:
ALTER table `table_name` auto_INCREMENT=(SELECT (MAX(`ID`)+1) FROM `another_table_name`);
最佳答案此代码将为您创建过程:CREATE PROCEDURE `tbl_wth_ai`(IN `ai_to_start` INT)BEGINSET @s=CONCAT('CREATE table IF NOT EXISTS `table_name` ( `ID` mediumint(6) unsigned NOT NulL auto_INCREMENT,PRIMARY KEY (`ID`)) ENGINE=MyISAM DEFAulT CHARSET=utf8 auto_INCREMENT = ',`ai_to_start`); PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;END;
然后你可以打电话给CALL tbl_wth_ai(2);在括号内传递参数.
例如:
CALL tbl_wth_ai((SELECT ID FROM `ttest` WHERE c1='b'));
@H_403_60@ 总结 以上是内存溢出为你收集整理的mysql – 如何从另一个表设置AUTO_INCREMENT全部内容,希望文章能够帮你解决mysql – 如何从另一个表设置AUTO_INCREMENT所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)