Creating a replica sql table

I need a query to create a table that is an exact replica, but with a different table name and without any data from the original table using an SQL query!

+3
source share
10 answers

You can try this

SELECT * INTO Table_Copy
FROM Table
where 1=2

It will create an empty table with the same structure.

+11
source

Jonathan has this (upvoted), and you should probably go for it because it's more portable. I usually use something like this:

SELECT TOP 0 * INTO [New_Table] FROM [Old_Table]

I think this better expresses what you are doing, but I like Jonathan because "TOP 0" is SQL Server and therefore it is more portable.

+4
source
  • SQL Server
  • Script → →
+3

MySQL SHOW CREATE TABLE table_name;

CREATE TABLE. , .

http://dev.mysql.com/doc/refman/5.1/en/show-create-table.html

+1
+1
SELECT * INTO Table_Copy
FROM Table
where 1=2

, - .

SELECT * INTO Table_Copy
FROM Table

.

+1

:

CREATE TABLE foo AS SELECT...

0
select * into newtablename from sourcetablename
go
truncate newtablename
go

, , truncate.

0

create table <new table name> as select * from <old tale name from which you would like to extract data>

, .

0

postgres INHERITS LIKE ( )

CREATE TABLE client_new ( LIKE);

CREATE TABLE client_new() INHERITS ()

INHERITS . () , (). LIKE , , . INHERITS, . , .

0

Source: https://habr.com/ru/post/1712458/


All Articles