標籤:
注意sub_parent_id欄位,通過該欄位,可以將tb_point_group_tree表中的資料自關聯為一個樹形的資料結構表格。
CREATE TABLE `tb_point_group_tree` (`cgroup_id` int(11) NOT NULL DEFAULT ‘0‘,`group_Id` int(11) NOT NULL AUTO_INCREMENT,`company_Id` int(11) DEFAULT NULL,`group_name` varchar(100) DEFAULT NULL,`group_type_id` int(11) DEFAULT NULL COMMENT ‘分組類型:-1—普通分組,-2—用電類型,-3—地區用電,-4—生產流程,-5—重點裝置‘,`sub_parent_id` int(11) DEFAULT ‘0‘ COMMENT ‘分組類型子項編號為自關聯的gruoup_Id,如果是父項,則該值為0‘,PRIMARY KEY (`group_Id`),KEY `FK_Reference_40` (`company_Id`) USING BTREE,KEY `tb_sub_parent_id_FK` (`sub_parent_id`),CONSTRAINT `tb_point_group_tree_ibfk_1` FOREIGN KEY (`company_Id`) REFERENCES `tb_customer_info` (`customer_Id`),CONSTRAINT `tb_sub_parent_id_FK` FOREIGN KEY (`sub_parent_id`) REFERENCES `tb_point_group_tree` (`group_Id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT=‘每個公司可以定義多個監測組,以方便管理‘;
MySQL建立自關聯表格舉例