0

I am creating a view and written the below code snippet :

CREATE OR REPLACE VIEW vclPersonData
    AS
SELECT * FROM phone_data UNION 
SELECT * FROM Address 

I get an error if the table doesn't exists, to come overthat i used If Exists but it too doesn't works for me.

Any help is thankful. Thanks in Advance.

0

1 Answer 1

2

You'll need two steps in your script:

  1. CREATE TABLE IF NOT EXISTS
  2. CREATE VIEW AS SELECT * FROM TABLE

If the table exists, step 1 will be harmless. If the table does not exist, step 1 will create it and step 2 will create an empty view.

If you only want the view to be created IF the table exist, check the existance of the table before:

BEGIN
SELECT 1 FROM TABLE;
CREATE VIEW AS SELECT * FROM TABLE
COMMIT
Sign up to request clarification or add additional context in comments.

2 Comments

I have to union 6 tables so i have to check that those tables exists or not , if not i have to create a one ...I there other way work around say that i dont have to create a table only skip union statement with the table that doesn't exists ?
You'll need a procedure for that, either a Stored Procedure on your MYSQL server, or a script. Could you explain what you are trying to do and how you are doing this, so we can further help you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.