Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this query mean?

Tags:

sql

I'm reading a book about SQL.

In that book, I saw strange query below:

SELECT * into mycustomer from customer WHERE 1=2

In this query, what is "WHERE 1=2" ?

like image 346
zihado Avatar asked Mar 18 '10 06:03

zihado


People also ask

What does query mean in it?

What is a query? A query is a question or a request for information expressed in a formal manner. In computer science, a query is essentially the same thing, the only difference is the answer or retrieved information comes from a database.

What is query give an example?

Query is another word for question. In fact, outside of computing terminology, the words "query" and "question" can be used interchangeably. For example, if you need additional information from someone, you might say, "I have a query for you." In computing, queries are also used to retrieve information.

What does query mean in code?

Download Glossary For Web Beginners. In standard English, a query means a request for information. In computer programming, it refers to the same thing, except the information is retrieved from a database. However, writing a query requires a set of pre-defined code to make the database understand the instruction.

What is query in very short answer?

A query can either be a request for data results from your database or for action on the data, or for both. A query can give you an answer to a simple question, perform calculations, combine data from different tables, add, change, or delete data from a database.


3 Answers

Usually used to copy structure of one table to into another, as in your case.

SELECT * INTO mycustomer FROM customer WHERE 1=2

This code creates an identical structure of table Customer in your new table MyCustomer. Note that in SQL Server, the constraints are not copied; so probably you would need to recreate the constraints.

like image 51
KMån Avatar answered Oct 17 '22 16:10

KMån


1=2 will always be false.

This is a way to specify a WHERE clause that will always evaluate to false.

A similar thing is WHERE 1=1 which always evaluates to true.

like image 34
codaddict Avatar answered Oct 17 '22 16:10

codaddict


Back in the old days when I was using classic ASP, I used the "WHERE 1=2" structure to retrieve the column definitions of the table and not its contents. Nowadays there are better ways of retrieving column definitions by using an object-relation mapping framework.

My guess is that the book you are reading is slightly outdated, or the context of this query is misplaced.

like image 1
Prutswonder Avatar answered Oct 17 '22 15:10

Prutswonder