view 又称虚拟表,view其实就一条查询SQL语句的结果集==>将常用的SQL查询结果集虚拟为一张表存放在内存中
create view as 视图名 (查询SQL语句结果集);–当再次使用时:select * from 视图名
①权限的控制!比如:某几个列允许用户查询,而其他列不允许,可以通过视图开放其中的一部分列,达到权限的控制
②简化复杂的查询!比如:查询每个栏目下的商品的平均价格并按平均价格排序,然后查出平均价格前3高的栏目
①create view v as select cat_id, avg(shop_price) as pj from goods group by cat_id
②select * from v order by pj limit 0,3
①视图虚拟表: 是物理表的一个"投影",两者是相互影响的;更改物理表,虚拟表也会更改,同理,更改虚拟表,物理表也会更改!
但是:如果虚拟表中含有函数(经过计算…),则不能修改!(即物理表和虚拟表的列能一一对应,则虚拟表中该列能修改改一行影响一行)
同理:增加和删除操作也是和修改一样
1.对于VIEW存储的SQL语句是简单的select语句,所以当对视图查询时候就是对SQL语句的拼接==>对物理表的间接拼接查询(合并:merge)
2.对于VIEW存储的SQL语句已经是逻辑复杂的select语句了,这时对视图的拼接查询会更麻烦!这时候mysql会先执行视图的创建语句,把结果集形成一张临时表,再对临时表(temptable)进行操作
MySQL数据库中可以通过algorithm(算法)定义对视图的处理情况 create algorithm = merge/temptalbe view v_name as …
[不写该属性,则由MySQL自行判断]
mysql> create view v1 as select * from goods where shop_price>300;
Query OK, 0 rows affected (0.38 sec)
mysql> select * from v1 where shop_price < 500;
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
| goods_id | goods_name | cat_id | brand_id | goods_sn | goods_number | shop_price | market_price | click_count |
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
| 8 | 飞利浦9@9v | 3 | 4 | ecs000008 | 1 | 399.00 | 478.79 | 10 |
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
1 row in set (0.00 sec)
mysql> create algorithm=merge view v2
-> as select * from goods where shop_price>300;
Query OK, 0 rows affected (0.23 sec)
mysql> select * from v2 where shop_price <500;
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
| goods_id | goods_name | cat_id | brand_id | goods_sn | goods_number | shop_price | market_price | click_count |
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
| 8 | 飞利浦9@9v | 3 | 4 | ecs000008 | 1 | 399.00 | 478.79 | 10 |
+----------+---------------+--------+----------+-----------+--------------+------------+--------------+-------------+
1 row in set (0.00 sec)
mysql> create algorithm=temptable view v3 as select * from goods order by cat_id asc,shop_price desc;
Query OK, 0 rows affected (0.18 sec)
mysql> select * from goods order by cat_id asc,shop_price desc group by cat_id;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by cat_id' at line 1
mysql> select * from v3 group by cat_id;
ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'shop.v3.goods_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
mysql> select cat_id from v3 group by cat_id;
+--------+
| cat_id |
+--------+
| 2 |
| 3 |
| 4 |
| 5 |
| 8 |
| 11 |
| 13 |
| 14 |
| 15 |
+--------+
9 rows in set (0.00 sec)
mysql> show engines \G
*************************** 1. row ***************************
Engine: MEMORY
Support: YES
Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES
Comment: Collection of identical MyISAM tables
Transactions: NO
XA: NO
Savepoints: NO
*************************** 3. row ***************************
Engine: CSV
Support: YES
Comment: CSV storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 4. row ***************************
Engine: FEDERATED
Support: NO
Comment: Federated MySQL storage engine
Transactions: NULL
XA: NULL
Savepoints: NULL
*************************** 5. row ***************************
Engine: PERFORMANCE_SCHEMA
Support: YES
Comment: Performance Schema
Transactions: NO
XA: NO
Savepoints: NO
*************************** 6. row ***************************
Engine: MyISAM
Support: YES
Comment: MyISAM storage engine
Transactions: NO
XA: NO
Savepoints: NO
*************************** 7. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
XA: YES
Savepoints: YES
*************************** 8. row ***************************
Engine: BLACKHOLE
Support: YES
Comment: /dev/null storage engine (anything you write to it disappears)
Transactions: NO
XA: NO
Savepoints: NO
*************************** 9. row ***************************
Engine: ARCHIVE
Support: YES
Comment: Archive storage engine
Transactions: NO
XA: NO
Savepoints: NO
9 rows in set (0.00 sec)
事务:一般用于账户金额相关的内容操作。
事务可以保证多个操作原子性,要么全成功,要么全失败。对于数据库来说事务保证批量的 DML 要么全成功,要么全
失败。事务具有四个特征 ACID
事务基本概念:
当执行 DML 语句是其实就是开启一个事务
关于事务的回滚需要注意:只能回滚 insert、 delete 和 update 语句,不能回滚 select(回滚 select 没有任何意义),对于
create、 drop、 alter 这些无法回滚.事务只对 DML 有效果。
rollback,或者 commit 后事务就结束了
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update a2 set money = money+1000 where uname='张三';
Query OK, 1 row affected (0.04 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from a2;
+--------+---------+
| uname | money |
+--------+---------+
| 张三 | 7666.66 |
| 李四 | 7777.77 |
+--------+---------+
2 rows in set (0.00 sec)
mysql> updata a2 set money = money -1000 where uname='李四';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'updata a2 set money = money -1000 where uname='李四'' at line 1
mysql> rollback;
Query OK, 0 rows affected (0.14 sec)
mysql> select * from a2;
+--------+---------+
| uname | money |
+--------+---------+
| 张三 | 6666.66 |
| 李四 | 7777.77 |
+--------+---------+
2 rows in set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update a2 set money = money+1000 where uname='张三';
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update a2 set money = money -1000 where uname='李四';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.17 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> update a2 set money = money +500 where uname='李四';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update a2 set money = money-1000 where uname='张三';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> commit;
Query OK, 0 rows affected (0.12 sec)
mysql> select * from a2;
+--------+---------+
| uname | money |
+--------+---------+
| 张三 | 6666.66 |
| 李四 | 7277.77 |
+--------+---------+
2 rows in set (0.00 sec)