如何使用 dashDB table 中名为 _id 的列?

How can I use a column named _id from a dashDB table?

我在 Cloudant 中有一个数据库,其中的文档 ID 是 _id

将此数据从 Cloudant 复制到 dashDB 后,我有 2 个单独的表,我想使用此 _id 列加入它们。在 运行 SQL 中,我尝试了下面的方法,但这不会 运行。我在这里错过了什么?我是否需要将列名称 _id 替换为不带下划线的名称?

select m.title, m.year, g.value 
from MOVIES m
inner join MOVIES_GENRE g on m._ID = g._ID;

TL;DR:正如@gmiley 指出的,问题是由 _ID 列名引起的,它不是普通的标识符(见下面的定义)和因此需要在您的 SQL 语句中用双引号 "_ID" 或单引号 '_ID' 括起来。

select m.title, m.year, g.value 
from MOVIES m
inner join MOVIES_GENRE g on m."_ID" = g."_ID";

不同于普通标识符 引号标识符区分大小写"_id""_ID" 不同,而 title 与 [=18 相同=]).如果您要在语句中指定 "_id",则会出现错误,指示未找到该列。

既然您已经提到您已经使用 Cloudant warehousing process 来填充 DashDB 表,那么可能值得一提的是,在架构发现期间生成 DDL 时,属性 名称是大写的.

示例:具有此结构的 JSON 个文档的内容

 {
  "_id": "000018723bdb4f2b06f830f676cfafd6",
  "_rev": "1-91f98642f125315b929be5b5436530e7",
  "date_received": "2016-12-04T17:46:47.090Z",
  ...
 } 

将映射到三列:

  • _ID 类型 VARCHAR(...)
  • _REV 类型 VARCHAR(...)
  • DATE_RECEIVED 类型...
  • ...

希望对您有所帮助!


来自DB2 SQL reference

An ordinary identifier is an uppercase letter followed by zero or more characters, each of which is an uppercase letter, a digit, or the underscore character. Note that lowercase letters can be used when specifying an ordinary identifier, but they are converted to uppercase when processed. An ordinary identifier should not be a reserved word.

Examples: WKLYSAL WKLY_SAL

A delimited identifier is a sequence of one or more characters enclosed by double quotation marks. Leading blanks in the sequence are significant. Trailing blanks in the sequence are not significant, although they are stored with the identifier. Two consecutive quotation marks are used to represent one quotation mark within the delimited identifier. In this way an identifier can include lowercase letters.

Examples: "WKLY_SAL" "WKLY SAL" "UNION" "wkly_sal"