嘗試了一下mysql的檢視,主要是想解決分表後的統計不會太麻煩的問題

bulletming發表於2019-05-18

mysql> create view abc as select * from free_space union all select * from free_space;
Query OK, 0 rows affected (3.81 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| abc            |
| free_space     |
+----------------+
2 rows in set (0.00 sec)

mysql> show create view abc;
+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| View | Create View                                                                                                                                                                                           |
+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| abc  | CREATE ALGORITHM=UNDEFINED ` SQL SECURITY DEFINER VIEW `abc` AS select `free_space`.`a` AS `a` from `free_space` union all select `free_space`.`a` AS `a` from `free_space` |
+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from abc;
Empty set (0.01 sec)

mysql> insert into abc values(1);
ERROR 1471 (HY000): The target table abc of the INSERT is not insertable-into
mysql>
mysql>
mysql> insert into free_space values(1);
Query OK, 1 row affected (1.27 sec)

mysql> select * from abc;
+------+
| a    |
+------+
|    1 |
|    1 |
+------+
2 rows in set (0.00 sec)

mysql> insert into free_space values(2);
Query OK, 1 row affected (1.83 sec)

mysql> select * from abc;
+------+
| a    |
+------+
|    1 |
|    2 |
|    1 |
|    2 |
+------+
4 rows in set (0.00 sec)

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/82392/viewspace-155676/,如需轉載,請註明出處,否則將追究法律責任。

相關文章