简单的sql语句汇总(sqlserver)

1.修改字段的默认值

alter table 表名  add default 默认值 for  字段名称
例子:alter table Students  add default 18 for Age;

alter  table 表名 add  constraint  DF_TABLEName_FieldName  default  默认值  for  字段名称;
例子:alter  table Brands add  constraint  DF_Brands_Name  default 'hello'  for  Name;

2.新增表 

alter table 表名 add 字段名称 类型 not null  default 默认值;
例子:alter table Company add Name varchar(50) not null  default '';

3.设置表中已有字段为空

alter  table 表名 alter column [列名] 类型 NULL;
例子:alter  table Company alter column [Address] varchar(100) NULL;

 4.为表中已有字段添加说明

复杂的写法
例子:EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'年龄' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Students', @level2type=N'COLUMN',@level2name=N'Age'

简单的写法
EXEC sys.sp_addextendedproperty 'MS_Description', '说明' , 'user','dbo','table','表名','column','字段名称';
例子:EXEC sys.sp_addextendedproperty 'MS_Description', '年龄' , 'user','dbo','table','Students','column','Age';

 5.查询数据库中int类型为null的数据

select * from Student where CourseID is null;

写在后面的话:都是很简单的语句,为了自己查询方便,特此记录下来,持续更新中......

猜你喜欢

转载自www.cnblogs.com/jas0203/p/11150586.html