// system & session reference

SQL ROWID pseudocolumn

ROWID is the address of a row: where it is stored (Oracle, PostgreSQL's ctid) or its internal integer key (SQLite). It is fast to look up and not meant to be kept.

카테고리
System & session
Returns
ROWID (Oracle), integer (SQLite), tid (PostgreSQL)
In this category
10 of 13

구문

ROWID  -- Oracle, SQLite
ctid  -- PostgreSQL

Each block is a separate form. The support table and the examples show which engine takes which.

엔진 지원

엔진Support메모
SQL Server Not built in no documented row address
PostgreSQL Different name or syntax ctid (block, tuple); changes on every UPDATE
MySQL Not built in none; InnoDB stores rows in primary key order
SQLite 예 rowid, also _rowid_ and oid; INTEGER PRIMARY KEY is an alias for it
Oracle 예 ROWID pseudocolumn

예시

SQLite

CREATE TABLE tags (name TEXT);
INSERT INTO tags VALUES ('a'), ('a'), ('b');
DELETE FROM tags WHERE rowid NOT IN (SELECT MIN(rowid) FROM tags GROUP BY name);
SELECT rowid, name FROM tags;  -- 1 a, 3 b

PostgreSQL

CREATE TABLE tags (name text);
INSERT INTO tags VALUES ('a');
SELECT ctid FROM tags;  -- (0,1)
UPDATE tags SET name = 'b';
SELECT ctid FROM tags;  -- (0,2)

알아 두면 좋은 정보

  • PostgreSQL's ctid changes each time the row is updated, because UPDATE writes a new row version. Never store it.
  • Oracle ROWIDs change when rows move: ALTER TABLE ... MOVE, partition row movement, or export and reimport.
  • In SQLite an INTEGER PRIMARY KEY column is the rowid, so SELECT rowid returns the same values under the column's name. Tables created WITHOUT ROWID have none.
  • The classic use is removing duplicates: keep MIN(rowid) per group in Oracle and SQLite. For PostgreSQL use ROW_NUMBER() over the key columns instead.

Questions about ROWID

Does SQL Server have ROWID?

Not as a documented feature. Give the table a key (an IDENTITY column or a natural key) and use ROW_NUMBER() when you need to tell duplicate rows apart.

Can I use ROWID as a primary key?

In SQLite, yes: declare id INTEGER PRIMARY KEY and it is the rowid. In Oracle and PostgreSQL, no, because the address can change.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.