以下 MySQL 结构应该正常化。它会使您的查询在某些情况下编写起来更加复杂,但它会使应用程序更强大,并且能够成倍增长而不会影响性能。我们有一个大型 MySQL 数据库,其中包含许多相关表,这些表为人们保存各种采访、笔记和其他非常棒的数据的外键!一个注意事项是,如果您使用 group 作为表名,请记住使用 `` 标记,例如:
`group`
这样 MySQL 就不会尝试使 aINNER JOIN group ON (foo=bar)
和 expect无效GROUP BY
。您还必须在应用程序的前端设置限制,以防止在没有父设备的情况下添加设备(如果这是所需的目标)。但这并不难做到。不管怎样,看看这些例子,享受有趣的实验/编程吧!
在线演示:http ://www.sqlfiddle.com/#!2/e9e94/2/0
这是建议的 MySQL 表结构,其中包含最少的数据,以说明您的问题中每个所需案例的一个实例:复制并粘贴到 .sql 文件并使用 phpMyAdmin 导入到空数据库
-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jun 07, 2013 at 08:14 PM
-- Server version: 5.5.27
-- PHP Version: 5.4.7
SET FOREIGN_KEY_CHECKS=0;
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `stackoverflow`
--
-- --------------------------------------------------------
--
-- Table structure for table `device`
--
CREATE TABLE IF NOT EXISTS `device` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `device`
--
INSERT INTO `device` (`id`) VALUES
(1),
(2),
(3),
(4),
(5),
(6);
-- --------------------------------------------------------
--
-- Table structure for table `group`
--
CREATE TABLE IF NOT EXISTS `group` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `group`
--
INSERT INTO `group` (`id`) VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8);
-- --------------------------------------------------------
--
-- Table structure for table `groups_have_devices`
--
CREATE TABLE IF NOT EXISTS `groups_have_devices` (
`group_id` int(11) NOT NULL,
`device_id` int(11) NOT NULL,
PRIMARY KEY (`group_id`,`device_id`),
KEY `device_id` (`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `groups_have_devices`
--
INSERT INTO `groups_have_devices` (`group_id`, `device_id`) VALUES
(4, 6);
-- --------------------------------------------------------
--
-- Table structure for table `groups_have_people`
--
CREATE TABLE IF NOT EXISTS `groups_have_people` (
`group_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
PRIMARY KEY (`group_id`,`person_id`),
KEY `person_id` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `groups_have_people`
--
INSERT INTO `groups_have_people` (`group_id`, `person_id`) VALUES
(1, 2),
(5, 5);
-- --------------------------------------------------------
--
-- Table structure for table `groups_have_sites`
--
CREATE TABLE IF NOT EXISTS `groups_have_sites` (
`group_id` int(11) NOT NULL,
`site_id` int(11) NOT NULL,
PRIMARY KEY (`group_id`,`site_id`),
KEY `site_id` (`site_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `groups_have_sites`
--
INSERT INTO `groups_have_sites` (`group_id`, `site_id`) VALUES
(2, 2),
(3, 4),
(6, 6),
(7, 8);
-- --------------------------------------------------------
--
-- Table structure for table `people_have_devices`
--
CREATE TABLE IF NOT EXISTS `people_have_devices` (
`person_id` int(11) NOT NULL,
`device_id` int(11) NOT NULL,
PRIMARY KEY (`person_id`,`device_id`),
KEY `device_id` (`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `people_have_devices`
--
INSERT INTO `people_have_devices` (`person_id`, `device_id`) VALUES
(1, 1),
(2, 2),
(3, 3);
-- --------------------------------------------------------
--
-- Table structure for table `person`
--
CREATE TABLE IF NOT EXISTS `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- Dumping data for table `person`
--
INSERT INTO `person` (`id`) VALUES
(1),
(2),
(3),
(4),
(5),
(6);
-- --------------------------------------------------------
--
-- Table structure for table `site`
--
CREATE TABLE IF NOT EXISTS `site` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
--
-- Dumping data for table `site`
--
INSERT INTO `site` (`id`) VALUES
(1),
(2),
(3),
(4),
(5),
(6),
(7),
(8);
-- --------------------------------------------------------
--
-- Table structure for table `sites_have_devices`
--
CREATE TABLE IF NOT EXISTS `sites_have_devices` (
`site_id` int(11) NOT NULL,
`device_id` int(11) NOT NULL,
PRIMARY KEY (`site_id`,`device_id`),
KEY `device_id` (`device_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `sites_have_devices`
--
INSERT INTO `sites_have_devices` (`site_id`, `device_id`) VALUES
(3, 4),
(4, 5);
-- --------------------------------------------------------
--
-- Table structure for table `sites_have_people`
--
CREATE TABLE IF NOT EXISTS `sites_have_people` (
`site_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
PRIMARY KEY (`site_id`,`person_id`),
KEY `person_id` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `sites_have_people`
--
INSERT INTO `sites_have_people` (`site_id`, `person_id`) VALUES
(1, 1),
(2, 3),
(5, 4),
(6, 6);
--
-- Constraints for dumped tables
--
--
-- Constraints for table `groups_have_devices`
--
ALTER TABLE `groups_have_devices`
ADD CONSTRAINT `groups_have_devices_ibfk_2` FOREIGN KEY (`device_id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `groups_have_devices_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `group` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `groups_have_people`
--
ALTER TABLE `groups_have_people`
ADD CONSTRAINT `groups_have_people_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `groups_have_people_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `group` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `groups_have_sites`
--
ALTER TABLE `groups_have_sites`
ADD CONSTRAINT `groups_have_sites_ibfk_2` FOREIGN KEY (`site_id`) REFERENCES `site` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `groups_have_sites_ibfk_1` FOREIGN KEY (`group_id`) REFERENCES `group` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `people_have_devices`
--
ALTER TABLE `people_have_devices`
ADD CONSTRAINT `people_have_devices_ibfk_2` FOREIGN KEY (`device_id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `people_have_devices_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `sites_have_devices`
--
ALTER TABLE `sites_have_devices`
ADD CONSTRAINT `sites_have_devices_ibfk_2` FOREIGN KEY (`device_id`) REFERENCES `device` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `sites_have_devices_ibfk_1` FOREIGN KEY (`site_id`) REFERENCES `site` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
--
-- Constraints for table `sites_have_people`
--
ALTER TABLE `sites_have_people`
ADD CONSTRAINT `sites_have_people_ibfk_2` FOREIGN KEY (`person_id`) REFERENCES `person` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `sites_have_people_ibfk_1` FOREIGN KEY (`site_id`) REFERENCES `site` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
SET FOREIGN_KEY_CHECKS=1;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
这是查找每个组的所有子设备的查询。
SELECT
`group`.`id` AS `group_id`,
`device`.`id` AS `device_id`
FROM
`group`
INNER JOIN groups_have_devices
ON (group.id=groups_have_devices.group_id)
INNER JOIN device
ON (groups_have_devices.device_id=device.id)
UNION ALL
SELECT
`group`.`id` AS `group_id`,
`device`.`id` AS `device_id`
FROM
`group`
INNER JOIN groups_have_people
ON (group.id=groups_have_people.group_id)
INNER JOIN person
ON (groups_have_people.person_id=person.id)
INNER JOIN people_have_devices
ON (person.id=people_have_devices.person_id)
INNER JOIN device
ON (people_have_devices.device_id=device.id)
UNION ALL
SELECT
`group`.`id` AS `group_id`,
`device`.`id` AS `device_id`
FROM
`group`
INNER JOIN groups_have_sites
ON (group.id=groups_have_sites.group_id)
INNER JOIN site
ON (groups_have_sites.site_id=site.id)
INNER JOIN sites_have_devices
ON (site.id=sites_have_devices.site_id)
INNER JOIN device
ON (sites_have_devices.device_id=device.id)
UNION ALL
SELECT
`group`.`id` AS `group_id`,
`device`.`id` AS `device_id`
FROM
`group`
INNER JOIN groups_have_sites
ON (group.id=groups_have_sites.group_id)
INNER JOIN site
ON (groups_have_sites.site_id=site.id)
INNER JOIN sites_have_people
ON (site.id=sites_have_people.site_id)
INNER JOIN person
ON (sites_have_people.person_id=person.id)
INNER JOIN people_have_devices
ON (person.id=people_have_devices.person_id)
INNER JOIN device
ON (people_have_devices.device_id=device.id)
ORDER BY
group_id
这是获取所有设备及其直接父级的查询。
SELECT
device.id AS device_id,
person.id AS person_id,
NULL AS site_id,
NULL AS group_id
FROM
device
INNER JOIN people_have_devices
ON (device.id=people_have_devices.device_id)
INNER JOIN person
ON (people_have_devices.person_id=person.id)
UNION ALL
SELECT
device.id AS device_id,
NULL AS person_id,
site.id AS site_id,
NULL AS group_id
FROM
device
INNER JOIN sites_have_devices
ON (device.id=sites_have_devices.device_id)
INNER JOIN site
ON (sites_have_devices.site_id=site.id)
UNION ALL
SELECT
device.id AS device_id,
NULL AS person_id,
NULL AS site_id,
group.id AS group_id
FROM
device
INNER JOIN groups_have_devices
ON (device.id=groups_have_devices.device_id)
INNER JOIN `group`
ON (groups_have_devices.group_id=group.id)
您可以像这样进一步获取特定个人、组或网站的直接子设备
SELECT
device_id
FROM (
SELECT
device.id AS device_id,
NULL AS person_id,
site.id AS site_id,
NULL AS group_id
FROM
device
INNER JOIN sites_have_devices
ON (device.id=sites_have_devices.device_id)
INNER JOIN site
ON (sites_have_devices.site_id=site.id)
) sub_query
WHERE
sub_query.site_id='3'