Press "Enter" to skip to content

Category: MySQL入门很简单

MySQL的增删改

数据库通过插入、更新和删除等方式来改变表中的记录。插入数据是向表中插入新的记录,通过insert语句实现。更新数据是改变表中已经存在的数据,使用update语句来实现。删除数据是删除表中不再使用的数据,通过delete语句来实现。

插入数据

mysql> create table product(id int(10) primary key not  null, name varchar(20) not null, function varchar(50), company varchar(20) not null, address varchar(50));
\\为所有字段插入数据
mysql> insert into product values(1001,'ABC药物','治疗感冒','ABC制药厂','北京市昌平区十三陵');
\\列出所有字段
mysql> insert into product(id,name,function,company,address) values(1002,'BCD','治.头疼 ','BCD制药厂','北京市海淀区蓝靛厂');
\\变更顺序,不影响输入
mysql> insert into product(id,function,name,address,company) values(1003,'治疗癌症','AB康 复丸','北京市顺义区牛栏山','AB康复制药厂');
\\为表的指定字段插入数据
mysql> insert into product(id,name,company) values(1004,'EF咳嗽灵','EF健康公司');
mysql> insert into product(id,company,name) values(1005,'北京市制药厂','OK创可贴');
\\同时插入多条数据
mysql> insert into product values (1006,'头疼灵1号','治疗头疼','廊坊大厂制药厂','河北省廊 坊市大厂县'),  (1007,'头疼灵2号','延缓头疼','廊坊香河制药厂','河北省廊坊市香河县'),  (1008,'头疼灵3号','镇痛安眠','廊坊三河制药厂','河北省廊坊市三河县');
mysql> select * from product;
+------+---------------+--------------+-----------------------+-----------------------------+
| id   | name          | function     | company               | address                     |
+------+---------------+--------------+-----------------------+-----------------------------+
| 1001 | ABC药物       | 治疗感冒     | ABC制药厂             | 北京市昌平区十三陵          |
| 1002 | BCD           | 治疗头疼     | BCD制药厂             | 北京市海淀区蓝靛厂          |
| 1003 | AB康复丸      | 治疗癌症     | AB康复制药厂          | 北京市顺义区牛栏山          |
| 1004 | EF咳嗽灵      | NULL         | EF健康公司            | NULL                        |
| 1005 | OK创可贴      | NULL         | 北京市制药厂          | NULL                        |
| 1006 | 头疼灵1号     | 治疗头疼     | 廊坊大厂制药厂        | 河北省廊坊市大厂县          |
| 1007 | 头疼灵2号     | 延缓头疼     | 廊坊香河制药厂        | 河北省廊坊市香河县          |
| 1008 | 头疼灵3号     | 镇痛安眠     | 廊坊三河制药厂        | 河北省廊坊市三河县          |
+------+---------------+--------------+-----------------------+-----------------------------+

更新数据

修改单行数据
mysql> update product set function='治疗咽炎、咳嗽',address='北京市通州区马驹桥' where id=1004;
\\同时修改多个数据
mysql> update product set address=='河北省廊坊市香河县燕郊镇',company='廊坊制药厂' where id>1005 and id<=1008;
mysql> select * from product;
+------+---------------+-----------------------+--------------------+--------------------------------------+
| id   | name          | function              | company            | address                              |
+------+---------------+-----------------------+--------------------+--------------------------------------+
| 1001 | ABC药物       | 治疗感冒              | ABC制药厂          | 北京市昌平区十三陵                   |
| 1002 | BCD           | 治疗头疼              | BCD制药厂          | 北京市海淀区蓝靛厂                   |
| 1003 | AB康复丸      | 治疗癌症              | AB康复制药厂       | 北京市顺义区牛栏山                   |
| 1004 | EF咳嗽灵      | 治疗咽炎、咳嗽        | EF健康公司         | 北京市通州区马驹桥                   |
| 1005 | OK创可贴      | NULL                  | 北京市制药厂       | NULL                                 |
| 1006 | 头疼灵1号     | 治疗头疼              | 廊坊制药厂         | 河北省廊坊市香河县燕郊镇             |
| 1007 | 头疼灵2号     | 延缓头疼              | 廊坊制药厂         | 河北省廊坊市香河县燕郊镇             |
| 1008 | 头疼灵3号     | 镇痛安眠              | 廊坊制药厂         | 河北省廊坊市香河县燕郊镇             |
+------+---------------+-----------------------+--------------------+--------------------------------------+
8 rows in set (0.00 sec)

删除数据

mysql> delete from product where id=1005;
\\默认删除所有行数据
mysql> delete from product;

MySQL的查询数据

查询数据是指从数据库中获取所需的数据。查询数据是数据库中最常用,也是最重要的操作。用户可以根据自己的需求,使用不同的查询方式。


基本查询语句

select属性列表
from 表名和视图列表
where 条件表达式1
group by 属性名1 [having 条件表达式2]
order by 属性名2 [asc|desc]
\\显示员工表内的全部数据
mysql> select * from employee;
+-----+------+----------+------+-----+----------+
| num | d_id | name     | age  | sex | homeaddr |
+-----+------+----------+------+-----+----------+
|   1 | 1001 | zhangsan |   20 |   1 | beijing  |
|   2 | 1002 | lisi     |   24 |   0 | taiyuan  |
|   3 | 1004 | wangwu   |   29 |   1 | tianjin  |
|   4 | 1005 | zhaoliu  |   35 |   1 | tieling  |
|   5 | 1007 | maba     |   18 |   1 | luoyang  |
+-----+------+----------+------+-----+----------+
\\显示员工中小于25岁的人,并按num序号从大到小排列
mysql> select * from employee where age < 25 order by num desc;
+-----+------+----------+------+-----+----------+
| num | d_id | name     | age  | sex | homeaddr |
+-----+------+----------+------+-----+----------+
|   5 | 1007 | maba     |   18 |   1 | luoyang  |
|   2 | 1002 | lisi     |   24 |   0 | taiyuan  |
|   1 | 1001 | zhangsan |   20 |   1 | beijing  |
+-----+------+----------+------+-----+----------+
2 rows in set (0.00 sec)
\\查询指定字段,改变显示顺序,并按年龄排序
mysql> select age,name from employee order by age;
+------+----------+
| age  | name     |
+------+----------+
|   18 | maba     |
|   20 | zhangsan |
|   24 | lisi     |
|   29 | wangwu   |
|   35 | zhaoliu  |
+------+----------+
5 rows in set (0.00 sec)
\\查询指定记录,员工号大于等于1005的人员
mysql> select * from employee where d_id >= 1005;
+-----+------+---------+------+-----+----------+
| num | d_id | name    | age  | sex | homeaddr |
+-----+------+---------+------+-----+----------+
|   4 | 1005 | zhaoliu |   35 |   1 | tieling  |
|   5 | 1007 | maba    |   18 |   1 | luoyang  |
+-----+------+---------+------+-----+----------+
2 rows in set (0.00 sec)
\\带IN关键字的查询,将集合中的记录显示出来
mysql> select * from employee where num in(3,5);
+-----+------+--------+------+-----+----------+
| num | d_id | name   | age  | sex | homeaddr |
+-----+------+--------+------+-----+----------+
|   3 | 1004 | wangwu |   29 |   1 | tianjin  |
|   5 | 1007 | maba   |   18 |   1 | luoyang  |
+-----+------+--------+------+-----+----------+
\\带between and的范围查询
mysql> select age,name from employee where age between 15 and 25 order by age;
+------+----------+
| age  | name     |
+------+----------+
|   18 | maba     |
|   20 | zhangsan |
|   24 | lisi     |
+------+----------+
\\not是可选参数
mysql> select age,name from employee where age not between 15 and 25 order by age;
+------+---------+
| age  | name    |
+------+---------+
|   29 | wangwu  |
|   35 | zhaoliu |
+------+---------+
\\使用like字符匹配查询所有名字中带有a的人员
mysql> select num,name from employee where name like '%a%';
+-----+----------+
| num | name     |
+-----+----------+
|   1 | zhangsan |
|   3 | wangwu   |
|   4 | zhaoliu  |
|   5 | maba     |
+-----+----------+

\\可以使用is null来查询空值
\\使用and进行多条件查询,年龄小于25,名字带a的男性
mysql> select name,age,sex from employee where age between 15 and 25
    -> and sex = 1
    -> and name like '%a%';
+----------+------+-----+
| name     | age  | sex |
+----------+------+-----+
| zhangsan |   20 |   1 |
| maba     |   18 |   1 |
+----------+------+-----+
\\使用distinct来除重
mysql> select sex from employee;
+-----+
| sex |
+-----+
|   1 |
|   0 |
|   1 |
|   1 |
|   1 |
+-----+
mysql> select distinct sex from employee;
+-----+
| sex |
+-----+
|   1 |
|   0 |
+-----+
\\按组进行查询,并按组显示
mysql> select * from employee group by sex;
+-----+------+----------+------+-----+----------+
| num | d_id | name     | age  | sex | homeaddr |
+-----+------+----------+------+-----+----------+
|   2 | 1002 | lisi     |   24 |   0 | taiyuan  |
|   1 | 1001 | zhangsan |   20 |   1 | beijing  |
+-----+------+----------+------+-----+----------+
\\group by与group_contcat()函数一起使用,分组显示全部内容
mysql> select sex,group_concat(name) from employee group by sex;
+-----+------------------------------+
| sex | group_concat(name)           |
+-----+------------------------------+
|   0 | lisi                         |
|   1 | zhangsan,wangwu,zhaoliu,maba |
+-----+------------------------------+
\\group by与集合函数一起使用
mysql> select sex,count(sex) from employee group by sex;
+-----+------------+
| sex | count(sex) |
+-----+------------+
|   0 |          1 |
|   1 |          4 |
+-----+------------+
\\group by 与having一起使用
mysql> select sex,count(sex) from employee group by sex having count(sex)>3;
+-----+------------+
| sex | count(sex) |
+-----+------------+
|   1 |          4 |
+-----+------------+
\\where作用于表或者视图,是表和试图的查询条件。
\\having作用于分组后的记录,用于选择满足条件的组。
\\多个字段分组,第一个组相同,则按第二个组排序,同样1001的按年龄排
mysql> select d_id,group_concat(name),age from employee group by d_id,age;
+------+--------------------+------+
| d_id | group_concat(name) | age  |
+------+--------------------+------+
| 1001 | maba               |   18 |
| 1001 | zhangsan           |   20 |
| 1002 | lisi               |   24 |
| 1004 | wangwu             |   29 |
| 1005 | zhaoliu            |   35 |
+------+--------------------+------+
\\group by与with rollup进行汇总
mysql> select sex,count(sex) from employee group by sex with rollup;
+-----+------------+
| sex | count(sex) |
+-----+------------+
|   0 |          1 |
|   1 |          4 |
| NULL |          5 |
+-----+------------+
\\使用limit限制查询结果的数量
mysql> select * from employee limit 2;
+-----+------+----------+------+-----+----------+
| num | d_id | name     | age  | sex | homeaddr |
+-----+------+----------+------+-----+----------+
|   1 | 1001 | zhangsan |   20 |   1 | beijing  |
|   2 | 1002 | lisi     |   24 |   0 | taiyuan  |
+-----+------+----------+------+-----+----------+
2 rows in set (0.00 sec)
mysql> select * from employee limit 4;
+-----+------+----------+------+-----+----------+
| num | d_id | name     | age  | sex | homeaddr |
+-----+------+----------+------+-----+----------+
|   1 | 1001 | zhangsan |   20 |   1 | beijing  |
|   2 | 1002 | lisi     |   24 |   0 | taiyuan  |
|   3 | 1004 | wangwu   |   29 |   1 | tianjin  |
|   4 | 1005 | zhaoliu  |   35 |   1 | tieling  |
+-----+------+----------+------+-----+----------+
4 rows in set (0.00 sec)
\\指定初始位置
mysql> select * from employee limit 2,4;
+-----+------+---------+------+-----+----------+
| num | d_id | name    | age  | sex | homeaddr |
+-----+------+---------+------+-----+----------+
|   3 | 1004 | wangwu  |   29 |   1 | tianjin  |
|   4 | 1005 | zhaoliu |   35 |   1 | tieling  |
|   5 | 1001 | maba    |   18 |   1 | luoyang  |
+-----+------+---------+------+-----+----------+
3 rows in set (0.00 sec)

tips:

在MySQL5.7以后的group by中会直接报1055的错误。
原因是5.7后的版本默认开启only_full_group_by属性,需要在group by查询中使用any_value()函数。
解决的方法一个是改查询语句,一个是改配置文件。
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]字段新增一行配置
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
然后,
sudo service mysql restart


使用集合函数查询

\\count()函数,统计函数
mysql> select count(*) from employee;
\\sum()函数,求和函数
mysql> select d_id,sum(age) from employee;
\\avg()函数,求平均值函数
mysql> select avg(age) from employee group by sex;
\\max()函数,求最大值函数
mysql> select max(age) from employee;
\\min()函数,求最小值函数
mysql> select min(age) from employee;

连接查询

\\内连接查询
mysql> select * from employee;
+-----+------+--------+-----+-----+----------------------------------+
| num | d_id | name   | age | sex | homeaddr                         |
+-----+------+--------+-----+-----+----------------------------------+
|   1 | 1001 | 张三   |  26 | 男  | 北京市海淀区学院路16号           |
|   2 | 1001 | 李四   |  24 | 女  | 北京市昌平区北七家镇             |
|   3 | 1004 | Aric   |  18 | 男  | England                          |
+-----+------+--------+-----+-----+----------------------------------+
3 rows in set (0.00 sec)
mysql> select * from department;
+------+-----------+--------------+---------------------+
| d_id | d_name    | function     | address             |
+------+-----------+--------------+---------------------+
| 1001 | 研发部    | 产品研发     | 3号楼5层            |
| 1002 | 生产部    | 产品制造     | 5号楼1层            |
| 1003 | 销售部    | 产品销售     | 1号楼销售大厅       |
+------+-----------+--------------+---------------------+
mysql> select num,name,employee.d_id,age,sex,d_name,function from employee,department where employee.d_id=department.d_id;
+-----+--------+------+-----+-----+-----------+--------------+
| num | name   | d_id | age | sex | d_name    | function     |
+-----+--------+------+-----+-----+-----------+--------------+
|   1 | 张三   | 1001 |  26 | 男  | 研发部    | 产品研发     |
|   2 | 李四   | 1001 |  24 | 女  | 研发部    | 产品研发     |
|   4 | 王五   | 1003 |  25 | 女  | 销售部    | 产品销售     |
mysql> select num,name,employee.d_id,age,sex,d_name,function from employee,department where employee.d_id=department.d_id;
+-----+--------+------+-----+-----+-----------+--------------+
| num | name   | d_id | age | sex | d_name    | function     |
+-----+--------+------+-----+-----+-----------+--------------+
|   1 | 张三   | 1001 |  26 | 男  | 研发部    | 产品研发     |
|   2 | 李四   | 1001 |  24 | 女  | 研发部    | 产品研发     |
|   4 | 王五   | 1003 |  25 | 女  | 销售部    | 产品销售     |
+-----+--------+------+-----+-----+-----------+--------------+
\\左连接查询 left join 表名 on 条件
mysql> select num,name,employee.d_id,age,sex,d_name,function from employee left join department  on employee.d_id=department.d_id;
\\右连接查询 right join 表名 on 条件
mysql> select num,name,employee.d_id,age,sex,d_name,function from employee right join department  on employee.d_id=department.d_id;

子查询

\\带in关键字的子查询
mysql> select * from employee where d_id in(select d_id from department);
+-----+------+--------+-----+-----+----------------------------------+
| num | d_id | name   | age | sex | homeaddr                         |
+-----+------+--------+-----+-----+----------------------------------+
|   1 | 1001 | 张三   |  26 | 男  | 北京市海淀区学院路16号           |
|   2 | 1001 | 李四   |  24 | 女  | 北京市昌平区北七家镇             |
|   4 | 1003 | 王五   |  25 | 女  | 北京市朝阳区双桥                 |
+-----+------+--------+-----+-----+----------------------------------+
\\not查询反向
mysql> select * from employee where d_id not in(select d_id from department);
+-----+------+------+-----+-----+----------+
| num | d_id | name | age | sex | homeaddr |
+-----+------+------+-----+-----+----------+
|   3 | 1004 | Aric |  18 | 男  | England  |
+-----+------+------+-----+-----+----------+


MySQL的触发器

触发器(Trigger)是由事件来触发某个操作。当数据库系统执行这些事件时,就会激活触发器执行相应的操作。

创建触发器

创建只有一个执行语句的触发器

\\create trigger 触发器名 before|after 触发事件 on 表名
\\      for each row 执行语句
mysql> create trigger user_trig1 before insert on user for each row insert into  trigger_time values(now());

mysql> insert into user(id,name,num,phone,address,age,sex) values(2,'su bowen',2,1861225,'tongzhou',4,1);
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;
+----+----------+------+---------+----------+-----+------+
| id | name        | num  | phone   | address  | age | sex  |
+----+----------+------+---------+----------+-----+------+
|  1 | su jingxuan |    1 | 1861109 | beijing  |  35 |    1 |
|  2 | su bowen    |    2 | 1861225 | tongzhou |   4 |    1 |
mysql> select * from trigger_time;
+---------------------+
| exec_time           |
+---------------------+
| 2017-08-13 16:22:14 |
+---------------------+

创建有多个执行语句的触发器

一般情况下,MySQL默认使用;作为结束执行语句。在创建触发器过程中需要用到;。为了解决这个问题可以用DELIMITER语句。如"delimiter&&",可以将结束符号变成&&。当触发器创建完成之后,可以用命令delimiter;来将结束符号还原。

mysql> delimiter&&
mysql> create trigger user_trig2 after delete on user for each row begin insert into trigger_time values('21:00:01'); insert into trigger_time values('22:00:02'); end&&
mysql>delimiter ;
mysql> create table trigger_time(exec_time time);
mysql> delete from user where id=2;
mysql> select * from trigger_time;
+-----------+
| exec_time |
+-----------+
| 21:00:01  |
| 22:00:02  |
+-----------+
mysql> select * from user;                                                            
+----+-------------+------+---------+---------+-----+------+
| id | name        | num  | phone   | address | age | sex  |
+----+-------------+------+---------+---------+-----+------+
|  1 | su jingxuan |    1 | 1861109 | beijing |  35 |    1 |
+----+-------------+------+---------+---------+-----+------+

查看触发器

mysql> show triggers\G
*************************** 1. row ***************************
             Trigger: user_trig1
               Event: INSERT
               Table: user
           Statement: insert into  trigger_time values(now())
              Timing: BEFORE
             Created: 2017-08-13 16:20:33.95
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
             Definer: sujx@%
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
*************************** 2. row ***************************
             Trigger: user_trig2
               Event: DELETE
               Table: user
           Statement: begin
insert into trigger_time values('21:00:01');
insert into trigger_time values('22:00:02');
end
              Timing: AFTER
             Created: 2017-08-13 16:30:24.44
            sql_mode: ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
             Definer: sujx@%
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: latin1_swedish_ci
2 rows in set (0.00 sec)
\\可以在trigger表中查看触发器信息
mysql> select * from information_schema.triggers\G

触发器的使用

在MySQL中,触发器执行的顺序是before触发器、表操作、after触发器。

删除触发器

mysql> drop trigger user_trig1;

MySQL的视图

视图是从过一个或者多个表中导出来的表,是一种虚拟存在的表。试图可以使用用户的操作更方便,并且可以保障数据库系统的安全。

简介

视图可以从已经存在的视图基础上定义。数据库中存放了视图的定义,而并没有视图中的数据。使用视图查询数据时,数据库系统会从原来的表中取出对应的数据。视图的数据以来于原来表中的数据。
MySQL的视图不支持输入参数的功能。

作用

  1. 操作简单化
  2. 增加数据的安全性
  3. 提高表的逻辑独立性

创建视图

创建视图是指在已存在的数据库表上建立视图。视图可以建立在一张表中,也可以建立在多张表中。
创建视图时,需要有create view的权限。同时,应该具有查询设计的列的select权限。在MySQL数据库下面的user表中保存这些权限的信息,可以使用select查询。
algorithem,表示试图选择的算法,包括undefined、merge和temptable。undefined表示自动选择算法,merge表示合并,temptable表示存入临时表。
cascaded是可选参数,表示更新试图要满足所有视图和表的条件,为默认值。local表示更新视图时,要满足该试图本身的条件即可。

\\在已有表上创建视图user_view1
mysql> create view user_view1 as select * from user;
mysql> desc user;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  | UNI | NULL    |       |
| num     | int(8)      | YES  |     | NULL    |       |
| phone   | varchar(20) | YES  |     | NULL    |       |
| address | varchar(40) | NO   | MUL | NULL    |       |
| age     | int(4)      | NO   |     | NULL    |       |
| sex     | tinyint(1)  | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
mysql> desc user_view1;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   |     | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| num     | int(8)      | YES  |     | NULL    |       |
| phone   | varchar(20) | YES  |     | NULL    |       |
| address | varchar(40) | NO   |     | NULL    |       |
| age     | int(4)      | NO   |     | NULL    |       |
| sex     | tinyint(1)  | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
\\两者显示出来的表的结果是一样的
\\在user表中创建只包含姓名、性别、年龄的视图
mysql> create view user_view2(name,sex,age) as select name,sex,age from user;
mysql> desc user_view2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(30) | YES  |     | NULL    |       |
| sex   | tinyint(1)  | YES  |     | NULL    |       |
| age   | int(4)      | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
\\在多个表中建立跨表的视图
mysql> create algorithm=merge view stu_view( name,age,sex,stu_id,grade) as select name,age,sex,stu_id,grade from user,example2 where user.id = example2.id with local check option;
mysql> desc stu_view;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name   | varchar(30) | YES  |     | NULL    |       |
| age    | int(4)      | NO   |     | NULL    |       |
| sex    | tinyint(1)  | YES  |     | NULL    |       |
| stu_id | int(11)     | NO   |     | NULL    |       |
| grade  | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+

查看视图

查看视图是指查看数据库中已存在的视图的定义。查看视图必须要有show view的权限。

\\使用describe语句
mysql> desc stu_view;
\\使用show table status语句查看视图基本信息,注意like参数和单引号''
\\stu_view是虚拟表,所以属性值大多为空
mysql> show table status like 'stu_view'\G
*********** 1. row ********
           Name: stu_view
         Engine: NULL
        Version: NULL
     Row_format: NULL
           Rows: NULL
 Avg_row_length: NULL
    Data_length: NULL
Max_data_length: NULL
   Index_length: NULL
      Data_free: NULL
 Auto_increment: NULL
    Create_time: NULL
    Update_time: NULL
     Check_time: NULL
      Collation: NULL
       Checksum: NULL
 Create_options: NULL
        Comment: VIEW
1 row in set (0.00 sec)
\\使用show create view查看视图详细信息
mysql> show create view stu_view\G
*************** 1. row **************
                View: stu_view
         Create View: CREATE ALGORITHM=MERGE DEFINER=`sujx`@`%` SQL SECURITY DEFINER VIEW `stu_view` AS select `user`.`name` AS `name`,`user`.`age` AS `age`,`user`.`sex` AS `sex`,`example2`.`stu_id` AS `stu_id`,`example2`.`grade` AS `grade` from (`user` join `example2`) where (`user`.`id` = `example2`.`id`) WITH LOCAL CHECK OPTION
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)
\\在系统views表中查看视图详细信息
\\在MySQL中,所有视图的定义都存在information_schema数据库下的views表中。
mysql> select * from information_schema.views\G
*************** 1. row **************
       TABLE_CATALOG: def
        TABLE_SCHEMA: sujx
          TABLE_NAME: stu_view
     VIEW_DEFINITION: select `sujx`.`user`.`name` AS `name`,`sujx`.`user`.`age` AS `age`,`sujx`.`user`.`sex` AS `sex`,`sujx`.`example2`.`stu_id` AS `stu_id`,`sujx`.`example2`.`grade` AS `grade` from `sujx`.`user` join `sujx`.`example2` where (`sujx`.`user`.`id` = `sujx`.`example2`.`id`)
        CHECK_OPTION: LOCAL
        IS_UPDATABLE: YES
             DEFINER: sujx@%
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
**************** 2. row ***************
       TABLE_CATALOG: def
        TABLE_SCHEMA: sujx
          TABLE_NAME: user_view1
     VIEW_DEFINITION: select `sujx`.`user`.`id` AS `id`,`sujx`.`user`.`name` AS `name`,`sujx`.`user`.`num` AS `num`,`sujx`.`user`.`phone` AS `phone`,`sujx`.`user`.`address` AS `address`,`sujx`.`user`.`age` AS `age`,`sujx`.`user`.`sex` AS `sex` from `sujx`.`user`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: sujx@%
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
************** 3. row **************
       TABLE_CATALOG: def
        TABLE_SCHEMA: sujx
          TABLE_NAME: user_view2
     VIEW_DEFINITION: select `sujx`.`user`.`name` AS `name`,`sujx`.`user`.`sex` AS `sex`,`sujx`.`user`.`age` AS `age` from `sujx`.`user`
        CHECK_OPTION: NONE
        IS_UPDATABLE: YES
             DEFINER: sujx@%
       SECURITY_TYPE: DEFINER
CHARACTER_SET_CLIENT: utf8
COLLATION_CONNECTION: utf8_general_ci
3 rows in set (0.01 sec)

修改视图

修改视图是指修改数据库中存在的表的定义。

\\使用create or replace view语句修改视图
mysql> create or replace algorithm=temptable view stu_view(name,sex,grade) as select name,sex,grade from user,example2 where user.id = example2.id;
Query OK, 0 rows affected (0.01 sec)

mysql> desc stu_view;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(30) | YES  |     | NULL    |       |
| sex   | tinyint(1)  | YES  |     | NULL    |       |
| grade | float       | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
\\alter语句修改视图
mysql> alter view stu_view(stu_id,name,sex,grade) as select stu_id,name,sex,grade from user,example2 where user.id=example2.id;
mysql> desc stu_view;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| stu_id | int(11)     | NO   |     | NULL    |       |
| name   | varchar(30) | YES  |     | NULL    |       |
| sex    | tinyint(1)  | YES  |     | NULL    |       |
| grade  | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

更新视图

更新视图是指通过视图来插入、更新和删除表中的数据。因为视图是一个虚拟表,其中没有数据。通过视图来更新时,都是转换到基本表来更新。更新视图时,只能更新权限范围内的数据。超过范围,就不能更新。

删除视图

mysql> drop view if exists stu_view;