当前位置:网站首页>常用SQL语句总结
常用SQL语句总结
2020-11-06 21:19:00 【程序猿欧文】
一、基础Sql语句
1、创建数据库:Create DataBase dbName;
2、删除数据库:Drop DataBase dbName;
3、创建新表:Create Table tabName(col1 type1 [not null] [primary key] ,col2 type2 [not null ], ........);
根据已有表创建新表的两种方式:A:Create Table tab_new like tab-old;
B:Create Table tab_new as Select col1,col2,....from tab_old definition only;
4、删除新表:Drop Table tabName;
5、为表增加一列:Alter Table tabName add column col type;
6、为表添加主键与删除主键:添加主键:Alter Table tabName add primary key(col);
删除主键:Alter Table tabName drop primary key(col);
7、为表创建和删除索引:创建索引:Create [unique] index indexName on tabName(col ....)
删除索引:Drop index indexName;
8、创建和删除视图:创建视图:Create view viewName as Select statement;
删除视图:Drop view viewName;
9、基本的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;
10、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),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
11、使用表连接:
(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(全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录);
12、使用分组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之前。
13、Having的使用:
如对部分分组进行过滤,就需要用到Having,因为聚合函数不能再Where语句中使用,所以得使用Having来代替。
注:使用Having子句时,其应位于Group by之后,并且Having语句中不能包含未分组的列名。
二、复杂Sql语句
1、复制表(仅复制表结构):Select * into b from a where 1<>1;Select top 0 * into b from a;
2、拷贝表(拷贝数据):Insert into b(a,b,c) Select d,e,f from a;
3、子查询:
SELECT 语句可以嵌套在其他语句中,比如 SELECT,INSERT,UPDATE 以及 DELETE 等,这些被嵌套的 SELECT 语句就称为子查询,可以这么说当一个查询依赖于另外一个查询结果时就可以使用子查询。子查询有两种类型,一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数;另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的数据表。
(1)单值子查询:
单值子查询的语法和普通的 SELECT 语句没有什么不同,唯一的限制就是子查询的返回值必须只有一行记录,而且只能有一个列。这样的子查询又被称为标量子查询,标量子查询可以用在 SELECT 语句的列表中、表达式中、WHERE 语句中等很多场合。 (2).........版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4556923
边栏推荐
- 理解格式化原理
- 每个大火的“线上狼人杀”平台,都离不开这个新功能
- 6.1.1 handlermapping mapping processor (1) (in-depth analysis of SSM and project practice)
- 5.5 controlleradvice notes - SSM in depth analysis and project practice
- Jmeter——ForEach Controller&Loop Controller
- Free patent download tutorial (HowNet, Espacenet)
- 零基础打造一款属于自己的网页搜索引擎
- What are Devops
- 新建一个空文件占用多少磁盘空间?
- 有了这个神器,快速告别垃圾短信邮件
猜你喜欢
Gather in Beijing! The countdown to openi 2020
一篇文章带你了解CSS 分页实例
[C / C + + 1] clion configuration and running C language
What are manufacturing and new automation technologies?
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
Shh! Is this really good for asynchronous events?
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
StickEngine-架构12-通信协议
Flink的DataSource三部曲之一:直接API
随机推荐
Mongodb (from 0 to 1), 11 days mongodb primary to intermediate advanced secret
What to do if you are squeezed by old programmers? I don't want to quit
It is really necessary to build a distributed ID generation service
nacos、ribbon和feign的簡明教程
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
前端未來趨勢之原生API:Web Components
5.5 controlleradvice notes - SSM in depth analysis and project practice
StickEngine-架构11-消息队列(MessageQueue)
一篇文章带你了解CSS对齐方式
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
hdu3974 Assign the task線段樹 dfs序
[C / C + + 1] clion configuration and running C language
代码生成器插件与Creator预制体文件解析
ES6学习笔记(四):教你轻松搞懂ES6的新增语法
C語言I部落格作業03
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
一篇文章带你了解CSS3图片边框
Electron application uses electronic builder and electronic updater to realize automatic update
Discussion on the development practice of aspnetcore, a cross platform framework
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!