1. 连接到 MySQL 数据库
mysql -uroot -p123456
2. 创建 School 数据库
mysql> create database School;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| School |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
3. 使用 School 数据库
mysql> use School;
Database changed
4. 创建 Class 表
创建一个名为 Class
的表,用于存储学生的信息。表结构如下:
-
id
: 主键,整数类型,4 字节,自动递增。 -
name
: 唯一键,不可为空,用于存储学生姓名。 -
address
: 字符串类型,最大长度 40 字节,存储学生地址。 -
hobby
: 可变长度字符串类型,用于存储学生爱好。mysql> create table Class (id int auto_increment primary key,name varchar(50) unique not null,address char(40),hobby varchar(255));
Query OK, 0 rows affected (0.00 sec)mysql> show tables;
+------------------+
| Tables_in_School |
+------------------+
| Class |
+------------------+
1 row in set (0.00 sec)mysql> desc Class;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | UNI | NULL | |
| address | char(40) | YES | | NULL | |
| hobby | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)
5. 插入数据
mysql> insert into Class values ('1','aaa','beijing','read');
Query OK, 1 row affected (0.00 sec)
mysql> insert into Class (name,address,hobby) values ('bbb','shanghai','swim'),('ccc','guangzhou','cook');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
6. 查询数据
mysql> select * from Class;
+----+------+-----------+-------+
| id | name | address | hobby |
+----+------+-----------+-------+
| 1 | aaa | beijing | read |
| 2 | bbb | shanghai | swim |
| 3 | ccc | guangzhou | cook |
+----+------+-----------+-------+
3 rows in set (0.00 sec)
7. 更新数据
假设我们要更新学生 aaa 的地址为 'nanjing':
mysql> update Class set address = 'naning' where name = 'aaa';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Class;
+----+------+-----------+-------+
| id | name | address | hobby |
+----+------+-----------+-------+
| 1 | aaa | naning | read |
| 2 | bbb | shanghai | swim |
| 3 | ccc | guangzhou | cook |
+----+------+-----------+-------+
3 rows in set (0.01 sec)
8. 删除数据
如果我们要删除学生 bbb的信息:
mysql> delete from Class where name = 'bbb';
Query OK, 1 row affected (0.00 sec)
mysql> select * from Class;
+----+------+-----------+-------+
| id | name | address | hobby |
+----+------+-----------+-------+
| 1 | aaa | naning | read |
| 3 | ccc | guangzhou | cook |
+----+------+-----------+-------+
2 rows in set (0.00 sec)
9.复制表
mysql> create table Class2 like Class;
Query OK, 0 rows affected (0.01 sec)
mysql> desc Class2;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | UNI | NULL | |
| address | char(40) | YES | | NULL | |
| hobby | varchar(255) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into Class2 select * from Class;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Class2;
+----+------+-----------+-------+
| id | name | address | hobby |
+----+------+-----------+-------+
| 1 | aaa | naning | read |
| 3 | ccc | guangzhou | cook |
+----+------+-----------+-------+
2 rows in set (0.00 sec)
10.修改字段属性
将id修改为4位整型并自动补0
mysql> alter table Class modify id int(4) zerofill;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Class;
+------+------+-----------+-------+
| id | name | address | hobby |
+------+------+-----------+-------+
| 0001 | aaa | naning | read |
| 0003 | ccc | guangzhou | cook |
+------+------+-----------+-------+
2 rows in set (0.00 sec)
mysql> desc Class;
+---------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------------------+------+-----+---------+-------+
| id | int(4) unsigned zerofill | NO | PRI | NULL | |
| name | varchar(50) | NO | UNI | NULL | |
| address | char(40) | YES | | NULL | |
| hobby | varchar(255) | YES | | NULL | |
+---------+--------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
11.修删除字段
mysql> alter table Class drop hobby;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from Class;
+------+------+-----------+
| id | name | address |
+------+------+-----------+
| 0001 | aaa | naning |
| 0003 | ccc | guangzhou |
+------+------+-----------+
2 rows in set (0.00 sec)
12.删除表
delete删除后表还在,表内数据被删除
mysql> delete from Class2;
Query OK, 2 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_School |
+------------------+
| Class |
| Class2 |
+------------------+
2 rows in set (0.00 sec)
mysql> select * from Class2;
Empty set (0.00 sec)
drop删除后表被删除且无法回滚
mysql> drop table Class2;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_School |
+------------------+
| Class |
+------------------+
1 row in set (0.00 sec)
13.远程登录mysql
创建lisi用户,指定登录网段为192.168.1.0,只拥有查看权限
mysql> create user 'lisi'@'192.168.1.%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> grant select,show view on School.* to 'lisi'@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
使用192.168.1.0网段其他机器验证
[root@centos7-3 ~]#mysql -h 192.168.1.22 -u lisi -p123123
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 8
Server version: 5.7.17 Source distribution
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| School |
+--------------------+
2 rows in set (0.00 sec)
mysql> use School;
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 tables;
+------------------+
| Tables_in_School |
+------------------+
| Class |
+------------------+
1 row in set (0.00 sec)
mysql> select * from Class;
+------+------+-----------+
| id | name | address |
+------+------+-----------+
| 0001 | aaa | naning |
| 0003 | ccc | guangzhou |
+------+------+-----------+
2 rows in set (0.00 sec)
尝试使用lisi用户执行delete命令
mysql> delete from Class;
ERROR 1142 (42000): DELETE command denied to user 'lisi'@'192.168.1.33' for table 'Class'
出现报错权限不足,提权后再试
grant 命令给于lisi用户全部权限
mysql> use School;
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> GRANT ALL PRIVILEGES ON Class TO 'lisi'@'192.168.1.%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
再次验证
mysql> delete from Class;
Query OK, 2 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_School |
+------------------+
| Class |
+------------------+
1 row in set (0.00 sec)
mysql> select * from Class;
Empty set (0.00 sec)
mysql> drop table Class;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)