
- 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
MySQL add “prefix” to every column?
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 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
- Python - Add a prefix to column names in a Pandas DataFrame
- Concatenate some of the column values in a single line with MySQL and prefix a particular string “MR.” to every string
- Add leading zeros to a MySQL column?
- MySQL query to delete last two words from every column value
- How to add a NOT NULL column in MySQL?
- How to add column using alter in MySQL?\n
- How to add comment to column in MySQL using Python?
- Convert the case of every value in a MySQL Column to uppercase
- Is it impossible to add a column in MySQL specifically before another column?
- MySQL query to get sum of each column where every column has same number of values?
- Add to existing value in MySQL column using CONCAT function?
- How to add a column in a table in MySQL?
- How to add not null constraint to existing column in MySQL?
- How to add a column to a MySQL table in Python?
- Increment column value ‘ADD’ with MySQL SET clause

Advertisements