mysql建立唯一索引,避免資料重複插入

2022-07-01 08:27:12 字數 302 閱讀 5344

多台伺服器使用乙個資料庫時,有時就會出現重複插入的情況,eg:people表中的姓名和身份證號

此時可以給姓名和身份證號建立唯一索引,

建立語句:alter table people add unique index(name,cardid);

建立後,平常的插入語句插入相同的name+cardid會報錯,

因此在插入的時候,插入語句應該為:insert ignore into people......

只要在insert語句中加入ignore關鍵字即可,再次插入相同的name+cardid時,不會報錯,只是返回0

mysql 唯一索引 mysql建立唯一索引

檢視索引 show index from 資料庫表名 alter table 資料庫add index 索引名稱 資料庫欄位名稱 primary key 主鍵索引 alter table table name add primary key column unique 唯一索引 alter tabl...

mysql建立唯一索引

檢視索引 show index from 資料庫表名 alter table 資料庫add index 索引名稱 資料庫欄位名稱 primary key 主鍵索引 alter table table name add primary key column unique 唯一索引 alter tabl...

mysql建立唯一索引

檢視索引 show index from 資料庫表名 alter table 資料庫add index 索引名稱 資料庫欄位名稱 primary key 主鍵索引 alter table table name add primary key column unique 唯一索引 alter tabl...