半天搞定MySQL(全)一

半天搞定MySQL(全)

半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)四
半天搞定MySQL(全)五
半天搞定MySQL(全)六
博主用的是8.0版本的MySQL,储存引擎是InnoDB,关于InnoDB这里不详细解释,需要的话推荐了解这篇博文(或者自行百度)https://www.jianshu.com/p/519fd7747137

cmd登陆数据库

C:\Users\12160>mysql -u root -p
Enter password: ********

退出数据库

mysql> exit
Bye

1. 用户及用户权限

  1. 添加用户
mysql> create user 'user'@'localhost' identified by '12345678';
Query OK, 0 rows affected (0.01 sec)
  1. 赋予用户权限
    查找,删除等
  mysql> grant select,insert,update,delete,drop,alter on *.* to 'user'@'localhost';
Query OK, 0 rows affected (0.01 sec)

操作外键,索引权限

mysql> grant references on *.* to 'user'@'localhost';
Query OK, 0 rows affected (0.01 sec)
mysql> grant index on *.* to 'user'@'localhost';
Query OK, 0 rows affected (0.01 sec)
  1. 查看用户权限
mysql> show grants for 'user'@'localhost';
+--------------------------------------------------------------------------------+
| Grants for user@localhost                                                      |
+--------------------------------------------------------------------------------+
| GRANT SELECT, INSERT, UPDATE, DELETE, DROP, ALTER ON *.* TO `user`@`localhost` |
+--------------------------------------------------------------------------------+
1 row in set (0.01 sec)
  1. 撤销用户权限
mysql> grant all on *.* to 'Amos'@'%';
Query OK, 0 rows affected (0.01 sec)

mysql> revoke all on *.* from 'Amos'@'%';
Query OK, 0 rows affected (0.01 sec)

2. 管理MySQL命令

  1. USE 数据库名 :
sql mysql> use class 
Database changed
  1. 列出 MySQL的数据库列表
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| class              |
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
7 rows in set (0.02 sec)
  1. 显示指定数据库的所有表

使用该命令前需要使用 use 命令来选择要操作的数据库。

mysql> show tables;
+-----------------+
| Tables_in_class |
+-----------------+
| score           |
| student_login   |
| students        |
| teacher         |
| teacher_course  |
| teacher_login   |
+-----------------+
6 rows in set (0.01 sec)
  1. 显示数据表的详细索引信息
mysql> show index from teacher;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| teacher |          0 | PRIMARY  |            1 | name        | A         |           2 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
1 row in set (0.02 sec)
  1. 输出Mysql数据库管理系统的性能及统计信息
mysql> show table status from class;
+----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| Name           | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation          | Checksum | Create_options | Comment |
+----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
| score          | InnoDB |      10 | Dynamic    |    2 |           8192 |       16384 |               0 |            0 |         0 |           NULL | 2020-06-07 18:02:39 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
| student_login  | InnoDB |      10 | Dynamic    |    2 |           8192 |       16384 |               0 |            0 |         0 |           NULL | 2020-06-08 11:28:09 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
| students       | InnoDB |      10 | Dynamic    |    2 |           8192 |       16384 |               0 |            0 |         0 |           NULL | 2020-06-08 13:40:56 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
| teacher        | InnoDB |      10 | Dynamic    |    2 |           8192 |       16384 |               0 |            0 |         0 |           NULL | 2020-06-08 13:47:56 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
| teacher_course | InnoDB |      10 | Dynamic    |    0 |              0 |       16384 |               0 |            0 |         0 |           NULL | 2020-06-08 14:54:36 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
| teacher_login  | InnoDB |      10 | Dynamic    |    2 |           8192 |       16384 |               0 |            0 |         0 |         100001 | 2020-06-08 11:41:33 | NULL        | NULL       | utf8mb4_0900_ai_ci |     NULL |                |         |
+----------------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+--------------------+----------+----------------+---------+
6 rows in set (0.02 sec)

3. 数据库基本操作
创建数据库

mysql> create database skill;
Query OK, 1 row affected (0.01 sec)

删除数据库

mysql> drop database skill;
Query OK, 0 rows affected (0.02 sec)

数据类型
MySQL支持多种类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。

UTF-8:一个汉字=3个字节 GBK:一个汉字=2个字节

数值类型

类型 大小
TINYINT 1 byte
SMALLINT 2 bytes
INT或INTEGER 3 bytes
BIGINT 8 bytes
FLOAT 4 bytes
DOUBLE 8 bytes
DECIMAL 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2

日期和时间类型
TIMESTAMP类型有专有的自动更新特性

类型 大小(bytes) 格式 用途
DATE 3 YYYY-MM-DD 日期值
TIME 3 HH:MM:SS 时间值或持续时间
YEAR 4 YYYY 年份值
DATETIME 8 YYYY-MM-DD HH:MM:SS 混合日期和时间值
TIMESTAMP 4 YYYYMMDD HHMMSS 混合日期和时间值,时间戳

字符串类型

类型 大小 用途
CHAR 0-255 bytes 定长字符串
VARCHAR 0-65535 bytes 变长字符串
TINYBLOB 0-255 bytes 不超过 255 个字符的二进制字符串
TINYTEXT 0-255 bytes 短文本字符串
BLOB 0-65 535 bytes 二进制形式的长文本数据
TEXT 0-65 535 bytes 长文本数据
MEDIUMBLOB 0-16 777 215 bytes 二进制形式的中等长度文本数据
MEDIUMTEXT 0-16 777 215 bytes 中等长度文本数据
LONGBLOB 0-4 294 967 295 bytes 二进制形式的极大文本数据
LONGTEXT 0-4 294 967 295 bytes 极大文本数据

经常变化的字段用 varchar
知道固定长度的用 char
尽量用 varchar
超过 255 字符的只能用 varchar
或者 text 能用 varchar 的地方不用 text

创建数据表
(这里博主踩了个坑,单引号必须使用左上角 ~` 这个单引号,左边的单引号 是不行的)

mysql> create table if not exists `city_spots`(
    -> `city_name` varchar(20) primary key,
    -> `place_of_interest'` varchar(20),
    -> `city_score` int,
    -> `cost_rmb` float,
    -> `time_way` float,
    -> `time_stay` float);
Query OK, 0 rows affected (0.03 sec)

查看数据表详情

mysql> desc city_spots;
+--------------------+-------------+------+-----+---------+-------+
| Field              | Type        | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+-------+
| city_name          | varchar(20) | NO   | PRI | NULL    |       |
| place_of_interest' | varchar(20) | YES  |     | NULL    |       |
| city_score         | int         | YES  |     | NULL    |       |
| cost_rmb           | float       | YES  |     | NULL    |       |
| time_way           | float       | YES  |     | NULL    |       |
| time_stay          | float       | YES  |     | NULL    |       |
+--------------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

删除数据表

mysql> drop table `city_spots`;
Query OK, 0 rows affected (0.04 sec)

半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)四
半天搞定MySQL(全)五
半天搞定MySQL(全)六

猜你喜欢

转载自blog.csdn.net/lovedingning/article/details/106815566