Column doens't exists in PostgreSQL (WHERE column_name = column_value) [duplicate] - sql

This question already has answers here:
delete "column does not exist"
(1 answer)
Simple Postgresql Statement - column name does not exists
(2 answers)
Column 'mary' does not exist
(2 answers)
postgres - where in (list) - column does not exist
(2 answers)
Closed 2 years ago.
I have the following table in PostgreSQL:
id and grade are INTs, note and subject both VARCHARs
When I run the command:
SELECT * FROM grades
WHERE subject = "latin";
I get the following error:
In pgAdmin4:
ERROR: column "latin" does not exist
LINE 2: WHERE subject = "latin"
^
SQL state: 42703
Character: 37
And in cmd:
ERROR: column "latin" does not exist
LINE 1: SELECT * FROM upisi WHERE subject = "latin";
I'm coming from MySQL so I thought this would work.
Works fine if I put grade = something in the WHERE clause. Any idea why this might be the case?

You need single quotes for character constants. (double quotes are used to quote identifiers)
SELECT * FROM grades
WHERE subject = 'latin';
If you use WHERE subject = "latin", the DBMS expects "latin" to be a column name, which it is not.

It's as simple as the wrong type of quote marks. You wanted:
SELECT * FROM grades
WHERE subject = 'latin';
To explain:
Single quotes, like 'latin', are the standard way to write a string in standard SQL, and should work on all DBMSes.
Double quotes, in Postgres and some other DBMSes, are a way of quoting identifiers - so if your column name had a space in for some reason (there's not many good reasons to do it, but it's possible), then you might write SELECT * FROM grades WHERE "subject name" = 'latin' - "subject name" is the name of the column, 'latin' is a string.
Although double quotes are in the SQL standard, other DBMSes use different syntax for quoting identifiers, so may treat double quotes as an alternative for writing strings.
-- Postgres (also works in Microsoft SQL Server, but isn't the default style)
SELECT * FROM grades WHERE "subject name" = 'latin'
-- MySQL
SELECT * FROM grades WHERE `subject name` = 'latin'
-- Microsoft SQL Server
SELECT * FROM grades WHERE [subject name] = 'latin'
But if you always use single quotes for strings, and avoid names that need quoting, you'll run into fewer problems.
-- Works pretty much everywhere
SELECT * FROM grades WHERE subject = 'latin'

Related

Postgresql Column Doesn't Exist [duplicate]

This question already has answers here:
sql statement error: "column .. does not exist"
(1 answer)
PostgreSQL "Column does not exist" but it actually does
(6 answers)
ERROR: column of relation does not exist PostgreSQL ,Unable to run insert query
(3 answers)
Postgresql Column Not Found, But Shows in Describe
(1 answer)
Closed 4 years ago.
I'm running a simple select query:
SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMIT 10;
And getting the following error:
ERROR: column return_part_i.cntrcttrmntnind does not exist LINE 1:
SELECT return_part_i.CntrctTrmntnInd FROM return_part_i LIMI...
^ HINT: Perhaps you meant to reference the column "return_part_i.CntrctTrmntnInd". SQL state: 42703 Character: 8
I have tried the query with and without the table identifier. I am copying the field name directly from the hint. This is happening with numerous fields in the database.
if you really have a camel case in you column name then you must wrap the column name with double quote
SELECT "CntrctTrmntnInd" FROM return_part_i LIMIT 10;
PostgreSQL columns (object) name are case sensitive when specified with double quotes. Unquoted identifiers are automatically used as lowercase so the correct case sequence must be write with double quotes
and as correctly suggested by Raymond Nijland if you want a LIMIT in result you should use an order by
SELECT "CntrctTrmntnInd" FROM return_part_i ORDER BY "CntrctTrmntnInd" LIMIT 10;

Why can't postgresql execute select statement whose 'where' clause has a key-matching predicate? [duplicate]

This question already has an answer here:
Strange behaviour in Postgresql
(1 answer)
Closed 6 years ago.
select * from note where noteKey = 1
This statement gives me the following error.
ERROR: column "notekey" does not exist
LINE 1: select * from note where noteKey = 1
^
HINT: Perhaps you meant to reference the column "note.noteKey".
I tried note.noteKey, but got the same error. (I used postico by the way, and the noteKey is the primary key of the table note).
What could possibly be wrong with the statement?
You need to connect with psql and run \d note.
If that shows the column is called noteKey with an uppercase K then you either have two options
Rename the column ALTER TABLE note RENAME COLUMN "noteKey" TO notekey;
Forever use double quotes " to query it.
In PostgreSQL we never quote column names. This forces them to be caps sensitive. When not quoted, they're automatically lower cased.
This
select * from note where noteKey = 1
Is the same as
select * from note where notekey = 1
Which is different from
select * from note where "noteKey" = 1

Postgresql escape single quote in where clause [duplicate]

This question already has answers here:
Insert text with single quotes in PostgreSQL
(8 answers)
Closed 3 years ago.
so I am trying to run a script like this one:
select id
from owner
where owner.name = "john's"
and I am getting this error: ERROR: column "john's" does not exist.
Also I tried like this: where owner.name = 'john\'s', but it dit not work
Anyone knows how I can run a query like this one?
You can escape single quotes when you double them.
For example:
= 'john''s'
Try this
select id
from owner
where owner.name = (E'john\'s')::text
Update:
we can escape most of the characters using this statement
select id
from owner
where owner.name = (E'john\character you want to escape's')::text

What's wrong with my sql in oracle?

What's wrong with my sql in oracle? There are some data in my table,I select all of them and I can get them.but I can not search them if I add a condition.When did I add single or double quotation marks in my sql?Now I find that when I write some search statement,I must add single quotation marks.And when I write some insert statement,I must add double quotation marks.Or my sql will run bad.How to judge when should I use the different quotation in my sql?
select * from T_STUDENT
and the result is:
sex(varchar2) phone(varchar2) birthtime(timestamp)
1 13553812147 2016-06-03 16:02:00.799 **
When I add a search condition,but the result is null.
//error:ORA-000904
select * from T_STUDENT where phone='13553812147'
//error:ORA-000904
select * from T_STUDENT where PHONE='13553812147'
//run well but result is null
select * from T_STUDENT where 'phone'='13553812147'
And the same question I meet in the insert statement.
//error:ORA-000904
insert into T_STUDENT (sex,phone,birthtime) values('1','12345645454','2016-06-04 16:02:00.799')
//error:ORA-000928 missing select keyword
insert into T_STUDENT ('sex','phone','birthtime') values('1','12345645454','2016-06-04 16:02:00.799')
//run well but must add double quotation marks
insert into T_STUDENT ("sex","phone","birthtime") values('1','12345645454','2016-06-04 16:02:00.799')
This is because your table was defined using double quotes around the column names:
create table t_student
( "sex" varchar2(1)
, "phone" varchar2(30)
, "birthtime" timestamp
);
Using double quotes makes the names case-sensitive and so for ever after they must be referenced in double quotes, since by default Oracle is nicely case-insensitive. For this reason you should never use double quotes when creating tables, views etc.
I've had the chance to look at this and Tony Andrews' answer is correct, you actually get invalid identifier as error message when you type an invalid column, which includes a case mismatch, though the error message will mention the exact identifier:
SQL> select * from T_STUDENT where phone='13553812147';
select * from T_STUDENT where phone='13553812147'
*
ERROR at line 1:
ORA-00904: "PHONE": invalid identifier
SQL> select * from T_STUDENT where funny_bunny='13553812147';
select * from T_STUDENT where funny_bunny='13553812147'
*
ERROR at line 1:
ORA-00904: "FUNNY_BUNNY": invalid identifier
The only thing missing from his answer is that Oracle will always make an internal cast to uppercase for any unquoted identifier, as the full error message illustrates. That's why phone='13553812147' won't match a column defined as "phone" (but "phone"='13553812147' will do).
Last but not least, single quotes define plain strings rather than object names so when you do this:
select * from T_STUDENT where 'phone'='13553812147'
... you aren't filtering by phone column at all. Instead, you have a constant condition that's always false (text "phone" equals text "13553812147").

Select statement don't work with double quotation in Oracle [duplicate]

This question already has answers here:
How to deal with SQL column names that look like SQL keywords?
(17 answers)
Closed 10 years ago.
My table has column called select (or NOT, or other reserved keys).
I tried select 'select' column but I couldn't because oracle throw exception like this:
invalid user.table.column or table.column or column specification
My select statement like this:
select f.select
from foo f.
I was searching internet and I found accepted answer: How do I escape a reserved word in Oracle?
then, I changed my query: Select "f"."select" from foo f, but throw exception too like this:
"c"."select": invalit identifier.
How to use "select" column in select (or update, insert) statement?
P.S: I'm using pl/sql developer tool for querying data.
Try,
Select f."select" from foo f;
When identifiers are quoted in Oracle, they become case sensitive.
So it's likely that you have to write the following:
select f."SELECT" from foo f
(or "Select", or however you've actually written the column name)
This should work fine:
Select f."select" from foo f