mysql中的_rowid
前言
在Oracle
資料庫的表中的每一行資料都有一個唯一的識別符號,稱為rowid
,在Oracle
內部通常就是使用它來訪問資料的。
而在MySQL
中也有一個類似的隱藏列_rowid
來標記唯一的標識。但是需要注意_rowid
並不是一個真實存在的列,其本質是一個非空唯一列
的別名。
PS:本文是基於MySQL 5.7
進行研究的
_rowid到底是什麼
在前文提到了_rowid
並不是一個真實存在的列,其本質是一個非空唯一列
的別名。為什麼會這麼說呢?
因為在某些情況下_rowid
是不存在的,其只存在於以下情況:
-
當表中存在一個數字型別
的單列主鍵時,
_rowid
其實就是指的是這個主鍵列 -
當表中不存在主鍵
但存在一個數字型別
的非空唯一列
時,
_rowid
其實就是指的是對應非空唯一列 。
需要注意以下情況是不存在_rowid
的
- 主鍵列 或者非空唯一列 的型別不是數字型別
- 主鍵 是聯合主鍵
- 唯一 列不是非空的。
詳情可以參考MySQL
官方文件
內容:
If a table has a PRIMARY KEY or UNIQUE NOT NULL index that consists of a single column that has an integer type, you can use_rowid to refer to the indexed column in SELECT statements, as follows:
- _rowid refers to the PRIMARY KEY column if there is a PRIMARY KEY consisting of a single integer column. If there is a PRIMARY KEY but it does not consist of a single integer column,_rowid cannot be used.
- Otherwise,_rowid refers to the column in the first UNIQUE NOT NULL index if that index consists of a single integer column. If the first UNIQUE NOT NULL index does not consist of a single integer column,_rowid cannot be used.