Rahul Rawat
Rahul Rawat

Reputation: 1

Why the sql is giving result even if the column doesn't exist in the table?

I am using MySql in SQLite3 where i just want to check if a column exists in a table or not? For this i used the command Select and try Catch but whenever i am giving the columns name in numbers like 1,'2' "3" like this it return the columns with same numbers like 1,2 ,3 without showing errors why?

select 20 from Student;

20
20
20
20
20
20

shows 20 till number of rows exists in my actual Student table.

Upvotes: 0

Views: 122

Answers (2)

Osher Shuman
Osher Shuman

Reputation: 215

SHOW COLUMNS FROM `table` LIKE 'fieldname';

For MySQL this will return a column's information if the column exists where table is your table name and 'fieldname' is the column name. It will return nothing if the column does not exist

Upvotes: 0

FanoFN
FanoFN

Reputation: 7114

I learned that it's not a good thing to set column names with numerical value but if your column names are indeed in numerical values, you should add backticks\backquotes to your query. For example:

SELECT `20` FROM Student;

This way you'll get the following error if the column doesn't exist:

Error Code: 1054
Unknown column '20' in 'field list'

Upvotes: 1

Related Questions