将SQL Default值01添加到数据库列

时间:2014-08-19 16:09:04

标签: sql sql-server types

如何将以下默认值添加到sql数据库表?

01

我尝试了各种数据类型,但0仍在消失。我甚至没能在交易所或谷歌上找到一个例子,但我认为这是非常直接的,我是一个numpty !!!

我试过没有运气的数字/整数/ varchar。感谢。

2 个答案:

答案 0 :(得分:3)

create table MyTable (Id int, Value varchar(100) not null default('01'))
Insert into MyTable (Id) values (1) 

Select Value
From MyTable 

will be '01'

Sql fiddle

答案 1 :(得分:1)

另一种选择可能是:

打开您的表设计器,您将看到如下内容:

enter image description here

如果您更喜欢代码:

USE ["name of your dataBase"] 
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_1](
[id] [int] IDENTITY(1,1) NOT NULL,
[sample02] [nvarchar](50) NOT NULL,
[sample03] [nvarchar](50) NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED 
(
[id] 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

ALTER TABLE [dbo].[Table_1] ADD  CONSTRAINT [DF_Table_1_sample02]  DEFAULT ('01') FOR [sample02]
GO

最后,您可以访问此微软帖子:Specify Default Values for Columns