简体   繁体   English

如何使用Doctrine更改mysql表的auto_increment

[英]How to alter the auto_increment of a mysql-table using Doctrine

the question says it all it think :-) What is the best way to execute a query such as: 该问题说明了所有问题:-)执行查询的最佳方法是什么,例如:

ALTER TABLE tablename AUTO_INCREMENT = 1;

using Doctrine? 使用主义?

You'll have to just execute the query as normal from within the PHP I'm afraid, as this is not something that Doctrine supports. 恐怕您只需要在PHP内正常执行查询,因为Doctrine不支持该查询。

Note that if you run ALTER TABLE tablename AUTO_INCREMENT = 1; 请注意,如果您运行ALTER TABLE tablename AUTO_INCREMENT = 1; then the next value will be the highest currently in the table + 1. You aren't explicitly setting the value to 1 if there are already records in the table. 那么下一个值将是表中当前的最高值+1。如果表中已经有记录,则无需将其显式设置为1。

Are you trying to manipulate the auto-increment value of your table? 您是否要操纵表的自动增量值? If you want to do this, you COULD do 如果您想这样做,可以

SET @@auto_increment_increment=10;

Keep in mind that this is a system wide variable that will affect ALL tables. 请记住,这是一个系统范围的变量,将影响所有表。

Generally, I would discourage you to manipulate the way that the primary key (I just assume thsi) is generated. 通常,我不鼓励您操纵主键(我只是假设)的生成方式。

If you need to give a custom value, say i * 10, you could either write a trigger/function that does the job or a more easily, you can create a view provides you with the last inserted id (say 150) and adds your value (eg 10) to it so you can get 160. 如果您需要给定一个自定义值,例如i * 10,则可以编写执行此任务的触发器/函数,或者编写一个更简单的触发器/函数,可以创建一个为您提供最后插入的ID(例如150)并添加您的ID的视图值(例如10),则可以得到160。

Doctrine itself does not support this (as far as I know) and that's good in my opinion. 据我所知,教义本身并不支持这一点,我认为这很好。 If you need to set different increments to the primary keys, PostgreSWL and Oracale for example are able to do so since they use sequences for the generation of the keys that can be manipulated by the dev 如果您需要为主键设置不同的增量,例如PostgreSWL和Oracale可以这样做,因为它们使用序列来生成可以由开发人员操纵的键

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

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