简体   繁体   English

oracle中的sql有什么问题?

[英]What's wrong with my sql in oracle?

What's wrong with my sql in oracle? oracle中的sql有什么问题? 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? 我的表中有一些数据,我选择了所有这些数据就可以了。但是如果添加条件,我将无法搜索它们。何时在sql中添加单引号或双引号?现在我发现了写一些搜索语句,我必须添加单引号。当我写一些插入语句时,必须添加双引号。否则我的sql会运行不正常。如何判断何时应该在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. 当我添加搜索条件时,结果为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 . 使用双引号会使名称区分大小写,因此在以后必须使用双引号对其进行引用,因为默认情况下,Oracle很好地不区分大小写 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. 他的答案唯一缺少的是,Oracle将始终对所有未引用的标识符进行大写内部转换,如完整的错误消息所示。 That's why phone='13553812147' won't match a column defined as "phone" (but "phone"='13553812147' will do). 这就是为什么phone='13553812147'与定义为"phone"的列不匹配的原因(但是"phone"='13553812147'可以)。

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. ...您根本没有按“ phone列进行过滤。 Instead, you have a constant condition that's always false (text "phone" equals text "13553812147"). 相反,您有一个始终为false的恒定条件(文本“ phone”等于文本“ 135538​​12147”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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