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;
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 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
In MySQL user manual, it says the following command is way faster than the RENAME command since no table copy is required.
ALTER TABLE table1 RENAME TO table2;
http://dev.mysql.com/doc/refman/5.7/en/alter-table.html
However, is there anyway that we can quickly rename/overwrite a table into an existing table in MYSQL 5.6? I have tried the following command and it shows error "You have an error in your SQL synthax;"
ALTER TABLE table1 RENAME OVERWRITE TO table2;
Could any guru enlighten? Thanks.
Not quite sure where you saw that. But ...
RENAME TABLE old_table TO new_table;
This statement is equivalent to
the following ALTER TABLE statement:
ALTER TABLE old_table RENAME new_table;
Source: http://dev.mysql.com/doc/refman/5.7/en/rename-table.html
If you want to rename a table and a table with that name already exists, you need to first do.
DROP TABLE old_table
Because
MySQL checks the destination table name before checking whether the
source table exists. For example, if new_table already exists and
old_table does not, the following statement fails as shown here:
(gives an example)
Is there a simple ALTER TABLE that will allow this, or should I simply CREATE ... LIKE ...; ... INSERT SELECT?
Temp tables live in tempdb, not your data files, so you're gonna have to recreate it there. Try:
SELECT * INTO MyTable FROM #tempTable
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.