SQL Server 表可以有两个标识列吗?

发布于 2024-07-10 02:57:41 字数 84 浏览 10 评论 0原文

我需要将一列作为主键,另一列作为自动递增订单号字段。 这可能吗?

编辑:我想我只会使用复合数作为订单号。 不管怎么说,多谢拉。

I need to have one column as the primary key and another to auto increment an order number field. Is this possible?

EDIT: I think I'll just use a composite number as the order number. Thanks anyways.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

败给现实 2024-07-17 02:57:41
CREATE TABLE [dbo].[Foo](
    [FooId] [int] IDENTITY(1,1) NOT NULL,
    [BarId] [int] IDENTITY(1,1) NOT NULL
)

返回

Msg 2744, Level 16, State 2, Line 1
Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed.

所以,不,你不能有两个标识列。 您当然可以使主键不自动递增(身份)。

编辑: msdn:CREATE TABLE (Transact-SQL)创建表 (SQL Server 2000)

每个表只能创建一个标识列。

CREATE TABLE [dbo].[Foo](
    [FooId] [int] IDENTITY(1,1) NOT NULL,
    [BarId] [int] IDENTITY(1,1) NOT NULL
)

returns

Msg 2744, Level 16, State 2, Line 1
Multiple identity columns specified for table 'Foo'. Only one identity column per table is allowed.

So, no, you can't have two identity columns. You can of course make the primary key not auto increment (identity).

Edit: msdn:CREATE TABLE (Transact-SQL) and CREATE TABLE (SQL Server 2000):

Only one identity column can be created per table.

水染的天色ゝ 2024-07-17 02:57:41

则可以使用具有默认值的第二列的序列

--Create the Test schema
CREATE SCHEMA Test ;
GO

-- Create a sequence
CREATE SEQUENCE Test.SORT_ID_seq
    START WITH 1
    INCREMENT BY 1 ;
GO

-- Create a table
CREATE TABLE Test.Foo
    (PK_ID int IDENTITY (1,1) PRIMARY KEY,
    SORT_ID int not null  DEFAULT (NEXT VALUE FOR Test.SORT_ID_seq));
GO

INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )

SELECT * FROM Test.Foo 

-- Cleanup
--DROP TABLE Test.Foo
--DROP SEQUENCE Test.SORT_ID_seq
--DROP SCHEMA Test

如果您使用 SQL Server 2012 http://technet.microsoft, 。 com/en-us/library/ff878058.aspx

You can use Sequence for second column with default value IF you use SQL Server 2012

--Create the Test schema
CREATE SCHEMA Test ;
GO

-- Create a sequence
CREATE SEQUENCE Test.SORT_ID_seq
    START WITH 1
    INCREMENT BY 1 ;
GO

-- Create a table
CREATE TABLE Test.Foo
    (PK_ID int IDENTITY (1,1) PRIMARY KEY,
    SORT_ID int not null  DEFAULT (NEXT VALUE FOR Test.SORT_ID_seq));
GO

INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )
INSERT INTO Test.Foo VALUES ( DEFAULT )

SELECT * FROM Test.Foo 

-- Cleanup
--DROP TABLE Test.Foo
--DROP SEQUENCE Test.SORT_ID_seq
--DROP SCHEMA Test

http://technet.microsoft.com/en-us/library/ff878058.aspx

笑叹一世浮沉 2024-07-17 02:57:41

添加一个标识列,然后添加一个计算列,其公式是标识列的名称

现在两者将同时递增

Add one identity column and then add a computed column whose formula is the name of the identity column

Now both will increment at the same time

感性 2024-07-17 02:57:41

不,不可能有多个身份列。

企业管理器甚至不允许您设置> 1 列作为标识。 当第二列被设置为标识时

还要注意,@@identity 返回打开连接的最后一个标识值,如果一个表可能有多个标识列,则该值将毫无意义。

No it is not possible to have more than one identity column.

The Enterprise Manager does not even allow you to set > 1 column as identity. When a second column is made identity

Also note that @@identity returns the last identity value for the open connection which would be meaningless if more than one identity column was possible for a table.

无声静候 2024-07-17 02:57:41
create table #tblStudent
(
    ID int primary key identity(1,1),
    Number UNIQUEIDENTIFIER DEFAULT NEWID(),
    Name nvarchar(50)
)

两个标识列是不可能的,但如果您接受使用唯一标识符列,那么此代码也可以完成相同的工作。 您还需要一个额外的列 - 名称列 - 用于插入值。

用法示例:

insert into #tblStudent(Name) values('Ali')

select * from #tblStudent

Ps: NewID() 函数创建 type 的唯一值唯一标识符。

create table #tblStudent
(
    ID int primary key identity(1,1),
    Number UNIQUEIDENTIFIER DEFAULT NEWID(),
    Name nvarchar(50)
)

Two identity column is not possible but if you accept to use a unique identifier column then this code does the same job as well. And also you need an extra column - Name column- for inserting values.

Example usage:

insert into #tblStudent(Name) values('Ali')

select * from #tblStudent

Ps: NewID() function creates a unique value of type uniqueidentifier.

暖心男生 2024-07-17 02:57:41

主键不需要是标识列。

您不能有两个 Identity 列。

你可以通过触发器得到接近你想要的东西......

The primary key doesn't need to be an identity column.

You can't have two Identity columns.

You could get something close to what you want with a trigger...

别理我 2024-07-17 02:57:41

在 sql server 中,不可能有多个列作为标识。

in sql server it's not possible to have more than one column as identity.

凯凯我们等你回来 2024-07-17 02:57:41

我刚刚创建了一个代码,允许您在同一个表上插入两个身份。 让我与您分享,以防有帮助:

create trigger UpdateSecondTableIdentity
On TableName For INSERT
as
update TableName
set SecondIdentityColumn = 1000000+@@IDENTITY
where ForstId = @@IDENTITY;

谢谢,

I've just created a code that will allow you inserting two identities on the same table. let me share it with you in case it helps:

create trigger UpdateSecondTableIdentity
On TableName For INSERT
as
update TableName
set SecondIdentityColumn = 1000000+@@IDENTITY
where ForstId = @@IDENTITY;

Thanks,

带刺的爱情 2024-07-17 02:57:41

在大多数关系数据库(例如 SQL Server、Oracle 或 MySQL)中,一张表通常只能有一个标识列。 标识列是在向表中添加新行时自动生成唯一值的列。

表中包含多个标识列会导致关于哪一列作为主键产生歧义。
但是,您可以通过使用其他方法来实现类似的功能,例如:

  1. 复合键:
CREATE TABLE Orders (
    OrderID INT IDENTITY(1,1),
    ProductID INT,
    Quantity INT,
    -- Other columns related to orders

    CONSTRAINT PK_Orders PRIMARY KEY (OrderID, ProductID)
);

In most relational databases like SQL Server, Oracle, or MySQL, a table can typically have only one identity column. An identity column is a column that automatically generates unique values when new rows are added to the table.

Having multiple identity columns within a table would create ambiguity regarding which column serves as the primary key.
However, you can achieve similar functionality by using other approaches, such as:

  1. Composite Keys:
CREATE TABLE Orders (
    OrderID INT IDENTITY(1,1),
    ProductID INT,
    Quantity INT,
    -- Other columns related to orders

    CONSTRAINT PK_Orders PRIMARY KEY (OrderID, ProductID)
);
无名指的心愿 2024-07-17 02:57:41

解决方法是创建一个递增计数器的 INSERT 触发器。

所以我有一张表,它有一个标识 col :applicationstatusid。 它也是主键。
我想自动增加另一个列:applicationnumber

所以这是我写的触发器。

 create trigger [applicationstatus_insert] on [ApplicationStatus] after insert as 
       update [Applicationstatus] 
       set [Applicationstatus].applicationnumber =(applicationstatusid+ 4000000) 
       from [Applicationstatus] 
       inner join inserted on [applicationstatus].applicationstatusid = inserted.applicationstatusid

A workaround would be to create an INSERT Trigger that increments a counter.

So I have a table that has one identity col : applicationstatusid. its also the primary key.
I want to auto increment another col: applicationnumber

So this is the trigger I write.

 create trigger [applicationstatus_insert] on [ApplicationStatus] after insert as 
       update [Applicationstatus] 
       set [Applicationstatus].applicationnumber =(applicationstatusid+ 4000000) 
       from [Applicationstatus] 
       inner join inserted on [applicationstatus].applicationstatusid = inserted.applicationstatusid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文