如何在 sqlite 中获取唯一的列指示符

How to get unique column indicator in sqlite

出于某种原因,我无法在我的 return 列表中包含“唯一”列。有什么建议吗?

此查询有效:

SELECT 
        m.tbl_name as table_name,
        il.name as key_name,
        ii.name as column_name,
        case il.origin when 'pk' then 1 else 0 END as is_primary,
        il.partial,
        il.seq as seq_in_index
    FROM sqlite_master AS m,
        pragma_index_list(m.name) AS il,
        pragma_index_info(il.name) AS ii
    WHERE 
        m.type = 'table'

但是查询失败

SELECT 
        m.tbl_name as table_name,
        il.name as key_name,
        ii.name as column_name,
        case il.origin when 'pk' then 1 else 0 END as is_primary,
        case il.unique when 1 then 1 else 0 END as is_unique,
        il.partial,
        il.seq as seq_in_index
    FROM sqlite_master AS m,
        pragma_index_list(m.name) AS il,
        pragma_index_info(il.name) AS ii
    WHERE 
        m.type = 'table'

对于 SQLite(和其他数据库)unique 是一个 keyword(保留字),因此您必须将其括在反引号或方括号或双引号内:

case il.[unique] when 1 then 1 else 0 END as is_unique 

但你也可以这样写:

il.[unique] as is_unique