
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Create view in MySQL only if it does not already exist?
To create a view only if it does not already exist, you can use the following syntax −
CREATE OR REPLACE VIEW yourViewName AS SELECT *FROM yourTableName;
To understand the above syntax, let us create a table.
The query to create a table is as follows −
mysql> create table createViewDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.58 sec)
Insert some records in the table using insert command.
The query is as follows −
mysql> insert into createViewDemo(Name) values('John'); Query OK, 1 row affected (0.22 sec) mysql> insert into createViewDemo(Name) values('Carol'); Query OK, 1 row affected (0.15 sec) mysql> insert into createViewDemo(Name) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into createViewDemo(Name) values('Sam'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using a select statement.
The query is as follows −
mysql> select *from createViewDemo;
Here is the output −
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.00 sec)
Here is the query to create a view only if it does not already exist −
mysql> CREATE OR REPLACE VIEW New_ViewDemo AS select *from createViewDemo; Query OK, 0 rows affected (0.13 sec)
Let us check the records of view.
The query is as follows −
mysql> select *from New_ViewDemo;
The following is The output −
+----+-------+ | Id | Name | +----+-------+ | 1 | John | | 2 | Carol | | 3 | Bob | | 4 | Sam | +----+-------+ 4 rows in set (0.02 sec)
- Related Articles
- MySQL create user if it does not exist?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- How to check if a table exists in MySQL and create if it does not already exist?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Add object to array in JavaScript if name does not already exist?
- How to create a folder if it does not exist in C#?
- How can I create a python directory if it does not exist?
- How can I create a directory if it does not exist using Python?
- Insert records in MongoDB collection if it does not exist?
- Does NOT EQUAL exist in MySQL?
- How to insert only those records that does not exist in a MySQL table?
- Run Cron Job Only If It Isn’t Already Running in Linux
- Select from table where value does not exist with MySQL?
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- MongoDB query to determine if a specific value does not exist?

Advertisements