mysql中的视图

mysql中的视图

视图就是存储下来的SELECT语句,没有存任何其他内容,是基于某些基表的查询结果。一般情况不允许往视图中插入数据

视图是一个虚表,视图所依赖的表称之为基表

创建视图

CREATE VIEW

查看帮助:

mysql> help create view
Name: 'CREATE VIEW'
Description:
Syntax:
CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { user | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

1,创建一个视图,用来存放查询students所有信息

mysql> CREATE VIEW students_view AS SELECT * FROM students;
Query OK, 0 rows affected (0.36 sec)

//可以看到创建视图成功后,tables里面多了一个表(就是创建的视图)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| classes            |
| fsx                |
| mysql              |
| performance_schema |
| students           |
| test               |
+--------------------+
7 rows in set (0.00 sec)
//不同于真正的表,可以查看该视图的具体信息
mysql> SELECT * FROM students_view ;
+-----+--------+-----+------+------+------+------+---------------------+
| sid | name   | age | sex  | cid1 | cid2 | tid  | create_time         |
+-----+--------+-----+------+------+------+------+---------------------+
|   1 | tony   |  18 | BOY  |    2 |    4 |    3 | 2018-05-15 12:09:28 |
|   2 | Cindy  |  21 | GRIL |    3 |    6 | NULL | 2018-05-15 12:15:30 |
|   3 | Alice  |  19 | GRIL |    1 | NULL | NULL | 2018-05-15 13:11:37 |
|   4 | Kobe   |  25 | BOY  |   11 |    1 |    2 | 2018-05-15 13:18:52 |
|   5 | Tom    |  19 | BOY  |    4 |    3 |    7 | 2018-05-15 13:12:21 |
|   6 | fsx    |  24 | BOY  |    1 |    3 |    9 | 2018-05-15 13:13:05 |
|   7 | Qpy    |  21 | GRIL |    3 |    1 | NULL | 2018-05-15 12:15:37 |
|   8 | Coco   |  27 | GRIL |    6 |    2 |    8 | 2018-05-15 12:15:38 |
|   9 | Bob    |  20 | BOY  |    2 |    1 | NULL | 2018-05-15 12:15:53 |
|  10 | Liming |  22 | BOY  |    6 |    3 |    2 | 2018-05-15 12:13:28 |
+-----+--------+-----+------+------+------+------+---------------------+
10 rows in set (0.00 sec)
/使用SHOW TABLE STATUS\G找到studetns_view表的信息
mysql> SHOW TABLE STATUS\G 
*************************** 3. row ***************************
           Name: students_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
mysql> DESC students_view;
+-------------+---------------------+------+-----+---------------------+-------+
| Field       | Type                | Null | Key | Default             | Extra |
+-------------+---------------------+------+-----+---------------------+-------+
| sid         | tinyint(3) unsigned | NO   |     | 0                   |       |
| name        | varchar(30)         | NO   |     | NULL                |       |
| age         | tinyint(4)          | NO   |     | NULL                |       |
| sex         | enum('BOY','GRIL')  | YES  |     | NULL                |       |
| cid1        | tinyint(4)          | YES  |     | NULL                |       |
| cid2        | tinyint(4)          | YES  |     | NULL                |       |
| tid         | tinyint(4)          | YES  |     | NULL                |       |
| create_time | timestamp           | NO   |     | 0000-00-00 00:00:00 |       |
+-------------+---------------------+------+-----+---------------------+-------+
8 rows in set (0.00 sec)

当我们创建好一个关于students表的VIEW后,下次想要查询students信息就直接使用VIEW,更加方便

mysql> SELECT name,cid1 FROM students_view;
+--------+------+
| name   | cid1 |
+--------+------+
| tony   |    2 |
| Cindy  |    3 |
| Alice  |    1 |
| Kobe   |   11 |
| Tom    |    4 |
| fsx    |    1 |
| Qpy    |    3 |
| Coco   |    6 |
| Bob    |    2 |
| Liming |    6 |
+--------+------+
10 rows in set (0.00 sec)

注意:不违反基表的存储法则,是可以向视图中插入数据的,但是一般很难不违反基表的存储法则。通常情况下,我们使用视图只是作为一个查找的虚表,并不是为了插入数据。所以,尽量不要想着给视图插入数据的操作。

删除视图

查看删除视图的帮助:

mysql> HELP DROP VIEW
Name: 'DROP VIEW'
Description:
Syntax:
DROP VIEW [IF EXISTS]
    view_name [, view_name] ...
    [RESTRICT | CASCADE]

1,删除students_view视图

mysql>  DROP VIEW students_view;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW TABLES;
+-------------------+
| Tables_in_classes |
+-------------------+
| courses           |
| students          |
| subjects          |
| teachers          |
+-------------------+
4 rows in set (0.00 sec)

基于视图查询,是没有索引的,读取速度不叫慢,所以一般情况不建议使用视图,只有在一些特殊情况下使用(跟安全相关)。

有些数据支持雾化视图,即可以将查询的结果保存下来。这样再次查询的时候,就不用去执行目表中的查询操作,一次查询即可。但是也有新的问题:如果母表更新了,使用雾化视图时就会得不到最新数据。所以在更新不频繁的表上使用雾化视图更好。

使用SHOW CREATE VIEW view_name可以查看创建的视图的具体过程、映射关系和字符集、排序规则信息

mysql> SHOW CREATE VIEW stu_view\G
*************************** 1. row ***************************
                View: stu_view
         Create View: CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_view` AS select `students`.`sid` AS `sid`,`students`.`name` AS `name`,`students`.`age` AS `age`,`students`.`sex` AS `sex`,`students`.`cid1` AS `cid1`,`students`.`cid2` AS `cid2`,`students`.`tid` AS `tid`,`students`.`create_time` AS `create_time` from `students`
character_set_client: utf8
collation_connection: utf8_general_ci
1 row in set (0.00 sec)

mysql> SHOW CREATE VIEW stu_view;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View     | Create View                                                                                                                                                                                                                                                                                                                                                | character_set_client | collation_connection |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| stu_view | CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `stu_view` AS select `students`.`sid` AS `sid`,`students`.`name` AS `name`,`students`.`age` AS `age`,`students`.`sex` AS `sex`,`students`.`cid1` AS `cid1`,`students`.`cid2` AS `cid2`,`students`.`tid` AS `tid`,`students`.`create_time` AS `create_time` from `students` | utf8                 | utf8_general_ci      |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)

//同理使用SHOW命令也可以查看如何创建的表、库、索引等
mysql> SHOW CREATE TABLE tearchers;
ERROR 1146 (42S02): Table 'classes.tearchers' doesn't exist
mysql> SHOW CREATE TABLE teachers;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                    |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| teachers | CREATE TABLE `teachers` (
  `tid` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
  `tname` varchar(30) NOT NULL,
  `age` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql命令中的-e选项

mysql命令选项-e,可以在非mysql交互模式下实现对数据库的管理

[root@mysql ~]# mysql -e 'CREATE DATABASE edb;'
[root@mysql ~]# mysql -e 'SHOW DATABASES;'
+--------------------+
| Database           |
+--------------------+
| information_schema |
| classes            |
| edb                |
| fsx                |
| mysql              |
| performance_schema |
| students           |
| test               |
+--------------------+

脚本批插入数据

1,写一个脚本实现对students插入几组数据

[root@mysql ~]# mysql -e 'SELECT * FROM classes.students;'
+-----+--------+-----+------+------+------+------+---------------------+
| sid | name   | age | sex  | cid1 | cid2 | tid  | create_time         |
+-----+--------+-----+------+------+------+------+---------------------+
|   1 | tony   |  18 | BOY  |    2 |    4 |    3 | 2018-05-15 12:09:28 |
|   2 | Cindy  |  21 | GRIL |    3 |    6 | NULL | 2018-05-15 12:15:30 |
|   3 | Alice  |  19 | GRIL |    1 | NULL | NULL | 2018-05-15 13:11:37 |
|   4 | Kobe   |  25 | BOY  |   11 |    1 |    2 | 2018-05-15 13:18:52 |
|   5 | Tom    |  19 | BOY  |    4 |    3 |    7 | 2018-05-15 13:12:21 |
|   6 | fsx    |  24 | BOY  |    1 |    3 |    9 | 2018-05-15 13:13:05 |
|   7 | Qpy    |  21 | GRIL |    3 |    1 | NULL | 2018-05-15 12:15:37 |
|   8 | Coco   |  27 | GRIL |    6 |    2 |    8 | 2018-05-15 12:15:38 |
|   9 | Bob    |  20 | BOY  |    2 |    1 | NULL | 2018-05-15 12:15:53 |
|  10 | Liming |  22 | BOY  |    6 |    3 |    2 | 2018-05-15 12:13:28 |
+-----+--------+-----+------+------+------+------+---------------------+

先在终端常是一次添加一行数据:

[root@mysql ~]# mysql -e "INSERT INTO classes.students (name,age,sex,cid1,cid2,tid) VALUES('james',25,'BOY',3,2,2);"
[root@mysql ~]# mysql -e "SELECT * FROM classes.students;"
+-----+--------+-----+------+------+------+------+---------------------+
| sid | name   | age | sex  | cid1 | cid2 | tid  | create_time         |
+-----+--------+-----+------+------+------+------+---------------------+
|   1 | tony   |  18 | BOY  |    2 |    4 |    3 | 2018-05-15 12:09:28 |
|   2 | Cindy  |  21 | GRIL |    3 |    6 | NULL | 2018-05-15 12:15:30 |
|   3 | Alice  |  19 | GRIL |    1 | NULL | NULL | 2018-05-15 13:11:37 |
|   4 | Kobe   |  25 | BOY  |   11 |    1 |    2 | 2018-05-15 13:18:52 |
|   5 | Tom    |  19 | BOY  |    4 |    3 |    7 | 2018-05-15 13:12:21 |
|   6 | fsx    |  24 | BOY  |    1 |    3 |    9 | 2018-05-15 13:13:05 |
|   7 | Qpy    |  21 | GRIL |    3 |    1 | NULL | 2018-05-15 12:15:37 |
|   8 | Coco   |  27 | GRIL |    6 |    2 |    8 | 2018-05-15 12:15:38 |
|   9 | Bob    |  20 | BOY  |    2 |    1 | NULL | 2018-05-15 12:15:53 |
|  10 | Liming |  22 | BOY  |    6 |    3 |    2 | 2018-05-15 12:13:28 |
|  11 | james  |  25 | BOY  |    3 |    2 |    2 | 2018-05-16 09:36:16 |
+-----+--------+-----+------+------+------+------+---------------------+

编写脚本mysql_test.sh

在编写前创建一个test2的表

mysql> create TABLE test2(sid TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,age TINYINT NOT NULL,sex ENUM('girl','boy'),cid1 TINYINT NOT NULL,cid2 TINYINT NOT NULL,tid TINYINT NOT NULL,time TIMESTAMP);
Query OK, 0 rows affected (0.16 sec)
#/bin/bash

while [ 1 ]
do
##生成随机姓名##
name=''
for i in {1..3}
do
        name=$name$(tr -dc a-z < /dev/urandom | head -c1)
done
####

age=$(shuf -i 16-28 -n 1)
cid1=$(shuf -i 1-10 -n 1)
cid2=$(shuf -i 1-10 -n 1)
tid=$(shuf -i 1-5 -n 1)

##性别随机生成##
pool=(1 2)
num=${#pool[*]}

sex=${pool[$((RANDOM%num))]}
case $sex in
        1) sex="girl";;
        2) sex="boy";;
esac
####

`mysql -e "INSERT INTO classes.test2 (name,age,sex,cid1,cid2,tid) VALUES('$name',$age,'$sex',$cid1,$cid2,$tid)"`
done

执行完脚本后,查看数据库test2表中内容

mysql> SELECT * FROM test2;
+-----+------+-----+------+------+------+-----+---------------------+
| sid | name | age | sex  | cid1 | cid2 | tid | time                |
+-----+------+-----+------+------+------+-----+---------------------+
|   1 | vxt  |  22 | boy  |    1 |    2 |   3 | 2018-05-16 10:50:02 |
|   2 | hly  |  21 | boy  |    9 |    8 |   5 | 2018-05-16 10:50:02 |
|   3 | kew  |  17 | boy  |    3 |   10 |   4 | 2018-05-16 10:50:03 |
|   4 | tnz  |  25 | girl |   10 |    5 |   5 | 2018-05-16 10:50:03 |
|   5 | imh  |  16 | girl |    9 |    4 |   2 | 2018-05-16 10:50:03 |
|   6 | fdu  |  25 | boy  |    1 |    9 |   4 | 2018-05-16 10:50:03 |
|   7 | fro  |  21 | girl |    9 |   10 |   3 | 2018-05-16 10:50:03 |
|   8 | ykf  |  27 | girl |    4 |    5 |   1 | 2018-05-16 10:50:03 |
|   9 | gff  |  26 | girl |    9 |    2 |   5 | 2018-05-16 10:50:03 |
|  10 | fwx  |  16 | boy  |    3 |    7 |   1 | 2018-05-16 10:50:03 |
|  11 | vus  |  24 | girl |    8 |    9 |   1 | 2018-05-16 10:50:03 |
|  12 | tac  |  20 | boy  |    4 |    6 |   3 | 2018-05-16 10:50:03 |
|  13 | dev  |  21 | boy  |    5 |    6 |   2 | 2018-05-16 10:50:04 |
|  14 | nfu  |  24 | boy  |    6 |    8 |   3 | 2018-05-16 10:50:04 |
|  15 | akf  |  24 | girl |    1 |    2 |   3 | 2018-05-16 10:50:04 |
|  16 | wsk  |  24 | boy  |    3 |    6 |   4 | 2018-05-16 10:50:04 |
|  17 | wih  |  25 | boy  |    1 |    6 |   5 | 2018-05-16 10:50:04 |
|  18 | cif  |  18 | boy  |    5 |    2 |   2 | 2018-05-16 10:50:04 |
|  19 | avj  |  18 | boy  |    7 |    2 |   4 | 2018-05-16 10:50:04 |
|  20 | akh  |  25 | girl |    2 |    1 |   2 | 2018-05-16 10:50:04 |
|  21 | zdf  |  22 | girl |    1 |    1 |   5 | 2018-05-16 10:50:04 |
|  22 | uzc  |  28 | boy  |    2 |    3 |   3 | 2018-05-16 10:50:04 |
//由于PRIMARY KEY值最大到255所以这个只能创建255个行数据


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值