Languages

MySQL drop view if exists

3 points
Asked by:
Kate_C
20320

How can I drop view only when this view exists in MySQL?
I want to check if the view exists because when it doesn't and I will drop it, then I will get error.

I know I can remove view without checking it's existence with this commnad:

DROP VIEW `users_view`;

But it won't protect me from getting error if this view already was removed earlier or never existed.

1 answer
1 points
Answered by:
Kate_C
20320

Solution:

DROP VIEW IF EXISTS `users_view`;

This SQL will check if the view exists and if it does, it will drop this view.

If we execute it when view doesn't exist, we will get only warrning.

I tested it on windows with HeidiSQL client. When I executed this mysql query I got Warrning: Note: Unknown VIEW: 'test.users_view'. I have all warnings enabled.

From MySQL documentation:

DROP VIEW IF EXISTS `view_name`;

 

0 comments Add comment
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - friendly IT community for everyone.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join