Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create if not exists view?

Tags:

sql

mysql

view

h2

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

like image 571
kumar kasimala Avatar asked Jul 23 '10 09:07

kumar kasimala


People also ask

How do you create a view in SQL if not exists?

Here is another method, where you don't have to duplicate the contents of the view: IF (NOT EXISTS (SELECT 1 FROM sys. views WHERE name = 'data_VVV')) BEGIN EXECUTE('CREATE VIEW data_VVVV as SELECT 1 as t'); END; GO ALTER VIEW data_VVVV AS SELECT VCV. xxxx, VCV.

How do I create a view in SQL?

The basic syntax for creating a view in MySQL is as follows: CREATE VIEW [db_name.] view_name [(column_list)] AS select-statement; [db_name.] is the name of the database where your view will be created; if not specified, the view will be created in the current database.

Can we CREATE VIEW without table?

can we create a view without base tables? No.


1 Answers

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

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.

like image 134
Sachin R Avatar answered Sep 21 '22 14:09

Sachin R