当前位置:网站首页>常用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
边栏推荐
- From zero learning artificial intelligence, open the road of career planning!
- FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
- What are manufacturing and new automation technologies?
- Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- [C / C + + 1] clion configuration and running C language
- 小游戏云开发入门
- Three Python tips for reading, creating and running multiple files
- vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
- 一部完整的游戏,需要制作哪些音乐?
猜你喜欢
What are PLC Analog input and digital input
Elasticsearch数据库 | Elasticsearch-7.5.0应用搭建实战
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
Building and visualizing decision tree with Python
文件过多时ls命令为什么会卡住?
Individual annual work summary and 2019 work plan (Internet)
Interface pressure test: installation, use and instruction of siege pressure test
01. SSH Remote terminal and websocket of go language
华为云微认证考试简介
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]
随机推荐
Flink的DataSource三部曲之一:直接API
vue-codemirror基本用法:实现搜索功能、代码折叠功能、获取编辑器值及时验证
What are PLC Analog input and digital input
nacos、ribbon和feign的簡明教程
Windows 10 tensorflow (2) regression analysis of principles, deep learning framework (gradient descent method to solve regression parameters)
Advanced Vue component pattern (3)
小游戏云开发入门
vue任意关系组件通信与跨组件监听状态 vue-communication
Solve the problem of database insert data garbled in PL / SQL developer
Brief introduction of TF flags
01. SSH Remote terminal and websocket of go language
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
開源一套極簡的前後端分離專案腳手架
[efficiency optimization] Nani? Memory overflow again?! It's time to sum up the wave!!
What are the criteria for selecting a cluster server?
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
零基础打造一款属于自己的网页搜索引擎
一篇文章带你了解HTML表格及其主要属性介绍
【转发】查看lua中userdata的方法
Analysis of etcd core mechanism