共計(jì) 2000 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。
自動(dòng)寫代碼機(jī)器人,免費(fèi)開通
今天丸趣 TV 小編給大家分享的是 mysql 隱藏字段 rowid 在什么時(shí)候是可見的,相信很多人都不太了解,為了讓大家更加了解,所以給大家總結(jié)了以下內(nèi)容,一起往下看吧。一定會(huì)有所收獲的哦。
通過我自己的一番實(shí)戰(zhàn),可以確定的是,只要?jiǎng)?chuàng)建表,這個(gè) rowid 一定是存在的,唯一區(qū)別就是顯示和隱士的區(qū)別,也就是是否可以通過 select _rowid from table 查詢出來
那么問題來了,哪些情況下 rowid 是顯示的?1 、當(dāng)表中有主鍵并且是數(shù)值型的時(shí)候才是顯示的
2、當(dāng)表中沒有主鍵的時(shí)候,但是表中有唯一非空并且是數(shù)值型的時(shí)候才是顯示的
接下來,創(chuàng)建表來實(shí)戰(zhàn)看下,是否是這樣的
先創(chuàng)建一個(gè)帶有主鍵并且是數(shù)值型的表
create table z1(id bigint(20) primary key
)engine=innodb;
再創(chuàng)建一個(gè)帶有主鍵不是數(shù)值型的表 (雖然業(yè)務(wù)不會(huì)這樣創(chuàng)建,只是為了證明 rowid)
create table z2(name varchar(20) primary key
)engine=innodb;
再創(chuàng)建一個(gè)沒有主鍵但是有唯一鍵并且是數(shù)值型非空的表
create table z3( name int(11) not null,
unique(name)
)engine=innodb charset=utf8
此時(shí)再創(chuàng)建一個(gè)沒有主鍵,并且有唯一鍵,但是可以為空或者不是數(shù)值型的表
create table z4( name varchar(11) not null,
unique(name)
)engine=innodb charset=utf8;
create table z5( name int(11) ,
unique(name)
)engine=innodb charset=utf8;
再來看看官網(wǎng)咋說的,再理解下
官網(wǎng)連接:https://dev.mysql.com/doc/refman/5.7/en/create-index.html
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.
此時(shí)我再創(chuàng)建一個(gè)表,表中只有一個(gè)字段,并且是字符串類型的,看下生成的隱式 rowid,達(dá)到最大值會(huì)發(fā)生什么?(注意此時(shí)底層會(huì)默認(rèn)生成一個(gè) 6 字節(jié)的指針,最大值為 2^48 次冪)
此時(shí)用 gdb 工具讓 rowid 達(dá)到最大值再插入看看會(huì)怎么樣? 答:先找到 mysqld 的進(jìn)程 pid,命令
ps aux | grep mysqld
gdb -p 你的 mysql 的 pid -ex p dict_sys- row_id=1 -batch
可以看到此時(shí)插入了 3 條數(shù)據(jù)
這個(gè)時(shí)候把 rowid 變?yōu)?2^48 次冪之后,再插入看下效果
gdb -p 29410 -ex p dict_sys- row_id=281474976710656 -batch
此時(shí)再插入三條數(shù)據(jù),此時(shí) a1 a2 被覆蓋了,所以在不滿足上述二種情況的時(shí)候,生成的隱式 rowid 在用盡之后,之前的記錄會(huì)被覆蓋,所以創(chuàng)建表一定要有主鍵 id,避免發(fā)生覆蓋,雖然概率比較低,這個(gè)只是用主鍵的其中一個(gè)原因哈
所以綜上所述:看我 xmind 那個(gè)總結(jié),自己再理解消化下吧。
關(guān)于 mysql 隱藏字段 rowid 在什么時(shí)候是可見的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的參考價(jià)值,可以學(xué)以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。
向 AI 問一下細(xì)節(jié)正文完