简体   繁体   English

MySQL表-截断还是不截断?

[英]MySQL table - to truncate or not to truncate?

I've got a table that exists in both my live (hosted) DB and my development (local) DB. 我的活动(托管)数据库和开发(本地)数据库中都存在一个表。 I want to get a bunch of records into the live table. 我想将一堆记录放入实时表中。

What I've been doing is as follows: 我一直在做如下:

  1. Export/Import a copy of the live table back to the dev DB using phpMyAdmin. 使用phpMyAdmin将活动表的副本导出/导入回开发数据库。
  2. Use a C batch file to load the new data into the dev table. 使用C批处理文件将新数据加载到dev表中。
  3. Export/Import a copy of the updated dev table back to the live DB, again using phpMyAdmin. 再次使用phpMyAdmin将更新的dev表的副本导出/导入回实时数据库。

This all works fine, as far as it goes. 就一切而言,这一切都很好。

The problem is this: if a record has been deleted in the live table, step 1 doesn't remove it from the dev table (even if you choose the "Replace table with file" option). 问题是这样的:如果在活动表中删除了一条记录,则步骤1不会将其从dev表中删除(即使您选择了“用文件替换表”选项)。 This record then gets recreated in the live table at step 3. 然后,在步骤3中在活动表中重新创建该记录。

My question: should I truncate the dev table (after backing up, of course) before I import at step 1? 我的问题:在步骤1导入之前,我应该截断dev表(当然是在备份之后)吗? Will my import set the auto-increment on the dev table to the same point as it is on the live table? 我的导入功能是否会将dev表上的自动增量设置为与实时表上的自动增量相同? Or am I about to stuff things up horribly? 还是我要使事情变得可怕?

Thanks. 谢谢。

EDIT: Here's the table. 编辑:这是桌子。 (Can't get it to format better; sorry.) (无法使其格式更好;抱歉。)

Column - Type - NULL - Default 列-类型-NULL-默认

cnum smallint(6) No Cnum smallint(6)否
unum smallint(6) No 1 unum smallint(6)否1
cat_subject smallint(2) No 0 cat_subject smallint(2)否0
cat_major smallint(2) No 0 cat_major smallint(2)否0
cat_minor smallint(2) No 0 cat_minor smallint(2)否0
cat_flavour char(1) Yes NULL cat_flavor char(1)是NULL
unmod varchar(255) No unmod varchar(255)否

Index: 指数:

Keyname Type Unique Packed Column Cardinality Collation Null Comment 关键字名称类型唯一填充列基数归类空注释

PRIMARY BTREE Yes No cnum 2214 A No 主BTREE是否cnum 2214 A否

Truncating a table in MySQL will reset the autonumber/identity column. 在MySQL中截断表格将重置自动编号/标识列。 Unless your script explicitly inserts the autonumber from live into dev you are going to have a major problem on your hands. 除非您的脚本明确将livenumber从live插入到dev中,否则您将面临一个重大问题。

If you can post some table structures and code I would be able to give you a better answer with some code. 如果您可以发布一些表结构和代码,我将可以通过一些代码为您提供更好的答案。

UPDATE: 更新:

Just to clarify a little about autonumbers and MySQL. 只是为了澄清一些有关自动编号和MySQL的信息。 Lets create a table as follows: 让我们创建一个表,如下所示:

CREATE TABLE `testcust` (
 `TestCustID` int(11) NOT NULL AUTO_INCREMENT,
  `Name` varchar(32) DEFAULT NULL,
 PRIMARY KEY (`TestCustID`)
) ENGINE=InnoDB;

If I inserted data with the following query: 如果我使用以下查询插入数据:

INSERT INTO `test`.`TestCust`
(`TestCustID`,
  `Name`
)
VALUES
(
  NULL,
 'Pieter'
);

And ran a select * on this table I would see something like this. 并在此表上运行select *,我会看到类似的内容。

TestCustID     Name
 '1',          'Pieter'

Since I did not specify the TestCustID MySQl will generate one. 由于我未指定TestCustID,因此MySQl将生成一个。 If I ran the following insert query: 如果我运行以下插入查询:

INSERT INTO `test`.`TestCust`
 (`TestCustID`,
  `Name`
 )
 VALUES
 (
     15,
     'Pieter'
 );

And then ran a select * on the table I would see 然后在表格上运行select *

TestCustID     Name
 '1'          'Pieter'
 '15'         'Pieter'

So if you truncated your table it will reset the TestCustID to 1(starting from scratch).If you explicitly specify the autonumber from your production/live system in your insert statements you will be in sync if you dont specify your autonumbers in the insert statements you will go out of sync. 因此,如果您对表进行了截断,则会将TestCustID重置为1(从头开始)。如果您在插入语句中从生产/活动系统中明确指定了自动编号,那么如果您未在插入语句中指定自动编号,则会同步您将不同步。

Hope that clarifies it a bit more. 希望能进一步澄清它。

After truncate You don't need to export/import that Primary Auto Increment column. 截断后,您不需要导出/导入“主要自动增量”列。 Just import rest of the columns. 只需导入其余的列。 It will manage auto increment column by itself automatically. 它会自动管理自动递增列。

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

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