MySQL的视图的操作
今天也是分享一些MySQL的操作;
继续练习继续分享
视图并不是真实存在的表,主要是将常用的到字段或者数据整合成一个 “表”。
1、创建视图
#创建表格:
MariaDB [linux]> create table t4 (id int(10) not null auto_increment primary key,name varchar(20),job varchar(10));
Query OK, 0 rows affected (0.00 sec)
#插入数据
MariaDB [linux]> insert into t4 values(1,"liangjiawei","teacher"),(2,"huangzhiming","dancer");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [linux]> insert into t4 values(3,"liangchangqi","student"),(4,"guofengjiao","farmer");
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
#查看一下数据
MariaDB [linux]> select * from t4;
+----+--------------+---------+
| id | name | job |
+----+--------------+---------+
| 1 | liangjiawei | teacher |
| 2 | huangzhiming | dancer |
| 3 | liangchangqi | student |
| 4 | guofengjiao | farmer |
+----+--------------+---------+
4 rows in set (0.00 sec)
#创建视图--。这个指定了两个
MariaDB [linux]> create view v1 as select name,job from t4;
Query OK, 0 rows affected (0.00 sec)
#指定要求创建视图
MariaDB [linux]> create view v2 as select * from t4 where id >=2;
Query OK, 0 rows affected (0.00 sec)
2、查看视图
#查看视图
MariaDB [linux]> desc v1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| job | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
#查看视图里边的内容
MariaDB [linux]> select * from v2;
+----+--------------+---------+
| id | name | job |
+----+--------------+---------+
| 2 | huangzhiming | dancer |
| 3 | liangchangqi | student |
| 4 | guofengjiao | farmer |
+----+--------------+---------+
3 rows in set (0.00 sec)
3、删除视图
#删除视图
MariaDB [linux]> drop view v1;
Query OK, 0 rows affected (0.00 sec)
#加入if来判断
MariaDB [linux]> drop view if exists v1;
Query OK, 0 rows affected (0.00 sec)
4、修改视图
#直接修改
MariaDB [linux]> alter view v2 as select id,job from t4;
Query OK, 0 rows affected (0.00 sec)
#查看内容
MariaDB [linux]> select * from v2;
+----+---------+
| id | job |
+----+---------+
| 1 | teacher |
| 2 | dancer |
| 3 | student |
| 4 | farmer |
+----+---------+
4 rows in set (0.00 sec)
分享环节:
hi,亲爱的朋友们:
- 感谢你们耐心完这个笔记,如果笔记中出现的一些软件包、资源找不到的可以直接留言&私聊,我看见了就回复;
- 资源免费共享;有需要滴滴,(仅仅是我有的)
我的坚持初衷:💕立志要成为一名架构师
- 不断地去坚持学,其中的各种各样的难度,不言而喻~!
- 坚持不是一件容易的事情,但它却是成功的关键。做起来吧~!
如果你也想要坚持:那么组团吧,咋们一块互相监督;一天一点分享也是进步;最怕就是孤军奋战!加油吧,追梦人~!