1、基础Sql语句html
一、建立数据库:Create DataBase dbName;sql
二、删除数据库:Drop DataBase dbName;数据库
三、建立新表:Create Table tabName(col1 type1 [not null] [primary key] ,col2 type2 [not null ], ........);ide
根据已有表建立新表的两种方式:A:Create Table tab_new like tab-old;函数
B:Create Table tab_new as Select col1,col2,....from tab_old definition only;spa
四、删除新表:Drop Table tabName;htm
五、为表增长一列:Alter Table tabName add column col type;blog
六、为表添加主键与删除主键:添加主键:Alter Table tabName add primary key(col);排序
删除主键:Alter Table tabName drop primary key(col);索引
七、为表建立和删除索引:建立索引:Create [unique] index indexName on tabName(col ....)
删除索引:Drop index indexName;
八、建立和删除视图:建立视图:Create view viewName as Select statement;
删除视图:Drop view viewName;
九、基本的sql语句: 查询:Select * from Table where 范围;Select * from Table where field1 like ‘%value1%'(模糊查询)
插入:Insert into Table(field1,field2,...) values(value1,value2,.....);
删除:Delete from Table where 范围;
· 更新:Update Table set field1=value1 where 范围;
排序:Select * from Table order by field1 Desc【降序】| Asc【升序】;
总数:Select count as TotalCount from Table ;
求和:Select sum(field1) as sumVaule from Table;
平均:Select avg(field1) as avgValue from Table;
最大:Select max(field1) as maxValue from Table;
最小:Select min(field1) as minVaule from Table;
十、Sql中的几个高级查询运算词:
A: UNION 运算符
UNION运算符经过组合其余两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一块儿使用时(即 UNION ALL),不消除重复行。两种状况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
B: EXCEPT 运算符
EXCEPT 运算符经过包括全部在 TABLE1 中但不在 TABLE2 中的行并消除全部重复行而派生出一个结果表。当 ALL 随 EXCEPT 一块儿使用时 (EXCEPT ALL),不消除重复行。
C: INTERSECT 运算符
INTERSECT 符经过只包括 TABLE1 和 TABLE2 中都有的行并消除全部重复行而派生出一个运算结果表。当 ALL 随 INTERSECT 一块儿使用时 (INTERSECT ALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
十一、使用表链接:
(1)内链接(Inner Join):Inner Join TableName ON condition;
(2)不等值链接:不等值链接即为在链接的条件中可使用小于(<)、大于(>)、不等于(<>)等运算符,并且还可使用LIKE、BETWEEN AND等运算符,甚至还可使用函数。示例:Select field1,field2 from table1 Inner Jion table2 on table1.field1=table2.field1 where table1.field3<table2.field4
(3)交叉链接:隐式:Select t1.field1,t2.field3 from table1 as t1,table2 as t2;
显式:Select t1.field1,t2.field3 from table1 as t1 cross Jion table2 as t2;
(4)外链接:A:Left outer Join (左外链接(左链接):结果集既包括链接表的匹配行,也包括左链接表的全部行);
B:Right outer Join (右外链接(右链接):结果集既包括链接表的匹配链接行,也包括右链接表的全部行);
C:Full outer Join(全外链接:不只包括符号链接表的匹配行,还包括两个链接表中的全部记录);
十二、使用分组Group by:
示例:Select 类别,摘要,sum(field2) as sumValue from tableName Group by 类别;
注:一张表,一旦分组完成后,查询后只能获得组相关的信息。组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准);在SQLServer中分组时:不能以text,ntext,image类型的字段做为分组依据;在select统计函数中的字段,不能和普通的字段放在一块儿。在使用Group by实现分组时,若是有where过滤条件,必须写在Group by以前。
1三、Having的使用:
如对部分分组进行过滤,就须要用到Having,由于聚合函数不能再Where语句中使用,因此得使用Having来代替。
注:使用Having子句时,其应位于Group by以后,而且Having语句中不能包含未分组的列名。
2、复杂Sql语句
一、复制表(仅复制表结构):Select * into b from a where 1<>1;Select top 0 * into b from a;
二、拷贝表(拷贝数据):Insert into b(a,b,c) Select d,e,f from a;
三、子查询:
SELECT 语句能够嵌套在其余语句中,好比 SELECT,INSERT,UPDATE 以及 DELETE 等,这些被嵌套的 SELECT 语句就称为子查询,能够这么说当一个查询依赖于另一个查询结果时就可使用子查询。子查询有两种类型,一种是只返回一个单值的子查询,这时它能够用在一个单值可使用的地方,这时子查询能够看做是一个拥有返回值的函数;另一种是返回一列值的子查询,这时子查询能够看做是一个在内存中临时存在的数据表。
(1)单值子查询:
IN运算符示例:
四、between的用法:Select * from tableName where time between time1 and time2;
Select a,b,c from tableName where a not between 数值1 and 数据2;
注:between限制查询数据范围时包括了边界值,not between不包括;
五、两张关联表,删除主表中已经在副表中没有的信息:Delete from table1 where not exists (Select * from table2 where table1.field1=table2.field2);
六、四张表联合查询:select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where ....
七、查询前十条数据:Select top 10 * from TableName where 范围;
八、随机取出十条数据:Select top 10 * from TableName order by newid();
九、选择在每一组b值相同的数据中对应的a最大的记录的全部信息:Select a,b,c from table1 as t1 where a=(Select max(a) from table2 as t2 where t1.b=t2.b);
十、包括全部在 TableA 中但不在 TableB和TableC 中的行并消除全部重复行而派生出一个结果表:(select a from tableA ) except (select a from tableB) except (select a from tableC)
十一、列出数据库里全部的表名:select name from sysobjects where type='U' // U表明用
十二、列出表里的全部的列名:select name from syscolumns where id=object_id('TableName')
1三、删除表中的重复记录:(1)Delete from TableName where id not in (Select max(id) from TableName group by col1,col2 ......);
(2)Select distinct * into temp from TableName
delete from TableName
insert into Tablename seletct * from temp
(3)alter table tablename
--添加一个自增列
add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b
1四、一条sql语句实现数据库分页:(1)Select top num * from TableName
where field1>(Select max(field1) from (Select totalNum field1 from TableName order by fireld1) A)
order by field1
(2)Select * from (Select row_number() over (order by field1) rownumber,* from TableName) A
Where rownumber between startNum And [start+count-1];
注:有关sql查询分页的内容能够参考博客(https://www.cnblogs.com/zcttxs/archive/2012/04/01/2429151.html),这里再也不赘述。