本篇文章給大家分享的是有關(guān)怎么在mysql中設(shè)置多個(gè)主鍵,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話(huà)不多說(shuō),跟著小編一起來(lái)看看吧。

實(shí)現(xiàn)方式:
表結(jié)構(gòu)不用動(dòng)。一個(gè)主鍵Id 加索引實(shí)現(xiàn)

如圖類(lèi)型設(shè)置索引類(lèi)型為Unique 選擇欄位,命個(gè)名就行。索引方式btree 就好。ok啦~
補(bǔ)充:mysql實(shí)現(xiàn)多表主鍵不重復(fù)
同一個(gè)數(shù)據(jù)庫(kù)中有兩張表,里面字段都是一樣,只是因?yàn)榇娴臄?shù)據(jù)要區(qū)分開(kāi)。但是主鍵不能重復(fù)。具體實(shí)現(xiàn)如下:
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='用戶(hù)表' 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;
新增一個(gè)序列:
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;
查詢(xún)當(dāng)前值:
select currval('seq_test');創(chuàng)建nextval函數(shù),用于獲取序列下一個(gè)值:
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;
查詢(xún)下一個(gè)值
select nextval('seq_test');<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>

以上就是怎么在mysql中設(shè)置多個(gè)主鍵,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站欄目:怎么在mysql中設(shè)置多個(gè)主鍵-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://www.chinadenli.net/article6/iipig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、定制開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、移動(dòng)網(wǎng)站建設(shè)、建站公司、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容