SQL语言介绍及分类详解
什么是SQL
Structured Query Language,结构化查询语音,对关系型数据库中的数据进行定义和操作的语言方法。
结构化查询语言是一种数据库查询和程序设计语言
SQL的分类
SQL结构化查询语言包含6个部分:
- 数据查询语言DQL
- 作用是从表中获取数据,确定数据怎样在应用程序给出
- select,where,order by,group by和having
- select user,host,password from mysql.user;
- 数据操作语言DML
- insert,update,delete
- 例:delete from mysql.user where user=‘dc‘;
- 事务处理语言TPL
- 他的语句能确保被DML语句影响的表的所有行及时得以更新。
- begintransaction,commit,rollback
- 数据控制语言DCL
- 他的语句通过grant或revoke获得许可
- 数据定义语言DDL
- create,alter,drop
- 创建新表 creat table
- 删除表 drop table
- 指针控制语言CCL
- declare,cursor,fetch into,update where current
小结:SQL语句最常见的分类一般就是3类
DDL--数据定义语言create,alter,drop<---运维
DML--数据操作语言select,insert,delete,update<---开发
DCL--数据控制语言grant,revoke,commit,rollback<---运维
MySQL数据库常见管理应用
创建数据库
- create database <数据库名> ←注意库名不能数字开头
- mysql> create database oldboy;<---创建默认数据库,使用默认字符编码
- mysql> show create database oldboy \G
- mysql> create database oldboy_gbk default character set gbk collate gbk_chinese_ci;<---创建gbk字符集数据库
- mysql> create database oldboy_utf8 default character set utf8 collate utf8_general_ci;<---创建utf8字符集数据库
- 提示:字符集的不一致时数据库中文乱码的罪魁祸首。
- 提示:如果编译的时候制定了特定的字符集编码,则以后创建对应字符集的数据库就不需要指定字符集。
企业创建数据库
- 根据开发的程序确定字符集(建议utf8)!
- 编译的时候指定字符集
- -DDEFAULT_CHARSET=utf8 \
- -DDEFAULT_COLLATION=utf8_general_ci \
- 建库的时候默认创建即可,create database oldboy;
- 编译的时候没有指定字符集或指定了和程序不同的字符集,指定字符集创建数据库即可
- mysql> create database oldboy_gbk default character set gbk collate gbk_chinese_ci;<---创建gbk字符集数据库
- mysql> create database oldboy_utf8 default character set utf8 collate utf8_general_ci;<---创建utf8字符集数据库
数据库要支持创建库的字符集
显示数据库
- show databases;
- show databases lick ‘old%‘;
- select database();<---显示当前数据库
- use oldboy;
- select database();
- select version();
- select user();
- select now();
删除数据库
-drop database oldboy;
学习潜意识就查看帮助:
mysql> help drop database
连接数据库
- use <数据库名>;
- use oldboy;
当前数据库包含的表信息
- 切到数据库里面去查看
- use oldboy;
- show tables;
- show tables like ‘user‘;
- show tables in oldboy_gbk;
删除MySQL系统多于账号
- drop user ‘user‘@‘主机域‘;
- drop user ‘oldboy‘@‘localhost‘;
注意:删除后的寄过如上面返回的结果
如果drop删除不了(一般是特殊字符或大写),可以用下面方式删除(以root用户,oldboy主机为例)
delete from mysql.user where user=‘root‘ and host=‘oldboy‘;
fluch privileges;<---处理用户相关,最好在操作完成后都刷新一下权限
创建MySQL用户及赋予用户权限
通过help查看grant命令帮助
- 通过help查看grant命令帮助
- 。。。省略
- CREATE USER ‘jeffrey‘@‘localhost‘ IDENTIFIED BY ‘mypass‘;
- GRANT ALL ON db1.* TO ‘jeffrey‘@‘localhost‘;
- GRANT SELECT ON db2.invoice TO ‘jeffrey‘@‘localhost‘;
- GRANT USAGE ON . TO ‘jeffrey‘@‘localhost‘ WITH MAX_QUERIES_PER_HOUR 90;
- 。。。省略
- 运维人员比较常用的创建用户的方法是,使用grant命令在创建用户的同事进行权限授权,具体授权例子为:
- grant all on db1.* to ‘jeffrey‘@‘localhost‘ identified by ‘mypass‘;
- 上述grant命令帮助里还提供了一个先用create命令创建用户,然后再用grant授权的方法,及创建用户和授权权限分开进行,例:
- create user ‘jeffrey‘@‘localhost‘ identified by ‘mypass‘;
- grant all on db1.* to ‘jeffrey‘@‘localhost‘;
- 以上两条命令相当于下面一条命令:
- grant all on db1.* to ‘jeffrey‘@‘localhost‘ identified by ‘mypass‘;
通过grant命令你跟创建用户并授权
- grant命令简单语法如下:
- grant all privileges on dbname.* to [email protected] identified by ‘passwd‘;
- 列表说明如下:
grant | all privileges | on dbname.* | to [email protected] | identified by ‘passwd‘ |
---|---|---|---|---|
授权命令 | 对应权限 | 目标:库和表 | 用户名和客户端主机 | 用户密码 |
说明:上述命令是授权localhost主机上通过用户username管理dbname数据库的所有权限,密码为passwd。其中username,dbname,passwd可根据业务的情况修改。
- 操作案例1:
- 创建oldboy用户,对test库具备所有权限,允许从localhost主机登陆管理数据库,密码是oldboy123
- 实现上述操作的具体命令为:
- grant all privileges on test.* to ‘oldboy‘@‘localhost‘ identified by ‘oldboy123‘;
mysqlselect user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)
mysqlgrant all privileges on test.* to [email protected]‘localhost‘ identified by ‘oldboy123‘;
Query OK, 0 rows affected (0.00 sec)
mysqlflush privileges;
Query OK, 0 rows affected (0.00 sec)
mysqlselect user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| root | 127.0.0.1 |
| oldboy | localhost |
| root | localhost |
+--------+-----------+
3 rows in set (0.00 sec)
mysqlshow grants for [email protected];
+---------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘oldboy‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515‘ |
| GRANT ALL PRIVILEGES ON `test`.* TO ‘oldboy‘@‘localhost‘ |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql
create和grant配合法
- 首先差功能键用户username及密码passwd,授权主机loaclhost。
- create user ‘username‘@‘localhost‘ identified by ‘passwd‘;
- 然后授权localhost主机上通过用户username管理dbname数据库的所有权限,无需密码
- grant all on dbanme.* to ‘username‘@‘localhost‘;
- 操作案例2
- 创建oldgirl用户,对test库具备所有权限,玉虚从localhost主机登陆管理数据库,密码是oldgirl123。
- 查看当前数据库用户情况,然后执行对应命令创建用户如下:
mysql> select user,host from mysql.user;
+--------+-----------+
| user | host |
+--------+-----------+
| root | 127.0.0.1 |
| oldboy | localhost |
| root | localhost |
+--------+-----------+
3 rows in set (0.00 sec)
mysql> create user ‘oldgirl‘@‘localhost‘ identified by ‘oldgirl123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on test.* to ‘oldgirl‘@‘localhost‘;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+----------------------------------------------------------------------------------------------------------------+
| Grants for [email protected] |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO ‘oldgirl‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*2CADADD54086D5EB4C9F10E0430084D7F179885C‘ |
| GRANT ALL PRIVILEGES ON `test`.* TO ‘oldgirl‘@‘localhost‘ |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
授权局域网内主机远程链接数据库:
- 根据grant命令语法,我们知道[email protected]‘localhost’位置为授权访问数据库的主机,localhost可以用域名,IP地址或IP段来替代,因此,要授权局域网内主机可以通过如下方法实现:
- 百分号匹配法
- mysql> grant all on . to [email protected]‘10.0.0.%‘ identified by ‘test123‘;
- 子网掩码配置法
- mysql> grant all on . to [email protected]‘10.0.0.0/255.255.255.0‘ identified by ‘test123‘;
- 百分号匹配法
mysql> grant all on *.* to [email protected]‘192.168.206.%‘ identified by ‘test123‘;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+---------+---------------+
| user | host |
+---------+---------------+
| root | 127.0.0.1 |
| test | 192.168.206.% |
| oldboy | localhost |
| oldgirl | localhost |
| root | localhost |
+---------+---------------+
5 rows in set (0.00 sec)
mysql> fluch privileges;
- 通过mysql客户端连接异地数据库服务:
- 本地mysql -uroot -poldboy123连接数据库相当于mysql -uroot -poldboy123 -hlocalhost
- 要远程连接10.0.0.7的数据库,命令为mysql -utest -p -h10.0.0.7,如果要能成功连接,还需要在10.0.0.7的数据库服务器上通通过如下授权
- mysql> grant all on . to [email protected]‘10.0.0.%‘ identified by ‘test123‘;