view视图,视图是什么?视图就是一个虚拟的表,定义我们所想要看到的一个view,纯属个人理解~
1、创建一个view数据库
mysql> CREATE DATABASE view; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +----------------------+ | Database | +----------------------+ | information_schema | | mysql | | performance_schema | | test | | view | +----------------------+ 11 rows in set (0.00 sec)
2、进入view库并创建我们定义的view表
mysql> USE view #进入view库 Database changed mysql> CREATE VIEW user_info AS #创建user_info view的表 -> SELECT user,password,host FROM mysql.user; #定义只从mysql.user取出user,password,host Query OK, 0 rows affected (0.00 sec)
3、查看是否是我们所想定义的那样
mysql> SHOW TABLES; #查看表是否创建成功 +----------------+ | Tables_in_view | +----------------+ | user_info | +----------------+ 1 row in set (0.00 sec) mysql> SELECT * FROM user_info; #如我们所想定义的那样 +------+-------------------------------------------+-----------+ | user | password | host | +------+-------------------------------------------+-----------+ | root | | localhost | | root | | jroa | | root | | 127.0.0.1 | | root | | ::1 | | | | localhost | | | | jroa | | oa | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 127.0.0.1 | +------+-------------------------------------------+-----------+ 7 rows in set (0.00 sec)
再通过一个例子来扩展下!~~
1、创建两张正常表,分别为表a 及 表b ~好像哪里乖乖的^-^
mysql> CREATE TABLE a ( #创建表a -> id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, -> name VARCHAR(60) NOT NULL, -> price DOUBLE NOT NULL -> ); Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE b( #创建表b -> id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL, -> name VARCHAR(60) NOT NULL, -> qty INT NOT NULL, -> gen_time DATETIME NOT NULL -> ); Query OK, 0 rows affected (0.04 sec)
2、为俩张表造点数据
mysql> DESC a; #查看表a结构 +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(60) | NO | | NULL | | | price | double | NO | | NULL | | +-------+------------------+------+-----+---------+----------------+ 3 rows in set (0.00 sec) mysql> DESC b; #查看表b结构 +----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(60) | NO | | NULL | | | qty | int(11) | NO | | NULL | | | gen_time | datetime | NO | | NULL | | +----------+------------------+------+-----+---------+----------------+ 4 rows in set (0.02 sec) mysql> INSERT INTO a (name,price) VALUES #插入数据 -> ('apple',3.5), -> ('pear',3.2), -> ('banana',4.5), -> ('oracle',5.5); Query OK, 4 rows affected (0.00 sec) Records: 4 Duplicates: 0 Warnings: 0 mysql> INSERT INTO b (name,qty,gen_time) VALUES #插入数据 -> ('apple',5,now()), -> ('banana',7,now()); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0
3、定义我们的view表
mysql> CREATE VIEW deail AS #创建view表,表名为deail -> SELECT a.name AS Name, #定义字段名称映射 -> a.price AS Price, #定义字段名称映射 -> b.qty AS Qty, #定义字段名称映射 -> a.price * b.qty AS Total #定义a字段乘以b字段的结果映射 -> FROM a,b WHERE a.name=b.name; #从表a 及 表b读取,并且条件为表俩边那么相等 Query OK, 0 rows affected (0.00 sec)
4、验证是否是我们所想的那样
mysql> SELECT * FROM deail; #果真如我们想的那样 +--------+-------+-----+-------+ | Name | Price | Qty | Total | +--------+-------+-----+-------+ | apple | 3.5 | 5 | 17.5 | | banana | 4.5 | 7 | 31.5 | +--------+-------+-----+-------+ 2 rows in set (0.00 sec)
5、再插入俩条数据进行佐证
mysql> INSERT INTO a (name,price) VALUES #插入俩条数据,好像又是怪怪的^-^ -> ('XX',4.8), -> ('OO',5.7); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> INSERT INTO b (name,qty,gen_time) VALUES #这里同样插入俩条数据,好像也是怪怪的 -> ('XX',7,now()), -> ('OO',6,now()); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM deail; #验证结果 +--------+-------+-----+-------+ | Name | Price | Qty | Total | +--------+-------+-----+-------+ | apple | 3.5 | 5 | 17.5 | | banana | 4.5 | 7 | 31.5 | | XX | 4.8 | 7 | 33.6 | | OO | 5.7 | 6 | 34.2 | +--------+-------+-----+-------+ 4 rows in set (0.00 sec)