如何自动增加表格中的列? [英] How do I auto-increment a column in my table?

查看:80
本文介绍了如何自动增加表格中的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Sql Server 2008在Visual Studio2010中使用产品实例表构建数据库,并且需要使ProductId列自动递增,但是我无法在列属性菜单中找到该属性.我正在使用c#和asp.net,如果相关的话.我已经看到了创建表并将列设置为自动增量的代码,但是由于这是我第一次编写代码,因此我不知道将代码放在何处.我知道创建新表的唯一方法是通过VS gui(如果有道理).

I'm building a database with a product instance table in Visual Studio2010 with Sql Server 2008, and I need to make the ProductId column autoincremented, but I cannot find the attribute in the column properties menu. I'm using c# and asp.net, if that is relevant. I've seen the code to create the table and set the column to autoincrement, but as this is my first go-round with coding, I don't know where to put the code. The only way I know to create a new table is through the VS gui, if that makes sense.

推荐答案

将身份"规范设置为是"

Set the Identity specification to yes

示例SQL:

CREATE TABLE [dbo].[HomePageImages](
    [RecordId] [int] IDENTITY(1,1) NOT NULL,
    [AlternateText] [varchar](100) NOT NULL,
    [ImageName] [varchar](50) NOT NULL,
    [NavigateUrl] [varchar](200) NOT NULL,
    [ImageUrl]  AS ('/content/homepageimages/'+[ImageName]),
    [DisplayFrom] [datetime] NULL,
    [DisplayTo] [datetime] NULL,
 CONSTRAINT [PK_HomePageImages] PRIMARY KEY CLUSTERED 
(
    [RecordId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

这篇关于如何自动增加表格中的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆