MYSQL学习笔记_1

一、基本SQL语句

1. 增、删、改、查及排序:

① 插入:insert into table(field1,field2) values(value1,value2)

② 删除:delete from table where 范围

③ 更新:update table set field1=value1 where 范围

④ 查询:select * from table where 范围

⑤ 排序:select * from table1 order by field1,field2 [desc]   -- desc为降序

2. 聚合函数:

① 总数: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

3. 基础语句:

① 创建数据库:CREATE DATABASE database-name

② 删除数据库:drop database dbname

③备份sql server,创建 备份数据的 device

USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'

开始备份:BACKUP DATABASE pubs TO testBack

④ 创建新表: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

⑤ 删除新表:drop table tabname

⑥ 增加一个列:Alter table tabname add column col type

注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。

⑦主键:

添加主键:Alter table tabname add primary key(col)

删除主键:Alter table tabname drop primary key(col)

⑧ 索引:

创建索引:create [unique] index idxname on tabname(col….)

删除索引:drop index idxname

注:索引是不可更改的,想更改必须删除重新建。

⑨视图:

创建视图:create view viewname as select statement

删除视图:drop view viewname

二、特殊查询

1. 带in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (18,20);

  select * from t_student where age not in (18,20);

2. 带between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29;

select * frome t_student where age not between 21 and 29;

3. 带like的模糊查询:select 字段1,字段2… frome 表名 where 字段 [not] like ‘字符串’;

“%”代表任意字符;

“_”代表单个字符;

例:select * frome t_student where stuName like ‘张三”;

select * frome t_student where stuName like ‘张三%”;

select * frome t_student where stuName like ‘%张三%”;//含有张三的任意字符

select * frome t_student where stuName like ‘张三_”

4. 空值查询:select 字段1,字段2…frome 表名 where 字段 is[not] null;

5. 带and的多条件查询:

select 字段1,字段2…frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]

例:select * frome t_student where gradeName=’一年级’ and age=23;

6. 带or的多条件查询

select 字段1,字段2…frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]

例:select * frome t_student where gradeName=’一年级’ or age=23;//或者,条件只要满足一个

7. distinct去重复查询:select distinct 字段名 from 表名;

8. 对查询结果排序order by:select 字段1,字段2…from 表名 order by 属性名 [asc|desc]

例:select * frome t_student order by age desc;//降序,从大到小

select * frome t_student order by age asc;//升序,asc默认可以不写

9.limit 分页查询:select 字段1,字段2,…from 表名 limit 初始位置,记录数;

例:select * from t_student limit 0,5;

10. 分组查询:group by (先排序后分组)

① 举例说明:如果要用到group by 一般用到的就是“每这个字” 例如说明现在有一个这样的表:每个部门有多少人 就要用到分组的技术

例:select DepartmentID as '部门名称' COUNT(*) as '个数' from BasicDepartment group by DepartmentID

② 当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:

-- 执行where子句查找符合条件的数据;

-- 使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;最后用having 子句去掉不符合条件的组。

-- having 子句中的每一个元素也必须出现在select列表中。有些数据库例外,如oracle.

-- having子句和where子句都可以用来设定限制条件以使查询结果满足一定的条件限制。

-- having子句限制的是组,而不是行。where子句中不能使用聚集函数,而having子句中可以

③ having 和 where的区别:

where :是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,where条件中不能包含聚合函数,使用where条件过滤出特定的行

having :是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚合函数,使用having 条件过滤出特定的组,也可以使用多个分组标准进行分组

11. 特殊语句:

① 随机取出10条数据:select top 10 * from tablename order by newid()

② 随机选择记录:select newid()

③ 删除重复记录:Delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)

④ 列出数据库里所有的表名:select name from sysobjects where type='U'

⑤ 列出表里的所有的:select name from syscolumns where id=object_id('TableName')

⑥ 选择从10到15的记录:select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc

三、多表连接查询

注意:多表查询时,一定要找到两个表中相互关联的字段,并且作为条件使用

语法:select field1,field2 from table1 inner | left | right join table2 on table1.field1 = table2.field2 and 其他条件;

① 内连接查询:内连接查询和多表连接查询效果是一样的

② 左外连接查询:左边表中的数据会优先显示,右边表中的数据符合条件才会显示,不符合条件的会以null进行填充

③ 右外连接查询:右边表中的数据优先全部显示,与左连接正好相反

④ 全连接查询:显示左右表中全部数据,是在内连接的基础上增加“左右两边没有显示的数据”(Mysql不提供full JOIN关键字,使用UNION实现)

四、合并查询

1.union:使用union关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;

2.union all:使用union all,不会去除掉重复的记录;

六、子查询

1.带in关键字的子查询(一个查询语句的条件可能落在另一个select语句的查询结果中)

2.带比较运算符的子查询(子查询可以使用比较运算符:> = <)

3.带exists关键字的子查询(加入子查询查询到记录,则进行外层查询,否则,不执行外层查询)

4.带any关键字的子查询(any关键字表示满足其中任一条件)

5.带all关键字的子查询(all关键字表示满足所有条件)


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

posted @ 2019-05-03 18:19  简单安安静  阅读(153)  评论(0编辑  收藏  举报