MySQL建立自關聯表格舉例

來源:互聯網
上載者:User

標籤:

注意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建立自關聯表格舉例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.