简体   繁体   English

如何自动增加表格中的列?

[英]How do I auto-increment a column in my table?

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. 我正在使用Sql Server 2008在Visual Studio2010中构建一个带有产品实例表的数据库,我需要自动增加ProductId列,但是我在列属性菜单中找不到该属性。 I'm using c# and asp.net, if that is relevant. 我正在使用c#和asp.net,如果这是相关的。 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. 我知道创建一个新表的唯一方法是通过VS gui,如果这是有道理的。

Set the Identity specification to yes 将Identity规范设置为yes

在此输入图像描述

Sample SQL: 示例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

In SQL server 08 you want to set the "Identity" property to 'Yes' and define it's initial value (defaults to 1) as well as its increment (also defaults to 1). 在SQL Server 08中,您希望将“Identity”属性设置为“Yes”并定义它的初始值(默认为1)及其增量(也默认为1)。

This will cause it to increment by 1 on every new record. 这将导致它在每个新记录上增加1。

you need to use the identity yes property in sql. 你需要在sql中使用标识yes属性。

It's in the property window at the bottom - sorry not running windows. 它位于底部的属性窗口中 - 抱歉没有运行窗口。

This will auto increment your id everytime a new record is inserted. 每次插入新记录时,这将自动增加您的ID。

To get the id value you can use sqls SCOPE_IDENTITY() function to retrieve the id created. 要获取id值,可以使用sqls SCOPE_IDENTITY()函数来检索创建的id。

In SSMS, find your table in the Object Explorer, right/click, and select Design. 在SSMS中,在对象资源管理器中找到您的表,右键单击,然后选择“设计”。 Select the column you want to autoincrement, and look at the Column Properties section underneath. 选择要自动增量的列,然后查看下面的“列属性”部分。 There should be an Identity Specification item with a [+] sign. 应该有一个带有[+]符号的标识规范项。 Expand it, choose "Yes" for "Is Identity", and if necessary, set the increment (sometimes you may want the new value to be more than one more than the last) and the starting value as the "seed" (1 is fine for an empty table, but if you have existing data, set the seed to be greater than the greatest record). 展开它,为“Is Identity”选择“Yes”,如有必要,设置增量(有时您可能希望新值比最后一个多一个),并将起始值设置为“种子”(1是适用于空表,但如果您有现有数据,请将种子设置为大于最大记录)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用自动增量主键在表中插入空行? - How do I insert an empty row in a table with an auto-Increment primary key? 如何使用带有ASP.Net/C#的MySQL ODBC将自动增量ID插入MySQL表? - How do I insert the auto-increment ID into a MySQL table using MySQL ODBC with ASP.Net/C#? 从表中删除内容后,如何在 C# 的内置 SQL 数据库中将自动增量重置为从 1 开始? - How do I reset Auto-Increment to start from 1 in built-in SQL Database of C# after deleting contents from table? 如何使用VSTS构建过程自动增加nuget软件包的版本? - How do I auto-increment nuget package version using VSTS build process? .NET 5 - 防止更新自动增量列? - .NET 5 - Prevent update on auto-increment column? SQL:如何在数据库中自动增加值 - SQL: How to Auto-increment a value in the database 如何自动增加词典的键? - How to auto-increment a key of a Dictionary? 如何从实体框架代码优先方法自动增加 oracle 表? - How to auto-increment oracle table from Entity Framework code-first approach? WPF Datagrid自动增量列生成错误的值? - Wpf datagrid auto-increment column generating wrong values? 在 DataGridView 中自动增加 ID 列的最佳方法? - Best way to auto-increment ID column in a DataGridView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM