文章目录
- 1. 关系型数据库介绍
- 2. mysql安装与配置
- 3. mysql的程序组成
- 4. mysql数据库操作
- 5. 示例
- 5.1 创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)
- 5.2 查看下该新建的表有无内容(用select语句)
- 5.3 往新建的student表中插入数据(用insert语句)
- 5.4 修改lisi的年龄为50
- 5.5 查询student表中名字叫zhangshan的记录
- 5.6 查询student表中名字叫zhangshan且年龄大于20岁的记录
- 5.7 查询student表中年龄在23到30之间的记录
- 5.8 修改wangwu的年龄为100
- 5.9 删除student中名字叫zhangshan且年龄小于等于20的记录
- 5.10 以age字段降序排序
- 5.11 查询student表中年龄最小的3位同学
- 5.12 查询student表中年龄最大的4位同学
1. 关系型数据库介绍
1.1 数据结构模型
数据结构模型主要有:
- 层次模型
- 网状结构
- 关系模型
关系模型:
- 二维关系:row,column
- 数据库管理系统:DBMS
- 关系:Relational,RDBMS
1.2 RDBMS专业名词
常见的关系型数据库管理系统:
- MySQL:MySQL,MariaDB,Percona-Server
- PostgreSQL:简称为pgsql
- Oracle
- MSSQL
SQL : Structure Query Language,结构化查询语言
约束 : constraint,向数据表提供的数据要遵守的限制
-
主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
- 一个表只能存在一个
-
惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
- 一个表可以存在多个
-
外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
检查性约束
索引:将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储
1.3 关系型数据库的常见组件
关系型数据库的常见组件有:
- 数据库:database
- 表:table,由行(row)和列(column)组成
- 索引:index
- 视图:view
- 用户:user
- 权限:privilege
- 存储过程:procedure
- 存储函数:function
- 触发器:trigger
- 事件调度器:event scheduler
1.4 SQL语句
SQL语句有三种类型:
- DDL:Data Defination Language,数据定义语言
- DML:Data Manipulation Language,数据操纵语言
- DCL:Data Control Language,数据控制语言
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANT:授权 REVOKE:移除授权 |
2. mysql安装与配置
2.1 mysql安装
mysql安装方式有三种:
- 源代码:编译安装
- 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
- 程序包管理器管理的程序包:
- rpm:有两种
- OS Vendor:操作系统发行商提供的
- 项目官方提供的
- deb
- rpm:有两种
安装mysql
[root@localhost ~]# yum list all|grep mariadb 查看mariadb有哪些包
mariadb.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-backup.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-common.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-connector-c.i686 3.0.7-1.el8 AppStream
mariadb-connector-c.x86_64 3.0.7-1.el8 AppStream
mariadb-connector-c-config.noarch 3.0.7-1.el8 AppStream
mariadb-connector-c-devel.i686 3.0.7-1.el8 AppStream
mariadb-connector-c-devel.x86_64 3.0.7-1.el8 AppStream
mariadb-connector-odbc.x86_64 3.0.7-1.el8 AppStream
mariadb-devel.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-embedded.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-embedded-devel.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-errmsg.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-gssapi-server.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-java-client.noarch 2.2.5-2.el8 AppStream
mariadb-oqgraph-engine.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-server.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-server-galera.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-server-utils.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
mariadb-test.x86_64 3:10.3.17-1.module+el8.1.0+3974+90eded84 AppStream
[root@localhost ~]# yum -y install mariadb mariadb-common mariadb-devel mariadb-server
2.2 mysql配置
- 启动mysql
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:
LISTEN 0 128 0.0.0.0:22 0.0.0.0:
LISTEN 0 128 [::]:22 [::]
[root@localhost ~]# service mysqld start
Starting MySQL. SUCCESS!
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
启动mysql并设置开机自动启动
[root@localhost ~]# systemctl enable --now mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@localhost ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.3 database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.se>
Active: active (running) since Mon 2020-10-19 14:5>
Docs: man:mysqld(8)
https://mariadb.com/kb/en/library/systemd/
Process: 15358 ExecStartPost=/usr/libexec/mysql-che>
Process: 15223 ExecStartPre=/usr/libexec/mysql-prep>
Process: 15199 ExecStartPre=/usr/libexec/mysql-chec>
Main PID: 15326 (mysqld)
Status: "Taking your SQL requests now..."
Tasks: 30 (limit: 12320)
Memory: 83.9M
CGroup: /system.slice/mariadb.service
└─15326 /usr/libexec/mysqld --basedir=/usr
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysql-prepare-d>
Oct 19 14:58:17 localhost.localdomain mysqld[15326]: >
Oct 19 14:58:17 localhost.localdomain systemd[1]: Sta>
lines 1-25/25 (END)
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:* 确保3306端口已经监听起来
LISTEN 0 128 [::]:22 [::]:*
在日志文件中找出临时密码(只有mysql需要在日志里找到密码才能登陆)
[root@localhost ~]# ls /var/log/mariadb/
mariadb.log
[root@localhost ~]# grep 'password' /var/log/mariadb/mariadb.log 这里用的是mariadb所以不需要用密码
登陆
[root@localhost ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> 看到有这样的标识符则表示成功登录了
第二种登陆方式(此种方式不建议使用,避免泄露密码)
[root@localhost ~]# mysql -uroot -pmaqiang123 直接在后面跟密码
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
设置密码
[root@localhost ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password = password('maqiang123'); 设置密码为maqiang123,用password加密之后在数据库的表里就看不到改密码了
Query OK, 0 rows affected (0.000 sec)
[root@localhost ~]# mysql -uroot 设置密码之后就需要用密码登陆,否则登陆失败
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@localhost ~]# mysql -uroot -p 设置密码之后就用-p指定密码登陆
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> select * from mysql.user \G 查看
......
Password: *31FBE3B6BBC9714F78109B4A42C5869DC227B39B 这两处就是密码,但显示的是密文
......
authentication_string: *31FBE3B6BBC9714F78109B4A42C5869DC227B39B
......
修改密码
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; 将密码改成123456
Query OK, 0 rows affected (0.000 sec)
为避免mysql自动升级,这里需要卸载最开始安装的yum源(用的mysql需要做此步骤)
rpm -e mysql57-community-release
3. mysql的程序组成
- 客户端
- mysql:CLI交互式客户端程序
- mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
- mysqldump:mysql备份工具
- mysqladmin
- 服务器端
- mysqld
3.1 mysql工具使用
常用的选项:
-V : 查看当前使用的mysql版本
[root@localhost ~]# mysql -V
mysql Ver 15.1 Distrib 10.3.17-MariaDB, for Linux (x86_64) using readline 5.1
-hHOST :指定服务器主机,默认为localhost,推荐使用ip地址
[root@localhost ~]# mysql -uroot -pmaqiang123 -h127.0.0.1 第一种方式
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye
[root@localhost ~]# mysql -uroot -pmaqiang123 -hlocalhost 第二种方式
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
-uUSERNAME :指定用户名,默认为root
-P# :指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
[root@localhost ~]# mysql -uroot -pmaqiang123 -P3306 -h127.0.0.1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
-e :不登录mysql执行sql语句后退出,常用于脚本
[root@localhost ~]# mysql -uroot -pmaqiang123 -e 'show databases';
+--------------------+
| Database |
+--------------------+
| information_schema | 这里会看到3个数据库
| mysql |
| performance_schema |
+--------------------+
[root@localhost ~]# 此时自动退出
3.2 服务器监听的两种socket地址
socket类型 | 说明 |
---|---|
ip socket | 默认监听在tcp的3306端口,支持远程通信 |
unix sock | 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock) 仅支持本地通信 server地址只能是:localhost,127.0.0.1 |
[root@localhost ~]# ls /var/lib/mysql/
aria_log.00000001 multi-master.info
aria_log_control mysql
ib_buffer_pool mysql.sock
ibdata1 mysql_upgrade_info
ib_logfile0 performance_schema
ib_logfile1 tc.log
ibtmp1
[root@localhost ~]# mysql -uroot -pmaqiang123 -S /var/lib/mysql/mysql.sock 使用套接字来连接,此种方式只能连接主机,不能连接其他主机
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
4. mysql数据库操作
4.1 DDL操作
4.1.1 数据库操作
语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME'; 大写表示固定语法,中括号表示条件(可有可无)
查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
创建数据库
MariaDB [(none)]> create database abc; 创建数据库abc
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| abc | 数据库abc创建成功
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
删除数据库
MariaDB [(none)]> drop database if exists abc; 删除数据库abc(if exists表示条件:如果成立则删除abc)
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
4.1.2 表操作
创建数据库
MariaDB [(none)]> create database abc; 创建数据库abc
Query OK, 1 row affected (0.000 sec)
查看数据库abc
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.001 sec)
进入数据库abc
MariaDB [(none)]> use abc;
Database changed
MariaDB [abc]>
创建表
MariaDB [abc]> create table student(id int not null,name varchar(10),age tinyint); 创建表student,id不能为空;名字长度不能超过10;年龄是整数,不能超过255岁
Query OK, 0 rows affected (0.005 sec)
查看表
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| student |
+---------------+
1 row in set (0.000 sec)
删除表
MariaDB [abc]> drop table student;
Query OK, 0 rows affected (0.003 sec)
MariaDB [abc]> show tables; 查看表已被删除
Empty set (0.000 sec)
查看表的结构
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.002 sec)
创建表时添加主键
MariaDB [abc]> CREATE TABLE IF NOT EXISTS student11(id int not null default 1 primary key);
Query OK, 0 rows affected (0.008 sec)
MariaDB [abc]> desc student11;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 1 | | 此时该表有主键(Key为PRI)
+-------+---------+------+-----+---------+-------+
1 row in set (0.001 sec)
修改表的结构
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
添加
MariaDB [abc]> alter table student add score float; add添加
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
| score | float | YES | | NULL | | 此时就有score类型
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.001 sec)
删除
MariaDB [abc]> alter table student drop age; drop删除age字段
Query OK, 0 rows affected (0.006 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | float | YES | | NULL | | age已被删除
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
修改
MariaDB [abc]> alter table student modify score float not null; 相当于在modify后面重新写了一个类型
Query OK, 0 rows affected (0.011 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | float | NO | | NULL | | 此时score的Null为NO
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
4.1.3 用户操作
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
- IP地址,如:192.168.50.135
- 通配符
- %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
- _:匹配任意单个字符
创建用户
MariaDB [abc]> create user tom@127.0.0.1 identified by 'maqiang123'; 账户:tom@127.0.0.1 ; 密码:maqiang123
Query OK, 0 rows affected (0.001 sec)
MariaDB [abc]> create user jerry@localhost; 创建一个不需要用密码登陆的用户
Query OK, 0 rows affected (0.000 sec)
登陆
[root@localhost ~]# mysql -utom -pmaqiang123 -h127.0.0.1 用密码登陆
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 24
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
[root@localhost ~]# mysql -ujerry 不用密码登陆
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> set password = password('123456'); 登陆进去设置密码为123456
Query OK, 0 rows affected (0.000 sec)
删除用户
MariaDB [abc]> drop user tom@127.0.0.1; 此处没有用引号删除
Query OK, 0 rows affected (0.000 sec)
MariaDB [abc]> drop user 'jerry'@'localhost'; 此处用引号删除,最好用引号删除
Query OK, 0 rows affected (0.000 sec)
在192.168.50.131上创建用户wangwu使其在192.168.50.135能登陆(不设置密码)
MariaDB [(none)]> create user wangwu@'192.168.50.135'; 在192.168.50.131上创建数据库用户wangwu,不设置密码
Query OK, 0 rows affected (0.000 sec)
在192.168.50.135上登陆
[root@server ~]# mysql -uwangwu -h192.168.50.131
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> create user kk@'192.168.50.135' identified by '123456'; 在192.168.50.131上创建数据库用户kk,设置密码为123456
Query OK, 0 rows affected (0.000 sec)
登陆
[root@server ~]# mysql -ukk -p123456 -h192.168.50.131
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; 普通用户登陆只能查看数据库,且只能查看其中一个,因为没权限
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.001 sec)
4.1.4 查看命令SHOW
查看支持的所有字符集
MariaDB [abc]> 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 |
+----------+-----------------------------+---------------------+--------+
40 rows in set (0.001 sec)
查看当前数据库支持的所有存储引擎
MariaDB [abc]> show engines;
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| MRG_MyISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | Stores tables as CSV files | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | Non-transactional engine with good performance and small data footprint | NO | NO | NO |
| ARCHIVE | YES | gzip-compresses tables for a low storage footprint | NO | NO | NO |
| FEDERATED | YES | Allows to access tables on other MariaDB servers, supports transactions and more | YES | NO | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| SEQUENCE | YES | Generated tables filled with sequential values | YES | NO | YES |
| InnoDB | DEFAULT | Supports transactions, row-level locking, foreign keys and encryption for tables | YES | YES | YES |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.000 sec)
查看存储引擎的第二种方式
MariaDB [abc]> show engines\G 分号换成\G
*************************** 1. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: MRG_MyISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: CSV
Support: YES
Comment: Stores tables as CSV files
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 5. row ***************************
Engine: MyISAM
Support: YES
Comment: Non-transactional engine with good performance and small data footprint
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: ARCHIVE
Support: YES
Comment: gzip-compresses tables for a low storage footprint
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: FEDERATED
Support: YES
Comment: Allows to access tables on other MariaDB servers, supports transactions and more
Transactions: YES
XA: NO
Savepoints: YES
*************************** 8. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: SEQUENCE
Support: YES
Comment: Generated tables filled with sequential values
Transactions: YES
XA: NO
Savepoints: YES
*************************** 10. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, foreign keys and encryption for tables
Transactions: YES
XA: YES
Savepoints: YES
*************************** 11. row ***************************
Engine: Aria
Support: YES
Comment: Crash-safe tables with MyISAM heritage
Transactions: NO
XA: NO
Savepoints: NO
11 rows in set (0.001 sec)
进入数据库查看其包含的所有表
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| student |
+---------------+
1 row in set (0.000 sec)
不进入数据库查看其包含的所有表
MariaDB [(none)]> show tables from abc;
+---------------+
| Tables_in_abc |
+---------------+
| student |
+---------------+
1 row in set (0.000 sec)
查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
查看表结构
MariaDB [(none)]> desc abc.student; 此种方式是在数据库外面看
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
查看某表是怎么创建出来的
MariaDB [abc]> show create table student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
查看某数据库是怎么创建的
MariaDB [abc]> show create database abc;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| abc | CREATE DATABASE `abc` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.000 sec)
查看某表的状态
MariaDB [abc]> show tables; 查看所有表
+---------------+
| Tables_in_abc |
+---------------+
| hellostudent |
| student |
| student1 |
| student2 |
| student3jjyy |
+---------------+
5 rows in set (0.001 sec)
MariaDB [abc]> show table status like 'student'\G; 引号里直接跟名字指挥查看到这个名字的表;表的名字需要引号引起来
*************************** 1. row ***************************
Name: student
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: 2020-10-20 14:06:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
1 row in set (0.001 sec)
MariaDB [abc]> show table status like 'student%'\G; student 后面加%,则会把student后面所有的东西全都查看
*************************** 1. row ***************************
Name: student
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: 2020-10-20 14:06:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
*************************** 2. row ***************************
Name: student1
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: 2020-10-20 14:41:32
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
*************************** 3. row ***************************
Name: student2
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: 2020-10-20 14:41:12
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
*************************** 4. row ***************************
Name: student3jjyy
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: 2020-10-20 14:41:19
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
4 rows in set (0.001 sec)
MariaDB [abc]> show table status like '%student'\G; student 前面加%,则会把student前面所有的东西全都查看
*************************** 1. row ***************************
Name: hellostudent
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: 2020-10-20 14:42:29
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
*************************** 2. row ***************************
Name: student
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: 2020-10-20 14:06:16
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
2 rows in set (0.001 sec)
MariaDB [abc]> show table status like 'student_'\G; student后面跟下划线则会把student后面多一个的查看出来(下划线表示单个字符,%表示多个字符)
*************************** 1. row ***************************
Name: student1
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: 2020-10-20 14:41:32
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
*************************** 2. row ***************************
Name: student2
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: 2020-10-20 14:41:12
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Max_index_length: 0
Temporary: N
2 rows in set (0.000 sec)
4.1.5 获取帮助
查看如何创建数据库
MariaDB [abc]> help create database;
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] 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://mariadb.com/kb/en/create-database/
4.2 DML操作
DML操作包括增(INSERT)、删(DELETE)、改(UPDATE)、查(SELECT),均属针对表的操作。
4.2.1 INSERT语句
//DML操作之增操作insert
//语法:INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
先查看表
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| hellostudent |
| student1 |
| student10 |
| student11 |
| student2 |
| student3jjyy |
+---------------+
6 rows in set (0.000 sec)
查看student1结构
MariaDB [abc]> desc student1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
添加
添加一个字段
MariaDB [abc]> insert student1 value(1,'tom',20); 插入一条
Query OK, 1 row affected (0.001 sec)
添加多个字段
MariaDB [abc]> insert student1 values(2,'hehe',15),(3,'gege',10),(4,'kkja',16),(5,'ooa',34),(6,'xixi',20); 插入多条
Query OK, 5 rows affected (0.001 sec)
Records: 5 Duplicates: 0 Warnings: 0
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
+----+------+------+
6 rows in set (0.000 sec)
只添加一个id,其他为空
MariaDB [abc]> insert student1(id) value(7);
Query OK, 1 row affected (0.002 sec)
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | NULL | NULL | 添加l了一个id为7,其他为空
+----+------+------+
7 rows in set (0.000 sec)
添加id和age
MariaDB [abc]> insert student1(id,age) value(7,20);
Query OK, 1 row affected (0.002 sec)
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | NULL | NULL |
| 7 | NULL | 20 | 添加了一个id为7,age为20的字段,id里同时有两个7说明时唯一键,不是主键
+----+------+------+
8 rows in set (0.001 sec)
查
MariaDB [abc]> select * from student1; * 表示所有的项目
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
+----+------+------+
1 row in set (0.000 sec)
只查看id
MariaDB [abc]> select id from student1;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.000 sec)
看id和name
MariaDB [abc]> select id,name from student1; 查看多项时中间用逗号
+----+------+
| id | name |
+----+------+
| 1 | tom |
+----+------+
1 row in set (0.000 sec)
4.2.2 SELECT语句
字段column表示法
表示符 | 代表什么? |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
* 表示查看所有的字段
MariaDB [maqiang]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.000 sec)
只查看id这一个字段
MariaDB [maqiang]> select id from student;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 7 |
| 8 |
| 9 |
| 10 |
| 11 |
+----+
10 rows in set (0.000 sec)
查看id和age两个字段
MariaDB [maqiang]> select id,age from student;
+----+------+
| id | age |
+----+------+
| 1 | 20 |
| 2 | 23 |
| 3 | 25 |
| 4 | 28 |
| 5 | 26 |
| 7 | 50 |
| 8 | 10 |
| 9 | 100 |
| 10 | 15 |
| 11 | 20 |
+----+------+
10 rows in set (0.000 sec)
把原有的id,name,age分别改成编号,名字,年龄
MariaDB [maqiang]> select id as 编号,name as 名字,age as 年龄 from student;
+--------+-------------+--------+
| 编号 | 名字 | 年龄 |
+--------+-------------+--------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+--------+-------------+--------+
10 rows in set (0.000 sec)
id不改,把name,age分别改成n,a
MariaDB [maqiang]> select id,name as n,age as a from student;
+----+-------------+------+
| id | n | a |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.000 sec)
查看age最大的名字和年龄
MariaDB [maqiang]> select * from student; 查看所有字段
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
| 4 | kk | 80 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
| 11 | dada | NULL |
| 12 | | NULL |
+----+--------+------+
8 rows in set (0.000 sec)
MariaDB [maqiang]> select name,max(age) as age from student; 查看年龄最大的名字和年龄
+------+------+
| name | age |
+------+------+
| tom | 100 |
+------+------+
1 row in set (0.001 sec)
条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | AND OR NOT |
查看所有的
MariaDB [maqiang]> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+--------+------+
3 rows in set (0.000 sec)
查找age>50的所有列表
MariaDB [maqiang]> select * from student where age >50;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.001 sec)
查找age>50的名字(name)
MariaDB [maqiang]> select name from student where age >50;
+--------+
| name |
+--------+
| wangwu |
+--------+
1 row in set (0.000 sec)
(between)查找age在10~30之间的所有信息
MariaDB [maqiang]> select * from student where age between 10 and 30;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
+----+------+------+
1 row in set (0.000 sec)
and 的用法
MariaDB [maqiang]> select * from student where name = 'tom' and age < 50;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
+----+------+------+
1 row in set (0.000 sec)
or的用法
MariaDB [maqiang]> select * from student where name = 'tom' or age < 50;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
+----+------+------+
3 rows in set (0.001 sec)
like的用法
MariaDB [maqiang]> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
| 4 | kk | 80 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+--------+------+
6 rows in set (0.001 sec)
查看name的结尾像u的结果
MariaDB [maqiang]> select * from student where name like '%u'; %后面跟的u,表示以u结尾
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
+----+--------+------+
1 row in set (0.000 sec)
查看name的开头像l的结果
MariaDB [maqiang]> select * from student where name like 'l%'; %前面跟l,表示以l开头
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.000 sec)
rlike的用法(基于正则表达式)
MariaDB [maqiang]> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
| 4 | kk | 80 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+--------+------+
6 rows in set (0.000 sec)
查看以k结尾的结果
MariaDB [maqiang]> select * from student where name rlike 'k$'; $ 表示以什么什么结尾
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | kk | 80 |
+----+------+------+
1 row in set (0.000 sec)
查看以l开头的结果
MariaDB [maqiang]> select * from student where name rlike '^l'; ^ 表示以什么什么开头
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
+----+------+------+
1 row in set (0.000 sec)
每个点代表一个字符
MariaDB [maqiang]> select * from student where name rlike '....'; 此处并没有表示开头结尾,所有会匹配5个字符以上的wangwu
+----+--------+------+
| id | name | age |
+----+--------+------+
| 3 | hehe | 40 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+--------+------+
3 rows in set (0.000 sec)
MariaDB [maqiang]> select * from student where name rlike '^....$'; 此处表示了开头结尾,只会匹配4个字符
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | hehe | 40 |
| 7 | lisi | 50 |
+----+------+------+
2 rows in set (0.000 sec)
NULL和NOT NULL的用法
MariaDB [maqiang]> insert student(id,name) values(11,'dada'),(12,''); 创建id为11,name为dada的age为空(NULL),id为12,name为空的name可以匹配出来,但是NULL匹配不出来
Query OK, 2 rows affected (0.002 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [maqiang]> select * from student;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
| 4 | kk | 80 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
| 11 | dada | NULL |
| 12 | | NULL |
+----+--------+------+
8 rows in set (0.000 sec)
MariaDB [maqiang]> select * from student where age is NULL;
+----+------+------+
| id | name | age |
+----+------+------+
| 11 | dada | NULL |
| 12 | | NULL |
+----+------+------+
2 rows in set (0.000 sec)
MariaDB [maqiang]> select * from student where age is NOT NULL;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 2 | tom | 70 |
| 3 | hehe | 40 |
| 4 | kk | 80 |
| 7 | lisi | 50 |
| 9 | wangwu | 100 |
+----+--------+------+
6 rows in set (0.000 sec)
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个结果 |
降序(用desc)
MariaDB [maqiang]> select * from student where age is not NULL order by age desc;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
| 4 | kk | 80 |
| 2 | tom | 70 |
| 7 | lisi | 50 |
| 3 | hehe | 40 |
| 1 | tom | 20 |
+----+--------+------+
6 rows in set (0.001 sec)
升序(用asc,可以省略)
MariaDB [maqiang]> select * from student where age is not NULL order by age asc;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | tom | 20 |
| 3 | hehe | 40 |
| 7 | lisi | 50 |
| 2 | tom | 70 |
| 4 | kk | 80 |
| 9 | wangwu | 100 |
+----+--------+------+
6 rows in set (0.000 sec)
根据降序结果只取前面3个结果
MariaDB [maqiang]> select * from student where age is not NULL order by age desc limit 3;
+----+--------+------+
| id | name | age |
+----+--------+------+
| 9 | wangwu | 100 |
| 4 | kk | 80 |
| 2 | tom | 70 |
+----+--------+------+
3 rows in set (0.000 sec)
根据升序结果,跳过前两个结果后取3个结果
MariaDB [maqiang]> select * from student where age is not NULL order by age limit 2,3;
+----+------+------+
| id | name | age |
+----+------+------+
| 7 | lisi | 50 |
| 2 | tom | 70 |
| 4 | kk | 80 |
+----+------+------+
3 rows in set (0.000 sec)
4.2.2.1 group by的用法&内连接&左连接&右连接
创建一个info的表
MariaDB [maqiang]> create table info(id int not null primary key auto_increment,name varchar(50) not null,department varchar(100),salary int);
Query OK, 0 rows affected (0.005 sec)
查看info表格
MariaDB [maqiang]> desc info;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| department | varchar(100) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0.001 sec)
往表格添加内容(只添加部门和工资两项)
MariaDB [maqiang]> insert info(name,department,salary) values('tom','sales',5000),('jerry','dev',20000),('zhangshan','dev',10000),('lisi','sales',30000),('wangwu','office',8000),('qianliu','financial',10000),('zhaoqi','financial',20000),('sunba','operation',25000),('zhoujiu','operation',9000),('wutian','dev',15000);
Query OK, 10 rows affected (0.001 sec)
Records: 10 Duplicates: 0 Warnings: 0
查看改表格内容
MariaDB [maqiang]> select * from info;
+----+-----------+------------+--------+
| id | name | department | salary |
+----+-----------+------------+--------+
| 1 | tom | sales | 5000 |
| 2 | jerry | dev | 20000 |
| 3 | zhangshan | dev | 10000 |
| 4 | lisi | sales | 30000 |
| 5 | wangwu | office | 8000 |
| 6 | qianliu | financial | 10000 |
| 7 | zhaoqi | financial | 20000 |
| 8 | sunba | operation | 25000 |
| 9 | zhoujiu | operation | 9000 |
| 10 | wutian | dev | 15000 |
+----+-----------+------------+--------+
10 rows in set (0.000 sec)
1. 查看名字,部门,最高工资,再将这一结果组成个新组(group by的用法)
MariaDB [maqiang]> select name,department,max(salary) as salary from info group by department;
+---------+------------+--------+
| name | department | salary |
+---------+------------+--------+
| jerry | dev | 20000 | 显示了每个部门的最高工资
| qianliu | financial | 20000 |
| wangwu | office | 8000 |
| sunba | operation | 25000 |
| tom | sales | 30000 |
+---------+------------+--------+
5 rows in set (0.001 sec)
2.查看名字,部门,最低工资,再将这一结果组成个新组
MariaDB [maqiang]> select name,department,min(salary) as salary from info group by department;
+---------+------------+--------+
| name | department | salary |
+---------+------------+--------+
| jerry | dev | 10000 |
| qianliu | financial | 10000 |
| wangwu | office | 8000 | 显示了每个部门的最低工资
| sunba | operation | 9000 |
| tom | sales | 5000 |
+---------+------------+--------+
5 rows in set (0.000 sec)
3. 添加jn字段
MariaDB [maqiang]> alter table info add jn int not null;
Query OK, 0 rows affected (0.002 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [maqiang]> desc info;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | NULL | |
| department | varchar(100) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| jn | int(11) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.000 sec)
MariaDB [maqiang]> select * from info;
+----+-----------+------------+--------+----+
| id | name | department | salary | jn |
+----+-----------+------------+--------+----+
| 1 | tom | sales | 5000 | 0 |
| 2 | jerry | dev | 20000 | 0 |
| 3 | zhangshan | dev | 10000 | 0 |
| 4 | lisi | sales | 30000 | 0 |
| 5 | wangwu | office | 8000 | 0 |
| 6 | qianliu | financial | 10000 | 0 |
| 7 | zhaoqi | financial | 20000 | 0 |
| 8 | sunba | operation | 25000 | 0 |
| 9 | zhoujiu | operation | 9000 | 0 |
| 10 | wutian | dev | 15000 | 0 |
+----+-----------+------------+--------+----+
10 rows in set (0.001 sec)
4. 修改info字段里面的值
MariaDB [maqiang]> update info set jn = 1 where id = 1;
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [maqiang]> update info set jn = 2 where id = 2;
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
......
MariaDB [maqiang]> select * from info;
+----+-----------+------------+--------+----+
| id | name | department | salary | jn |
+----+-----------+------------+--------+----+
| 1 | tom | sales | 5000 | 1 |
| 2 | jerry | dev | 20000 | 2 |
| 3 | zhangshan | dev | 10000 | 3 |
| 4 | lisi | sales | 30000 | 4 |
| 5 | wangwu | office | 8000 | 5 |
| 6 | qianliu | financial | 10000 | 6 |
| 7 | zhaoqi | financial | 20000 | 7 |
| 8 | sunba | operation | 25000 | 8 |
| 9 | zhoujiu | operation | 9000 | 9 |
| 10 | wutian | dev | 15000 | 10 |
+----+-----------+------------+--------+----+
10 rows in set (0.000 sec)
5.创建第二张表格(该表格里job_number与第一张表里的jn是一个意思)
MariaDB [maqiang]> create table basic_info(job_number int not null,age int not null);
Query OK, 0 rows affected (0.003 sec)
MariaDB [maqiang]> desc basic_info;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| job_number | int(11) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+------------+---------+------+-----+---------+-------+
2 rows in set (0.001 sec)
MariaDB [maqiang]> insert basic_info values(1,21),(3,25),(5,30),(7,26),(9,28),(10,30),(2,20);
Query OK, 7 rows affected (0.001 sec)
Records: 7 Duplicates: 0 Warnings: 0
MariaDB [maqiang]> select * from basic_info;
+------------+-----+
| job_number | age |
+------------+-----+
| 1 | 21 |
| 3 | 25 |
| 5 | 30 |
| 7 | 26 |
| 9 | 28 |
| 10 | 30 |
| 2 | 20 |
+------------+-----+
7 rows in set (0.000 sec)
6. 现在有两张表格
MariaDB [maqiang]> select * from basic_info;
+------------+-----+
| job_number | age |
+------------+-----+
| 1 | 21 |
| 3 | 25 |
| 5 | 30 |
| 7 | 26 |
| 9 | 28 |
| 10 | 30 |
| 2 | 20 |
+------------+-----+
7 rows in set (0.000 sec)
MariaDB [maqiang]> select * from info;
+----+-----------+------------+--------+----+
| id | name | department | salary | jn |
+----+-----------+------------+--------+----+
| 1 | tom | sales | 5000 | 1 |
| 2 | jerry | dev | 20000 | 2 |
| 3 | zhangshan | dev | 10000 | 3 |
| 4 | lisi | sales | 30000 | 4 |
| 5 | wangwu | office | 8000 | 5 |
| 6 | qianliu | financial | 10000 | 6 |
| 7 | zhaoqi | financial | 20000 | 7 |
| 8 | sunba | operation | 25000 | 8 |
| 9 | zhoujiu | operation | 9000 | 9 |
| 10 | wutian | dev | 15000 | 10 |
+----+-----------+------------+--------+----+
10 rows in set (0.000 sec)
7. 内连接:取两张表格共有的值
第一种查看方法是没有改名字(用的where)
MariaDB [maqiang]> select * from info inner join basic_info where info.jn = basic_info.job_number; inner join表示内连接
+----+-----------+------------+--------+----+------------+-----+
| id | name | department | salary | jn | job_number | age |
+----+-----------+------------+--------+----+------------+-----+
| 1 | tom | sales | 5000 | 1 | 1 | 21 |
| 2 | jerry | dev | 20000 | 2 | 2 | 20 |
| 3 | zhangshan | dev | 10000 | 3 | 3 | 25 |
| 5 | wangwu | office | 8000 | 5 | 5 | 30 |
| 7 | zhaoqi | financial | 20000 | 7 | 7 | 26 |
| 9 | zhoujiu | operation | 9000 | 9 | 9 | 28 |
| 10 | wutian | dev | 15000 | 10 | 10 | 30 |
+----+-----------+------------+--------+----+------------+-----+
7 rows in set (0.000 sec)
第二种方法是把info改成a,basic_info改成b(用的on)
MariaDB [maqiang]> select * from info as a inner join basic_info as b on a.jn = b.job_number;
+----+-----------+------------+--------+----+------------+-----+
| id | name | department | salary | jn | job_number | age |
+----+-----------+------------+--------+----+------------+-----+
| 1 | tom | sales | 5000 | 1 | 1 | 21 |
| 2 | jerry | dev | 20000 | 2 | 2 | 20 |
| 3 | zhangshan | dev | 10000 | 3 | 3 | 25 |
| 5 | wangwu | office | 8000 | 5 | 5 | 30 |
| 7 | zhaoqi | financial | 20000 | 7 | 7 | 26 |
| 9 | zhoujiu | operation | 9000 | 9 | 9 | 28 |
| 10 | wutian | dev | 15000 | 10 | 10 | 30 |
+----+-----------+------------+--------+----+------------+-----+
7 rows in set (0.001 sec)
8. 左连接: 把左边(第一张表)有的取出来,左边有但右边没有的留空
MariaDB [maqiang]> select * from info as a left join basic_info as b on a.jn = b.job_number;
+----+-----------+------------+--------+----+------------+------+
| id | name | department | salary | jn | job_number | age |
+----+-----------+------------+--------+----+------------+------+
| 1 | tom | sales | 5000 | 1 | 1 | 21 |
| 3 | zhangshan | dev | 10000 | 3 | 3 | 25 |
| 5 | wangwu | office | 8000 | 5 | 5 | 30 |
| 7 | zhaoqi | financial | 20000 | 7 | 7 | 26 |
| 9 | zhoujiu | operation | 9000 | 9 | 9 | 28 |
| 10 | wutian | dev | 15000 | 10 | 10 | 30 |
| 2 | jerry | dev | 20000 | 2 | 2 | 20 |
| 4 | lisi | sales | 30000 | 4 | NULL | NULL | 此处的4,6,8 第一张表有,但第二张表没有,所以内容留空
| 6 | qianliu | financial | 10000 | 6 | NULL | NULL |
| 8 | sunba | operation | 25000 | 8 | NULL | NULL |
+----+-----------+------------+--------+----+------------+------+
10 rows in set (0.000 sec)
9. 右连接: 把右边(第二张表)有的取出来,右边有但左边没有的留空
MariaDB [maqiang]> select * from info as a right join basic_info as b on a.jn = b.job_number;
+------+-----------+------------+--------+------+------------+-----+
| id | name | department | salary | jn | job_number | age |
+------+-----------+------------+--------+------+------------+-----+
| 1 | tom | sales | 5000 | 1 | 1 | 21 |
| 2 | jerry | dev | 20000 | 2 | 2 | 20 |
| 3 | zhangshan | dev | 10000 | 3 | 3 | 25 |
| 5 | wangwu | office | 8000 | 5 | 5 | 30 |
| 7 | zhaoqi | financial | 20000 | 7 | 7 | 26 |
| 9 | zhoujiu | operation | 9000 | 9 | 9 | 28 |
| 10 | wutian | dev | 15000 | 10 | 10 | 30 | 这里没有留空部分是因为第二张表有的东西第一张表都有,无法留空
+------+-----------+------------+--------+------+------------+-----+
7 rows in set (0.000 sec)
现在在第二张表里插入11(第一张表没有11)
MariaDB [maqiang]> insert basic_info value(11,35);
Query OK, 1 row affected (0.002 sec)
此时第二张表有11
MariaDB [maqiang]> select * from basic_info;
+------------+-----+
| job_number | age |
+------------+-----+
| 1 | 21 |
| 3 | 25 |
| 5 | 30 |
| 7 | 26 |
| 9 | 28 |
| 10 | 30 |
| 2 | 20 |
| 11 | 35 |
+------------+-----+
8 rows in set (0.000 sec)
再次执行右连接,此时会把11取出来并留空
MariaDB [maqiang]> select * from info as a right join basic_info as b on a.jn = b.job_number;
+------+-----------+------------+--------+------+------------+-----+
| id | name | department | salary | jn | job_number | age |
+------+-----------+------------+--------+------+------------+-----+
| 1 | tom | sales | 5000 | 1 | 1 | 21 |
| 2 | jerry | dev | 20000 | 2 | 2 | 20 |
| 3 | zhangshan | dev | 10000 | 3 | 3 | 25 |
| 5 | wangwu | office | 8000 | 5 | 5 | 30 |
| 7 | zhaoqi | financial | 20000 | 7 | 7 | 26 |
| 9 | zhoujiu | operation | 9000 | 9 | 9 | 28 |
| 10 | wutian | dev | 15000 | 10 | 10 | 30 |
| NULL | NULL | NULL | NULL | NULL | 11 | 35 |
+------+-----------+------------+--------+------+------------+-----+
8 rows in set (0.000 sec)
10 . 取两张表里的一个内容(以tom为例)
MariaDB [maqiang]> select * from info,basic_info where info.name = 'tom' and info.jn = basic_info.job_number;
+----+------+------------+--------+----+------------+-----+
| id | name | department | salary | jn | job_number | age |
+----+------+------------+--------+----+------------+-----+
| 1 | tom | sales | 5000 | 1 | 1 | 21 | 取第一张表和第二张表里的第一张表里的名字叫tom且第一张表里的jn要等于第二张表里的job_number的内容
+----+------+------------+--------+----+------------+-----+
1 row in set (0.000 sec)
11.取两张表里的一个内容的某一项(只取name和age)
MariaDB [maqiang]> select info.name,basic_info.age from info,basic_info where info.name = 'tom' and info.jn = basic_info.job_number; 取第一张表名字是tom且第一张表的jn要等于第二张表的job_number的内容其中的第一张表的名字和第二张表的年龄
+------+-----+
| name | age |
+------+-----+
| tom | 21 |
+------+-----+
1 row in set (0.000 sec)
4.2.3 update语句
//DML操作之改操作update
//语法:UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
1. 修改name
查看此时第一个7里面的name为NULL
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | NULL | NULL |
| 7 | NULL | 20 |
+----+------+------+
8 rows in set (0.000 sec)
定义student1这个表里的age=NULL的选项的name改为hehe
MariaDB [abc]> update student1 set name = 'hehe' where age is NULL;
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看修改后的内容
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | hehe | NULL | 此时name变成hehe
| 7 | NULL | 20 |
+----+------+------+
8 rows in set (0.000 sec)
2. 修改age
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | hehe | NULL |
| 7 | NULL | 20 |
+----+------+------+
8 rows in set (0.000 sec)
修改student1这个表里的age=NULL那一项的age变成120
MariaDB [abc]> update student1 set age = 120 where age is NULL;
Query OK, 1 row affected (0.003 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | hehe | 120 |
| 7 | hehe | 20 |
+----+------+------+
8 rows in set (0.000 sec)
4.2.4 delete语句
- delete是删除表的内容,drop是删除表本身
//DML操作之删操作delete
//语法:DELETE FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
查看数据库
MariaDB [abc]> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| hellostudent |
| student1 |
| student10 |
| student11 |
| student2 |
| student3jjyy |
+---------------+
6 rows in set (0.000 sec)
查看表里的所有内容
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 2 | hehe | 15 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | hehe | 120 |
| 7 | hehe | 20 |
+----+------+------+
8 rows in set (0.000 sec)
删除id=2的那一行
MariaDB [abc]> delete from student1 where id = 2;
Query OK, 1 row affected (0.004 sec)
此时id=2那一行已被删除
MariaDB [abc]> select * from student1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | tom | 20 |
| 3 | gege | 10 |
| 4 | kkja | 16 |
| 5 | ooa | 34 |
| 6 | xixi | 20 |
| 7 | hehe | 120 |
| 7 | hehe | 20 |
+----+------+------+
7 rows in set (0.001 sec)
直接删除表格里的所有内容
MariaDB [abc]> delete from student1;
Query OK, 7 rows affected (0.004 sec)
MariaDB [abc]> select * from student1;
Empty set (0.000 sec)
4.2.5 truncate语句
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
1. 查看info
MariaDB [maqiang]> select * from info;
+----+-----------+------------+--------+----+
| id | name | department | salary | jn |
+----+-----------+------------+--------+----+
| 1 | tom | sales | 5000 | 1 |
| 2 | jerry | dev | 20000 | 2 |
| 3 | zhangshan | dev | 10000 | 3 |
| 4 | lisi | sales | 30000 | 4 |
| 5 | wangwu | office | 8000 | 5 |
| 6 | qianliu | financial | 10000 | 6 |
| 7 | zhaoqi | financial | 20000 | 7 |
| 8 | sunba | operation | 25000 | 8 |
| 9 | zhoujiu | operation | 9000 | 9 |
| 10 | wutian | dev | 15000 | 10 |
+----+-----------+------------+--------+----+
10 rows in set (0.000 sec)
2.删除info
MariaDB [maqiang]> delete from info;
Query OK, 10 rows affected (0.001 sec)
MariaDB [maqiang]> select * from info;
Empty set (0.000 sec)
3.此时再创建字段时,其id接着之前的来(删除之前最后一个id为10,那么用delete删除之后再创建时的id为11)
MariaDB [maqiang]> insert info(name,department,salary,jn) values('tom','sales',5000,1);
Query OK, 1 row affected (0.001 sec)
MariaDB [maqiang]> select * from info;
+----+------+------------+--------+----+
| id | name | department | salary | jn |
+----+------+------------+--------+----+
| 11 | tom | sales | 5000 | 1 |
+----+------+------------+--------+----+
1 row in set (0.000 sec)
4.此时用truncate 删除再次创建字段时其id会重置为1
MariaDB [maqiang]> truncate info;
Query OK, 0 rows affected (0.009 sec)
MariaDB [maqiang]> select * from info;
Empty set (0.000 sec)
MariaDB [maqiang]> insert info(name,department,salary,jn) values('tom','sales',5000,1);
Query OK, 1 row affected (0.001 sec)
MariaDB [maqiang]> select * from info;
+----+------+------------+--------+----+
| id | name | department | salary | jn |
+----+------+------------+--------+----+
| 1 | tom | sales | 5000 | 1 |
+----+------+------------+--------+----+
1 row in set (0.000 sec)
4.3 DCL操作
4.3.1 创建授权grant
权限类型(priv_type)
权限类型 | 代表什么? |
---|---|
ALL | 所有权限 |
SELECT | 读取内容的权限 |
INSERT | 插入内容的权限 |
UPDATE | 更新内容的权限 |
DELETE | 删除内容的权限 |
指定要操作的对象db_name.table_name
表示方式 | 意义 |
---|---|
* . * | 所有库的所有表 |
db_name | 指定库的所有表 |
db_name.table_name | 指定库的指定表 |
WITH GRANT OPTION:被授权的用户可将自己的权限副本转赠给其他用户,说白点就是将自己的权限完全复制给另一个用户。不建议使用。
授权tom用户在192.168.50.135上远程登录访问abc数据库
MariaDB [(none)]> grant all on abc.student to tom@192.168.50.135 identified by 'maqiang123'; 授所有权192.168.50.135上的tom用户能登陆访问192.168.50.131上的abc数据库包括该数据库下的student表
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
192.168.50.135登陆访问
[root@server ~]# mysql -utom -pmaqiang123 -h192.168.50.131
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 33
Server version: 10.3.17-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases; 能看到abc的数据库
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
+--------------------+
2 rows in set (0.001 sec)
MariaDB [(none)]> show tables from abc; 能看到该数据库下的student表
+---------------+
| Tables_in_abc |
+---------------+
| student |
+---------------+
查
MariaDB [abc]> desc student; 能查看该表的结构
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | float | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.001 sec)
增
MariaDB [abc]> alter table student add age tinyint; 在远程主机上增加age一栏
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [abc]> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(50) | YES | | NULL | |
| score | float | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | | 此时age已存在
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.001 sec)
改
MariaDB [abc]> drop table student; 在远程主机上(192.168.50.135)删除student
Query OK, 0 rows affected (0.004 sec)
MariaDB [abc]> show tables;
Empty set (0.001 sec)
4.3.2 查看授权
查看当前登录用户的授权信息
MariaDB [(none)]> show grants;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*31FBE3B6BBC9714F78109B4A42C5869DC227B39B' WITH GRANT OPTION |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.000 sec)
查看指定用户tom的授权信息
MariaDB [(none)]> show grants for tom@192.168.50.135;
+-----------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.50.135 |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.50.135' IDENTIFIED BY PASSWORD '*31FBE3B6BBC9714F78109B4A42C5869DC227B39B' |
| GRANT ALL PRIVILEGES ON `abc`.`student` TO 'tom'@'192.168.50.135' |
+-----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.000 sec)
4.3.3 取消授权REVOKE
MariaDB [(none)]> revoke select on abc.student from tom@192.168.50.135; 删除select权限
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> show grants for tom@192.168.50.135;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for tom@192.168.50.135 |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'tom'@'192.168.50.135' IDENTIFIED BY PASSWORD '*31FBE3B6BBC9714F78109B4A42C5869DC227B39B' |
| GRANT INSERT, UPDATE, DELETE, CREATE, DROP, REFERENCES, INDEX, ALTER, CREATE VIEW, SHOW VIEW, TRIGGER, DELETE HISTORY ON `abc`.`student` TO 'tom'@'192.168.50.135' |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.000 sec)
刷新权限
MariaDB [(none)]> flush privileges;
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
- GRANT或REVOKE等执行权限操作会保存于表中,mysql的服务进程会自动重读授权表,并更新至内存中
- 对于不能够或不能及时重读授权表的命令,可手动让mysql的服务进程重读授权表
5. 示例
5.1 创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age)
创建名叫maqiang的数据库
MariaDB [(none)]> create database maqiang;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
| maqiang |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.000 sec)
添加字段
MariaDB [maqiang]> create table student (id int not null primary key auto_increment,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.003 sec)
MariaDB [maqiang]> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.001 sec)
5.2 查看下该新建的表有无内容(用select语句)
MariaDB [maqiang]> select * from student;
Empty set (0.000 sec)
5.3 往新建的student表中插入数据(用insert语句)
MariaDB [maqiang]> insert student values(1,'tom',20),(2,'jerry',23),(3,'wangqing',25),(4,'sean',28),(5,'zhangshan',26),(6,'zhangshan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotian',20);
Query OK, 11 rows affected (0.001 sec)
Records: 11 Duplicates: 0 Warnings: 0
MariaDB [maqiang]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
5.4 修改lisi的年龄为50
MariaDB [maqiang]> update student set age = 50 where name = 'lisi';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [maqiang]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
5.5 查询student表中名字叫zhangshan的记录
MariaDB [maqiang]> select * from student where name = 'zhangshan';
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
+----+-----------+------+
2 rows in set (0.000 sec)
5.6 查询student表中名字叫zhangshan且年龄大于20岁的记录
MariaDB [maqiang]> select * from student where name = 'zhangshan' and age > 20;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 5 | zhangshan | 26 |
+----+-----------+------+
1 row in set (0.001 sec)
5.7 查询student表中年龄在23到30之间的记录
MariaDB [maqiang]> select * from student where age between 23 and 30;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
4 rows in set (0.001 sec)
5.8 修改wangwu的年龄为100
MariaDB [maqiang]> update student set age = 100 where name = 'wangwu';
Query OK, 1 row affected (0.001 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [maqiang]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 6 | zhangshan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
11 rows in set (0.000 sec)
5.9 删除student中名字叫zhangshan且年龄小于等于20的记录
MariaDB [maqiang]> delete from student where name = 'zhangshan' and age <= 20;
Query OK, 1 row affected (0.002 sec)
MariaDB [maqiang]> select * from student;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | wangqing | 25 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotian | 20 |
+----+-------------+------+
10 rows in set (0.000 sec)
5.10 以age字段降序排序
MariaDB [maqiang]> select * from student order by age desc;
+----+-------------+------+
| id | name | age |
+----+-------------+------+
| 9 | wangwu | 100 |
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
| 3 | wangqing | 25 |
| 2 | jerry | 23 |
| 1 | tom | 20 |
| 11 | qiuxiaotian | 20 |
| 10 | qiuyi | 15 |
| 8 | chenshuo | 10 |
+----+-------------+------+
10 rows in set (0.001 sec)
5.11 查询student表中年龄最小的3位同学
MariaDB [maqiang]> select * from student order by age limit 3;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 8 | chenshuo | 10 |
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
+----+----------+------+
3 rows in set (0.000 sec)
5.12 查询student表中年龄最大的4位同学
MariaDB [maqiang]> select * from student order by age desc limit 4;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 9 | wangwu | 100 |
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangshan | 26 |
+----+-----------+------+
4 rows in set (0.000 sec)