//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
//创建数据库DNP
mysql> CREATE DATABASE IF NOT EXISTS DNP;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| DNP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库DNP
mysql> CREATE DATABASE IF NOT EXISTS DNP;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| DNP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> DROP DATABASE IF EXISTS DNP;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
//创建数据库时,设置数据库的字符集:
//CHARACTER SET:指定数据库采用的字符集,utf8不能写成utf-8,建议使用utf8mb4字符集
//COLLATE:指定数据库字符集的排序规则,utf8mb4的默认排序规则为utf8mb4_general_ci(通过show character set查看)
mysql> CREATE DATABASE dbtest CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
//数据库客户端字符编码需要和服务端字符集保持一致
//SET NAMES:指定客户端字符集
mysql> SET NAMES utf8mb4;
//字符集配置都可写入MySQL配置文件中启动MySQL服务时自动加载
//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
//在数据库dnp里创建表dunianpei
mysql> CREATE DATABASE dnP;
Query OK, 1 row affected (0.00 sec)
mysql> use dnP;
Database changed
//创建dunianpei表
mysql> CREATE TABLE dunianpei (id int(50) NOT NULL,name VARCHAR(120) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.00 sec)
mysql> show tables; //查看当前数据库有哪些表
+---------------+
| Tables_in_dnP |
+---------------+
| dunianpei |
+---------------+
1 row in set (0.00 sec)
//删除表
//语法:DROP TABLE [ IF EXISTS ] 'table_name';
//删除表dunianpei
mysql> DROP TABLE dunianpei;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec
MySQL用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录;HOST用于限制此用户可通过哪些主机远程连接mysql程序。
HOST的值可为:
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建数据库用户dnp
mysql> CREATE USER 'dnp'@'127.0.0.1' IDENTIFIED BY 'dnp123456.';
Query OK, 0 rows affected (0.00 sec)
//刷新授权表
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
//使用新创建的用户和密码登录
[root@localhost ~]# mysql -udnp -pdnp123456. -h127.0.0.1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//删除数据库用户
//语法:DROP USER 'username'@'host';
//删除数据库用户dnp
mysql> DROP USER 'dnp'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)
//刷新授权表
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW CHARACTER SET; //查看支持的所有字符集
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
mysql> SHOW VARIABLES LIKE '%char%'; //查看客户端的字符编码
......
mysql> SELECT CHARSET(email) FROM tbtest; //查看某表中某字段使用的字符编码
mysql> SHOW ENGINES; //查看当前数据库支持的所有存储引擎
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dnP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW TABLES FROM dnP; //不进入某数据库而列出其包含的所有表
+---------------+
| Tables_in_dnP |
+---------------+
| dunianpei |
+---------------+
1 row in set (0.00 sec)
//查看表结构
//语法:DESC [db_name.]table_name;
mysql> DESC dnP.dunianpei;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(50) | NO | | NULL | |
| name | varchar(120) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
//查看某表的创建命令,可以看到创建表时设置的参数
//语法:SHOW CREATE TABLE table_name;
mysql> SHOW CREATE TABLE dnP.dunianpei;
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| dunianpei | CREATE TABLE `dunianpei` (
`id` int(50) NOT NULL,
`name` varchar(120) NOT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-----------+------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
//查看某表的状态
//语法:SHOW TABLE STATUS LIKE 'table_name'\G
mysql> use dnP;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLE STATUS LIKE 'dunianpei'\G //查看dunianpei表的状态
*************************** 1. row ***************************
Name: dunianpei
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2022-07-26 14:11:27
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
//修改数据库属性
//语法:ALTER DATABASE 'DB_NAME' CHARACTER SET charset_name | COLLATE collation_name
//修改数据库字符集为utf8
mysql> ALTER DATABASE dnP CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)
//修改表
//语法:ALTER TABLE [option]
//给dunianpei表添加新的一列,先查看表结构
mysql> DESC dunianpei;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(50) | NO | | NULL | |
| name | varchar(120) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
//给dunianpei表添加一列'phone'
mysql> ALTER TABLE dunianpei ADD phone int(11);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
//查看表结构
mysql> DESC dunianpei;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(50) | NO | | NULL | |
| name | varchar(120) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
//修改dunianpei表中'phone'列为'sex'
mysql> ALTER TABLE dunianpei CHANGE phone sex varchar(4);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC dunianpei;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(50) | NO | | NULL | |
| name | varchar(120) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
//修改表名为Sia
mysql> ALTER TABLE dunianpei RENAME TO Sia;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES;
+---------------+
| Tables_in_dnP |
+---------------+
| Sia |
+---------------+
1 row in set (0.00 sec)
//修改数据库字符编码
mysql> ALTER DATABASE dbtest CHARACTER SET utf8 COLLATE utf8_general_ci;
//修改表字符编码
mysql> ALTER TABLE tbtest CHARACTER SET utf8 COLLATE utf8_general_ci;
//获取命令使用帮助
//语法:HELP commond;
mysql> HELP CREATE TABLE;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
[IGNORE | REPLACE]
[AS] query_expression
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition: {
col_name column_definition
| {INDEX | KEY} [index_name] [index_type] (key_part,...)
......
......
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。
//DML操作之增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
mysql> use dnP;
Database changed
//一次插入一条记录
mysql> INSERT INTO Sia (id,name,age) VALUE (1,'tom',20);
Query OK, 1 row affected (0.00 sec)
//一次插入多条记录
mysql> INSERT INTO Sia (id,name,age) VALUES (2,'jerry',23),(3,'zbh',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',NULL);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0
字段column表示法
表示符 | 代表什么? |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | AND OR NOT |
ORDER BY:排序,默认为升序(ASC)
ORDER BY语句 | 意义 |
---|---|
ORDER BY ‘column_name’ | 根据column_name进行升序排序 |
ORDER BY ‘column_name’ DESC | 根据column_name进行降序排序 |
ORDER BY ’column_name’ LIMIT 2 | 根据column_name进行升序排序 并只取前2个结果 |
ORDER BY ‘column_name’ LIMIT 1,2 | 根据column_name进行升序排序 并且略过第1个结果取后面的2个结果 |
//DML操作之查操作select
//语法:SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> use dnP;
Database changed
mysql> select * from Sia;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 1 | tom | 20 | NULL |
| 2 | jerry | 23 | NULL |
| 3 | zbh | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
| 7 | lisi | NULL | NULL |
| 1 | tom | 20 | NULL |
+----+-----------+------+------+
8 rows in set (0.00 sec)
mysql> SELECT name FROM Sia;
+-----------+
| name |
+-----------+
| tom |
| jerry |
| zbh |
| sean |
| zhangshan |
| zhangshan |
| lisi |
| tom |
+-----------+
8 rows in set (0.00 sec)
mysql> SELECT * FROM Sia WHERE age >= 25;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 3 | dnp | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
+----+-----------+------+------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM zbhtable WHERE age >= 25 AND name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.00 sec)
mysql> SELECT * FROM Sia WHERE age BETWEEN 23 and 28;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 2 | jerry | 23 | NULL |
| 3 | dnp | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
+----+-----------+------+------+
4 rows in set (0.00 sec)
mysql> select * from Sia where age is not null;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 1 | tom | 20 | NULL |
| 2 | jerry | 23 | NULL |
| 3 | dnp | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
| 1 | tom | 20 | NULL |
+----+-----------+------+------+
7 rows in set (0.00 sec)
mysql> select * from Sia where age is null;
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 7 | lisi | NULL | NULL |
+----+------+------+------+
1 row in set (0.00 sec)
mysql> select * from Sia where name like '%z%';
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 3 | zbh | 25 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
+----+-----------+------+------+
3 rows in set (0.00 sec)
//DML操作之改操作update
//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from Sia;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 1 | tom | 20 | NULL |
| 2 | jerry | 23 | NULL |
| 3 | zbh | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
| 7 | lisi | NULL | NULL |
| 1 | tom | 20 | NULL |
+----+-----------+------+------+
8 rows in set (0.00 sec)
mysql> update Sia set age = 30 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Sia where name = 'lisi';
+----+------+------+------+
| id | name | age | sex |
+----+------+------+------+
| 7 | lisi | 30 | NULL |
+----+------+------+------+
1 row in set (0.00 sec)
//DML操作之删操作delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> select * from Sia;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 1 | tom | 20 | NULL |
| 2 | jerry | 23 | NULL |
| 3 | zbh | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
| 7 | lisi | 30 | NULL |
| 1 | tom | 20 | NULL |
+----+-----------+------+------+
8 rows in set (0.00 sec)
mysql> delete from Sia where id = 7; //删除某条记录
Query OK, 1 row affected (0.00 sec)
mysql> select * from Sia;
+----+-----------+------+------+
| id | name | age | sex |
+----+-----------+------+------+
| 1 | tom | 20 | NULL |
| 2 | jerry | 23 | NULL |
| 3 | zbh | 25 | NULL |
| 4 | sean | 28 | NULL |
| 5 | zhangshan | 26 | NULL |
| 6 | zhangshan | 20 | NULL |
| 1 | tom | 20 | NULL |
+----+-----------+------+------+
7 rows in set (0.00 sec)
mysql> delete from Sia; //删除整张表的内容
Query OK, 7 rows affected (0.00 sec)
mysql> select * from Sia;
Empty set (0.00 sec)
mysql> desc Sia;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(50) | NO | | NULL | |
| name | varchar(120) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| sex | varchar(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
//语法:TRUNCATE table_name;
mysql> select * from Sia;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | dnp | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)
mysql> truncate Sia;
Query OK, 0 rows affected (0.01 sec)
mysql> select * from Sia;
Empty set (0.00 sec)
mysql> desc Sia;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
权限类型(priv_type)
权限类型 | 代表什么? |
---|---|
ALL | 所有权限 |
SELECT | 读取内容的权限 |
INSERT | 插入内容的权限 |
UPDATE | 更新内容的权限 |
DELETE | 删除内容的权限 |
指定要操作的对象db_name.table_name
表示方式 | 意义 |
---|---|
. | 所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。
语法:
GRANT priv_type,... ON [object_type] db_name.table_name TO ‘username'@'host' [IDENTIFIED BY 'password'] [WITH GRANT OPTION];
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dnP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//授权tom用户在数据库本机上登录访问所有数据库
mysql> grant all on *.* to 'tom'@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)
//也可表示为:
grant all on *.* to 'tom'@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权tom用户在192.168.48.150上远程登录访问dnP数据库
mysql> grant all on dnP.* to 'tom'@'192.168.48.150' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
//授权tom用户在所有位置上远程登录访问dnP数据库
mysql> grant all on dnP.* to 'tom'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
授权后利用student(图形化数据库)进行连接
//查看当前登录用户的授权信息
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//查看指定用户tom的授权信息
mysql> show grants for tom;
+----------------------------------------------+
| Grants for tom@% |
+----------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'%' |
| GRANT ALL PRIVILEGES ON `dnP`.* TO 'tom'@'%' |
+----------------------------------------------+
2 rows in set (0.00 sec)
mysql> show grants for 'tom'@'localhost';
+--------------------------------------------------+
| Grants for tom@localhost |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> show grants for 'tom'@'127.0.0.1';
+--------------------------------------------------+
| Grants for [email protected] |
+--------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'tom'@'127.0.0.1' |
+--------------------------------------------------+
1 row in set (0.00 sec)
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> show grants for 'tom'@'192.168.48.150';
+-----------------------------------------------------------+
| Grants for [email protected] |
+-----------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.48.150' |
| GRANT ALL PRIVILEGES ON `dnP`.* TO 'tom'@'192.168.48.150' |
+-----------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> revoke all on dada.* from 'tom'@'192.168.48.150';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'tom'@'192.168.48.150';
+----------------------------------------------------------------+
| Grants for [email protected] |
+----------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.48.150' |
| GRANT ALL PRIVILEGES ON `dnP`.* TO 'tom'@'192.168.48.150' |
+----------------------------------------------------------------+
2 rows in set (0.00 sec)
1.搭建mysql服务
[root@localhost ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
[root@localhost ~]# yum module disable mysql
[root@localhost ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel --nogpgcheck
[root@localhost ~]# rpm -e mysql57-community-release
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# grep "password" /var/log/mysqld.log
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.39
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> set password = password('123456');
[root@localhost ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
mysql> create database if not exists dnP;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dnP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use dnP;
Database changed
mysql> create table student (id int(11) not null key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.00 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
3.查看下该新建的表有无内容(用select语句)
mysql> select * from student;
Empty set (0.00 sec)
4.往新建的student表中插入数据(用insert语句),结果应如下所示:
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | dnP | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
mysql> insert into student (id,name,age) value (1,'tom',20);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student (id,name,age) values (2,'jerry',23),(3,'dnP',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | dnP | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.00 sec)
5.修改lisi的年龄为50
mysql> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.00 sec)
6.以age字段降序排序
mysql> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | dnP | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 6 | zhangshan | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
+----+-------------+------+
11 rows in set (0.00 sec)
7.查询student表中年龄最小的3位同学跳过前2位
mysql> select * from student order by age limit 2,3;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
3 rows in set (0.00 sec)
8.查询student表中年龄最大的4位同学
mysql> select * from student order by age desc limit 4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | dnP | 25 |
+----+-----------+------+
4 rows in set (0.00 sec)
9.查询student表中名字叫zhangshan的记录
mysql> select * from student where name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
2 rows in set (0.00 sec)
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student where age > 20 and name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.00 sec)
11.查询student表中年龄在23到30之间的记录
mysql> select * from student where age > 23 and age < 30;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 3 | dnP | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
3 rows in set (0.00 sec)
12.修改wangwu的年龄为100
mysql> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student where name = 'wangwu';
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.00 sec)
13.删除student中名字叫zhangshan且年龄小于等于20的记录
mysql> delete from student where name = 'zhangshan';
Query OK, 2 rows affected (0.00 sec)
mysql> delete from student where age <= 20;
Query OK, 4 rows affected (0.00 sec)
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | jerry | 23 |
| 3 | dnP | 25 |
| 4 | sean | 28 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+----------+------+
5 rows in set (0.00 sec)