简体   繁体   中英

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").

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM