SQL Tutorial – Copy Table columns without rows in SQL.

You have a table and you want to create a new table with the same column structure but you do not want to copy the rows/data from the existing table.

Solution –

To copy the column names from an old table without copying the data, we use the CREATE TABEL command with a subquery that returns no rows.

CREATE TABLE actors As
SELECT
    *
FROM
    employees
WHERE
    1 = 0

When you use CREATE TABLE As SELECT ( CTAS ) , then all the rows from this query is used to create the table unless you specify a False condition in the WHERE clause. Since We do not want to copy the data, we used 1 = 0 in the WHERE clause which makes this condition False and SQL will not copy the rows from the existing table and will only create an empty table with all the column names in the SELECT statement.

Rating: 1 out of 5.

Leave a Reply