user表,身份證號碼要唯一,手機號碼,郵箱要唯一
實現(xiàn)方式:
表結(jié)構(gòu)不用動。一個主鍵Id 加索引實現(xiàn)

如圖類型設(shè)置索引類型為Unique 唯一 選擇欄位,命個名就行。索引方式btree 就好。ok啦~
補充:mysql實現(xiàn)多表主鍵不重復(fù)
同一個數(shù)據(jù)庫中有兩張表,里面字段都是一樣,只是因為存的數(shù)據(jù)要區(qū)分開。但是主鍵不能重復(fù)。具體實現(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ù),用于獲取序列當(dāng)前值:
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;
查詢當(dāng)前值:
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 id="addUser" 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 id="addAdmin" 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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- MySQL中的主鍵以及設(shè)置其自增的用法教程
- 深入Mysql,SqlServer,Oracle主鍵自動增長的設(shè)置詳解
- mysql下創(chuàng)建字段并設(shè)置主鍵的php代碼