(一)什么是视图??
视图就是站在不同的角度取看待同一份数据。
(二)基本操作
表复制:
mysql>create table dept2 as select * from dept;
#复制
mysql> create table scholar1 as select * from scholor;
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
#查看
mysql> select * from scholar1;
+----+------+
| id | name |
+----+------+
| 1 | 小张 |
| 2 | 小李 |
| 3 | 小明 |
+----+------+
3 rows in set (0.00 sec)
I. 创建视图
mysql> create view scholar1_view as select * from scholar1;
Query OK, 0 rows affected (0.01 sec)
II.删除视图
mysql> drop view scholar1_view;
Query OK, 0 rows affected (0.01 sec)
注意:只有DOL语句才能够以view的形式创建
III.视图的作用
可以面向视图对象进行增删改查,对视图对象的增删改查,会导致原表数据被操作(视图和原表变化一致)
mysql> create view scholar1_view as select * from scholar1;
Query OK, 0 rows affected (0.01 sec)
#面向视图查询
mysql> select * from scholar1_view;
+----+------+
| id | name |
+----+------+
| 1 | 小张 |
| 2 | 小李 |
| 3 | 小明 |
+----+------+
3 rows in set (0.00 sec)
#面向视图插入
mysql> insert into scholar1_view(id,name)values(4,'小红');
Query OK, 1 row affected (0.01 sec)
#面向视图查询
mysql> select * from scholar1_view;
+----+------+
| id | name |
+----+------+
| 1 | 小张 |
| 2 | 小李 |
| 3 | 小明 |
| 4 | 小红 |
+----+------+
4 rows in set (0.00 sec)
#查询原表数据
mysql> select * from scholar1;;
+----+------+
| id | name |
+----+------+
| 1 | 小张 |
| 2 | 小李 |
| 3 | 小明 |
| 4 | 小红 |
+----+------+
4 rows in set (0.00 sec)
#面向视图删除
mysql> delete from scholar1_view;
Query OK, 4 rows affected (0.01 sec)
#面向视图查询
mysql> select * from scholar1_view;
Empty set (0.00 sec)
#查询原表数据
mysql> select * from scholar1;
Empty set (0.00 sec)
mysql> create view emp_dept_view as select e.ename,e.sal,d.dname from emp e join dept d on e.deptno =d.deptno;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from emp_dept_view;
+--------+---------+------------+
| ename | sal | dname |
+--------+---------+------------+
| SMITH | 800.00 | RESEARCH |
| ALLEN | 1600.00 | SALES |
| WARD | 1250.00 | SALES |
| JONES | 2975.00 | RESEARCH |
| MARTIN | 1250.00 | SALES |
| BLAKE | 2850.00 | SALES |
| CLARK | 2450.00 | ACCOUNTING |
| SCOTT | 3000.00 | RESEARCH |
| KING | 5000.00 | ACCOUNTING |
| TURNER | 1500.00 | SALES |
| ADAMS | 1100.00 | RESEARCH |
| JAMES | 950.00 | SALES |
| FORD | 3000.00 | RESEARCH |
| MILLER | 1300.00 | ACCOUNTING |
+--------+---------+------------+
14 rows in set (0.00 sec)
mysql> update emp_dept_view set sal=1000 where dname ='ACCOUNTING';
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from emp_dept_view;
+--------+---------+------------+
| ename | sal | dname |
+--------+---------+------------+
| SMITH | 800.00 | RESEARCH |
| ALLEN | 1600.00 | SALES |
| WARD | 1250.00 | SALES |
| JONES | 2975.00 | RESEARCH |
| MARTIN | 1250.00 | SALES |
| BLAKE | 2850.00 | SALES |
| CLARK | 1000.00 | ACCOUNTING |
| SCOTT | 3000.00 | RESEARCH |
| KING | 1000.00 | ACCOUNTING |
| TURNER | 1500.00 | SALES |
| ADAMS | 1100.00 | RESEARCH |
| JAMES | 950.00 | SALES |
| FORD | 3000.00 | RESEARCH |
| MILLER | 1000.00 | ACCOUNTING |
+--------+---------+------------+
14 rows in set (0.00 sec)
mysql> select * from emp;
+-------+--------+-----------+------+------------+---------+---------+--------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO |
+-------+--------+-----------+------+------------+---------+---------+--------+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 1000.00 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 1000.00 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1000.00 | NULL | 10 |
+-------+--------+-----------+------+------------+---------+---------+--------+
14 rows in set (0.00 sec)
说明:
①对于比较复杂且需要被多次反复使用的SQL语句,可以使用视图对象,简化开发,并且非常有利于后期的维护,在修改的时候,只需修改视图对象所映射的SQL语句即可。②使用视图和使用表是一样的,同样可以像对表一样对视图对象进行增删改查。
③视图对象存在在硬盘上,不会消失。
④视图对应的语句只能是DQL语句
基本操作:增删改查(CRUD)
增加(Create)、读取数据(Retrieve)、更新(Update)、删除(Delete)
DBA常用命令:
数据的导出?
在windows的dos窗口中
命令:mysqldump 数据库名<指定表> >导出的物理地址 -uroot -p密码
效果:
数据的导入?
命令: source 物理地址
说明:先登录到MYSQL服务器上,然后在使用该命令
步骤如下:
① 登录到MYSQL
② 创建一个新的数据库
③ 使用该数据库
④通过source来进行初始化数据
#创建一个空数据库标签:rows,affected,0.00,视图,学习,sec,MYSQL,OK,Query From: https://blog.51cto.com/u_15314328/5732598
mysql> create database bjpowernode1;
Query OK, 1 row affected (0.01 sec)
#使用该数据库
mysql> use bjpowernode1;
Database changed
#导入数据进行初始化库
mysql> source F:\test\bjpowernode.sql
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected, 1 warning (0.02 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 220 rows affected (0.00 sec)
Records: 220 Duplicates: 0 Warnings: 0
mysql> show tables;
+------------------------+
| Tables_in_bjpowernode1 |
+------------------------+
| bjpowernode |
| dept |
| emp |
| emp_dept_view |
| flower |
| salgrade |
| scholar1 |
| scholar1_view |
| scholor |
| t_class |
| t_student |
| t_user |
| volunteer |
| volunteer1 |
+------------------------+
14 rows in set (0.00 sec)