Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy table without copying data

Tags:

mysql

CREATE TABLE foo SELECT * FROM bar

copies the table foo and duplicates it as a new table called bar.

How can I copy the schema of foo to a new table called bar without copying over the data as well?

like image 593
Matthew Avatar asked Oct 07 '22 15:10

Matthew


People also ask

How can I create a table without copying data?

Question: How can I create a SQL table from another table without copying any values from the old table? Answer: To do this, the SQL CREATE TABLE syntax is: CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2);

How do I copy an entire table into another table?

The SQL INSERT INTO SELECT Statement The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

How do I make a copy of a table?

In Object Explorer right-click the table you want to copy and select Design. Select the columns in the existing table and, from the Edit menu, select Copy. Switch back to the new table and select the first row. From the Edit menu, select Paste.


2 Answers

Try

CREATE TABLE foo LIKE bar;

so the keys and indexes are copied over as, well.

Documentation

like image 118
RCNeil Avatar answered Oct 13 '22 18:10

RCNeil


Try:

CREATE TABLE foo SELECT * FROM bar LIMIT 0

Or:

CREATE TABLE foo SELECT * FROM bar WHERE 1=0
like image 31
Andomar Avatar answered Oct 13 '22 16:10

Andomar