数据库是结构化信息或数据的有序集合,一般以电子形式存储在计算机系统中。通常由数据库管理系统 (DBMS) 来控制。在现实中,数据、DBMS 及关联应用一起被称为数据库系统,通常简称为数据库。
为了提高数据处理和查询效率,当今最常见的数据库通常以行和列的形式将数据存储在一系列的表中,支持用户便捷地访问、管理、修改、更新、控制和组织数据。另外,大多数数据库都使用结构化查询语言 (SQL) 来编写和查询数据。
关系数据库
面向对象数据库
分布式数据库
NoSQL 数据库
图形数据库
关系模型的数据库系统(RDBMS),简称关系型数据库,R是Relational的缩写。就是把数据放入表格中,这些表格可以相互关联。
数据结构模型主要有:
层次模型用树形结构表示数据之间的联系,它的数据结构类似一棵倒置的树,有且仅有一个根节点,其余节点都是非根节点。层次模型中的每个节点表示一个记录类型,记录之间是一对多的关系,即一个节点可以有多个子节点。
网状模型用网状结构表示数据之间的关系,网状模型的数据结构允许有一个以上的节点无双亲和至少有一个节点可以有多于一个的双亲。随着应用环境的扩大,基于网状模型的数据库的结构会变得越来越复杂,不利于最终用户掌握。
关系模型以数据表的形式组织数据,实体之间的关系通过数据表的公共属性表示,结构简单明了,并且有逻辑计算、数学计算等坚实的数学理论作为基础。关系模型是目前广泛使用的数据模型。
面向对象模型用面向对象的思维方式与方法来描述客观实体,它继承了关系数据库系统已有的优势,并且支持面向对象建模、对象存取与持久化以及代码级面向对象数据操作,是现在较为流行的新型数据模型。
任何一个数据库管理系统都是基于某种数据模型的,数据模型不同,相应的数据库管理系统就不同。
关系模型:
关系模型本质上就是若干个存储数据的二维表,即
表的每一行称为记录,记录的是一个逻辑意义上的数据。
表的每一列称为字段,同一个表的每一行记录都拥有相同的若干字段。字段定义了数据类型(整形、浮点型、字符串、日期等),以及是够能够为NULL。这里的NULL表示字段数据并不存在,并不是指数字0或者空字符串(空字符串指空格)。
数据库管理系统:
数据库管理系统(Database Management System)是一种操纵和管理数据库的大型软件,用于建立、使用和维护数据库,简称DBMS。它对数据库进行统一的管理和控制,以保证数据库的安全性和完整性。用户通过DBMS访问数据库中的数据,数据库管理员也通过DBMS进行数据库的维护工作。它可使多个应用程序和用户用不同的方法在同时或不同时刻去建立,修改和询问数据库。大部分数据库管理系统(DBMS)提供数据定义语言DDL和数据操作语言DML,供用户定义数据库的模式结构与权限约束,实现对数据的追加、删除等操作。
常见的关系型数据库管理系统:
MySQL:MySQL,mariaDB,Percona-Server
SQL Server:由微软开发的数据库管理系统,简称msSQL
PostgreSQL:简称为pgsql
Oracle
SQL(Structured Query Language):结构化查询语言。是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统
约束(constraint):约束用于规定表中的数据规则,是数据库中保证数据完整性的方法。如果存在违反约束的数据行为,行为会被约束终止。
约束类型:
索引:索引是帮助MySQL高效获取数据的数据结构,加快数据库的查询速度。对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息。一种能帮助mysql提高了查询效率的数据结构:索引数据结构。
索引类型:
主键索引:primary key
唯一索引:
复合索引:
全文索引:
空间索引:
前缀索引:
常见组件:
SQL语句有四大类:
即数据定义语言,定义语言就是定义关系模式、删除关系、修改关系模式以及创建数据库中的各种对象,比如表、聚簇、索引、视图、函数、存储过程和触发器等等。
主要是进行插入元组、删除元组、修改元组的操作。主要有insert、update、delete语法组成。
是用来进行数据库中数据的查询的,即最常用的select语句。
用来授权或回收访问数据库的某种特权,并控制数据库操纵事务发生的时间及效果,能够对数据库进行监视。比如常见的授权、取消授权、回滚、提交等等操作。
SQL语句类型 | 对应操作 |
---|---|
DDL | CREATE:创建 DROP:删除 ALTER:修改 |
DML | INSERT:向表中插入数据 DELETE:删除表中数据 UPDATE:更新表中数据 SELECT:查询表中数据 |
DCL | GRANR:授权 REVEKE:移除授权 |
DQL | SELECT:查询表中数据 |
MySQL的安装方式有三种:
//配置mysql的yum源
[root@kiwi222 ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
··········································略
2023-09-05 16:23:42 (136 MB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]
//安装MySQL5.7
[root@kiwi222 ~]# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
···················································略
Complete!
[root@kiwi222 ~]#
//MySQL配置
#启动mysql并设置开机自动启动
[root@kiwi222 ~]# systemctl enable --now mysqld
[root@kiwi222 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
//配置密码
#在日志文件中找出临时密码
[root@kiwi222 ~]# grep "password" /var/log/mysqld.log
2023-09-05T16:43:49.079508Z 1 [Note] A temporary password is generated for root@localhost: !A.<AkiDp557
#使用获取到的临时密码登录
[root@kiwi222 ~]# 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.38
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('kiwi111');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//卸载yum源
为避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@kiwi222 ~]# rpm -e mysql57-community-release
//安装环境依赖包
[root@kiwi222 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel
//下载二进制格式的mysql软件包
[root@kiwi222 ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.42-el7-x86_64.tar.gz
--2023-08-28 22:23:39-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.42-el7-x86_64.tar.gz
················································略
2023-08-28 22:32:15 (1.40 MB/s) - ‘mysql-5.7.42-el7-x86_64.tar.gz’ saved [746050963/746050963]
[root@kiwi222 ~]# ls
anaconda-ks.cfg mysql-5.7.42-el7-x86_64.tar.gz
[root@kiwi222 ~]#
//创建用户和组
[root@kiwi222 ~]# groupadd -r -g 306 mysql
[root@kiwi222 ~]# useradd -r -M -s /sbin/nologin -g 306 -u 306 mysql
[root@kiwi222 ~]#
//解压软件至/usr/local/
[root@kiwi222 src]# tar xf mysql-5.7.42-el7-x86_64.tar.gz -C /usr/local/
[root@kiwi222 ~]# cd /usr/local/
[root@kiwi222 local]# ls
apache apr-util etc include lib64 mysql-5.7.42-el7-x86_64 share
apr bin games lib libexec sbin src
[root@kiwi222 local]#
//创建软连接
[root@kiwi222 ~]# cd /usr/local/
[root@kiwi222 local]# ln -sv mysql-5.7.42-el7-x86_64/ mysql
'mysql' -> 'mysql-5.7.42-el7-x86_64/'
[root@kiwi222 local]#
//修改目录/usr/local/mysql的属主属组
[root@kiwi222 local]# chown -R mysql.mysql /usr/local/mysql
[root@kiwi222 local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 24 Aug 28 22:34 /usr/local/mysql -> mysql-5.7.42-el7-x86_64/
[root@kiwi222 local]#
//添加环境变量
[root@kiwi222 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@kiwi222 local]# source /etc/profile.d/mysql.sh
[root@kiwi222 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/apache/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@kiwi222 local]#
//建立数据存放目录
[root@kiwi222 local]# mkdir /opt/kiwi
[root@kiwi222 local]# chown -R mysql.mysql /opt/kiwi/
[root@kiwi222 local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 28 22:35 kiwi
[root@kiwi222 local]#
//初始化数据库
[root@kiwi222 local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
··········································略
2023-08-29T02:36:18.141256Z 0 [Warning] CA certificate ca.pem is self signed.
2023-08-29T02:36:18.246065Z 1 [Note] A temporary password is generated for root@localhost: =L1/v;!,I(hL
[root@kiwi222 local]#
##这里的最后会生成一个临时密码
##我这里的密码为:=L1/v;!,I(hL
//配置MySQL
[root@kiwi222 local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
[root@kiwi222 local]#
[root@kiwi222 local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@kiwi222 local]# ldconfig
[root@kiwi222 local]#
[root@kiwi222 mysql]# vim /etc/man_db.conf
MANDATORY_MANPATH /usr/local/mysql/man
//生成配置文件
[root@kiwi222 local]# cat > /etc/my.cnf <
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
[root@kiwi222 local]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@kiwi222 local]#
//配置服务启动脚本
[root@kiwi222 local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@kiwi222 local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@kiwi222 local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@kiwi222 local]#
//启动mysql
[root@kiwi222 local]# service mysqld start
Starting MySQL.Logging to '/opt/data/kiwi222.err'.
SUCCESS!
[root@kiwi222 local]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 511 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 80 *:3306 *:*
[root@kiwi222 local]# ps -ef | grep mysql
root 67129 1 0 22:38 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 67317 67129 0 22:38 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=kiwi222.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 67363 67008 0 22:39 pts/1 00:00:00 grep --color=auto mysql
[root@kiwi222 local]#
//修改密码
## 如果提示以下报错:
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
##则需要先创建一个软连接
[root@kiwi222 ~]# ln -s /usr/lib64/libncurses.so.6.1 /usr/lib64/libncurses.so.5
[root@kiwi222 ~]# ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
## 登陆时用临时密码
##我这里的密码为:=L1/v;!,I(hL
[root@kiwi222 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.39
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password = password('kiwi111');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@kiwi222 ~]#
[root@kiwi222 ~]# yum install -y mariadb*
MySQL的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后扎到的最终生效
/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf
MySQL常用配置文件参数:
参数 | 说明 |
---|---|
端口 = 3306 | 设置监听端口 |
socket = /tmp/mysql.sock | 指定套接字文件位置 |
basedir = /usr/local/mysql | 指定MySQL的安装路径 |
datadir = /data/mysql | 指定MySQL的数据存放路径 |
pid-file = /data/mysql/mysql.pid | 指定进程ID文件存放路径 |
用户 = MySQL | 指定MySQL以什么用户的身份提供服务 |
跳过名称解析 | 禁止MySQL对外部连接进行DNS解析使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式否则MySQL将无法正常处理连接请求 |
客户端
mysql:CLI交互式客户端程序
mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
mysqldump:mysql备份工具
mysqladmin:执行管理操作的客户端程序,用来检查服务器的配置和当前状态、创建和删除数据库等
服务器端
语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定mysql服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P (大写) //指定数据库监听的端口,如-P3307
-V (大写) //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本。
// 查看数据库版本
[root@kiwi222 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.39, for linux-glibc2.12 (x86_64) using EditLine wrapper
[root@kiwi222 ~]#
//登录数据库
[root@kiwi222 ~]# mysql -uroot -pkiwi111
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 9
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
## 后面可接 -hIP地址 进行远程连接登录
## 警告是指在命令行里直接使用-pPASSWORD的方式登录是不安全的行为。推荐不要在命令行界面直接使用密码登录。
//不进入数据库直接操作 -e
[root@kiwi222 ~]# mysql -uroot -pkiwi111 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| kiwi111 |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@kiwi222 ~]#
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] 数据库名字 [数据库选项];
//查看当前有哪些数据库
语法:SHOW DATABASES;
//删除数据库
语法:DROP DATABASE [IF EXISTS] 数据库名字;
## 注:带中括号的可加可不加
创建数据库kiwi111
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> CREATE DATABASE kiwi111;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kiwi111 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
删除数据库kiwi111
mysql> DROP DATABASE kiwi111;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
//创建表:
语法:CREATE TABLE [数据库名.]表名(字段名 字段类型, ... ,字段名 字段类型)[表选项];
//查看当前数据库有哪些表:
语法:SHOW TABLES;
//查看表结构:
语法:DESC 表名
//删除表
语法:DROP TABLE [ IF EXISTS ] 表名;
## 注:进行表操作之前需要先进入一个数据库
创建表kiwi
mysql> CREATE DATABASE kiwi111;
Query OK, 1 row affected (0.00 sec)
mysql> USE kiwi111; //进入数据库kiwikiwi111
Database changed
mysql> CREATE TABLE kiwi(id int NOT NULL,name VARCHAR(100) NOT NULL,age tinyint);
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES; //查看当前数据库所有表
+-------------------+
| Tables_in_kiwi111 |
+-------------------+
| kiwi |
+-------------------+
1 row in set (0.00 sec)
mysql> desc kiwi; //查看表结构
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
删除表kiwi
mysql> DROP TABLE kiwi;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES;
Empty set (0.00 sec)
mysql>
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
//创建数据库用户:
语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//删除数据库用户
语法:DROP USER 'username'@'host';
创建数据库用户kiwi123
mysql> CREATE USER 'kiwi123'@'192.168.234.33' IDENTIFIED BY '1';
Query OK, 0 rows affected (0.01 sec)
mysql> quit
Bye
//用kiwi123用户登录
[root@kiwi222 ~]# mysql -ukiwi123 -p -h192.168.234.33
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
删除数据库用户kiwi123
mysql> DROP USER 'kiwi123'@'192.168.234.33';
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@kiwi222 ~]# mysql -ukiwi123 -p -h192.168.234.33
Enter password:
ERROR 1130 (HY000): Host '192.168.234.33' is not allowed to connect to this MySQL server
[root@kiwi222 ~]#
//删除数据库用户之前需注意进行这个操作的数据库用户是否具有权限
//查看支持的所有字符集
语法: SHOW CHARACTER SET;
//查看当前数据库支持的所有存储引擎
语法:SHOW ENGINES;
//查看数据库信息
语法:SHOW DATABASES;
//不进入某数据库而列出其包含的所有表
语法:SHOW TABLES FROM table_name;
//查看表结构
DESC [数据库名.]table_name;
## 带上数据库名可不进入数据库直接看表
//查看某表的创建命令
语法:SHOW CREATE TABLE table_name;
//查看某表的状态
语法:SHOW TABLE STATUS LIKE 'table_name'\G
## 以/G结尾是为了让结果竖向显示
//查看支持的所有字符集
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 |
··································略
mysql>
//查看当前数据库支持的所有存储引擎
mysql> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
//查看数据库信息
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kiwi111 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
//不进入某数据库而列出其包含的所有表
mysql> SHOW TABLES FROM kiwi111;
+-------------------+
| Tables_in_kiwi111 |
+-------------------+
| kiwi |
+-------------------+
1 row in set (0.00 sec)
mysql>
//查看表结构
mysql> DESC kiwi111.kiwi;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
//查看某表的创建命令
mysql> SHOW CREATE TABLE kiwi;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
| kiwi | CREATE TABLE `kiwi` (
`id` int(11) NOT NULL,
`name` varchar(100) NOT NULL,
`age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
//查看某表的状态
mysql> SHOW TABLE STATUS LIKE 'kiwi';
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| kiwi | InnoDB | 10 | Dynamic | 0 | 0 | 16384 | 0 | 0 | 0 | NULL | 2023-09-05 10:15:57 | NULL | NULL | latin1_swedish_ci | NULL | | |
+------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
1 row in set (0.00 sec)
mysql> SHOW TABLE STATUS LIKE 'kiwi'\G
*************************** 1. row ***************************
Name: kiwi
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2023-09-05 10:15:57
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
mysql>
//获取命令使用帮助
//语法:HELP keyword;
//获取创建表的帮助
mysql> HELP CREATE TABLE;
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
····································略
//获取查看的帮助
mysql>
mysql> HELP SHOW;
Name: 'SHOW'
Description:
SHOW has many forms that provide information about databases, tables,
columns, or status information about the server. This section describes
those following:
SHOW {BINARY | MASTER} LOGS
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
·····························略
DML操作包括:
均属针对表的操作。
//DML操作之增操作insert
//语法:
INSERT [INTO] table_name [(column_name,...)] {VALUES | VALUE} (value1,...),(...),...
//插入一条数据
mysql> INSERT kiwi (id,name,age) VALUE (1,'xiaoxiao',21);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
+----+----------+------+
1 row in set (0.00 sec)
mysql>
//插入多条数据
mysql> INSERT kiwi (id,name,age) VALUES (2,'xiaoming',24),(3,'xiaogang',23),(4,'honghong',29);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 29 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
查询表中的记录 select
可以通过条件判断和通配符来查看某些特定的记录
字段column表示法
表示符 | 代表什么? |
---|---|
* | 所有字段 |
as | 字段别名,如col1 AS alias1 当表名很长时用别名代替 |
条件判断语句WHERE
操作类型 | 常用操作符 |
---|---|
操作符 | >,<,>=,<=,=,!= BETWEEN column# AND column# LIKE:模糊匹配 RLIKE:基于正则表达式进行模式匹配 IS NOT NULL:非空 IS NULL:空 |
条件逻辑操作 | and、or、not |
#操作符
> //大于某个值
< //小于某个值
>= //大于且包括等于某个值
<= //小于且包括等于某个值
!= //不等于
= //等于
BETWEEN A AND B //在A和B之间,且包括A和B
(省略)
#条件逻辑
条件A and 条件B //两个条件必须同时满足
条件A ro 条件B //两个条件满足其中某一个即可
not 条件A //不满足这个条件
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个结果 |
//查操作select
//语法:
SELECT column1,column2,... FROM table_name [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
//查询所有字段的记录
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 29 |
+----+----------+------+
4 rows in set (0.00 sec)
//查询字段name的记录
mysql> SELECT name FROM kiwi;
+----------+
| name |
+----------+
| xiaoxiao |
| xiaoming |
| xiaogang |
| honghong |
+----------+
4 rows in set (0.00 sec)
mysql>
//查询字段name和age的记录
mysql> SELECT name,age FROM kiwi;
+----------+------+
| name | age |
+----------+------+
| xiaoxiao | 21 |
| xiaoming | 24 |
| xiaogang | 23 |
| honghong | 29 |
+----------+------+
4 rows in set (0.00 sec)
mysql>
//查询所有字段的记录,将age进行升序排序后显示结果
mysql> SELECT * FROM kiwi order by age ASC;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 3 | xiaogang | 23 |
| 2 | xiaoming | 24 |
| 4 | honghong | 29 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
## ASC可加可不加,默认为ASC
//查询所有字段的记录,将age进行逆序排序后显示结果
mysql> SELECT * FROM kiwi order by age desc;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 4 | honghong | 29 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 1 | xiaoxiao | 21 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
//查询所有字段的记录,将age进行升序排序后,只取出前3条记录,显示出来
mysql> SELECT * FROM kiwi ORDER BY age LIMIT 3;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 3 | xiaogang | 23 |
| 2 | xiaoming | 24 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql>
//查询所有字段的记录,将age进行降序排序后,跳过第一条,再取出前2条记录,显示出来
mysql> SELECT * FROM kiwi ORDER BY age desc limit 1,2;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql>
//查询age=23的所有字段的记录(大于和小于类似)
mysql> SELECT * FROM kiwi WHERE age = 23;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 3 | xiaogang | 23 |
+----+----------+------+
1 row in set (0.00 sec)
mysql>
## age > 23
mysql> SELECT * FROM kiwi WHERE age > 23;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 2 | xiaoming | 24 |
| 4 | honghong | 29 |
+----+----------+------+
2 rows in set (0.00 sec)
mysql>
//查询age在20到26之间的所有字段的记录
mysql> SELECT * FROM kiwi WHERE age between 20 and 26;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql>
//查看全部字段的记录,并且age不为空的
mysql> SELECT * FROM kiwi WHERE age is NOT NULL;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 29 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
//查看全部字段的记录,并且age为空的
mysql> SELECT * FROM kiwi WHERE age IS NULL;
//update更新数据(修改)
//语法:
UPDATE table_name SET column1 = new_value1[,column2 = new_value2,...] [WHERE clause] [ORDER BY 'column_name' [DESC]] [LIMIT [m,]n];
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 29 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
//修改honghong的age为100
mysql> UPDATE kiwi SET age = 100 WHERE name = 'honghong';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 100 |
+----+----------+------+
4 rows in set (0.00 sec)
mysql>
//delete 删除记录
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
| 4 | honghong | 100 |
+----+----------+------+
4 rows in set (0.00 sec)
//删除name=honghong的记录
mysql> DELETE FROM kiwi WHERE name = 'honghong';
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql>
truncate与delete的区别:
语句类型 | 特点 |
---|---|
delete | DELETE删除表内容时仅删除内容,但会保留表结构 DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项 可以通过回滚事务日志恢复数据 非常占用空间 |
truncate | 删除表中所有数据,且无法恢复 表结构、约束和索引等保持不变,新添加的行计数值重置为初始值 执行速度比DELETE快,且使用的系统和事务日志资源少 通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放 对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据 不能用于加入了索引视图的表 |
//
语法:truncate table_name;
mysql> SELECT * FROM kiwi;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | xiaoxiao | 21 |
| 2 | xiaoming | 24 |
| 3 | xiaogang | 23 |
+----+----------+------+
3 rows in set (0.00 sec)
mysql>
//清空表内容
mysql> TRUNCATE kiwi;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM kiwi;
Empty set (0.01 sec) ## 没有任何记录
mysql>
mysql> DESC kiwi; ## 但是表结构还在
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
## 也就是说只会删除记录,而不会删除表结构
//删除表
mysql> DROP table kiwi;
Query OK, 0 rows affected (0.00 sec)
mysql> DESC kiwi;
ERROR 1146 (42S02): Table 'kiwi111.kiwi' doesn't exist
mysql>
## DROP是连带表一起删除
权限类型(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];
//建议每次授权后都执行一下flush privileges;刷新一下授权信息
//授权用户kiwi123在数据库本机上登录访问所有数据库
mysql> GRANT ALL ON *.* TO 'kiwi123'@'localhost' IDENTIFIED BY 'kiwi123';
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> GRANT ALL ON *.* TO 'kiwi123'@'127.0.0.1' IDENTIFIED BY 'kiwi123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
## 注:如果需要本机登录的话则localhost 和 127.0.0.1 要一起写
//使用用户kiwi123登录
[root@kiwi222 ~]# mysql -ukiwi123 -h127.0.0.1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//授权用户kiwi123在192.168.234.33远程登录访问数据库kiwi111
mysql> GRANT ALL ON kiwi111.* TO 'kiwi123'@'192.168.234.33' IDENTIFIED BY 'kiwi123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
[root@kiwi222 ~]# mysql -ukiwi123 -h192.168.234.33 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 5.7.39 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
授权kiwi123用户在192.168.179.10上远程登录访问数据库kiwi111里面的kiwi表
mysql> GRANT ALL ON kiwi111.kiwi TO 'kiwi123'@'192.168.234.330' IDENTIFIED BY 'kiwi123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
授权kiwi123用户在所有主机上远程登录访问数据库kiwi111
mysql> GRANT ALL ON kiwi111.* TO 'kiwi123'@'%' IDENTIFIED BY 'kiwi123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
//查看当前登录用户的授权信息
语法:SHOW GRANTS;
//查看指定用户的授权信息
语法:SHOW GRANTS FOR username;
//查看当前登录用户的授权信息
mysql> SHOW GRANTS;
+-------------------------------------------------------------------+
| Grants for kiwi123@192.168.234.33 |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kiwi123'@'192.168.234.33' |
| GRANT ALL PRIVILEGES ON `kiwi111`.* TO 'kiwi123'@'192.168.234.33' |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
//查看指定用户kiwi123的授权信息
mysql> SHOW GRANTS FOR kiwi123;
+------------------------------------------------------+
| Grants for kiwi123@% |
+------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kiwi123'@'%' |
| GRANT ALL PRIVILEGES ON `kiwi111`.* TO 'kiwi123'@'%' |
+------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
//查看指定用户kiwi123在本机的授权信息
mysql> SHOW GRANTS FOR 'kiwi123'@'localhost';
+------------------------------------------------------+
| Grants for kiwi123@localhost |
+------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'kiwi123'@'localhost' |
+------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
//查看指定用户kiwi123在主机192.168.234.33的授权信息
mysql> SHOW GRANTS FOR 'kiwi123'@'192.168.234.33';
+-------------------------------------------------------------------+
| Grants for kiwi123@192.168.234.33 |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kiwi123'@'192.168.234.33' |
| GRANT ALL PRIVILEGES ON `kiwi111`.* TO 'kiwi123'@'192.168.234.33' |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql>
//语法:
REVOKE priv_type,... ON db_name.table_name FROM 'username'@'host';
//取消用户kiwi123在主机192.168.234.33上可以访问kiwi111数据的授权
//先查看原来的授权信息
mysql> SHOW GRANTS FOR 'kiwi123'@'192.168.234.33';
+-------------------------------------------------------------------+
| Grants for kiwi123@192.168.234.33 |
+-------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'kiwi123'@'192.168.234.33' |
| GRANT ALL PRIVILEGES ON `kiwi111`.* TO 'kiwi123'@'192.168.234.33' |
+-------------------------------------------------------------------+
2 rows in set (0.01 sec)
mysql> REVOKE ALL ON kiwi111.* FROM 'kiwi123'@'192.168.234.33';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
//再次查看授权信息
mysql> SHOW GRANTS FOR 'kiwi123'@'192.168.234.33';
+--------------------------------------------------+
| Grants for kiwi123@192.168.234.33 |
+--------------------------------------------------+
| GRANT USAGE ON *.* TO 'kiwi123'@'192.168.234.33' |
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql>
注意:mysql服务进程启动时会读取mysql库中的所有授权表至内存中:
mysql> FLUSH PRIVILEGES;