mysql基础操作

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)
相关推荐
这个DBA有点耶4 小时前
NULL不是空——数据库里最反直觉的设计,90%新人踩过的坑
数据库·mysql·代码规范
这个DBA有点耶6 小时前
AI写的SQL跑崩了生产库,这锅谁背?
数据库·人工智能·程序员
镜舟科技7 小时前
Databricks 再提 LTAP,AI 时代的数据底座为何重回大一统叙事?
数据库·架构·agent
Databend8 小时前
从湖仓升级为 Agent 时代的数据控制面,Snowflake 和 Databricks 有哪些布局
大数据·数据库·agent
ClouGence11 小时前
SQL Server CDC 能放到 Always On 备库读吗?一文讲透原理与实践
数据库·sql server
先吃饱再说1 天前
存储的进化:从 MySQL 到浏览器缓存,数据到底住在哪?
数据库
Nturmoils1 天前
字段太多看不全,ksql 的展开模式和输出控制怎么用
数据库·后端
Databend1 天前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
这个DBA有点耶1 天前
SQL改写进阶:标量子查询的“隐形代价”与消除实战
数据库·mysql·架构
smallyoung1 天前
数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南
数据库·mysql·postgresql