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.
Here's the SQL statement to add AUTO INCREMENT constraint to id column. ALTER TABLE sales MODIFY id INT NOT NULL AUTO_INCREMENT PRIMARY KEY; Next we will add a couple of rows in sales table. As you can see, the MySQL has automatically increased and populated id column with values 7 and 8.
Syntax. In MySQL, the syntax to change the starting value for an AUTO_INCREMENT column using the ALTER TABLE statement is: ALTER TABLE table_name AUTO_INCREMENT = start_value; table_name.
Select the first filled cell of the leftmost column in step-2, then, select the last intended cell of the rightmost column in step-3. All columns will be auto-filled at once by pressing 'Ctrl+D'. This is a very useful shortcut if you use excel a lot.
Right click on the table and select "Edit". In "Edit" Table window, select "columns", and then select your PK column. Go to Identity Column tab and select "Generated as Identity" as Type, put 1 in both start with and increment field. This will make this column auto increment.
Set the Identity specification to yes
Sample 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).
This will cause it to increment by 1 on every new record.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With