MYSQL之数据操作

Posted 低调的人儿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL之数据操作相关的知识,希望对你有一定的参考价值。

一、介绍

 在mysql管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括

  1. 使用INSERT实现数据的插入
  2. UPDATE实现数据的更新
  3. 使用DELETE实现数据的删除
  4. 使用SELECT查询数据。

二、各种数据操作

插入数据 INSERT

1
2
3
4
5
6
7
8
9
10
11
#语法一: 按字段进行插入
insert into 表(字段1,字段2 ...) values (值1,值2 ...);
 
#语法二:按字段顺序插入
insert into values (值1,值2 ...);
 
#语法三: 插入多条记录
insert into values (值1,值2 ...) ,(值1,值2 ...)  ,(值1,值2 ...);
 
#语法四:插入查询结果
insert into 表(字段1,字段2 ...)  select 字段1,字段2 ... from 表;

 

四种插入数据方式:

一.插入一条数据
insert into student(id,name,age,sex,salary) values(1,\'小猪\',18,\'\',2500);


二.插入多条数据
insert into student(id,name,age,sex,salary) values(1,\'小猪\',18,\'\',2500) ,(2,\'小猪2\',28,\'\',2500),(3,\'小猪3\',38,\'\',2500);


三.直接插入
insert into student values(1,\'小猪\',18,\'\',2500);
ps:如果插入的数据个数和位置正好与表的字段个数和位置匹配,则可以省略表名后面的字段定义

四.查询并插入
insert into student(id,name,age) select id,name,age from tb ;
ps:从tb表中查询数据并插入到 student表中
代码示例

更新操作 UPDATE

#语法一: 更新整表数据

updateset 字段1= \'值1\', 字段2=\'值2\' ... ; 

 

#语法二:更新符合条件字段3的数据

updateset 字段1= \'值1\', 字段2=\'值2\' ... where 字段3 = 值3; 
方式一:
update student set name = \'猪猪哥\' ;
ps: student表中所有的 name 字段 的值 全部被更新为 \'猪猪哥\'

方式二:
update student set name= \'猪猪哥\' ,age =13  where id = 2;
ps: 更新 student 表中 name 和 age 字段的值,并且只更新 id = 2的一条记录
代码示例

删除操作 DELETE

#语法一:整表数据删除

delete from 表 ; 

 

#语法二:删除符合 where后条件的数据

delete fromwhere 字段1=值1; 
方式一:
delete from student;
ps:删除 student 表中所有的数据,注意:如果有自增主键,主键记录的值不会被删除.

方式二:
delete from student where id=1;
ps:只删除id 为1的数据.

方式三:
truncate student;
ps:清空表
代码示例

truncate和delete的区别?[面试题]

 

1TRUNCATE 在各种表上无论是大的还是小的都非常快。而DELETE 操作会被表中数据量的大小影响其执行效率.
2、TRUNCATE是一个DDL语言而DELETE是DML语句,向其他所有的DDL语言一样,他将被隐式提交,不能对TRUNCATE使用ROLLBACK命令。
3、TRUNCATE不能触发触发器,DELETE会触发触发器。
4、当表被清空后表和表的索引和自增主键将重新设置成初始大小,而delete则不能。

查询操作 SELECT

根据查询功能的不同,我们可以为查询划分为以下两类:

1、单表查询

一、简单查询

-- 创建表
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `age` tinyint(4) DEFAULT \'0\',
  `sex` enum(\'\',\'\',\'人妖\') NOT NULL DEFAULT \'人妖\',
  `salary` decimal(10,2) NOT NULL DEFAULT \'250.00\',
  `hire_date` date NOT NULL,
  `dept_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

-- 创建数据

-- 教学部
INSERT INTO `person` VALUES (\'1\', \'alex\', \'28\', \'人妖\', \'53000.00\', \'2010-06-21\', \'1\');
INSERT INTO `person` VALUES (\'2\', \'wupeiqi\', \'23\', \'\', \'8000.00\', \'2011-02-21\', \'1\');
INSERT INTO `person` VALUES (\'3\', \'egon\', \'30\', \'\', \'6500.00\', \'2015-06-21\', \'1\');
INSERT INTO `person` VALUES (\'4\', \'jingnvshen\', \'18\', \'\', \'6680.00\', \'2014-06-21\', \'1\');

-- 销售部
INSERT INTO `person` VALUES (\'5\', \'歪歪\', \'20\', \'\', \'3000.00\', \'2015-02-21\', \'2\');
INSERT INTO `person` VALUES (\'6\', \'星星\', \'20\', \'\', \'2000.00\', \'2018-01-30\', \'2\');
INSERT INTO `person` VALUES (\'7\', \'格格\', \'20\', \'\', \'2000.00\', \'2018-02-27\', \'2\');
INSERT INTO `person` VALUES (\'8\', \'周周\', \'20\', \'\', \'2000.00\', \'2015-06-21\', \'2\');

-- 市场部
INSERT INTO `person` VALUES (\'9\', \'月月\', \'21\', \'\', \'4000.00\', \'2014-07-21\', \'3\');
INSERT INTO `person` VALUES (\'10\', \'安琪\', \'22\', \'\', \'4000.00\', \'2015-07-15\', \'3\');

-- 人事部
INSERT INTO `person` VALUES (\'11\', \'周明月\', \'17\', \'\', \'5000.00\', \'2014-06-21\', \'4\');
-- 鼓励部
INSERT INTO `person` VALUES (\'12\', \'苍老师\', \'33\', \'\', \'1000000.00\', \'2018-02-21\', null);
准备表和数据
#查询语法:  

select [distinct]*(所有)|字段名,...字段名 from 表名;  #distinct 是去重关键字,可以不加

 

#查询所有字段信息

select * from person;

 

#查询指定字段信息

select id,name,age,sex,salary from person;

 

#别名查询,使用的as关键字,as可以省略的

select name,age as\'年龄\',salary \'工资\' from person;  

 

#直接对列进行运算,查询出所有人工资,并每人增加100块.

select (5/2);

select name, salary+100 from person;

 

#剔除重复查询

select distinct age from person;

二  条件查询

条件查询:使用 WHERE 关键字 对简单查询的结果集 进行过滤

   1. 比较运算符: > < >= <= = <>(!=)

    2. null 关键字: is null , not null

    3.逻辑运算符: 与 and 或 or (多个条件时,需要使用逻辑运算符进行连接)

#查询格式:

select [distinct]*(所有)|字段名,...字段名 from 表名 [where 条件过滤]

 

#比较运算符: > < >= <= = <>(!=)    is null 是否为null

select * from person where age = 23;

select * from person where age <> 23;

select * from person where age is null;

select * from person where age is not null;

 

#逻辑运算符: 与 andor 

select * from person where age = 23 and salary =29000; 

select * from person where age = 23 or salary =29000;

三 区间查询

关键字 between 10 and  20 :表示 获得10 到 20 区间的内容

# 使用  between...and  进行区间 查询

select * from person where salary between 4000 and 8000;

ps: between...and 前后包含所指定的值

等价于 select * from person where salary >= 4000 and salary <= 8000; 

四 集合查询

关键字: in, not null

#使用 in 集合(多个字段)查询

select * from person where age in(23,32,18);

等价于: select * from person where  age =23 or age = 32 or age =18;

 

#使用 in 集合 排除指定值查询

select * from person where age not in(23,32,18); 

五 模糊查询

关键字 like , not like

    %:  任意多个字符

       _  : 只能是单个字符

 

#模糊查询  like %:任意多个字符,  _:单个字符

 

#查询姓名以"张"字开头的

select * from person where name like \'张%\';

#查询姓名以"张"字结尾的

select * from person where name like \'%张\';

#查询姓名中含有"张"字的

select * from person where name like \'%张%\';

 

#查询 name 名称 是四个字符的人

select * from person where name like \'____\';

#查询 name 名称 的第二个字符是 \'l\'的人

select * from person where name like \'_l%\';

 

#排除名字带 a的学生

select * from student where name not like \'a%\'

六 排序查询

关键字: ORDER BY  字段1 DESC, 字段2 ASC

#排序查询格式:

select 字段|* from 表名 [where 条件过滤] [order by 字段[ASC][DESC]]

 

升序:ASC 默认为升序

降序:DESC

PS:排序order by 要写在select语句末尾

 

#按人员工资正序排列,注意:此处可以省略 ASC关键字

select * from person order by salary ASC;

select * from person order by salary;

 

#工资大于5000的人,按工资倒序排列

select * from person where salary >5000 order by salary DESC;

 

#按中文排序

select * from person order by name;

 

#强制中文排序

select * from person order by CONVERT(name USING gbk);

ps:UTF8 默认校对集是 utf8_general_ci , 它不是按照中文来的。你需要强制让MySQL

七 聚合函数

 聚合:  将分散的聚集到一起.   聚合函数: 对列进行操作,返回的结果是一个单一的值,除了 COUNT 以外,都会忽略空值

  COUNT:统计指定列不为NULL的记录行数;   SUM:计算指定列的数值和,如果指定列类型不是数值类型,那么计算结果为0;   MAX:计算指定列的最大值,如果指定列是字符串类型,那么使用字符串排序运算;   MIN:计算指定列的最小值,如果指定列是字符串类型,那么使用字符串排序运算;   AVG:计算指定列的平均值,如果指定列类型不是数值类型,那么计算结果为0;

#格式:

select 聚合函数(字段) from 表名;

 

#统计人员中最大年龄、最小年龄,平均年龄分别是多少

select max(age),min(age),avg(age) from person; 

八 分组查询

分组的含义: 将一些具有相同特征的数据 进行归类.比如:性别,部门,岗位等等

 怎么区分什么时候需要分组呢?  

  套路: 遇到 "每" 字,一般需要进行分组操作。

  例如: 1. 公司每个部门有多少人。

      2. 公司中有 多少男员工 和 多少女员工。

#分组查询格式:
select 被分组的字段 from 表名 group by 分组字段 [having 条件字段]
ps: 分组查询可以与 聚合函数 组合使用.

#查询每个部门的平均薪资
select avg(salary),dept from person  GROUP BY dept;

#查询每个部门的平均薪资 并且看看这个部门的员工都有谁?
select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept;
#GROUP_CONCAT(expr):按照分组,将expr字符串按逗号分隔,组合起来

#查询平均薪资大于10000的部门, 并且看看这个部门的员工都有谁?  
select avg(salary),dept,GROUP_CONCAT(name) from person GROUP BY dept; having avg(salary)>10000;

 where 与 having区别:
执行优先级从高到低:where > group by > having
1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。
2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数

九 分页查询

 好处:限制查询数据条数,提高查询效率

#查询前5条数据

select * from person limit 5;

 

#查询第5条到第10条数据

select * from person limit 5,5;

 

#查询第10条到第15条数据

select * from person limit 10,5;

 

ps: limit (起始条数),(查询多少条数);

十 正则表达式

MySQL中使用 REGEXP 操作符来进行正则表达式匹配。

模式描述
^ 匹配输入字符串的开始位置。 
$ 匹配输入字符串的结束位置。
. 匹配任何字符(包括回车和新行)
[...] 字符集合。匹配所包含的任意一个字符。例如, \'[abc]\' 可以匹配 "plain" 中的 \'a\'。
[^...] 负值字符集合。匹配未包含的任意字符。例如, \'[^abc]\' 可以匹配 "plain" 中的\'p\'。
p1|p2|p3 匹配 p1 或 p2 或 p3。例如,\'z|food\' 能匹配 "z" 或 "food"。\'(z|f)ood\' 则匹配 "zood" 或 "food"。

 

# ^  匹配 name 名称 以 "e" 开头的数据

select * from person where name REGEXP \'^e\';

 

# $  匹配 name 名称 以 "n" 结尾的数据

select * from person where name REGEXP \'n$\';

 

# . 匹配 name 名称 第二位后包含"x"的人员 "."表示任意字符

select * from person where name REGEXP \'.x\';

 

# [abci] 匹配 name 名称中含有指定集合内容的人员

select * from person where name REGEXP \'[abci]\';

 

# [^alex] 匹配 不符合集合中条件的内容 , ^表示取反

select * from person where name REGEXP \'[^alex]\';

#注意1:^只有在[]内才是取反的意思,在别的地方都是表示开始处匹配

#注意2 : 简单理解 name  REGEXP \'[^alex]\' 等价于 name != \'alex\'

 

# \'a|x\' 匹配 条件中的任意值

select * from person where name REGEXP \'a|x\';  

 

#查询以w开头以i结尾的数据

select * from person where name regexp \'^w.*i$\';

#注意:^w 表示w开头, .*表示中间可以有任意多个字符, i$表示以 i结尾

正则详情参考 :http://www.cnblogs.com/wangfengming/articles/8067037.html

十一 SQL 语句关键字的执行顺序

查询:姓名不同人员的最高工资,并且要求大于5000元,同时按最大工资进行排序并取出前5条.

select name, max(salary)

   

from person   

   

where name is not null  

   

group by以上是关于MYSQL之数据操作的主要内容,如果未能解决你的问题,请参考以下文章

用python操作mysql数据库(之代码归类)

linux中怎么查看mysql数据库版本

部分代码片段

0基础学习mysql之DML-表中数据的操作

用python操作mysql数据库(之简单查操作)

MySQL触发器