mysql基础正则表达式
数据结构模型主要有:sql
关系模型又分为:数据库
常见的关系型数据库管理系统:express
事务:多个操做被看成一个总体对待就称为一个事务(整个事务中的全部操做,要么所有完成,要么所有不完成,不可能停滞在中间某个环节。事务在执行过程当中发生错误,会被回滚(Rollback)到事务开始前的状态,就像这个事务历来没有执行过同样。)安全
要看一个关系型数据库是否支持事务,须要看其是否支持并知足ACID测试服务器
ACID:ACID是事务的一个基本标准数据结构
SQL:Structure Query Language,结构化查询语言app
约束:constraint,向数据表提供的数据要遵照的限制socket
索引:将表中的一个或多个字段中的数据复制一份另存,而且这些数据须要按特定次序排序存储
关系运算:
数据抽象方式:
关系型数据库的常见组件有:
SQL语句有三种类型:
SQL语句类型 | 对应操做 |
---|---|
DDL | CREATE:建立 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANT:受权 REVOKE:移除受权 |
mysql安装方式有三种:
//第一步下载mysql5.7的yum源 [root@cwh ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm --2019-04-22 15:04:39-- http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm ... ... 2019-04-22 15:04:46 (389 KB/s) - 已保存 “mysql57-community-release-el7-10.noarch.rpm” [25548/25548]) [root@cwh ~]# ls aaa anaconda-ks.cfg cwh4http.sh httpd.conf httpd-vhosts.conf httppz1.sh mysql57-community-release-el7-10.noarch.rpm //第二步安装下载下来的mysql源 [root@cwh ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm [root@cwh ~]# ls /etc/yum.repos.d/ 7CentOS-Base.repo mysql-community.repo mysql-community-source.repo redhat.repo //第三步安装mysql5.7 yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
//第一步启动mysql并查看端口是否开启 [root@cwh ~]# systemctl start mysqld [root@cwh ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 80 :::3306 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* //第二步在mysql日志文件中找出mysql临时密码 [root@cwh ~]# cat /var/log/mysqld.log |grep password 2019-04-22T07:40:39.531315Z 1 [Note] A temporary password is generated for root@localhost: xB>aDF_l>4>; //最后的xB>aDF_l>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.25 Copyright (c) 2000, 2019, 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> //这样显示出来了就表示临时登陆成功 //第四步修改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 'cwh123!'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye //第五步为避免mysql自动升级,这里须要卸载最开始安装的yum源 [root@localhost ~]# rpm -qa |grep mysql mysql-community-common-5.7.25-1.el7.x86_64 mysql-community-libs-5.7.25-1.el7.x86_64 mysql-community-libs-compat-5.7.25-1.el7.x86_64 mysql-community-server-5.7.25-1.el7.x86_64 mysql57-community-release-el7-10.noarch mysql-community-client-5.7.25-1.el7.x86_64 mysql-community-devel-5.7.25-1.el7.x86_64 [root@localhost ~]# yum -y remove mysql57-community-release-el7-10.noarch
//语法:mysql [OPTIONS] [database] //经常使用的OPTIONS: -uUSERNAME //指定用户名,默认为root -hHOST //指定服务器主机,默认为localhost,推荐使用ip地址 -pPASSWORD //指定用户的密码 -P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307 -V //查看当前使用的mysql版本 -e //不登陆mysql执行sql语句后退出,经常使用于脚本 //查看当前使用的mysql脚本 [root@localhost ~]# mysql -V mysql Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using EditLine wrapper //本地登陆 [root@cwh ~]# mysql -uroot -pcwh123! -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 13 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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> //此时显示已经登录上了 //注意,不推荐直接在命令行里直接用-pPASSWORD的方式登陆,而是使用-p选项,而后交互式输入密码 [root@cwh ~]# 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.25 MySQL Community Server (GPL) Copyright (c) 2000, 2019, 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> //查看mysql中有哪些数据库 [root@localhost ~]# mysql -uroot -p -h127.0.0.1 -e 'show databases' Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+
socket类型 | 说明 |
---|---|
ip socket | 默认监听在tcp的3306端口,支持远程通讯 |
unix sock | 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock 仅支持本地通讯 server地址只能是:localhost,127.0.0.1 |
//建立数据库 //语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME'; //建立数据库chengweihong [root@localhost ~]# mysql -uroot -p Enter password: mysql> mysql> create database if not exists chengweihong; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) //删除数据库 //语法:DROP DATABASE [IF EXISTS] 'DB_NAME'; //删除数据库chengweihong mysql> drop database if exists chengweihong; Query OK, 0 rows affected (0.02 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
//建立表 //语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型'; //在数据库chengweihong里建立表cwh //首先建立chengweihong数据库 mysql> create database chengweihong; Query OK, 1 row affected (0.01 sec) //在在数据库chengweihong中建立表 mysql> create table cwh(id int not null,name varchar(100),age tinyint); Query OK, 0 rows affected (0.04 sec) mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | +------------------------+ 1 row in set (0.00 sec) //具体查看表的内容 mysql> desc cwh; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(100) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.03 sec) //删除表 //语法:DROP TABLE [ IF EXISTS ] 'table_name'; //删除表cwh mysql> drop table if exists cwh; Query OK, 0 rows affected (0.01 sec) mysql> show tables; Empty set (0.00 sec)
mysql用户账号由两部分组成,如'USERNAME'@'HOST',表示此USERNAME只能今后HOST上远程登陆
这里('USERNAME'@'HOST')的HOST用于限制此用户可经过哪些主机远程链接mysql程序,其值可为:
//数据库用户建立 //语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password']; //建立数据库用户wangqing mysql> create user cwh@192.168.112.149 identified by 'cwh123!'; Query OK, 0 rows affected (0.01 sec) //在192.168.112.149上验证 [root@149 ~]# mysql -ucwh -p -h192.168.112.146 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> //由于没有配置权限因此查看的东西有限,看不到chengweihong的数据库 MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.01 sec) //删除数据库用户 //语法:DROP USER 'username'@'host'; mysql> drop user cwh@192.168.112.149; Query OK, 0 rows affected (0.01 sec) //删除用户后在192.168.112.149主机上验证 [root@149 ~]# mysql -ucwh -p -h192.168.112.146 Enter password: ERROR 1130 (HY000): Host '192.168.112.149' is not allowed to connect to this MySQL server //能够看出已经没法登录了
//1.查看有哪些数据库 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) //2.查看数据库中有哪些表格(要看那个数据的表格就先进入到数据库中) mysql> use chengweihong Database changed mysql> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | +------------------------+ 1 row in set (0.00 sec) //3.查看支持的全部字符集 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.01 sec) //4.查看当前数据库支持的全部存储引擎 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) //5.不进入某数据库而列出其包含的全部表 mysql> show tables from chengweihong; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | +------------------------+ 1 row in set (0.00 sec) //6.查看表结构 //语法:DESC [db_name.]table_name; mysql> desc chengweihong.cwh; +-------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | name | varchar(100) | YES | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) //7.查看某表的建立命令 //语法:SHOW CREATE TABLE table_name; mysql> show create table cwh; +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | cwh | CREATE TABLE `cwh` ( `id` int(11) NOT NULL, `name` varchar(100) DEFAULT 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> show create table cwh\G; *************************** 1. row *************************** Table: cwh Create Table: CREATE TABLE `cwh` ( `id` int(11) NOT NULL, `name` varchar(100) DEFAULT NULL, `age` tinyint(4) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec)
//获取建立表的帮助 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) }
DML操做包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操做。
1.一次插入一条完整数据
mysql> insert into cwh value(1,'tom',10); Query OK, 1 row affected (0.01 sec) mysql> select * from cwh; +----+------+------+ | id | name | age | +----+------+------+ | 1 | tom | 10 | +----+------+------+ 1 row in set (0.00 sec)
2.一次插入多条完整数据
mysql> insert into cwh values(2,'jerry',20),(3,'natasha',30) -> ; //由于最后没有输入分号 Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from cwh -> ; //由于ui后没输入; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | +----+---------+------+ 3 rows in set (0.00 sec)
3.一次插入一条指定字段的数据
mysql> insert into cwh(id,name) value(4,'aaa'); Query OK, 1 row affected (0.00 sec) mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | NULL | +----+---------+------+ 4 rows in set (0.00 sec)
4.一次插入多条指定字段的数据
mysql> insert into cwh(id,name) values(5,'bbb'),(6,'ccc'); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from cwh -> ; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | NULL | | 5 | bbb | NULL | | 6 | ccc | NULL | +----+---------+------+ 6 rows in set (0.00 sec)
字段column表示法
表示符 | 表示 |
---|---|
* | 全部字段 |
as | 字段别名, 当名字很长时用别名代替 |
条件判断语句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];
1.查看表的全部内容
mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | NULL | | 5 | bbb | NULL | | 6 | ccc | NULL | +----+---------+------+ 6 rows in set (0.00 sec)
2.按字段查看表的内容
mysql> select name from cwh; +---------+ | name | +---------+ | tom | | jerry | | natasha | | aaa | | bbb | | ccc | +---------+ 6 rows in set (0.00 sec)
3.查看表的内容并按某字段升序排序
mysql> select * from cwh order by age; +----+---------+------+ | id | name | age | +----+---------+------+ | 4 | aaa | NULL | | 5 | bbb | NULL | | 6 | ccc | NULL | | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | +----+---------+------+ 6 rows in set (0.00 sec)
4.查看表的内容按升序排序取前两个
mysql> select * from cwh order by id limit 2; +----+-------+------+ | id | name | age | +----+-------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | +----+-------+------+ 2 rows in set (0.00 sec)
5.查看表的内容按升序排序跳过第一个取后两个
mysql> select * from cwh order by id limit 1,2; +----+---------+------+ | id | name | age | +----+---------+------+ | 2 | jerry | 20 | | 3 | natasha | 30 | +----+---------+------+ 2 rows in set (0.00 sec)
6.查看表的内容只打印age大于25的信息
mysql> select * from cwh where age >=25; +----+---------+------+ | id | name | age | +----+---------+------+ | 3 | natasha | 30 | +----+---------+------+ 1 row in set (0.00 sec)
7.查看表的内容只打印age=25和name=tom的信息
mysql> select * from cwh where age=10 and name='tom'; +----+------+------+ | id | name | age | +----+------+------+ | 1 | tom | 10 | +----+------+------+ 1 row in set (0.00 sec)
8.查看表格打印id在2-4之间的信息
mysql> select * from cwh where id between 2 and 4; +----+---------+------+ | id | name | age | +----+---------+------+ | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | NULL | +----+---------+------+ 3 rows in set (0.00 sec)
9.查看表格打印age为空的信息
mysql> select * from cwh where age is null; +----+------+------+ | id | name | age | +----+------+------+ | 4 | aaa | NULL | | 5 | bbb | NULL | | 6 | ccc | NULL | +----+------+------+ 3 rows in set (0.00 sec)
10.查看表格打印age不为空的信息
mysql> select * from cwh where age is not null; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | +----+---------+------+ 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];
注意:此语句只能一条一条的修改记录,能够修改一条记录中的多个字段
1.修改表中的aaa的age由null改成40
mysql> update cwh set age=40 where name='aaa'; Query OK, 1 row affected (0.00 sec) mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | 40 | | 5 | bbb | NULL | | 6 | ccc | NULL | +----+---------+------+ 6 rows in set (0.00 sec)
2.修改表中一行记录中多个字段将id=6的ccc改成cwh,age改成60
mysql> update cwh set name='cwh',age=60 where id=6; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | 40 | | 5 | bbb | NULL | | 6 | cwh | 60 | +----+---------+------+ 6 rows in set (0.00 sec)
//DML操做之删操做delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
1.删除某条记录
mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | 40 | | 5 | bbb | NULL | | 6 | cwh | 60 | +----+---------+------+ 6 rows in set (0.00 sec) mysql> delete from cwh where id=5; Query OK, 1 row affected (0.00 sec) mysql> select * from cwh; +----+---------+------+ | id | name | age | +----+---------+------+ | 1 | tom | 10 | | 2 | jerry | 20 | | 3 | natasha | 30 | | 4 | aaa | 40 | | 6 | cwh | 60 | +----+---------+------+ 5 rows in set (0.00 sec)
2.删除整个表的内容
mysql> select * from cwh111; +----+------+------+ | id | name | age | +----+------+------+ | 1 | qqq | 10 | | 2 | www | 20 | +----+------+------+ 2 rows in set (0.00 sec) mysql> delete from cwh111; Query OK, 2 rows affected (0.00 sec) mysql> select * from cwh111; Empty set (0.00 sec)
truncate与delete的区别:
语句类型 | 特色 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 能够经过回滚事务日志恢复数据 很是占用空间 |
truncate | 删除表中全部数据,且没法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 经过释放存储表数据所用的数据页来删除数据,而且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
mysql> select * from cwh111; +----+------+------+ | id | name | age | +----+------+------+ | 1 | qqq | 10 | | 2 | www | 20 | +----+------+------+ 2 rows in set (0.00 sec) mysql> truncate cwh111; Query OK, 0 rows affected (0.01 sec) mysql> select * from cwh111; Empty 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];
1.受权cwh用户在数据库本机上登陆访问全部数据库
mysql> grant all on *.* to cwh@127.0.0.1 identified by '+7p6Mg2JsHgX1bIaOSwSykG8O2M='; //此处密码使用openssl rand 20 -base64 生成 mysql> show grants for cwh@127.0.0.1 -> ; +--------------------------------------------------+ | Grants for cwh@127.0.0.1 | +--------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'cwh'@'127.0.0.1' | +--------------------------------------------------+ 1 row in set (0.00 sec)
2.受权cwh用户在192.168.112.149上远程登陆访问chengweihong数据库
//首先受权 mysql> grant all on chengweihong.* to cwh@192.168.112.149 identifiied by '+7p6Mg2JsHgX1bIaOSwSykG8O2M='; Query OK, 0 rows affected, 1 warning (0.00 sec) //在查看受权内容 mysql> show grants for cwh@192.168.112.149; +---------------------------------------------------------------------+ | Grants for cwh@192.168.112.149 | +---------------------------------------------------------------------+ | GRANT USAGE ON *.* TO 'cwh'@'192.168.112.149' | | GRANT ALL PRIVILEGES ON `chengweihong`.* TO 'cwh'@'192.168.112.149' | +---------------------------------------------------------------------+ 2 rows in set (0.00 sec) //最后在192.168.112.149主机上验证 [root@146 ~]# mysql -ucwh -p -h192.168.112.146 Enter password: MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | +--------------------+ 2 rows in set (0.00 sec) MySQL [chengweihong]> show tables; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | | cwh111 | +------------------------+ 2 rows in set (0.00 sec)
3.受权cwh用户在全部位置上远程登陆访问chengweihong数据库的cwh表
//首先受权 mysql> grant all on chengweihong.cwh to 'cwh'@'%' identified by '+7p6Mg2JsHgX1bIaOSwSykG8O2M='; Query OK, 0 rows affected, 1 warning (0.00 sec) //在查看权限 mysql> show grants for cwh@'%'; +-----------------------------------------------------------+ | Grants for cwh@% | +-----------------------------------------------------------+ | GRANT USAGE ON *.* TO 'cwh'@'%' | | GRANT ALL PRIVILEGES ON `chengweihong`.`cwh` TO 'cwh'@'%' | +-----------------------------------------------------------+ 2 rows in set (0.00 sec) //在146主机上验证 [root@146 ~]# mysql -ucwh -p -h192.168.112.146 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 5.7.25 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | chengweihong | +--------------------+ 2 rows in set (0.00 sec) MySQL [(none)]> show tables from chengweihong; +------------------------+ | Tables_in_chengweihong | +------------------------+ | cwh | +------------------------+ 1 row in set (0.00 sec)
1.查看当前登陆用户的受权信息
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)
2.查看指定用户cwh的受权信息
mysql> show grants for cwh@'%'; +-----------------------------------------------------------+ | Grants for cwh@% | +-----------------------------------------------------------+ | GRANT USAGE ON *.* TO 'cwh'@'%' | | GRANT ALL PRIVILEGES ON `chengweihong`.`cwh` TO 'cwh'@'%' | +-----------------------------------------------------------+ 2 rows in set (0.00 sec)
//语法:REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
1.取消对cwh用户的受权
mysql> revoke all on chengweihong.cwh from cwh@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for cwh@'%'; +---------------------------------+ | Grants for cwh@% | +---------------------------------+ | GRANT USAGE ON *.* TO 'cwh'@'%' | +---------------------------------+ 1 row in set (0.00 sec)
注意:mysql服务进程启动时会读取mysql库中的全部受权表至内存中:
刷新受权表:
mysql> FLUSH PRIVILEGES;