一、MySQL简介
数据库概念
数据库(Database)是按照数据结构来组织、存储和管理数据的建立在计算机存储设备上的仓库。
MySQL基本介绍
MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。
AB公司被Sun公司收购—》Oracle又收购了Sun公司
1、Mysql是一种开源免费的数据库产品
2、Mysql对PHP的支持是最好(wamp或者lamp)
数据库:存储数据的仓库
1、数据库分类
1、网络数据库
网络数据库是指把数据库技术引入到计算机网络系统中,借助于网络技术将存储于数据库中的大量信息及时发布出去;而计算机网络借助于成熟的数据库技术对网络中的各种数据进行有效管理,并实现用户与网络中的数据库进行实时动态数据交互。
2、层级数据库
层次结构模型实质上是一种有根结点的定向有序树(在数学中"树"被定义为一个无回的连通图)。
3、关系数据库
关系数据库,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。
数据库的另外一种区分方式:基于存储介质
存储介质分为两种:磁盘和内存
关系型数据库:存储在磁盘中
非关系型数据库:存储在内存中
2、关系型数据库
典型关系型数据库
Oracle、DB2、Microsoft SQL Server、Microsoft Access、MySQL、SQLite
小型关系型数据库:Microsoft Access,SQLite
中型关系型数据库:SQL Server,Mysql
大型关系型数据库:Oracle,DB2
1、基本概念
关系数据库,是建立在关系模型基础上的数据库,借助于集合代数等数学概念和方法来处理数据库中的数据。现实世界中的各种实体以及实体之间的各种联系均用关系模型来表示。关系模型是由埃德加·科德于1970年首先提出的,并配合“科德十二定律”。现如今虽然对此模型有一些批评意见,但它还是数据存储的传统标准。关系模型由关系数据结构、关系操作集合、关系完整性约束三部分组成。
2、关系数据结构
指的数据以什么方式来存储,是一种二维表的形式存储
本质:二维表
姓名 年龄 身高 体重
张三 30 187 70
李四 40
3、关系操作集合
如何来关联和管理对应的存储数据,SQL指令
获取张三的年纪:已知条件为姓名
Select 年龄 from 二维表 where 姓名 = 张三;
4、关系完整性约束
数据内部有对应的关联关系,以及数据与数据之间也有对应的关联关系
姓名 年龄 身高 体重
张三 30 187 70
李四 40
表内约束:对应的具体列只能放对应的数据(不能乱放)
表间约束:自然界各实体都是有着对应的关联关系(外键)
3、SQL分类
Mysql当前跟Oracle是一个公司的:隶属于Oracle
SQL介绍
SQL基本介绍
结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。
SQL就是专门为关系型数据库而设计出来的。
1、数据查询语言(DQL:Data Query Language):
其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出。保留字SELECT是DQL(也是所有SQL)用得最多的动词,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。这些DQL保留字常与其他类型的SQL语句一起使用。
专门用于查询:代表指令为select/show
2、数据操作语言(DML:Data Manipulation Language):
其语句包括动词INSERT,UPDATE和DELETE。它们分别用于添加,修改和删除表中的行。也称为动作查询语言。
专门用于写数据:代表指令为insert,update和delete
3、事务处理语言(TPL):
它的语句能确保被DML语句影响的表的所有行及时得以更新。TPL语句包括BEGIN TRANSACTION,COMMIT和ROLLBACK。(不是所有的关系型数据库都提供事务安全处理)
专门用于事务安全处理:transaction,COMMIT,ROLLBACK
4、数据控制语言(DCL):
它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问。某些RDBMS可用GRANT或REVOKE控制对表单个列的访问。
专门用于权限管理:代表指令为grant和revoke
###5、数据定义语言(DDL):
其语句包括动词CREATE和DROP。在数据库中创建新表或删除表(CREAT TABLE 或 DROP TABLE);为表加入索引等。DDL包括许多与人数据库目录中获得数据有关的保留字。它也是动作查询的一部分。
专门用于结构管理:代表指令create和drop(alter)
二、MySQL搭建
环境:一台rockylinux 9 虚拟机,并且做好了基本配置和时钟同步,并使用Xshell连接操作
1、安装包文件
[root@mysql ~]# yum -y install lrzsz tar //后面会往进拖文件并解压会用到
[root@mysql ~]# mkdir /opt/software //压缩包文件的存放位置
[root@mysql ~]# cd /opt/software/
[root@mysql software]# rz -E //这里直接拖进来,等待传输完成
rz waiting to receive.
[root@mysql software]# ls
mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@mysql software]# useradd -r -M -s /sbin/nologin mysql //创建MySQL服务用户
[root@mysql software]# id mysql
uid=996(mysql) gid=993(mysql) groups=993(mysql)
[root@mysql software]# yum -y install libncurses* //安装MySQL所需的依赖包
2、解压安装
[root@mysql software]# tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ //将其解压到 /usr/local/目录下
[root@mysql local]# cd /usr/local/
[root@mysql local]# ls
bin etc games include lib lib64 libexec mysql-5.7.37-linux-glibc2.12-x86_64 sbin share src
[root@mysql local]# ln -sv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql //这里名字太长了,做个软链接
'mysql' -> 'mysql-5.7.37-linux-glibc2.12-x86_64/'
[root@mysql local]# ll mysql
lrwxrwxrwx. 1 root root 36 Jul 30 16:12 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
[root@mysql local]# mkdir /opt/data //创建一个mysql数据存放位置
[root@mysql local]# chown -R mysql.mysql /opt/data/ //修改/opt/data目录的所有者和所属组
[root@mysql local]# ll /opt
total 0
drwxr-xr-x. 2 mysql mysql 6 Jul 30 16:17 data
drwxr-xr-x. 2 root root 56 Jul 30 16:00 software
[root@mysql local]# chown -R mysql.mysql /usr/local/mysql //修改mysql目录的所有者和所属组
[root@mysql local]# ll mysql
lrwxrwxrwx. 1 mysql mysql 36 Jul 30 16:12 mysql -> mysql-5.7.37-linux-glibc2.12-x86_64/
3、初始化并启动数据库
[root@mysql local]# yum -y install libaio //直接安装数据库会报错,还需要安装一个包文件
//初始化数据库,此时主机还不能识别mysqld的命令,路径需要写全,指定数据存放在/opt/data/目录中
[root@mysql local]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data/
[root@mysql local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
'/usr/local/include/mysql' -> '/usr/local/mysql/include/'
//做一个软链接
//编辑mysql配置文件
[root@mysql local]# vim /etc/ld.so.conf.d/mysql.conf //在里面写入
/usr/local/mysql/lib
[root@mysql local]# ldconfig //执行一下看有无报错信息
[root@mysql local]# vim /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@mysql local]# vim mysql/support-files/mysql.server //在其中修改
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/usr/local/mysql //这里找到添加上就可以了
datadir=/opt/data
//配置mysql服务
[root@mysql local]# vim /usr/lib/systemd/system/mysqld.service //在里面写入以下内容
[Unit]
Description=mysql server daemon
After=network.targe
[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecStop=/usr/local/mysql/support-files/mysql.server stop
ExecReload=/bin/kill -HUP \$MAINPID
[Install]
WantedBy=multi-user.target
//启动mysql
[root@mysql local]# systemctl restart mysqld
[root@mysql local]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
4、配置数据库
//设置数据库密码,root是数据库用户root,不是系统的管理员用户
[root@mysql local]# /usr/local/mysql/bin/mysql -u root -e "set password=password('redhat')"
[root@mysql local]# /usr/local/mysql/bin/mysql -uroot -predhat
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 3
Server version: 5.7.37 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>
//这就可以登陆了
//配置mysql环境变量
[root@mysql local]# vim /etc/profile //在其最末尾写入
export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
[root@mysql local]# source /etc/profile
//此时就可以直接用mysql命令了
[root@mysql local]# mysql -uroot -predhat
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 4
Server version: 5.7.37 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>
mysql配置完成
三、MySQL基础组件与操作
Mysql中用到的操作指令就是SQL指令
1、关系型数据库的常见组件
关系型数据库的常见组件有:
数据库:database
表:table,由行(row)和列(column)组成
索引:index
视图:view
用户:user
权限:privilege
存储过程:procedure
存储函数:function
触发器:trigger
事件调度器:event scheduler
DDL操作
use + [数据库名]; //进入数据库
2、约束
constraint,向数据表提供的数据要遵守的限制
(1)、主键约束 (primary key)
一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
一个表只能存在一个
(2)、唯一键约束 (unique key)
一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
一个表可以存在多个
(3)、外键约束 (foreign key)
一个表中的某字段可填入数据取决于另一个表的主键已有的数据
普遍会和主键约束一起使用,用来确保数据的一致性。
对于两个具有关联关系的表来说,相关联字段中主键所在的表就是主表(父表),外键所在的表就是从表(子表)。
所以外键就是用来建立主表与从表的关联关系,为两个表的数据建立连接,约束两个表中数据的一致性和完整性。
(4)、检查约束 (check)
检查约束是用来检查数据表中字段值有效性的一种约束。
(5)、非空约束 (not NULL)
用来约束表中的字段不能为空。在表中某个列的定义后加上关键字NOT NULL作为限定词,来约束该列的取值不能为空
(6)、默认值约束 (default)
当数据表中某个字段不输入值的时候,自动为其添加一个已经设置好的值
3、数据库操作
(1)、创建数据库
语法:create database 数据库名字;
//创建数据库sq
mysql> create database sq;
Query OK, 1 row affected (0.00 sec)
(2)、查看当前实例有哪些数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sq |
| sys |
+--------------------+
5 rows in set (0.00 sec)
(3)、删除数据库
语法:drop database 数据库名;
删除数据库sq
mysql> drop database sq;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
4、表操作
(1)、创建表
语法: create table 表名 (字段和参数)
mysql> create database sq;
mysql> use sq;
mysql> create table student (id int not null ,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+--------------+
| Tables_in_sq |
+--------------+
| student |
+--------------+
1 row in set (0.00 sec)
(2)、删除表
语法:drop table + 表名
mysql> drop table student;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
5、管理表中的数据
插入数据 —— Insert
语法: insert into + 表名 + valuse();
//括号里面接要插入的数据
mysql> show tables;
+--------------+
| Tables_in_sq |
+--------------+
| student |
+--------------+
1 row in set (0.00 sec)
mysql> desc student;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | char(15) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into student values(1,'tom',18),(2,'marry',19),(3,'rose',19);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 19 |
+----+-------+------+
3 rows in set (0.00 sec)
修改更新数据
语法1:update + 表名 + set + 列名 = ‘要修改后的数据’;
语法2:update + 表名 + set + 列名 = ‘要修改后的数据’ + where + 筛选的条件 ;
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 18 |
| 2 | marry | 19 |
| 3 | rose | 19 |
| 4 | zhangsan | 18 |
| 5 | lisi | 20 |
| 6 | wangwu | 18 |
+----+----------+------+
6 rows in set (0.01 sec)
mysql> update student set age = '21';
//将表中所有age的数据改为21
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 21 |
| 2 | marry | 21 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
| 6 | wangwu | 21 |
+----+----------+------+
6 rows in set (0.00 sec)
mysql> update student set age = '20' where id <=2;
//也可以添加筛选条件
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
| 6 | wangwu | 21 |
+----+----------+------+
6 rows in set (0.00 sec)
删除数据
语法:delete from + 表名 + where + 筛选的条件;
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
| 6 | wangwu | 21 |
+----+----------+------+
6 rows in set (0.00 sec)
mysql> delete from student where id=6;
//删除id字段为6的那一行的信息
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
6、数据查询
查询所有 — select
语法1:select + * + from + 表名;
//查询全部
语法2:select + 列名 + from + 表名;
//查询多个列名中间使用’,‘隔开
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
mysql> select name,age from student;
+----------+------+
| name | age |
+----------+------+
| tom | 20 |
| marry | 20 |
| rose | 21 |
| zhangsan | 21 |
| lisi | 21 |
+----------+------+
5 rows in set (0.00 sec)
特定条件查找 — where
语法:select * from + 表名 + where + 筛选条件
操作符 >,<,>=,<=,=,!=
BETWEEN column# AND column#
LIKE:模糊匹配
RLIKE:基于正则表达式进行模式匹配
IS NOT NULL:非空
IS NULL:空
条件逻辑操作 AND
mysql> select * from student where id<=3;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
+----+-------+------+
3 rows in set (0.00 sec)
mysql> select * from student where name='lisi';
+----+------+------+
| id | name | age |
+----+------+------+
| 5 | lisi | 21 |
+----+------+------+
1 row in set (0.01 sec)
mysql> select * from student where age<=20;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
+----+-------+------+
2 rows in set (0.00 sec)
按行数查询 — limit
语法:select + *(或者是列名) + 表名 + limit + 条件;
mysql> select * from student limit 2;
//查询表中全部数据,从第0行开始的后二行的数据
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
+----+-------+------+
2 rows in set (0.00 sec)
mysql> select id,name from student limit 1,2;
//查询表中id,name列的数据,从第一行开始的后两行
+----+-------+
| id | name |
+----+-------+
| 2 | marry |
| 3 | rose |
+----+-------+
2 rows in set (0.00 sec)
降序(ASC)、升序(DESC)排序
语法:select + *(或是列名)+ from + 表名 + order + by + 列名 + ASC(或是 DESC);
//默认是降序
mysql> select * from student order by age;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
mysql> select * from student order by age ASC;
//ASC接在最后面就表示降序
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
mysql> select * from student order by age DESC;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
| 1 | tom | 20 |
| 2 | marry | 20 |
+----+----------+------+
5 rows in set (0.00 sec)
竖向显示数据
语法:select * from + 表名 + \G;
mysql> select * from student;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
mysql> select * from student \G;
*************************** 1. row ***************************
id: 1
name: tom
age: 20
*************************** 2. row ***************************
id: 2
name: marry
age: 20
*************************** 3. row ***************************
id: 3
name: rose
age: 21
*************************** 4. row ***************************
id: 4
name: zhangsan
age: 21
*************************** 5. row ***************************
id: 5
name: lisi
age: 21
5 rows in set (0.00 sec)
数据库用户授权
用户操作
mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录
这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
(1)IP地址,如:172.16.12.129
(2)通配符
%:匹配任意长度的任意字符,常用于设置允许从任何主机登录
_:匹配任意单个字符
数据库用户创建
语法:create user ’用户名‘ + @ + ’主机ip‘ + identified by + ’密码’;
//创建名为qin的数据库用户并设置密码redhat。
mysql> create user 'qin'@'192.168.100.111' identified by 'redhat';
Query OK, 0 rows affected (0.01 sec)
//exit;退出后使用qin用户登陆
[root@mysql ~]# mysql -u qin -h 192.168.100.111 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
//普通用户的权限特别小
删除数据库用户
语法:drop user + ’用户名‘ + @ + ’主机ip‘
//这里我们使用root用户登陆
mysql> drop user 'qin'@'192.168.100.111';
Query OK, 0 rows affected (0.00 sec)
数据库用户授权 —DCL
创建授权grant
权限类型(priv_type)
权限类型 代表什么?
ALL 所有权限
SELECT 读取内容的权限
INSERT 插入内容的权限
UPDATE 更新内容的权限
DELETE 删除内容的权限
指定要操作的对象db_name.table_name
表示方式 意义
. 所有库的所有表
db_name 指定库的所有表
db_name.table_name 指定库的指定表
给新用户添加selec权限
[root@mysql ~]# mysql -uroot -predhat //先使用root用户登陆
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 2
Server version: 5.7.37 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> create user 'shiqian'@'%' identified by 'redhat'; //创建shiqian用户
Query OK, 0 rows affected (0.01 sec)
mysql> grant select on sq.student to 'shiqian'@'%'; //给shiqian用户赋予select权限
Query OK, 0 rows affected (0.00 sec)
//验证
mysql> exit; //退出root用户登陆
Bye
[root@mysql ~]# mysql -ushiqian -predhat //使用shiqian用户登陆
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 3
Server version: 5.7.37 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> use sq //进入sq数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from student; //查看student表
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | tom | 20 |
| 2 | marry | 20 |
| 3 | rose | 21 |
| 4 | zhangsan | 21 |
| 5 | lisi | 21 |
+----+----------+------+
5 rows in set (0.00 sec)
查看用户权限
// 查看shiqian用户的权限
mysql> show grants;
+-------------------------------------------------+
| Grants for shiqian@% |
+-------------------------------------------------+
| GRANT USAGE ON *.* TO 'shiqian'@'%' |
| GRANT SELECT ON `sq`.`student` TO 'shiqian'@'%' |
+-------------------------------------------------+
2 rows in set (0.00 sec)
//查看root用户权限
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
撤销权限
mysql> show grants for 'shiqian'@'%';
+-------------------------------------------------+
| Grants for shiqian@% |
+-------------------------------------------------+
| GRANT USAGE ON *.* TO 'shiqian'@'%' |
| GRANT SELECT ON `sq`.`student` TO 'shiqian'@'%' |
+-------------------------------------------------+
2 rows in set (0.00 sec)
//能看到shiqian用户现在有两条权限
mysql> revoke all on sq.student from 'shiqian'@'%';
Query OK, 0 rows affected (0.00 sec)
//这里将shiqian用户的对sq下student表的select权限给撤销
mysql> show grants for 'shiqian'@'%';
+-------------------------------------+
| Grants for shiqian@% |
+-------------------------------------+
| GRANT USAGE ON *.* TO 'shiqian'@'%' |
+-------------------------------------+
1 row in set (0.00 sec)
//这里就只剩一条权限了
truncate语句
truncate与delete的区别:
语句类型 特点
delete DELETE删除表内容时仅删除内容,但会保留表结构
DELETE语句每次删除一行,并在事务日志中为所删除的每行记录一项
可以通过回滚事务日志恢复数据
非常占用空间
truncate 删除表中所有数据,且无法恢复
表结构、约束和索引等保持不变,新添加的行计数值重置为初始值
执行速度比DELETE快,且使用的系统和事务日志资源少
通过释放存储表数据所用的数据页来删除数据,并且只在事务日志中记录页的释放
对于有外键约束引用的表,不能使用TRUNCATE TABLE删除数据
不能用于加入了索引视图的表
//语法:TRUNCATE table_name;
四、实验
1.搭建mysql服务
//详情见上文
2.创建一个以你名字为名的数据库,并创建一张表student,该表包含三个字段(id,name,age),表结构如下:
mysql> 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.01 sec)
mysql> create table student1 (id int(11) not null primary key,name varchar(100) not null,age tinyint(4));
Query OK, 0 rows affected (0.01 sec)
mysql> desc student1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| age | tinyint(4) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
3.查看下该新建的表有无内容(用select语句)
4.往新建的student表中插入数据(用insert语句),结果应如下所示:
±—±------------±-----+
| id | name | age |
±—±------------±-----+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 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 |
±—±------------±-----+
mysql> insert into student1 values(1,'tom',20),(2,'jerry',23),(3,'chenyu',25),(4,'sean',28),(5,'zhangsan',26),(6,'zhangsan',20),(7,'lisi',null),(8,'chenshuo',10),(9,'wangwu',3),(10,'qiuyi',15),(11,'qiuxiaotan',20);
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> select * from student1;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | NULL |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotan | 20 |
+----+------------+------+
11 rows in set (0.00 sec)
5.修改lisi的年龄为50
mysql> update student1 set age=50 where name='lisi';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student1;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 3 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotan | 20 |
+----+------------+------+
11 rows in set (0.00 sec)
6.以age字段降序排序
mysql> select * from student1 order by age ASC;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 9 | wangwu | 3 |
| 8 | chenshuo | 10 |
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 6 | zhangsan | 20 |
| 11 | qiuxiaotan | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 25 |
| 5 | zhangsan | 26 |
| 4 | sean | 28 |
| 7 | lisi | 50 |
+----+------------+------+
11 rows in set (0.00 sec)
7.查询student表中年龄最小的3位同学跳过前2位
mysql> select * from student1 order by age limit 2,3;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 10 | qiuyi | 15 |
| 1 | tom | 20 |
| 11 | qiuxiaotan | 20 |
+----+------------+------+
3 rows in set (0.00 sec)
8.查询student表中年龄最大的4位同学
mysql> select * from student1 order by age DESC limit 4;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 7 | lisi | 50 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 3 | chenyu | 25 |
+----+----------+------+
4 rows in set (0.00 sec)
9.查询student表中名字叫zhangshan的记录
mysql> select * from student1 where name='zhangsan';
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
+----+----------+------+
2 rows in set (0.00 sec)
10.查询student表中名字叫zhangshan且年龄大于20岁的记录
mysql> select * from student1 where name='zhangsan' and age>20;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 5 | zhangsan | 26 |
+----+----------+------+
1 row in set (0.00 sec)
11.查询student表中年龄在23到30之间的记录
mysql> select * from student1 where age>23 and age<30;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 3 | chenyu | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
+----+----------+------+
3 rows in set (0.00 sec)
12.修改wangwu的年龄为100
mysql> update student1 set age=100 where name='wangwu';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student1;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 6 | zhangsan | 20 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotan | 20 |
+----+------------+------+
11 rows in set (0.00 sec)
13.删除student中名字叫zhangshan且年龄小于等于20的记录
mysql> delete from student1 where name='zhangsan' and age<=20;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student1;
+----+------------+------+
| id | name | age |
+----+------------+------+
| 1 | tom | 20 |
| 2 | jerry | 23 |
| 3 | chenyu | 25 |
| 4 | sean | 28 |
| 5 | zhangsan | 26 |
| 7 | lisi | 50 |
| 8 | chenshuo | 10 |
| 9 | wangwu | 100 |
| 10 | qiuyi | 15 |
| 11 | qiuxiaotan | 20 |
+----+------------+------+
10 rows in set (0.00 sec)