Use If exists for select statement in mysql - mysql

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.

You'll need two steps in your script:
CREATE TABLE IF NOT EXISTS
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

Related

I have issue with CREATE TABLE and then INSERT you table

I have issue width CREATE TABLE and then INSERT you table.
This is what I want to do. CREATE TABLE LIKE and then INSERT SELECT * FROM.
I be creating multiple tables at the same time. I will be using a table like sample table to create the tables. Then editing the new tables in the future.
This Sample table.
id
old_data
new_data
1
pool_00
pool_01
2
pool_00
pool_02
This is MySQL code I’m having issues with.
CREATE TABLE `new_data` LIKE `old_data`;
INSERT `new_data` SELECT * FROM `old_data`;
This is the error I get.
Error
SQL query:
CREATE TABLE new_data LIKE old_data
MySQL said: Documentation
#1146 - Table 'oerpn_survivor_us.old_data' doesn't exist.
I run this MySQL code and it works.
CREATE TABLE pool_01 LIKE pool_00;
INSERT pool_01 SELECT * FROM pool_00;
CREATE TABLE pool_02 LIKE pool_00;
INSERT pool_02 SELECT * FROM pool_00;
Thank you for your help.
Taking the error message verbatim:
#1146 - Table 'oerpn_survivor_us.old_data' doesn't exist.
I would assume that the issue is that the old_data table exists on some database other than the one you are current using. If so, then one fix here would be to simply scope old_data to the correct database. Your code should look something like:
CREATE TABLE db2.new_data SELECT * FROM db1.old_data;
Here db2 is your current database, which appears to be oerpn_survivor_us, and db1 should be replaced by the name of the database which contains the old_data table.

How to select content from a table into a new table in SQL stored procedures?

I was trying to write a procedure and needed to copy output_1 table into a new one.
This procedure :
BEGIN
SELECT * INTO newtable FROM output_1;
END
returns the following error :Undeclared variable: newtable
I thought it would create a new table and all its columns automatically.
How do I SELECT multiple columns of a table INTO a new table using a stored procedure?
EDIT :
In stored procedures, when you want to use a table to store data temporarily, you should consider using temporary tables.
Typically, if you try to store a table in a variable, you will get a multiple rows error ; in this case, temporary tables can replace variables.
CREATE TEMPORARY TABLE new_table AS SELECT * FROM output_1;
You cannot select into a table. You possibly intended
create table newtable as select * from output_1;
https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html

MySQL VIEW Incorrect key file for table try to repair it

I have a FooBar view like:
CREATE VIEW `FooBar` AS
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
When I SELECT * FROM FooBar I get:
Incorrect key file for table '/tmp/#sql_1234_5.MYI'; try to repair it
When I run the select statements like this:
SELECT * FROM `Foo`.`Bar`
UNION ALL
SELECT * FROM `Foo1`.`Bar`
Everything goes well. The problem is clearly with my FooBar view.
When I REPAIR TABLE FooBar I get
'WhiskerDatabase.VisualDiscrimSuperimposed_Results' is not BASE TABLE
Corrupt
I tried to DROP VIEW FooBarand reCREATE VIEW FooBar... but the problems persists.
And I can't locate the sql_1234_5.MYI file in /var/lib/mysql/MyDB/.
Found the explanation here. MySQL build a temporary file. The temporary file is too big to fit in memory.
Adjusting the system memory or using a LIMIT clause could help solve the problem.
I solve my problem by creating a procedure that create a table instead of a view. Such as:
DROP TABLE `FooBar` IF EXISTS;
CREATE TABLE `FooBar` AS SELECT * FROM `Foo`.`Bar`;
INSERT INTO `FooBar` SELECT * FROM `Foo1`.`Bar`;

SELECT INTO equivalent in mysql

I have been easily using
SELECT * INTO newtable IN 'another_database'
FROM original_table_in_separate_database;
to backup/copy data from one table to another table easily in MSSQL.
Now i am moving to MYSQL and cannot accomplish this task as this feature is not available in MYSQL.
Though CREATE TABLE ... SELECT can somehow accomplish the task in same database, but not with two different database.
Please help me if there is any idea :)
Thanks in advance .
You can use INSERT INTO .. SELECT FROM construct like
INSERT INTO db1.dbo.newtable
SELECT * FROM db2.dbo.original_table_in_separate_database;
Point to note: For INSERT INTO .. SELECT to work both the target table and inserting table must exist. Otherwise, use CREATE TABLE AS ... SELECT like
CREATE TABLE newtable
AS
SELECT * FROM db2.dbo.original_table_in_separate_database;

Create table in MySQL that matches another table?

I am using MySQL. I have a table called EMP, and now I need create one more table (EMP_TWO) with same schema, same columns, and same constraints. How can I do this?
To create a new table based on another tables structure / constraints use :
CREATE TABLE new_table LIKE old_table;
To copy the data across, if required, use
INSERT INTO new_table SELECT * FROM old_table;
Create table docs
Beware of the notes on the LIKE option :
Use LIKE to create an empty table based on the definition of another
table, including any column attributes and indexes defined in the
original table:
CREATE TABLE new_table LIKE original_table; The copy is created using the same
version of the table storage format as the original table. The SELECT
privilege is required on the original table.
LIKE works only for base tables, not for views.
CREATE TABLE ... LIKE does not preserve any DATA DIRECTORY or INDEX
DIRECTORY table options that were specified for the original table, or
any foreign key definitions.
If you want to copy only Structure then use
create table new_tbl like old_tbl;
If you want to copy Structure as well as data then use
create table new_tbl select * from old_tbl;
Create table in MySQL that matches another table?
Ans:
CREATE TABLE new_table AS SELECT * FROM old_table;
Why don't you go like this
CREATE TABLE new_table LIKE Select * from Old_Table;
or You can go by filtering data like this
CREATE TABLE new_table LIKE Select column1, column2, column3 from Old_Table where column1 = Value1;
For having Same constraint in your new table first you will have to create schema then you should go for data for schema creation
CREATE TABLE new_table LIKE Some_other_Table;
by only using the following command on MySQL command line 8.0 the following ERROR is displayed
[ mysql> select * into at from af;]
ERROR 1327 (42000): Undeclared variable: at
so just to copy the exact schema without the data in it you can use the create table with like statement as follows:
create table EMP_TWO like EMP;
and to copy table along with the data use:
create table EMP_TWO select * from EMP;
to only copy tables data after creating an empty table:
insert into EMP_TWO select * from EMP;