Why use "where 1=2" on a SQL CREATE TABLE syntax?

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=2;

I read this statement somewhere on the internet but I couldn't understand the WHERE 1=2.

Will anyone please, explain this?

46804 次浏览

This type of command is usually used to copy the structure of one table to another. In this case, EMPL_DEMO will have the same column structure of employees, except for the keys or constraints.

The 1=2 always evaluates to False which prevents you from copying any of the rows.

Trust in Google, my friend. From the bottom of the first result when you google that statement:

For example:

CREATE TABLE suppliers
AS (SELECT *
FROM companies WHERE 1=2);

This would create a new table called suppliers that included all columns from the companies table, but no data from the companies table.

CREATE TABLE (Create A New Table)

EMPL_DEMO (Called EMPL_DEMO)

AS (With The Data and structure of)

SELECT * FROM employees WHERE 1=2; (Everything in employees where 1=2. Since 1 is never 2 - copy the structure and all 0 matching rows)

..Essentially copy structure and not data.

This can be useful to copy structure of a table excluding its constraints, keys, indexes, identity property and data rows.

This query will create EMPL_DEMO table with no rows copied from employees table as WHERE 1=2 condition is always going to be evaluated as FALSE.

  CREATE TABLE EMPL_DEMO
AS
SELECT *
FROM employees
WHERE 1=2;

This syntax does the same, but it's more obvious, it creates a table with the same structure, with no data.

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees limit 0;

Oracle:

CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=2; //just structure not data


CREATE TABLE EMPL_DEMO AS SELECT * FROM employees WHERE 1=1; //ststructure and data

Better understanding in the Teradata Database:

CREATE TABLE EMPL_DEMO AS Memployees with no data; //structure


CREATE TABLE EMPL_DEMO AS Memployees with data; //structure and data

In SQL Server

select * into table1 from table2 where 1=2(Only Structure)

select * into table1 from table2 where 1=1(Structure with data)