• CREATE VIEW
    • 语法图
    • 示例
    • MySQL 兼容性
    • 另请参阅

    CREATE VIEW

    使用 CREATE VIEW 语句将 SELECT 语句保存为类似于表的可查询对象。TiDB 中的视图是非物化的,这意味着在查询视图时,TiDB 将在内部重写查询,以将视图定义与 SQL 查询结合起来。

    语法图

    CreateViewStmt:

    CreateViewStmt

    OrReplace:

    OrReplace

    ViewAlgorithm:

    ViewAlgorithm

    ViewDefiner:

    ViewDefiner

    ViewSQLSecurity:

    ViewSQLSecurity

    ViewName:

    ViewName

    ViewFieldList:

    ViewFieldList

    ViewCheckOption:

    ViewCheckOption

    示例

    1. mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY auto_increment, c1 INT NOT NULL);
    2. Query OK, 0 rows affected (0.11 sec)
    3. mysql> INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
    4. Query OK, 5 rows affected (0.03 sec)
    5. Records: 5 Duplicates: 0 Warnings: 0
    6. mysql> CREATE VIEW v1 AS SELECT * FROM t1 WHERE c1 > 2;
    7. Query OK, 0 rows affected (0.11 sec)
    8. mysql> SELECT * FROM t1;
    9. +----+----+
    10. | id | c1 |
    11. +----+----+
    12. | 1 | 1 |
    13. | 2 | 2 |
    14. | 3 | 3 |
    15. | 4 | 4 |
    16. | 5 | 5 |
    17. +----+----+
    18. 5 rows in set (0.00 sec)
    19. mysql> SELECT * FROM v1;
    20. +----+----+
    21. | id | c1 |
    22. +----+----+
    23. | 3 | 3 |
    24. | 4 | 4 |
    25. | 5 | 5 |
    26. +----+----+
    27. 3 rows in set (0.00 sec)
    28. mysql> INSERT INTO t1 (c1) VALUES (6);
    29. Query OK, 1 row affected (0.01 sec)
    30. mysql> SELECT * FROM v1;
    31. +----+----+
    32. | id | c1 |
    33. +----+----+
    34. | 3 | 3 |
    35. | 4 | 4 |
    36. | 5 | 5 |
    37. | 6 | 6 |
    38. +----+----+
    39. 4 rows in set (0.00 sec)
    40. mysql> INSERT INTO v1 (c1) VALUES (7);
    41. ERROR 1105 (HY000): insert into view v1 is not supported now.

    MySQL 兼容性

    • 目前 TiDB 中的视图不可插入且不可更新。

    另请参阅

    • CREATE TABLE
    • SHOW CREATE TABLE
    • DROP TABLE