user6064424
user6064424

Reputation:

Check if view exists in mysql

I have a mysql view called records_latest. What query can I use to check if this view already exists in mysql database?

I create this view in a script during start-up. I do not wish to create it more than once. So, I need to check if the view exists or not.

Upvotes: 2

Views: 8232

Answers (3)

Bơ Loong A Nhứi
Bơ Loong A Nhứi

Reputation: 563

To list all view:

SHOW FULL TABLES IN db_name_here WHERE TABLE_TYPE LIKE 'VIEW';

Filter by table or view name, no need re-creation:


SHOW FULL TABLES IN db_name_here LIKE 'table_or_view_name_here';

Upvotes: 0

Zenel Rrushi
Zenel Rrushi

Reputation: 2366

To show a list of views that are in you database the

SHOW FULL TABLES IN youdatabasename WHERE TABLE_TYPE LIKE '%VIEW%';

if you want to create a view with that name and you don't care how the view was before you can use the

CREATE OR REPLACE VIEW  records_latest ....

this will delete the previous view if it exists and then will create the view again

Upvotes: 7

JohnHC
JohnHC

Reputation: 11195

Try

CREATE or REPLACE VIEW records_latest...

This will make sure it refreshes on startup and will not duplicate if it exists

Upvotes: 1

Related Questions