如何实现非主键自增? - SQL服务器

发布于 2024-08-28 03:25:50 字数 388 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

禾厶谷欠 2024-09-04 03:25:50

如果在插入行时生成SupplierQuoteId 和PONumber,则两个“身份”列将按顺序分配(3504 与9553 匹配,3506 与9573 匹配,3508 与9593 匹配,等等)。如果这个假设成立,那么您大概可以将 PONumber 设置为计算列,如下所示:

CREATE TABLE SupplierQuote 
( 
supplierQuoteID int NOT NULL identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY, 
PONumber AS (10 * supplierQuoteID - 25487)
. 
. 
. 
); 

我将 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:

CREATE TABLE SupplierQuote 
( 
supplierQuoteID int NOT NULL identity (3504,2) CONSTRAINT supquoteid_pk PRIMARY KEY, 
PONumber AS (10 * supplierQuoteID - 25487)
. 
. 
. 
); 

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.)

爱人如己 2024-09-04 03:25:50

每个表不能有多个标识列。我认为最好的选择是将 PO 数据提取到一个单独的表中,然后将两者与 FK 列关联起来。

SupplierQuote
-------------
supplierQuoteID (PK/identity)
purchaseOrderID (FK to PurchaseOrder.purchaseOrderID)
otherColumn1

PurchaseOrder
-------------
purchaseOrderID (PK/identity)
otherColumn1

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.

SupplierQuote
-------------
supplierQuoteID (PK/identity)
purchaseOrderID (FK to PurchaseOrder.purchaseOrderID)
otherColumn1

PurchaseOrder
-------------
purchaseOrderID (PK/identity)
otherColumn1
月亮是我掰弯的 2024-09-04 03:25:50

你无法解决你的问题 - 每个表只能有一个 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.

萌逼全场 2024-09-04 03:25:50

如果每个供应商报价只有一个 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.

动次打次papapa 2024-09-04 03:25:50

我想我会使用触发器来填充“第二身份”。

I think I'd use a trigger to fill the "second identity".

思念绕指尖 2024-09-04 03:25:50

规避非标识列中的自动增量。(MS SQL)但我认为这不是最佳实践!只是一个快速修复解决方案。

    INSERT INTO [dbo].[Employee]
           ([EmpID]
           ,[Name]
           ,[Salary]
           ,[Address]
           ,[datecoded])
     VALUES
           ( (select top 1 EmpID from dbo.Employee order by EmpID desc) + 1
           , 'name_value'
           , 123456
           ,'address_value'
           , GETDATE())

Circumvent auto increment in non identity column.(MS SQL) I don't think this is the best practice though! JUst a quick fix solution.

    INSERT INTO [dbo].[Employee]
           ([EmpID]
           ,[Name]
           ,[Salary]
           ,[Address]
           ,[datecoded])
     VALUES
           ( (select top 1 EmpID from dbo.Employee order by EmpID desc) + 1
           , 'name_value'
           , 123456
           ,'address_value'
           , GETDATE())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文