MySQL聯合索引用法樣本_Mysql

來源:互聯網
上載者:User

本文執行個體講述了MySQL聯合索引。分享給大家供大家參考,具體如下:

員工表 userid
部門表 deptid
員工部門表

條件:一個員工可以對應多個部門

問題:怎麼樣設定資料庫,讓其不能重複添加 userid 和deptid?

uuid userid deptid
111
212
311(這個就不能讓其添加)

DROP TABLE IF EXISTS `dept`;CREATE TABLE `dept` ( `id` int(11) NOT NULL AUTO_INCREMENT, `deptname` char(32) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of dept-- ----------------------------INSERT INTO `dept` VALUES ('1', '1');INSERT INTO `dept` VALUES ('2', '2');
DROP TABLE IF EXISTS `employee`;CREATE TABLE `employee` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;-- ------------------------------ Records of employee-- ----------------------------INSERT INTO `employee` VALUES ('1', '11');
DROP TABLE IF EXISTS `employee_dept`;CREATE TABLE `employee_dept` ( `id` int(11) NOT NULL, `employeeid` int(11) NOT NULL, `deptid` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `bb` (`deptid`), KEY `myindex` (`employeeid`,`deptid`), CONSTRAINT `aa` FOREIGN KEY (`employeeid`) REFERENCES `employee` (`id`), CONSTRAINT `bb` FOREIGN KEY (`deptid`) REFERENCES `dept` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;-- ------------------------------ Records of employee_dept-- ----------------------------INSERT INTO `employee_dept` VALUES ('1', '1', '1');INSERT INTO `employee_dept` VALUES ('2', '1', '2');

備忘:建立聯合索引create index myindex on employee_dept (employeeid,deptid);

更多關於MySQL相關內容感興趣的讀者可查看本站專題:《MySQL索引操作技巧匯總》、《MySQL日誌操作技巧大全》、《MySQL事務操作技巧匯總》、《MySQL預存程序技巧大全》、《MySQL資料庫鎖相關技巧匯總》及《MySQL常用函數大匯總》

希望本文所述對大家MySQL資料庫計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.