Create a table, insert an id column, and automatically generate an id. The id is automatically generated.
USE [yuyongTest]
GO
-- Go: Execute now. The go statement segments the SQL Server script. After a segment is executed, the next segment can be executed, and the segments and segments are completely independent. Temporary variables that span go statements cannot be used universally.
Declare @ name char (10)
Select @ name = 'yuyong'
Print (@ name)
Go
Print (@ name)
Go
-- In this example, the second print will report an error. However, the error is limited to the segment (separated by go) and no other segments are passed.
-- Note that a segment is not equal to a transaction. A segment may contain multiple transactions.
SET ANSI_NULLSON
GO
SET quoted_identifieon
GO
Create table [dbo]. [student]
(
[Name] [nvarchar] (50) NOTNULL,
[Id] [uniqueidentifier] NOTNULL,
[Info] [nvarchar] (500) NULL,
-- Add an ID column
[Number] [int] IDENTITY (1, 1) NOTNULL,
-- Add a primary key
CONSTRAINT [PK_student] PRIMARYKEYCLUSTERED ([id] ASC) WITH (PAD_INDEX = OFF, expiration = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY]
GO
-- Add the default value. The id is automatically generated.
Alter table [dbo]. [student] add constraint [DF_student_id] DEFAULT (newid () FOR [id]
GO
You can use the management tool to do the same thing:
Automatically Generated id:
Insert ID column