For example suppose I have a table M2011_03 and I wish to copy the structure but not the data into a table M2011_04. M2011_04 currently does not exist. How do I do this?
I think you are looking for:
CREATE TABLE new_tbl LIKE orig_tbl;
More information: http://dev.mysql.com/doc/refman/5.1/en/create-table.html
either
a) paste the results of SHOW CREATE TABLE M2011_03 to create the '04 one.
or
b)
CREATE TABLE M2011_04 SELECT * FROM M2011_03;
TRUNCATE TABLE M2011_04;
Related
It is possible to get CREATE TABLE statement in specific database without using USE before?
Something like SHOW CREATE TABLE my_table IN my_database; (this doesn't works).
Thanks
show create table database_name.table_name;
I'm trying to "duplicate" a table from my db creating a temporary one. However I need indexes are not copied.
I'm using this query right now:
CREATE TEMPORARY TABLE temp365 LIKE contactlens;
but the result table contains also indexes. I checked out the documentation and I don't see a way to copy the structure without indexes.
Because I can't have static name for indexes, I was wondering how I can drop all of them using simple SQL.
I was starting trying to avoid to copy them at all, but it seems not possible.
It's just to provide some examples to #wchiquito's comment.
It's pretty easy to create a copy of a table without indexes using CREATE TABLE ... SELECT command.
If you don't need to copy any rows from original table just provide a false value in WHERE clause or specify 0 in LIMIT one. Some examples:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens WHERE 0;
or a bit different way:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] temp365 SELECT * FROM contactlens LIMIT 0;
This way you'll get the same result as with CREATE TABLE ... LIKE, but without indexes.
I have a problem with a mysql table which I'd like to clone and give another name. When I try the
CREATE TABLE data1 AS SELECT * FROM data;
It gets stuck in execution, and never actually do anything, and I have to abort.
The thing is that my table "data" has some extra commands in the sql source like
CREATE INDEX keywords ON data (keywords);
It has a few of these, and I suspect that this is what's causing the issue, since I have been able to clone other tables without these extra commmands. I'm new in sql, so I have no idea how to overcome this problem. Anyone care to help?
Use is create table like syntax. to create a table with the same structure and after this you can copy the data.
https://dev.mysql.com/doc/refman/5.7/en/create-table-like.html
a second solution is to use mysqldump to extract the table structur including the data, rename the table in dump file and Reimport the it.
-- To copy table with constraints
CREATE TABLE new_table_name LIKE old_table_name;
-- To copy without constraints
CREATE TABLE new_table_name
(SELECT * FROM old_table_name WHERE 0);
source
I was asked in a viva "how can you create a table structure without copying data from a database table?" I was quite sure with my answer. My answer was:`
CREATE TABLE new_table AS (SELECT *FROM old_table);
Was I right or wrong?
CREATE TABLE new_table AS (SELECT * FROM old_table where 0=1);
No. Your answer is incorrect. You can use this SQL query.
CREATE TABLE *new_table* AS (SELECT *FROM *old_table* WHERE *statement=false*);
Like this an example is following:
CREATE TABLE *new_table* AS (SELECT *FROM *old_table* WHERE *1=2*);
I think it will serve your purpose...:P
CREATE TABLE new_table AS SELECT * FROM old_table where 0=1;
Here in where clause we can use any unequality statement like where 1=2,2=3..etc., which should inform optimizer this 'where' condition will definitely return false, thus preventing any data to be copied from old_table.
if you are using workbench it has a option in left side of the screen "Data export"
just click on it select the db you want to copy structure of and there will be a drop down option select "Dump structure only" and export to a folder.
import this file where ever you want using option data import restore and select path and type new schema name and import. you have new schema with the structure you want.
I want to copy mysql table from one database to another using mysql command line
I am trying to executing commmand
DROP TABLE IF EXISTS `db1.tablename`; CREATE TABLE `db1.tablename` like `db2.tablename`;
but it is giving me error no database is selected.
but if I fire
use db2
DROP TABLE IF EXISTS `db1.tablename`; CREATE TABLE `db1.tablename` like `db2.tablename`;
then it is create table db1.tablename inside db 2.
How to fix it ?
I think your create statement should look like this:
CREATE TABLE `db1.tablename` SELECT * FROM `db2.tablename`;
The table name in your query includes the database name without separation. It should be something like:
DROP TABLE IF EXISTS `db1`.`tablename`;
CREATE TABLE `db1`.tablename` like `db2`.`tablename`;