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?
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?
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.
CREATE TABLE new_table LIKE old_table
LIKE old_table
is not a SQL Server construct...
name
and type
of the column, Consider that there are some other properties also as column structure - e.g. default value, alias and etc. ;).
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 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;
LIMIT
. But it seems MSSQL lacks it (which strikes me as a good reason to just not use MSSQL). Apparently they have TOP
, but google results have so many people not using that, so there's probably some hijink surrounding it.
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;
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.
In SQL Server
select * into table1 from table2 where 1=2(Only Structure)
select * into table1 from table2 where 1=1(Structure with data)
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