MySQL

一.数据库

1.简介

定义:数据库是“按照数据结构来组织、存储和管理数据的仓库”。是一个长期存储在计算机内的、有组织的、可共享的、统一管理的大量数据的集合。

(一)数据库是存放数据的仓库。

(二)数据库是一个按数据结构来存储和管理数据的计算机软件系统。

数据库的概念实际包括两层意思:
(1)数据库是一个实体,它是能够合理保管数据的“仓库”,用户在该“仓库”中存放要管理的事务数据,“数据”和“库”两个概念结合成为数据库。
(2)数据库是数据管理的新方法和技术,它能更合适的组织数据、更方便的维护数据、更严密的控制数据和更有效的利用数据。
(三)数据库作为最重要的基础软件,是确保计算机系统稳定运行的基石。

2.分类

数据库分层次数据库、网状数据库和关系数据库

关系模型:
二维关系:row,column

数据库管理系统:DBMS
关系:Relational,RDBMS

2.1关系数据库
关系型数据库,存储的格式可以直观地反映实体间的关系。关系型数据库和常见的表格比较相似,关系型数据库中表与表之间是有很多复杂的关联关系的。 常见的关系型数据库有Mysql,SqlServer等

2.2标准SQL语句
虽然关系型数据库有很多,但是大多数都遵循SQL(结构化查询语言,Structured Query Language)标准。 常见的操作有查询,新增,更新,删除,求和,排序等。
查询语句:SELECT param FROM table WHERE condition 该语句可以理解为从 table 中查询出满足 condition 条件的字段 param。
新增语句:INSERT INTO table (param1,param2,param3) VALUES (value1,value2,value3) 该语句可以理解为向table中的param1,param2,param3字段中分别插入value1,value2,value3。
更新语句:UPDATE table SET param=new_value WHERE condition 该语句可以理解为将满足condition条件的字段param更新为 new_value 值。
删除语句:DELETE FROM table WHERE condition 该语句可以理解为将满足condition条件的数据全部删除。
去重查询:SELECT DISTINCT param FROM table WHERE condition 该语句可以理解为从表table中查询出满足条件condition的字段param,但是param中重复的值只能出现一次。
排序查询:SELECT param FROM table WHERE condition ORDER BY param1该语句可以理解为从表table 中查询出满足condition条件的param,并且要按照param1升序的顺序进行排序。
总体来说, 数据库的SELECT,INSERT,UPDATE,DELETE对应了我们常用的增删改查四种操作。

二、MySQL

应用架构

单点(Single),适合小规模应用
复制(Replication),适合中小规模应用
集群(Cluster),适合大规模应用

索引功能

索引是一种特殊的文件(InnoDB 数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。

索引类别

1.普通索引

普通索引(由关键字 KEY 或 INDEX 定义的索引)的任务是加快对数据的访问速度。

2.索引

好处:一是简化了 MySQL 对这个索引的管理工作,这个索引也因此而变得更有效率;二是 MySQL 会在有新记录插入数据表时,自动检查新记录的这个字段的值是否已经在某个记录的这个字段里出现过了;如果是,MySQL 将拒绝插入那条新记录。也就是说,索引可以保证数据记录的独特性

3.主索引

4.外键索引

5.复合索引

三.MySQL安装

3.1mysql安装

mysqla安装方式有三种:

            1.源代码:编译安装

            2.二进制格式的程序包

            3.程序包管理器管理的程序包: 

                   rmp: 有两种

                                  1.OS Vendor:操作系统发行商提供

                                   2.项目官方提供的

                    deb

3.2安装MySQL

1.安装样本源

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
2.安装其他

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
3.清缓存

yum clean all

4.安装mysq

wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm \
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm

5.清缓存,建缓存

yum clean all

yum makecache

6.安装mysql的其他依赖包

wget http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.43-1.el7.x86_64.rpm http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.43-1.el7.x86_64.rpm http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.43-1.el7.x86_64.rpm http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.43-1.el7.x86_64.rpm http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.43-1.el7.x86_64.rpm

7.安装mysql依赖包

yum -y localinstall *.rpm

3.3配置mysql

1.查看mysql服务

systemctl status mysqld

2.启动mysql并设置开启自启

systemctl enable --now mysql

systemctl status mysqld

2.查看监听端口(确认3306起来了)

ss -antl

[root@localhost ~]# ss -antl
State     Recv-Q    Send-Q       Local Address:Port       Peer Address:Port   Process    
LISTEN    0         128                0.0.0.0:22              0.0.0.0:*                 
LISTEN    0         80                       *:3306                  *:*                 
LISTEN    0         128                   [::]:22                 [::]:

3.在日志中找出临时密码

grep "password" /var/log/mysqld.log

4.使用临时密码登录mysql

[root@localhost ~]# mysql -uroot -p
Enter password: (输入日志中的临时密码)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.43

Copyright (c) 2000, 2023, 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>

4.修改mysql登录密码

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> alter user 'root'@'localhost' identified by 'chenxi123.';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

3.4mysql的程序组成

客户端

    mysql_secure_installation: 安全初始化,建议安装完mysql以后执行此命令

[root@localhost ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

     mysqldump:备份工具

     mysqladmin

服务端

     mysqld

3.5MySQL的使用

//语法:mysql [OPTIONS] [database]

//常用的OPTIONS: -uUSERNAME //指定用户名,默认为root

-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址

-pPASSWORD //指定用户的密码 -P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307

-V //查看当前使用的mysql版本

-e //不登录mysql执行sql语句后退出,常用于脚本

error: package mysql57-community-release is not installed
[root@localhost ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.43, for Linux (x86_64) using  EditLine wrapper

[root@localhost ~]# mysql -uroot -pchenxi123. -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

mysql: [ERROR] mysql: option '-h' requires an argument
[root@localhost ~]# mysql -uroot -pchenxi123. -h127.0.0.1(127.0.0.1是自己的ip)
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 13
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> 

(不介意直接在命令行中使用密码)

[root@localhost ~]# mysql -uroot -p -h127.0.0.1
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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> 

[root@localhost ~]#  mysql -uroot -p -h127.0.0.1 -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

3.5服务器监听的两种socket地址

四.MySQL数据库操作

4.1DDL操作
4.1.1数据库操作

1.创建数据库

语法:create database [if not exists] 数据库名称;

创建chenxi数据库

mysql> create database if not exists chenxi; 
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenxi             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

2.删除数据库

语法:drop database if exists 数据库名称;

删除chenxi数据库

mysql> drop database if exists chenxi;
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)

4.1.2表操作

1.创建表

语法:create table 表名称

在chenxi数据库创建chenxi表

mysql> create database chenxi;   创建数据库
Query OK, 1 row affected (0.00 sec)

mysql> use chenxi    进入数据库
Database changed

mysql> create table chenxi(id int not null,name varchar(100) not null,age tinyint); 创建表
Query OK, 0 rows affected (0.01 sec)

2.删除表

语法:drop table [ if exists ] 表名称;

删除表cchenxi

mysql> drop table chenxi;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
Empty set (0.00 sec)

4.1.3用户操作

mysql用户帐号由两部分组成,如'USERNAME'@'HOST',表示此USERNAME只能从此HOST上远程登录

这里('USERNAME'@'HOST')的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:

IP地址,如:172.16.12.129

通配符:

    %:匹配任意长度的任意字符,常用于设置允许从任何主机登录

    _:匹配任意单个字符

数据库创建用户

语法:create user '用户'@'主机ip' [identified by 'password'];

1.创建数据库用户chenxi

mysql> create user 'chenxi'@'127.0.0.1' identified by 'chenxi123.';
Query OK, 0 rows affected (0.00 sec)

2.使用新用户和密码登录

[root@localhost ~]# mysql -uchenxi -pchenxi123. -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 17
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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.

删除数据库用户

语法:drop user '用户'@'主机ip';

删除chenxi数据库中的用户chenxi

[root@localhost ~]# mysql -uroot -pchenxi123. -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 20
Server version: 5.7.43 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, 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 'chenxi'@'127.0.0.1';
Query OK, 0 rows affected (0.00 sec)

4.1.4查看命令show

1.查看支持所有字符集

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)

2.查看当前数据库支持的所有存储引擎

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)

3.查看数据库信息

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenxi             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

4.不进人某数据库而列出包含的所有表
mysql> show tables from chenxi;
+------------------+
| Tables_in_chenxi |
+------------------+
| chenxi           |
+------------------+
1 row in set (0.00 sec)

5.查看表结构

语法:desc [db_name.]tables_name;

mysql> desc chenxi.chenxi;
+-------+--------------+------+-----+---------+-------+
| 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.02 sec)

6.查看某表的创建命令

语法:show create table table_name;


mysql> show create table chenxi.chenxi;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                            |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
| chenxi | CREATE TABLE `chenxi` (
  `id` int(11) NOT NULL,
  `name` varchar(100) NOT NULL,
  `age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

7.查看某表的状态

语法:show table states like 'table_name'\G

进入数据库

mysql> use chenxi;   
Database changed

查看表的状态

mysql> show table status like 'chenxi'\G; 
*************************** 1. row ***************************
           Name: chenxi
         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: 2023-09-05 21:20:49
    Update_time: NULL
     Check_time: NULL
      Collation: latin1_swedish_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

4.1.5获取帮助

语法:help keyword;

获取创建数据库的帮助

mysql> help create database; 
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_option] ...

create_option: [DEFAULT] {
    CHARACTER SET [=] charset_name
  | COLLATE [=] collation_name
}

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: https://dev.mysql.com/doc/refman/5.7/en/create-database.html

4.2 DML操作

DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。

4.2.1增(insert)语句

语法:insert [into] table_name { values | value} (value1,...)...

mysql> use chenxi;
Database changed
mysql> insert into chenxi (id,name,age) value (1,'tom',20);
Query OK, 1 row affected (0.00 sec)

mysql> insert into chenxi (id,name,age) value (2,'make',20),(3,'jerry',15);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

4.2.2 查(select)语句
字段column表示法

条件判断语句WHERE

ORDER BY:排序,默认为升序(ASC)

语法:select column1,...from table_name [where clause] [order by 'column_name' [desc]] ] ltmit [m,n];

mysql> use chenxi;
Database changed
mysql> select * from chenxi;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | make  |   20 |
|  3 | jerry |   15 |
+----+-------+------+
3 rows in set (0.00 sec)


mysql> select name from chenxi;
+-------+
| name  |
+-------+
| tom   |
| make  |
| jerry |
+-------+
3 rows in set (0.00 sec)

mysql> select * from chenxi order by age;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  3 | jerry |   15 |
|  1 | tom   |   20 |
|  2 | make  |   20 |
+----+-------+------+
3 rows in set (0.00 sec)


mysql> select * from chenxi order by age limit 1;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  3 | jerry |   15 |
+----+-------+------+
1 row in set (0.00 sec)


mysql> select * from chenxi order by age limit 1,2;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | tom  |   20 |
|  2 | make |   20 |
+----+------+------+
2 rows in set (0.00 sec)

mysql> select * from chenxi where age > 15;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | tom  |   20 |
|  2 | make |   20 |
+----+------+------+
2 rows in set (0.00 sec)


mysql> select * from chenxi where age > 15 and name = 'make';
+----+------+------+
| id | name | age  |
+----+------+------+
|  2 | make |   20 |
+----+------+------+
1 row in set (0.00 sec)

4.2.3 改(update)语句

语法: update table_name set column1 = new_value1,...[WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n]

mysql> select * from chenxi;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | make  |   20 |
|  3 | jerry |   15 |
+----+-------+------+
3 rows in set (0.00 sec)

mysql> update chenxi set age = 24 where name = 'tom';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from chenxi where name = 'tom';
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | tom  |   24 |
+----+------+------+
1 row in set (0.00 sec)

4.2.4 删(delete)语句

语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];

mysql> select * from chenxi;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   24 |
|  2 | make  |   20 |
|  3 | jerry |   15 |
+----+-------+------+
3 rows in set (0.00 sec)

mysql> delete from chenxi where id = 2;
Query OK, 1 row affected (0.00 sec)

mysql> select * from chenxi;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   24 |
|  3 | jerry |   15 |
+----+-------+------+
2 rows in set (0.00 sec)

mysql> delete from chenxi;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from chenxi;
Empty set (0.00 sec)

mysql> desc chenxi;
+-------+--------------+------+-----+---------+-------+
| 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)

4.2.5(truncate)语句

truncate与delete的区别:

语法:TRUNCATE table_name;

mysql> select * from chenxi;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | tom   |   20 |
|  2 | make  |   20 |
|  3 | jerry |   15 |
+----+-------+------+
3 rows in set (0.00 sec)

mysql> truncate chenxi;
Query OK, 0 rows affected (0.01 sec)

mysql> select * from chenxi;
Empty set (0.00 sec)

mysql> desc chenxi;
+-------+--------------+------+-----+---------+-------+
| 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)

4.3DCL操作
4.3.1 创建授权grant

权限类型

指定要操作的对象db_name.table_name

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 |
| chenxi             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)
授权chenxi用户在数据库本机上登录访问所有数据库

mysql> grant all on *.* to 'chenxi'@'localhost' identified by 'Chenxi123.'; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all on *.* to 'chenxi'@'127.0.0.1' identified by 'Chenxi123.';          
Query OK, 0 rows affected, 1 warning (0.00 sec)

4.3.2查看授权

查看当前用户授权

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)

4.3.3取消授权
语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
mysql> revoke all on *.* from 'chenxi'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:

  • GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
  • 对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
mysql> FLUSH PRIVILEGES;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值