视图没有 rowid 值

Phi*_*Phi 5 sqlite sql-view

视图没有 rowid,这是预期的吗?例子:

create table t2 (u text);
insert into t2 values ('x');
insert into t2 values ('y');
create table t1 (t text, i integer);
insert into t1 values ('a',1);
insert into t1 values ('b',2);
insert into t1 values ('c',1);
insert into t1 values ('d',2);
create view v as select * from t1 join t2 on t1.i=t2.rowid;
select rowid,* from t1;
ro t  i
-- -- --
1  a  1
2  b  2
3  c  1
4  d  2
select rowid,* from v;
ro t  i  u
-- -- -- ----------
   a  1  x
   b  2  y
   c  1  x
   d  2  y
Run Code Online (Sandbox Code Playgroud)

我通过在 ID=rowid 的位置添加“ID”列来解决此问题t1。在 SQLite VIEW 网页上,我没有发现对 rowid 的引用。

我想知道我是否滥用了这个观点。该视图用于呈现链接多个相关表的单个表。我需要通过其“rowid”访问视图,因为我的表是一次写入(通过数据提取器)然后是只读的。数据提取器知道它输入的内容和 rowid,然后避免创建冗余列。

CL.*_*CL. 3

rowid值标识表行;视图中不存储行。

要识别视图中使用的表中的行,您只需rowid在视图中包含该表中的值即可:

CREATE VIEW v AS SELECT t1.rowid, ... FROM t1 ...;
Run Code Online (Sandbox Code Playgroud)

无论如何,声明为INTEGER PRIMARY KEY 的列是 的别名rowid(因此不需要额外的存储)。如果您确实想使用这些rowid值,最好在表定义中显式包含这样的列:

CREATE TABLE t1 (
    id INTEGER PRIMARY KEY,
    t TEXT,
    i INTEGER
);
Run Code Online (Sandbox Code Playgroud)

(它的行为仍然与rowid; 您通过向其中插入 NULL 值来获取自动增量值。)