sqlite 视图没有rowid值

nukf8bse  于 2023-06-30  发布在  SQLite
关注(0)|答案(1)|浏览(140)

视图没有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

我通过向t1添加一个'ID'列来解决这个问题,其中ID=rowid。在SQLite VIEW网页上,我没有发现对rowid的引用。
我想知道我是否滥用了视野。该视图将显示链接多个相关表的单个表。我需要通过它的'rowid'访问视图,因为我的表是一次写入(通过数据提取器)然后只读的。数据提取器知道它输入了什么和rowid,然后避免创建冗余列。

pdsfdshx

pdsfdshx1#

rowid值标识表行;视图中不存储行。
要识别视图中使用的表中的行,只需在视图中包含该表中的rowid值:

CREATE VIEW v AS SELECT t1.rowid, ... FROM t1 ...;

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

CREATE TABLE t1 (
    id INTEGER PRIMARY KEY,
    t TEXT,
    i INTEGER
);

(It仍然表现为rowid;通过向其中插入NULL值可以得到autoincremented values。)

相关问题