Created
September 22, 2015 07:36
-
-
Save settermjd/f9793ea7a90a1febc7e2 to your computer and use it in GitHub Desktop.
Query across multiple databases in MySQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP TABLE IF EXISTS `departments`; | |
CREATE TABLE `departments` ( | |
`dept_no` char(4) NOT NULL, | |
`dept_name` varchar(40) NOT NULL, | |
PRIMARY KEY (`dept_no`), | |
UNIQUE KEY `dept_name` (`dept_name`), | |
KEY `idx_deptno` (`dept_no`) USING BTREE | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
DROP TABLE IF EXISTS `dept_emp`; | |
CREATE TABLE `dept_emp` ( | |
`emp_no` int(11) NOT NULL, | |
`dept_no` char(4) NOT NULL, | |
`from_date` date NOT NULL, | |
`to_date` date NOT NULL, | |
PRIMARY KEY (`emp_no`,`dept_no`), | |
KEY `emp_no` (`emp_no`), | |
KEY `dept_no` (`dept_no`), | |
CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) REFERENCES `employees` (`emp_no`) ON DELETE CASCADE, | |
CONSTRAINT `dept_emp_ibfk_2` FOREIGN KEY (`dept_no`) REFERENCES `departments` (`dept_no`) ON DELETE CASCADE | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
DROP TABLE IF EXISTS `employees`; | |
CREATE TABLE `employees` ( | |
`emp_no` int(11) NOT NULL, | |
`birth_date` date NOT NULL, | |
`first_name` varchar(14) NOT NULL, | |
`last_name` varchar(16) NOT NULL, | |
`gender` enum('M','F') NOT NULL, | |
`hire_date` date NOT NULL, | |
PRIMARY KEY (`emp_no`), | |
KEY `idx_lastname_firstname` (`last_name`,`first_name`) USING BTREE, | |
KEY `idx_hiredate` (`hire_date`) USING BTREE, | |
KEY `idx_birthdate` (`birth_date`) USING BTREE, | |
KEY `idx_empno` (`emp_no`) USING BTREE | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select emp.emp_no, emp.first_name, emp.last_name, dept.dept_name | |
from employees.employees emp, homestead.departments dept, homestead.dept_emp de | |
where (de.dept_no = dept.dept_no and de.emp_no = emp.emp_no) | |
limit 1, 20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment