Create a table, insert an id column, and automatically generate an id. The id is automatically generated.

Source: Internet
Author: User

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


 

 

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.