I have issue with CREATE TABLE and then INSERT you table - mysql

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.

Related

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

Use of SELECT INTO

Is it necessary to define the new table definition before using SELECT INTO query in MYSQL.
I am getting problem to execute the query when I writ e like:
SELECT *
INTO newtable
FROM oldtable
WHERE 1=0;
the error showing is:
Undeclared variabie: newtable
if you have newtable
try :
INSERT INTO newtable SELECT ...
if you don't have newtable
try :
CREATE TABLE newtable AS SELECT ...
The MySQL manual search engine is terrible but googling for something like mysql 5.5 select into will normally take you to the right page:
MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL
extension. Instead, MySQL Server supports the INSERT INTO ... SELECT
standard SQL syntax, which is basically the same thing.
If you read the documentation here it says:
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables
So, yes, you need to create the new table first.
You can use CREATE TABLE new LIKE old to create a new, empty table, which is a copy of the original table structure.

Temporary Tables Not Working in PHPMyAdmin

I run this query
CREATE TEMPORARY TABLE usercount SELECT * FROM users
I get this message
Your SQL query has been executed successfully ( Query took 0.1471 sec )
But when I try to access the newly created table using
SELECT * FROM usercount
I get this error
#1146 - Table 'abc_site.usercount' doesn't exist
Not sure why, I need to mention that I've did a good share of googling beforehand.
My version of PHPMyAdmin is 3.5.2.2 and MySQL 5.5.27
PHPMyAdmin (or rather PHP) closes the database connection after each screen. Thus your temporary tables disappear.
You can put multiple SQL statements in the SQL query box in PHPMyAdmin; this should be executed as one block and thus the temporary table is not deleted.
Temporary tables are temparar and after use thay Delete.
for example ,when insert data into database , first we can insert into temp table and thus when complete transaction , then insert into main table.
EXAMPLE :
//------------------------------------------
CREATE TEMPORARY TABLE TEMP
(
USERNAME VARCHAR(50) NOT NULL,
PASSWORD VARCHAR(50) NOT NULL,
EMAIL varchar(100),
TYPE_USER INT
);
INSERT INTO TEMP VALUES('A','A','A','1');
SELECT * FROM TEMP
//-----------------------------------------
Show A,A,A,1

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;

Use If exists for select statement in 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