在我的插入请求中增加我的ID [英] Increment my id in my insert request
问题描述
我有一个带有一些行的表.
i have a table with some rows.
idClient,名称,地址,国家/地区...
idClient, name, adress,country,...
我想知道如何在我的sql请求中通过自动递增idClient到此表中进行插入吗? 谢谢.
i want to know how i can do an insert into this table with auto increment my idClient in my sql request..? Thx.
我想发出这样的请求
insert into Client values((select max(idClient),...)
推荐答案
ALTER TABLE Client CHANGE idClient
idClient INT AUTO_INCREMENT PRIMARY KEY;
然后,当您插入表中时,从插入中排除自动递增的主键列:
Then when you insert into the table, exclude the auto-incrementing primary key column from your insert:
INSERT INTO Client (name, address, country)
VALUES ('name', 'address', 'country')...;
将生成idClient的新值.
The new value of idClient will be generated.
如果应用程序有多个实例同时插入行,则这是安全地执行此操作的唯一方法.使用您描述的MAX(idClient)方法将不起作用,因为它受竞争条件的限制.
This is the only way to do this safely if there are multiple instances of an application inserting rows at once. Using the MAX(idClient) method you describe will not work, because it's subject to race conditions.
这篇关于在我的插入请求中增加我的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!