
DML是(Data Manipulation Languages)数据定义语言的缩写
主要包括表记录的插入insert、更新update、删除select
插入记录
insert into table(field1,field2,...fieldn) values(value1,value2,...value)如:向表 emp 中插入以下记录:+--------+----------+---+------+| ename | hiredate | sal | deptno+--------+---------+----+------+| xxto | 2000-01-01 | kk | 1+--------+---------+---+-------+
mysql> insert into emp(ename,hiredate,sal,deptno) value('xxto','2000-01-01','kk',1);
在字段是设置默认值、允许为空、自增字段,可以不用在value后写值,字段名可不用写
如:只对表中的 ename 和 sal字段显式插入值
mysql> insert into emp(ename,sal) value ('dony', 1000); mysql> insert into emp(ename,sal) value ('dony', 1000);mysql> select * from emp;+------+-------+-------+------------+---------+--------+| age | ename | birth | hiredate | sal | deptno |+------+-------+-------+------------+---------+--------+| NULL | zzxl | NULL | 2000-01-01 | 2000.00 | 1 || NULL | dony | NULL | NULL | 1000.00 | NULL |+------+-------+-------+------------+---------+--------+2 rows in set (0.00 sec)
插入多条值
mysql> insert into emp(ename,sal) value ('name1', 1001), ('name2', 1002);mysql> insert into emp(ename,sal) value ('name1', 1001), ('name2', 1002); mysql> select * from emp;+------+-------+-------+------------+---------+--------+| age | ename | birth | hiredate | sal | deptno |+------+-------+-------+------------+---------+--------+| NULL | zzxl | NULL | 2000-01-01 | 2000.00 | 1 || NULL | dony | NULL | NULL | 1000.00 | NULL || NULL | name1 | NULL | NULL | 1001.00 | NULL || NULL | name2 | NULL | NULL | 1002.00 | NULL |+------+-------+-------+------------+---------+--------+4 rows in set (0.00 sec)
更新记录
update tablename set field1=value1,field2=value2,...fieldn =valuen[where condition]
如:将表 emp 中 ename 为 “name1”的 sal 从 1001 更改为 4000
mysql> update emp set sal=4000 where ename='name1';
同时更新两个数据表的数据
mysql> update emp a, dept b set a.sal=a.sal*b.deptno, b.deptname=a.ename where a.deptno=b.deptno;
查询记录
select * from tablename[where condition]
查询表中所有字段
mysql> select * from emp;
查询不重复字段值的记录(需要使用关键字distinct),如:查询字段deptno
mysql> select * from emp;+------+-------+-------+------------+---------+--------+| age | ename | birth | hiredate | sal | deptno |+------+-------+-------+------------+---------+--------+| NULL | zzxl | NULL | 2000-01-01 | 2000.00 | 1 || NULL | dony | NULL | NULL | 1000.00 | 2 || NULL | name1 | NULL | NULL | 4000.00 | 1 || NULL | name2 | NULL | NULL | 1002.00 | 2 |+------+-------+-------+------------+---------+--------+4 rows in set (0.00 sec)mysql> select distinct deptno from emp;+--------+| deptno |+--------+| 1 || 2 |+--------+2 rows in set (0.00 sec)
根据条件查询
mysql> select * from emp where deptno=1;
多条件查询
mysql> select * from emp where deptno=1 and sal<3000;
排序和查询
select * from tablename [where condition] [order by field1 [DESC|ASC], field2 [DESC|ASC,...field[DESC|ASC]]]默认是升序排列(ASC)field
如:吧emp表中的记录按照 sal 高低进行显示
mysql> select * from emp order by sal;
根据多个字段排序
从第一个字段排序,遇到相同的值则相同的记录以第二个字段重新裴谞,以此类推。
mysql> select * from emp order by deptno,sal desc;+------+-------+-------+------------+---------+--------+| age | ename | birth | hiredate | sal | deptno |+------+-------+-------+------------+---------+--------+| NULL | name1 | NULL | NULL | 4000.00 | 1 || NULL | zzxl | NULL | 2000-01-01 | 2000.00 | 1 || NULL | name2 | NULL | NULL | 1002.00 | 2 || NULL | dony | NULL | NULL | 1000.00 | 2 |+------+-------+-------+------------+---------+--------+
限制显示的记录
select ...[limit offset_start,row_count]
offset_start表示记录的起始偏移量,如果没写则默认为0,记录起始下标为0。row_count表示显示的行数
mysql> select * from emp order by sal limit 3;mysql> select * from emp order by sal limit 1,4;
聚合
对数据进行汇总、求和、最大值、最小值等条件的过滤
在 emp 表中统计记录数
mysql> select count(1) from emp;
统计字段相同值的记录数
mysql> select deptno, count(1) from emp group by deptno;
统计字段相同值得记录数和总记录数
mysql> select deptno, count(1) from emp group by deptno with rollup;
统计字段deptno大于2的记录数
mysql> select deptno, count(1) from emp group by deptno having count(1) > 2;
统计 字段sal 的总和、最高和最低记录
mysql> select sum(sal),max(sal),min(sal) from emp;
表连接

表连接
内连接
内连接和外连接的区别在于仅选出两张表中互相匹配的记录
如:显示出两张表(empdept)中 字段(deptno)值相同记录的ename和deptname字段
mysql> select ename, deptname from emp,dept where emp.deptno=dept.deptno;
外连接
外链接分为左连接和右连接
左连接:包含所有的左边表的记录甚至是右边表中没有和它匹配的记录。
右连接:包含所有的右边表的记录甚至是左边表中没有和它匹配的记录。
如:查询表(emp 和 dept)中的字段(ename 和 deptname),条件是两边相同值得字段(deptno)
左连接:
mysql> select ename, deptname from emp left join dept on dept.deptno=emp.deptno;
右连接:
mysql> select ename, deptname from dept right join emp on dept.deptno=emp.deptno;
子查询
当进行查询的时候,需要的条件是另外一个select语句的结构,这个时候就要用到子查询。用于子查询的关键字主要包括in、not in、=、!=、exists、not exists等。
从 emp 表中查询出所有部门在 dept 表中的记录
mysql> select * from emp where deptno in ( select deptno from dept );
如果子查询记录数唯一,可以用=代替in
mysql> select * from emp where deptno = (select deptno from dept);
表连接:
mysql> select emp.* from emp, dept where emp.deptno=dept.deptno;
记录联合
将两个表的数据按照一定的查询条件查询出来后,将结果合并到一起显示出来
需要使用union和union all关键字来实现
如: 将 emp 和 dept 表中的部门编号的集合显示出来:
使用union all:全部显示
mysql> select deptno from emp -> union all -> select deptno from dept;
使用union:去除重复后显示
mysql> select deptno from emp -> union -> select deptno from dept;
选择操作
显示数据表前50行
SELECT * FROM `库名`.`表名` ORDER BY `id` DESC LIMIT 0,50;
删除操作

删除操作
删除数据表指定ID行
delete from `库名`.`表名` where `id`=82;DELETE FROM tbname WHERE id > 49 AND id < 151
修改操作
修改数据表指定行的ID值为1
update `库名`.`表名` set `id`=1 where `id`=2;
更改索引值
让自动索引从3开始
ALTER TABLE `emp` AUTO_INCREMENT=3;
查询结果输出到excel文件
select * into outfile '/tmp/tmp.xls' from help_cat where 1 order by cat_id desc limit 0,20;
图片源于网络,如有侵权请联系删除!