经典常用SQL语句大全

创建表

--删除表
--DROP TABLE [dbo].[Test]

--创建表
CREATE TABLE [dbo].[Test] (
[Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY, ----自增主键
[Name] nvarchar(50) NULL  DEFAULT '默认值',  ----可设置默认值
[Remarks] nvarchar(MAX) NULL, --------------------字符串类型
[TypeId] int NULL,-------------------------------整型,取值范围[-231~231)
[ServicevalueExcludingVat] MONEY NULL, ----------货币型 
[ServicevalueEncludingVat] decimal(12,4) NULL, --精确数值型 共12位,小数点右4位
[VatPercentage] float NULL, ---------------------近似数值型
[AddDate] date NULL , ------------------------------日期
[AddTime] datetime NULL ,---------------------------时间
)

--添加表说明
EXECUTE sp_addextendedproperty   N'MS_Description',N'测试表',N'user',N'dbo',N'table',N'Test',NULL,NULL
--删除表说明
--EXEC sp_dropextendedproperty N'MS_Description','user','dbo','table', '表名', NULL,NULL

--添加字段说明
EXECUTE   sp_addextendedproperty   N'MS_Description',N'名称',N'user',N'dbo',N'table',N'Test',N'column',N'Name'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'备注',N'user',N'dbo',N'table',N'Test',N'column',N'Remarks'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'类型',N'user',N'dbo',N'table',N'Test',N'column',N'TypeId'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'服务价值,不包括增值税',N'user',N'dbo',N'table',N'Test',N'column',N'ServicevalueExcludingVat'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'服务价值,包括增值税',N'user',N'dbo',N'table',N'Test',N'column',N'ServicevalueEncludingVat'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'增值税百分比',N'user',N'dbo',N'table',N'Test',N'column',N'VatPercentage'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'创建日期',N'user',N'dbo',N'table',N'Test',N'column',N'AddDate'
EXECUTE   sp_addextendedproperty   N'MS_Description',N'创建时间',N'user',N'dbo',N'table',N'Test',N'column',N'AddTime'

--删除字段说明
--EXEC sp_dropextendedproperty N'MS_Description', 'user','dbo', 'table', '表名', 'column','字段名'
创建表

查询表结构【示例图】

--查询表结构
CREATE PROC [dbo].[aaa_select_table]  --创建存储过程 
    @name nvarchar(50)
as 
begin --开始
  declare @condition nvarchar(2000);
    set @condition=' where 1=1 ';
    if(@name<>'')
    set @condition=@condition+' and d.name like ''%'+@name+'%''';
    exec('
SELECT
表名=case   when   a.colorder=1   then   d.name   else   ''''   end,
表说明=case   when   a.colorder=1   then   isnull(f.value,'''')   else   ''''   end,
字段序号=a.colorder,
字段名=a.name,
字段说明=isnull(g.[value],''''),
类型=b.name,
长度=COLUMNPROPERTY(a.id,a.name,''PRECISION''),
标识=case   when   COLUMNPROPERTY(   a.id,a.name,''IsIdentity'')=1   then   ''''else   ''''   end,
主键=case   when   exists(SELECT   1   FROM   sysobjects   where   xtype=''PK''   and   name   in   (
SELECT   name   FROM   sysindexes   WHERE   indid   in(
SELECT   indid   FROM   sysindexkeys   WHERE   id   =   a.id   AND   colid=a.colid
)))   then   ''''   else   ''''   end,
允许空=case   when   a.isnullable=1   then   ''''else   ''''   end,
默认值=isnull(e.text,'''')
FROM   syscolumns   a
left   join   systypes   b   on   a.xusertype=b.xusertype
inner   join   sysobjects   d   on   a.id=d.id     and   d.xtype=''U''   and     d.name<>''dtproperties''
left   join   syscomments   e   on   a.cdefault=e.id
left   join   sys.extended_properties   g   on   a.id=g.major_id   and   a.colid=g.minor_id
left   join   sys.extended_properties   f   on   d.id=f.major_id   and   f.minor_id=0
'+@condition+' 
order   by   a.id,a.colorder
');
end;--结束
查询表结构

基本sql语句

插入:insert into table1 values(value1,value2)  
      insert into table1(field1,field2) values(value1,value2)  
      insert into table1(field1,field2) SELECT field1,field2 FROM table2
复制: select * into 目标表名 from 源表名 WHERE 1=2 (【复制表结构】即:让WHERE条件不成立)
            select * into 目标表名 from 源表名(【复制表结构及数据】要求目标表不存在,因为在插入时会自动创建)
删除:delete from table1 where 范围  
清空:truncate table table1
更新:update table1 set field1=value1 where 范围  
替换:update [表名] SET [字段] = replace([字段], '替换前内容', '替换后内容');
选择:select * from table1 where 范围  
查找:select * from table1 where field1 like '%value1%'
排序:select * from table1 order by field1 ASC,field2 DESC 
总数:select count(*) as totalcount from table1  
求和:select sum(field1) as sumvalue from table1  
平均:select avg(field1) as avgvalue from table1  
最大:select max(field1) as maxvalue from table1  
最小:select min(field1) as minvalue from table1  

外连接

--外连接   
A、left join:   
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。   
B:right join:   
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。   
C:join:   
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。 

高级查询运算词

A: UNION 运算符   
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。   
B: EXCEPT 运算符   
EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALLEXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。   
C: INTERSECT 运算符  
INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALLINTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。   
注:使用运算词的几个查询结果行必须是一致的。

其他

select * from table1 where time between time1 and time2  
select a,b,c, from table1 where a not between 数值1 and 数值2  
between 限制查询数据范围
select top 10 * from tablename order by newid()  
随机取出10条数据

猜你喜欢

转载自www.cnblogs.com/zj19940610/p/12049470.html