mysql 實現(xiàn)設置多個主鍵的操作
user表,身份證號碼要唯一,手機號碼,郵箱要唯一
實現(xiàn)方式:表結(jié)構(gòu)不用動。一個主鍵Id 加索引實現(xiàn)
如圖類型設置索引類型為Unique 唯一 選擇欄位,命個名就行。索引方式btree 就好。ok啦~
補充:mysql實現(xiàn)多表主鍵不重復
同一個數(shù)據(jù)庫中有兩張表,里面字段都是一樣,只是因為存的數(shù)據(jù)要區(qū)分開。但是主鍵不能重復。具體實現(xiàn)如下:
新建數(shù)據(jù)庫 mytest新建user表和admin表CREATE TABLE `user` ( `user_id` INT(11) NOT NULL, `user_name` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `phone` VARCHAR(255) NOT NULL, PRIMARY KEY (`user_id`))COMMENT=’用戶表’COLLATE=’utf8_general_ci’ENGINE=InnoDB;
CREATE TABLE `admin` ( `user_id` INT(11) NOT NULL, `user_name` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NOT NULL, `phone` VARCHAR(255) NOT NULL, PRIMARY KEY (`user_id`))COMMENT=’管理員表’COLLATE=’utf8_general_ci’ENGINE=InnoDB;
新建序列表:
CREATE TABLE `sequence` ( `seq_name` VARCHAR(50) NOT NULL, `current_val` INT(11) NOT NULL, `increment_val` INT(11) NOT NULL DEFAULT ’1’, PRIMARY KEY (`seq_name`))COMMENT=’序列表’COLLATE=’utf8_general_ci’ENGINE=InnoDB;
新增一個序列:
INSERT INTO sequence VALUES (’seq_test’, ’0’, ’1’);
創(chuàng)建currval函數(shù),用于獲取序列當前值:
delimiter #create function currval(v_seq_name VARCHAR(50)) returns integer(11) begin declare value integer; set value = 0; select current_val into value from sequence where seq_name = v_seq_name; return value;end;
查詢當前值:
select currval(’seq_test’);
創(chuàng)建nextval函數(shù),用于獲取序列下一個值:
delimiter #create function nextval (v_seq_name VARCHAR(50)) returns integer(11) begin update sequence set current_val = current_val + increment_val where seq_name = v_seq_name; return currval(v_seq_name);end;
查詢下一個值
select nextval(’seq_test’);具體實現(xiàn):
<insert parameterType='User'> <selectKey keyProperty='userId' resultType='int' order='BEFORE'> select nextval(’seq_test’); </selectKey> insert into user(user_id,user_name,password,phone) values (#{userId},#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, #{phone, jdbcType=VARCHAR}) </insert>
<insert parameterType='Admin'> <selectKey keyProperty='userId' resultType='int' order='BEFORE'> select nextval(’seq_test’); </selectKey> insert into admin(user_id,user_name,password,phone) values (#{userId},#{userName, jdbcType=VARCHAR},#{password, jdbcType=VARCHAR}, #{phone, jdbcType=VARCHAR}) </insert>最終實現(xiàn):
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. Window7安裝MariaDB數(shù)據(jù)庫及系統(tǒng)初始化操作分析2. MariaDB的安裝與配置教程3. SQLite教程(十二):鎖和并發(fā)控制詳解4. Centos7 下mysql重新啟動MariaDB篇5. MariaDB性能調(diào)優(yōu)工具mytop的使用詳解6. centos 7安裝mysql5.5和安裝 mariadb使用的命令7. access不能打開注冊表關(guān)鍵字錯誤處理方法(80004005錯誤)8. SQL案例學習之字符串的合并與拆分方法總結(jié)9. SQLite 性能優(yōu)化實例分享10. MariaDB數(shù)據(jù)庫的外鍵約束實例詳解
