SQLServr添加数据列

数据列定义

表中数据行的数据插入和数据类型都是基于数据列的,学会添加数据列在开发过程中是必不可少的。

使用SSMS数据库管理工具添加数据列

在数据表中添加一列或者多列步骤相同

1、连接数据库,选择数据表-》右键点击-》选择设计。

2、在新打开的窗口中输入中-》输入列名,数据类型,是否可空-》在下面输入列注释等属性-》点击保存按钮(或者ctrl+s)。

3、如果想在指定列前面添加数据列-》选择要指定列,右键点击-》插入数据列-》输入列名,列类型,是否可空,属性等,点击保存。

使用T-SQL脚本数据列

添加数据列

语法:alter table 数据库名.dbo.表名 add 列名  列类型 [not] null;

示例:

--添加可空数据列
alter table testss.dbo.test1 add height1 nvarchar(50) null;

--添加不可空数据列
alter table testss.dbo.test1 add height2 nvarchar(50) not null;

添加带注释的数据列

 语法:

alter table 数据库名.dbo.表名 add 列名 列数据类型 [not] null;
execute sp_addextendedproperty N'MS_Description', N'列说明', N'user', N'dbo', N'table', N'表明, N'column', N'列名';

示例:

alter table testss.dbo.test1 add height3 nvarchar(50) null;
execute sp_addextendedproperty N'MS_Description', N'身高3', N'user', N'dbo', N'table', N'test1', N'column', N'height3';

添加数据列时指定默认值

语法:alter table 数据库名.dbo.表名 add 列名 int not null default 值;

示例:alter table testss.dbo.test1 add testid int not null default 1;

添加多个数据列

语法:

alter table 数据库名.dbo.表名 add 列名 列类型 not null default 值,列名 列类型 null default 值;

示例:

alter table testss.dbo.test1 add height5 int not null default 1,
height6 nvarchar(20) null default '178cm';

总结

在生产或者开发阶段,数据列的添加建议使用T-SQL脚本,方便开发和生产,且易于维护。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值