MySQL 如果不存在则创建视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3316950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:38:17  来源:igfitidea点击:

create if not exists view?

sqlmysqlviewh2

提问by kumar kasimala

Is there any way to create view if not existsin MySQL or H2 Database?

create view if not exists在 MySQL 或 H2 数据库中有什么方法吗?

采纳答案by Sachin R

From section 12.1.12. CREATE VIEW Syntaxof the MySQL 5.0 Reference Manual:

来自第12.1.12MySQL 5.0 参考手册的CREATE VIEW 语法

CREATE VIEW Syntax

CREATE
    [OR REPLACE]
    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
    [DEFINER = { user | CURRENT_USER }]
    [SQL SECURITY { DEFINER | INVOKER }]
    VIEW view_name [(column_list)]
    AS select_statement
    [WITH [CASCADED | LOCAL] CHECK OPTION]

The CREATE VIEW statement creates a new view, or replaces an existing one if the OR REPLACE clause is given. This statement was added in MySQL 5.0.1. If the view does not exist, CREATE OR REPLACE VIEW is the same as CREATE VIEW. If the view does exist, CREATE OR REPLACE VIEW is the same as ALTER VIEW.

CREATE VIEW 语句创建一个新视图,如果给出 OR REPLACE 子句,则替换现有视图。该语句是在 MySQL 5.0.1 中添加的。如果视图不存在,CREATE OR REPLACE VIEW 与 CREATE VIEW 相同。如果视图确实存在,则 CREATE OR REPLACE VIEW 与 ALTER VIEW 相同。

回答by Andomar

The usual way is to overwrite a view using create or replace:

通常的方法是使用create or replace以下方法覆盖视图:

create or replace view YourView
as
select * from users

回答by Daniel

On H2 you can add IF NOT EXISTSbefore the view name you want to create. e.g.:

在 H2 上,您可以在要创建的视图名称之前添加IF NOT EXISTS。例如:

CREATE VIEW IF NOT EXISTS viewName (column1, column12) 
AS ( 
    SELECT column1, column2
    FROM clients 
);