首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

如何样在脚本中为字段指定默认值

2012-01-24 
怎么样在脚本中为字段指定默认值?我的表结构都是写好脚本后创建的。可是我怎么样在脚本中为字段指定默认值

怎么样在脚本中为字段指定默认值?
我的表结构都是写好脚本后创建的。
可是我怎么样在脚本中为字段指定默认值呢?
用企业管理器我会做,可是我需要在脚本中实现
比如:有一个stsdate的字段,我要指定其默认值为(getdate()),应该如何
修改下面的脚本呀?

create   table   [dbo].[t_employee_wage_history](
[id]   [int]   identity   not   null,
[employee_id]   [int]   not   null,
[ymd]   [int]   not   null,
[wage_change_type]   [int]   not   null,
[wage_item_id]   [int]   not   null,
[wage]   [decimal](19,   2)   not   null,
[stsdate]   [datetime]   not   null
)   on   [primary]
go


[解决办法]
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null Default GetDate()
) on [primary]
go
[解决办法]
CREATE TABLE [dbo].[tb] (
[id] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[dt] AS (getdate())
) ON [PRIMARY]
GO
[解决办法]
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null AS (getdate())
) on [primary]
go

[解决办法]
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null default getdate()
) on [primary]
go
[解决办法]
--先建立表
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null
) on [primary]
go
--后增加一个默认值的约束
alter table [t_employee_wage_history] add constraint df_wanghistory default getdate()for [stsdate]

热点排行