如何实现非主键自增? - SQL服务器
CREATE TABLE SupplierQuote
(
supplierQuoteID int identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY,
PONumber int identity (9553,20) NOT NULL
.
.
.
CONSTRAINT ponumber_uq UNIQUE(PONumber)
);
上面的 ddl 会产生错误:
消息 2744,级别 16,状态 2,第 1 行 指定多个标识列 对于表“SupplierQuote”。只有一个 每个表允许有标识列。
我该如何解决它?我希望 PONumber 自动递增。
CREATE TABLE SupplierQuote
(
supplierQuoteID int identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY,
PONumber int identity (9553,20) NOT NULL
.
.
.
CONSTRAINT ponumber_uq UNIQUE(PONumber)
);
The above ddl produces an error:
Msg 2744, Level 16, State 2, Line 1
Multiple identity columns specified
for table 'SupplierQuote'. Only one
identity column per table is allowed.
How can i solve it? I want PONumber to be auto-incremented.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果在插入行时生成SupplierQuoteId 和PONumber,则两个“身份”列将按顺序分配(3504 与9553 匹配,3506 与9573 匹配,3508 与9593 匹配,等等)。如果这个假设成立,那么您大概可以将 PONumber 设置为计算列,如下所示:
我将 sellerQuoteId 设置为 NOT NULL,这确保 PONumber 也将为 NOT NULL。同样,您不再需要 PONumber 的唯一约束,因为它始终是唯一的。 (如果您需要提高性能,可以在计算列上构建索引。)
If SupplierQuoteId and PONumber are generated when a row is inserted, then the two "identity" columns would be assigned in lockstep (3504 goes with 9553, 3506 goes with 9573, 3508 goes with 9593, etc.). If this assumption is true, then you presumably could make PONumber a calculated column, like so:
I made supplierQuoteId NOT NULL, which ensures that PONumber will also be NOT NULL. Similarly, you no longer need the unique constraint on PONumber, as it will always be unique. (It is possible to build indexes on calculated columns, if you need one for performance.)
每个表不能有多个标识列。我认为最好的选择是将 PO 数据提取到一个单独的表中,然后将两者与 FK 列关联起来。
You can't have more than one identity column per table. I think your best bet would be to pull the PO data into a separate table, then relate the two with a FK column.
你无法解决你的问题 - 每个表只能有一个
IDENTITY
列。没有办法解决这个问题,抱歉。唯一的“hackish”解决方案是拥有一个单独的表,只不过拥有一个 INT IDENTITY 字段,并在插入时从该辅助表中获取最新值到您的实体中(例如使用触发器)。不是很漂亮,但它可能对你有用。
You can't solve you - you can only have a single
IDENTITY
column per table. No way around that, sorry.The only "hackish" solution would be to have a separate table for nothing more than having an INT IDENTITY field, and grabbing the newest value from that helper table into your entity upon insertion (e.g. with a trigger). Not very pretty, but it might work for you.
如果每个供应商报价只有一个 PO id,那么为什么不简单地使用供应商报价 id 作为 PO id?
如果可以有多个,则必须有一个带有外键约束的单独表。您当然可以使用级联删除从该表中删除,但是如果您删除太多记录(导致锁定),这可能会很危险,或者就我个人而言,如果已创建采购订单号,我不想删除供应商报价,因为这意味着所引用的商品实际上已购买。您不想销毁实际购买的物品的记录。由于每个报价可能有多个 POS(我收到了六件商品的报价,首先购买了其中三件,然后下周购买了另外两件),并且由于您可能希望存储有关采购订单的特定信息,因此我推荐一个单独的表。从长远来看,做任何其他事情都会给你带来问题。
If there is only one PO id per supplier quote, then why not simply use the supplier quote id as the PO id?
If there can be more than one, you must have a sepapate table with a foreign key constraint. You can of course use cascade delete to delete from this table but this can be dangerous if you delete too many records (causing lockups) or personally I wouldn't want to delete a supplier quote if a PO number has been created as that means the item quoted was actually bought. You do not want to ever destroy records of things that were actually purchased. Since you will likely have multiple POS (I got a quote on six things and first bought three of them, then bought two others the next week) per quote and since it is likely you will want to store specific information about the purchase order, I recommend a separate table. To do anything else is going to cause you problems in the long run.
我想我会使用触发器来填充“第二身份”。
I think I'd use a trigger to fill the "second identity".
规避非标识列中的自动增量。(MS SQL)但我认为这不是最佳实践!只是一个快速修复解决方案。
Circumvent auto increment in non identity column.(MS SQL) I don't think this is the best practice though! JUst a quick fix solution.