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;
Related
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.
I was trying to use this statement SELECT * INTO new_table FROM old_table but it's giving me the error of undeclared value. I wanted to create a back table from one existing table to another new table.
Do I have first to create another table? or am I missing something.
the link below shows that the same statement can be used, so I don't why it's not working for me
https://www.w3schools.com/sql/sql_select_into.asp
You should use the INSERT ... SELECT statement for MySQL instead.
INSERT INTO new_table
SELECT *
FROM old_table
But new_table must exist. If you have MySQL version >= 8.0.19, then you can use instead the following syntax:
INSERT INTO new_table TABLE old_table;
You may check 13.2.6.1 INSERT ... SELECT Statement.
Background information
The SELECT...INTO is not exactly standard, it has selective support by vendors.
The statement does exist in MySQL, but is used for "query result to be stored in variables or written to a file".
So I have an assignment that is asking for me to add data into a table using the SELECT command. I've always known to use the insert into command or the Update command so I'm a little confused.
There are 2 tables, one named 'Book' and one named 'Fiction'. Book already has data in the table, I need to insert values from book into fiction using the select tool.
If someone could help explain it would be great,
Thank you!
What the assignment means, is to still use a INSERT query, but with an SELECT query attached to insert rows quickly from one table to another.
With INSERT ... SELECT, you can quickly insert many rows into a table
from the result of a SELECT statement, which can select from one or
many tables. For example:
INSERT INTO tbl_temp2 (fld_id)
SELECT tbl_temp1.fld_order_id
FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;
Source: https://dev.mysql.com/doc/refman/5.7/en/insert-select.html
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.
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