为什么在SQL CREATE表语法中使用“where 1=2”?

时间:2021-10-22 09:07:07
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.

我在网上看了这个声明,但我不明白1=2的地方。

Will anyone please, explain this?

有人能解释一下吗?

7 个解决方案

#1


102  

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.

这种类型的命令通常用于将一个表的结构复制到另一个表。在这种情况下,EMPL_DEMO将具有与员工相同的列结构,除了键或约束。

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

1=2总是计算为False,从而阻止您复制任何行。

#2


25  

CREATE TABLE (Create A New Table)

创建表(创建一个新表)

EMPL_DEMO (Called EMPL_DEMO)

EMPL_DEMO(称为EMPL_DEMO)

AS (With The Data and structure of)

AS(数据和结构

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)

从1=2的员工中选择*;(1=2时员工的一切。因为1永远不是2 -复制结构和所有的0匹配行)

..Essentially copy structure and not data.

. .本质上是复制结构而不是数据。

#3


13  

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;

#4


7  

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.

这个查询将创建EMPL_DEMO表,其中没有从employee表复制的行,因为1=2的条件总是被认为是错误的。

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

#5


6  

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.

这将创建一个名为供应商的新表,其中包括来自公司表的所有列,但没有来自公司表的数据。

#6


0  

Oracle:

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:

更好地理解Teradata数据库:

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

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

#7


0  

In SQL Server

在SQL Server

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

从表2中选择*到表1,其中1=2(仅为结构)

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

从表2中选择*到表1,其中1=1(数据结构)

#1


102  

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.

这种类型的命令通常用于将一个表的结构复制到另一个表。在这种情况下,EMPL_DEMO将具有与员工相同的列结构,除了键或约束。

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

1=2总是计算为False,从而阻止您复制任何行。

#2


25  

CREATE TABLE (Create A New Table)

创建表(创建一个新表)

EMPL_DEMO (Called EMPL_DEMO)

EMPL_DEMO(称为EMPL_DEMO)

AS (With The Data and structure of)

AS(数据和结构

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)

从1=2的员工中选择*;(1=2时员工的一切。因为1永远不是2 -复制结构和所有的0匹配行)

..Essentially copy structure and not data.

. .本质上是复制结构而不是数据。

#3


13  

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;

#4


7  

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.

这个查询将创建EMPL_DEMO表,其中没有从employee表复制的行,因为1=2的条件总是被认为是错误的。

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

#5


6  

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.

这将创建一个名为供应商的新表,其中包括来自公司表的所有列,但没有来自公司表的数据。

#6


0  

Oracle:

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:

更好地理解Teradata数据库:

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

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

#7


0  

In SQL Server

在SQL Server

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

从表2中选择*到表1,其中1=2(仅为结构)

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

从表2中选择*到表1,其中1=1(数据结构)