Mysql Federated Index Usage sample _mysql

Source: Internet
Author: User

This example describes the MySQL federated index. Share to everyone for your reference, specific as follows:

Employee Table UserID
Department Table DeptID
Employee Department Table

Condition: An employee can correspond to multiple departments

Question: How do I set up a database so that it cannot repeatedly add UserID and DeptID?

UUID UserID DeptID
111
212
311 (This is not allowed to add)

DROP TABLE IF EXISTS ' dept ';
CREATE TABLE ' dept ' (
 ' id ' int (one) not null auto_increment,
 ' Deptname ' char (+) 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 (one) not null auto_increment, ' name ' varchar (+) NOT NULL, PRIMARY KEY (' id ')) engin
E=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 () not NULL,
 ' EmployeeID ' int (one) not null,
 ' deptid ' int (one) 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 ');

Remarks: Creates a federated index create index Myindex on employee_dept (Employeeid,deptid);

More information about MySQL interested readers can view the site topics: "MySQL Index operation skills Summary", "MySQL Log Operation skills Encyclopedia", "MySQL Transaction operation skills Summary", "MySQL stored process skills encyclopedia", "MySQL database lock related skills summary" and " MySQL common function Big Summary "

I hope this article will help you with the MySQL database meter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.