简体   繁体   English

从备份创建新的MySql模式。SQL源文件会重置自动增量ID列吗?

[英]Create new MySql schema from Backup .SQL source file resets auto increment id columns?

I am developing a large database schema that I desire to make available to clients. 我正在开发一个大型database schema ,希望将其提供给客户。

I am new to MySql database administration. 我是MySql数据库管理的新手。 How do I restore a .sql file of the schema so that it also resets ALL auto incremental id columns back to 0 or Null again. 如何还原架构的.sql文件,以便也将所有自动增量ID列重新重置为0Null

I actually restored to a new schema name and inserted a test record and found the auto id columns were incrementing from where they left-off in the last schema name. 实际上,我恢复为新的架构名称并插入了一条测试记录,发现自动ID列从上一个架构名称中的剩余位置开始递增。

How do I address this in MySql database modeling? 如何在MySql数据库建模中解决此问题?

I want to always be able to 'spin-off' an exact copy of my current version data model for a paying customer. 我希望始终能够为付费客户“剥离”我当前版本数据模型的精确副本。

Thank you. 谢谢。

You can dump the schema only, without data this way: 您只能以这种方式转储模式,而没有数据:

$ mysqldump -d databasename > databasename-schema.sql

If you use InnoDB tables (which you should), the auto-increment value is always reset every time you restart the MySQL instance. 如果使用InnoDB表(应使用),则每次重新启动MySQL实例时,始终会重置自动增量值。 The value it is reset to in each table is equal to the max value currently stored in the table, plus 1. So if your tables are empty, the value will be 1. 在每个表中重置为的值等于表中当前存储的最大值加1。因此,如果表为空,则该值为1。

If you don't want to restart the MySQL instance after you restore, you can either strip the auto-increment clause in your CREATE TABLE statements before restoring: 如果您不想在还原后重新启动MySQL实例,则可以在还原之前去除CREATE TABLE语句中的auto-increment子句:

$ mysqldump -d databasename | sed -e 's/AUTO_INCREMENT=[0-9][0-9]*//' 
  > databasename-schema.sql

Or else you can ALTER TABLE to reset the auto-increment value after restoring. 否则,您可以在恢复后更改表以重置自动增量值。 This has to be done on a per-table basis, but you can query the list of tables that have auto-increment keys from the INFORMATION_SCHEMA: 这必须在每个表的基础上完成,但是您可以从INFORMATION_SCHEMA查询具有自动递增键的表的列表:

$ mysql -N -B -e "SELECT CONCAT('ALTER TABLE \`', table_schema, '\`.\`', 
  table_name, '\` AUTO_INCREMENT=1;') AS ddl FROM INFORMATION_SCHEMA.TABLES 
  WHERE table_schema = 'test' AND AUTO_INCREMENT IS NOT NULL" | mysql -v

您可以使用它来转储表结构,并且您将像之前定义的那样从头开始找到所有自动增量主键,它将不执行插入查询,并且将创建所有stored procedureviewstables

cat backup.sql | grep -v ^INSERT | mysql -u $USER -p

AUTO_INCREMENT is reset after AUTO_INCREMENT在重置后

TRUNCATE `table`

and when you run batch INSERT query afterwards (from your data backup), the AUTO_INCREMENT value will be automaticaly updated to proper value (inserted records+1). 当您随后(从数据备份)运行批处理INSERT查询时, AUTO_INCREMENT值将自动更新为正确的值(插入的记录+1)。

So you don't need to update AUTO_INCREMENT value manualy (which is rather a bad practice). 因此,您无需AUTO_INCREMENT更新AUTO_INCREMENT值(这是一种不好的做法)。

When you want to restore from backup more that one table that are constrained by foreign keys, you can disable FK checks before TRUNCATE+INSERT and enable it back when you're done. 如果要从备份中还原受外键约束的一个表以上,则可以在TRUNCATE + INSERT之前禁用FK检查,并在完成后将其重新启用。 Details can be found here: Truncate all tables (most of which have constraints). 可以在这里找到详细信息: 截断所有表(其中大多数都有约束)。 How to temporarily drop them 如何暂时放下它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM