Languages

MySQL drop view if exists

3 points
Asked by:
Kate_C
2895

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 answers
1 points
Answered by:
Kate_C
2895

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
Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.