Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql create view only if it doesn't already exist

How do I create a view only if it doesn't exist. If it does exist, I want to drop the view and redefine it. I also want no warnings or errors.

like image 672
Alexander Bird Avatar asked Feb 11 '10 16:02

Alexander Bird


People also ask

Can we create a view on a table that does not exists by using?

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.

Do not create table if already exists?

The IF NOT EXISTS is optional. It allows you to check if the table that you create already exists in the database. If this is the case, MySQL will ignore the whole statement and will not create any new table. Second, you specify a list of columns of the table in the column_list section, columns are separated by commas.

When to use create table if not exists?

The “CREATE TABLE if not exists” statement is very useful in creating a table because it will not create the table if the table of the same name already exists in the database.

How do I make MySQL read only?

MySQL doesn't support WITH READ ONLY for CREATE VIEW . The line DEFINER = CURRENT_USER is not needed if you use the limited user to create the view. Or you can use an admin user to create the view and in this case the line DEFINER = must contain the name of the user that will own the view.


2 Answers

You're going to kick yourself:

CREATE OR REPLACE VIEW ...

Details here. ;-)

like image 197
T.J. Crowder Avatar answered Oct 11 '22 08:10

T.J. Crowder


CREATE OR REPLACE VIEW <view name>
AS
<your select expression goes here>
like image 22
Roland Bouman Avatar answered Oct 11 '22 08:10

Roland Bouman