SQL语句小结
1、创建数据库
create database 数据库名
2、删除数据库
drop database 数据库名
3、创建表
1>、create table 表名 (col1 type1 [not null] [primary key],col2 type2 [not null]....)
2>、create table tab_new like tab_old(使用旧表创建新表)
4、增加一个列
alert table tabname add column col type
5、添加/删除主键
alert table tabname add/drop primary key(col)
6、创建索引
create [unique] index idxname on tabname(col...)
7、创建视图
create view viewname as select statement
8、基本sql语句
查询:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field=value where 范围
模糊查询:select * from table1 where field1 like '%value1%'
排序:select * from table1 order by field1,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
9、几个高级查询运算符
union运算符(联合查询)
(select *from tab1where....) union (select *from where...)
except运算符(EXCEPT 返回两个结果集的差(即从左查询中返回右查询没有找到的所有非重复值))
(SELECT * FROM TableA) EXCEPT (SELECT * FROM TableB)
distinct运算符
select distinct xing, ming from B(因为Distinct是作用于多列的,也就是说必须要xing和ming 全都相同的才会被剔除)
intersect运算符(INTERSECT 返回 两个结果集的交集(即两个查询都返回的所有非重复值))
SELECT * FROM TableA INTERSECT SELECT * FROM TableB
10、多表连接
inner join
select tClassName,tSName from TblClass inner join tblstudent on TblClass.tClassId=TblStudent.tSClassId
(只有两个表中的数据符合on条件,才会显示在结果集中)
left join(左表中的信息全部出现,右表中的信息必须能够匹配的才出现未匹配的项使用null填充)
right join(右表中的信息全部出现,左表中的信息必须能够匹配的才出现未匹配的项使用null填充)
full/cross join(所有数据都会出现,不匹配的项使用null填充只要想做连接查询,必须分析出两个表间的关系)
11、子查询
select *from tab1 where id in(select id from tab2 where....)括号中的sql语句则为子查询
12、多表查询
select tab1.*,tab2.* from tab1,tab2 where tab1.[字段]=tab2.[字段]
13、分组:group by
1>、 错误的,where中不能出现分组函数
select d.name , avg(e.salary) from s_dept d, s_emp e where d.id = e.dept_id group by d.name and avg(e.salary) > 1000;
2>、正确, having 紧跟 group by
select d.name , avg(e.salary) from s_dept d, s_emp e where d.id = e.dept_id group by d.name having avg(e.salary) > 1000;
14、as用法
select 字段 as 别名

SQL语句小结的更多相关文章
- MySQL 常用的sql语句小结(待续)
mysql 常用的sql语句 1.查看数据库各个表中的记录数 USE information_schema; SELECT table_name,table_rows FROM tables WHER ...
- postgresql中进行备份和回滚的常用sql语句小结
最近在项目中需要对已有的部分数据库数据进行备份,通过搜索和实践,把常用的sql以及过程记录如下, 1.常用的备份数据库思路,把需要备份的数据放到一个新表中,这个新表的记录与需要备份的表完全一样,然后备 ...
- 数据库 SQL语句小结(更新中)
################ Navicat,单条执行sql ################ Navicat,数据库管理工具, 在查询的页面有好多命令,若单条执行: 1:可选中要执行的一条sql ...
- 2014年小结之sql语句优化
之前接手一个数据统计处理的小程序,本来逻辑上并没什么,只是数据量略大,某些表的数据达到了千万级别..因为是统计,所以免不了各种连接各种查询,结果这个小程序写完后运行一次要1个小时..这的确有点出乎意料 ...
- SQL Server 数据库部分常用语句小结(二)
9. 查询备份还原数据库的进度. select command ,percent_complete ,est_time_to_go=convert(varchar,(estimated_complet ...
- SQL Server 数据库部分常用语句小结(一)
1. 查询某存储过程的访问情况 SELECT TOP 1000 db_name(d.database_id) as DBName, s.name as 存储名字, s.type_desc as 存储类 ...
- MyBatis源码分析-SQL语句执行的完整流程
MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以对配置和原生Map使用简 ...
- 数据库 数据库SQL语句三
转换函数 to_char()字符串转换日期函数 --查询大于某个日期的员工信息 select * from emp where hiredate>to_date('1980-02-12','yy ...
- 化繁为简——分解复杂的SQL语句
今天同事咨询一个SQL语句,如下所示,SQL语句本身并不复杂,但是执行效率非常糟糕,糟糕到一塌糊涂(执行计划也是相当复杂).如果查询条件中没有NOT EXISTS部分,倒是不要一秒就能查询出来. SE ...
随机推荐
- 5-Java-C(单位分数)
题目描述: 形如:1/a 的分数称为单位分数. 可以把1分解为若干个互不相同的单位分数之和. 例如: 1 = 1/2 + 1/3 + 1/9 + 1/18 1 = 1/2 + 1/3 + 1/10 + ...
- col - 过滤掉输入中的反向换行符
SYNOPSIS(总览) col [-bfx ] [ Fl l Ar num ] DESCRIPTION(描述) Col 过滤掉反向(以及半反向)换行符(LF: line feed or NL: ne ...
- 共享win7ip,虚拟机nat模式连接,电脑重启之后,无法连接
问题原因:VMware NAT Service没有设置为开机启动 解决办法: 1.按win+r,输入services.msc,点击确定: 2.服务窗口中找到VMware NAT Service,双击: ...
- 进程的互斥运行:CreateMutex函数实现只运行一个程序实例
HANDLE hMutex=CreateMutex(NULL,TRUE,"HDZBUkeyDoctorTool"); if(hMutex) { if(ERROR_ALREADY_E ...
- 导入.sql文件入数据库
1.创建数据库,例如abc create database abc; 2.进入数据库 use abc 3.导入e盘下的ssh.sql文件即可 source e:\ssh.sql; 截图如下:
- python 删除/查找重复项
l = [1,2,3,2,1] # l = ['你','我','他','她','你'] for i in l: print("the %s has found %s" % (i, ...
- 只允许特定IP访问本网站的前端写法
在开发的过程中,有时会遇到只允许特定的几个IP访问.今天来记录一下前端的写法. 首先,引入 <script src="http://pv.sohu.com/cityjson?ie=ut ...
- [Usaco2009 Nov]lights(高斯消元)
luogu 点灯游戏应该很多人都在小时候頽过吧 反正我直到现在也不会 很明显一个灯最多只需要点一次 然后高斯消元 解完肯定剩自由元(就是那些全是0的行) 然后这些都爆搜 由于剩下的自由元不会太多 所以 ...
- ubuntu Android Studio以及SDK安装
先使用迅雷下载linux版的Android Studio以及SDK,下载地址是https://dl.google.com/dl/android/studio/ide-zips/1.1.0/androi ...
- LeetCode(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...