MySQL基本操作
SQL语言共分为四大类:数据查询语言DQL,数据操纵语言DML,数据定义语言DDL,数据控制语言DCL。
DDL(data definition language)数据库定义语言:
创建表的时候用到的一些sql,比如说:CREATE、ALTER、DROP等。DDL主要是用在定义或改变表的结构,数据类型,表之间的链接和约束等初始化工作上。
DML(data manipulation language)数据操纵语言:
经常用到的 SELECT、UPDATE、INSERT、DELETE。 主要用来对数据库的数据进行一些操作。
DQL 数据查询语言:
数据查询语言DQL基本结构是由SELECT子句,FROM子句,WHERE 子句组成的查询块。
DCL(Data Control Language)数据库控制语言:
用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。
----------DDL----------
----------DML----------
插入数据insert
insert into 表名(字段1、字段2、字段3...字段n) values (值1、值2、值3...值n);
insert into 表名 values (值1、值2、值3...值n);
注:可以指定字段;可以插入多条记录;可以后面跟'... select ... from ... where;'插入查询结果。
更新数据update
update 表名 set 字段1=值1,字段2=值2 where 字段3=值3;
例子:更新管理员密码
update mysql.user set authentication_string=password('123456') where user='root' and host='localhost';
注:password()是加密函数。
删除数据delete
delete from 表名 where ...;
----------DQL----------
简单查询:select 字段1,字段2,字段3... from 数据库.数据表;
避免重复:select distinct 字段1,字段2,字段3... from 数据库.数据表;
通过运算查询:select 字段1,字段2*12 as 新字段 from 数据库.数据表;(as指别名)
单条件查询:where
多条件查询:and/or
关键字在什么之间:between and
例:select 字段1,字段2 from 表 where 字段2 between 5000 and 10000;
关键字:is null
例:select 字段1,字段2 from 表 where 字段2 is null;
select 字段1,字段2 from 表 where 字段2 is not null;
关键字:in集合查询
例:select 字段1,字段2 from 表 where 字段2 in (1000,2000,3000);
select 字段1,字段2 from 表 where 字段2 not in (1000,2000,3000);
关键字:like模糊查询
例:select 字段1,字段2 from 表 where 字段2 like 'ab%';
通配符‘%’代表多个任意字符;
通配符‘_’代表一个任意字符;
查询排序order by
按单列排序
例:select * from 表 order by 字段;(末尾默认不写是升序‘ASC’,降序是‘DESC’)
按多列排序
例:select * from 表 order by 字段1 DESC,字段2 ASC;
限制查询的记录数limit
例:select * from 表 order by 字段 DESC limit 5;(限制查看5条,默认从0开始查看,当查看从第四条开始共显示5条时‘limit 3,5’)
函数查询
COUNT()计数:select COUNT(*) from 表 where 字段=值;(统计字段对应的那个值所在的记录,在表内有多少个)
MAX()最大:select MAX(字段) from 表;
MIN()最小:select min(字段) from 表;
AVG()平均值:select AVG(字段) from 表;
SUM()求和:select SUM(字段) from 表;
GROUP_CONCAT()字段中的值合并到一起显示:select group_concat(字段1,字段2) from 表;
分组查询GROUP BY
例:select 字段1,SUM(字段2) from 表 group by 字段1;(将字段1的值分组去重,并统计每组的字段2求和)
正则表达式查询
例:select * from 表 where 字段 REGEXP '^ali';(使用正则表达式相关语法即可)
多表的连接查询
【未完。。。】
多条件连接查询
【未完。。。】
子查询
【未完。。。】
----------DCL----------
创建用户、赋予权限
grant 权限列表 on 库名.表名 to '用户名'@'客户端主机' [identified by '密码' with option参数];
权限列表:all、select、update
客户端主机:localhost、指定具体主机ip、利用‘%’指定一定范围的主机(192.168.122.%为指定192.168.122.0网段的所有主机)
option参数:MAX_QUERIES_PER_HOUR 定义每小时允许执行的查询数
MAX_UPDATES_PER_HOUR 定义每小时允许执行的更新数
MAX_CONNECTIONS_PER_HOUR 定义每小时可以建立的连接数
MAX_USER_CONNECTIONS 定义单个用户同时可以建立的连接数
grant select(字段1),insert(字段2,字段3) on db1.table1 to 'user4'@'localhost' identified by 'qqqq@123' with MAX_QUERIES_PER_HOUR 5;
grant all on *.* to 'user1'@'localhost' identified by 'qqq@12345';(全局设置)
grant all on db1.* to 'user2'@'localhost' identified by 'qqq@12345';(库级)
回收权限
查看自己的权限:show grants\G
查看别人的权限:show grants for admin1@'%'\G
revoke 权限列表 on 数据库名 from 用户名@‘客户端主机’;
更新权限
flush privileges;
删除用户
drop user 'user1'@'localhost';
delete from mysql.user where user='user1' and localhost='localhost';
修改用户密码
root修改自己密码
[shell环境]#mysqladmin -uroot -p'123' password 'new_password';
update mysql.user set authentication_string=password('new_password') where user='root' and host='localhost';
set password=password('new_passsword');
root修改其他用户密码
set password for user1@'localhost'=password('new_password');
update mysql.user set authentication_string=password('new_password') where user='user1' and host='localhost';
普通用户修改自己密码
set password=password('new_password');
丢失root用户密码
#vim /etc/my.cnf
[mysqld]
skip-grant-tables
#service mysqld restart
#mysql -uroot
mysql>update mysql.user set authentication_string=password('new_string') where user='root' and host='localhost';
mysqld>flush privileges;
登陆和退出mysql
#mysql -h 192.168.122.12 -P 3306 -u root -p 123 [db1] -e 'show tables'(免登陆查询表)
-h 指定主机名 默认localhost
-P mysql服务器端口 默认3306
-u 指定用户名 默认root
-p 指定登陆密码 默认空密码
[db1] 此处指定为登陆的数据库
-e 接SQL语句