DML是对表中的数据进行增删改操作
DQL是对表中数据的提取操作
数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端
每个命令后面都要有 ;
DML:
登录到数据库
mysql -u root -p
创建新的数据库
create database 数据库名字
删除数据库
drop database 数据库名字
查看所有数据库列表
show databases
使用创建好的数据库
use 数据库名字
创建表
creat table ‘表名’{‘列名1’ int(11),‘列名2’ varchar(50),‘列名3’ date}
在表中添加一列数据
alter table 表名 add 列名 数据类型(如:varchar(2))
吧表中所有分数改成90
update 表名 set 分数=90
把某表中名字为张三的分数改成90,年龄加1
update 表名 set 分数=90,年龄=年龄+1 where name=‘张三’
退出当前表
exit
修改数据库登陆密码
update user set authentication_string=password(‘修改后的密码’) where user=‘root’ and Host=‘localhost’
或
mysqladmin -u root -p password 修改后的密码
改完密码后要刷新数据库权限相关表
刷新数据库权限相关表
flush privileges
退出数据库系统
quit
删除表中的某行名为ww数据
delete from 表名 where name=‘ww’
删除表中的所有数据
delete from 表名或truncate table 表名
DQL:
查看某数据库中某表的所有信息
select * from 表名
查询表中指定列
select 列名1,列名2… from 表名
条件查询
where +
between…and:值在什么范围
IN(set):固定的范围值
is null:为空,is not null:不为空
and:与
or:或
not:非
各种运算符
如:select * from 表名 where 列名=‘值’ and 列名=‘值’
如:select * from 表名 where 列名 in(值1,值2…)
如:select * from 表名 where 列名 is null;
如:select * from 表名 where 列名 between 值1 and 值2
模糊查询
用 * 表示显示包括符合条件的字段的所有字段
查询有n个字母构成的关键字
select * from 表名 where 列 like ‘n个_’
如:select * from 表名 where 列 like ‘_____’
查询某列以某字母开头的所有记录
select * from 表名 where 列 like '某个字母%‘
查询某列以第二个字母为某字母的所有记录
select * from 表名 where 列 like '_某个字母%‘
查询某列中包含某字母的所有记录
select * from 表名 where 列 like '%某个字母%‘
去掉重复名字的操作
select distinct 列名 from 表名
如果列名为空就把他变成一个值
ifnull(列名,值)
排序
order by
默认升序排列
如:select * from employee order by salary asc(升序) 或 desc(降序),id asc
聚合函数
count():统计指定列不为NULL的记录行数
max():计算指定列的最大值,如果是字符则使用字符排序运算
min():计算列的最小值
sum():计算指定列的数值和
avg():计算制定列的平均值
如:
select count(*或列名),count(列名2) from 表名 where ifnull(salary,0)+ifnull(id,0) > 2500
select sum(salary),sum(id) from 表名
分组查询
group by
select 列名,group_concat(‘列名2’),min(列名2) from 表名 group by 列名