-- 更改表 ALTER TABLE `sys_user` DROP COLUMN `company_id`; -- 新建表 CREATE TABLE `company_department` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(32) NOT NULL COMMENT '部门名称' COLLATE 'utf8_general_ci', `company_id` INT(11) NOT NULL COMMENT '单位id', `parent_id` INT(11) NULL DEFAULT NULL COMMENT '父级id', PRIMARY KEY (`id`), INDEX `FK_company_department_company` (`company_id`), INDEX `FK_company_department_company_department` (`parent_id`), CONSTRAINT `FK_company_department_company` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE CASCADE, CONSTRAINT `FK_company_department_company_department` FOREIGN KEY (`parent_id`) REFERENCES `company_department` (`id`) ON DELETE SET NULL ) COMMENT='单位部门信息' COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `company_position` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(32) NOT NULL COMMENT '职位名称' COLLATE 'utf8_general_ci', `company_id` INT(11) NOT NULL COMMENT '单位id', PRIMARY KEY (`id`), INDEX `FK_company_position_company` (`company_id`), CONSTRAINT `FK_company_position_company` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE CASCADE ) COMMENT='单位职位信息' COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `company_user` ( `company_id` INT(11) NOT NULL COMMENT '公司id', `user_id` BIGINT(20) NOT NULL COMMENT '用户id', `admin` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '公司管理员', UNIQUE INDEX `user_id` (`user_id`), INDEX `FK_company_user_company` (`company_id`), CONSTRAINT `FK_company_user_company` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE CASCADE, CONSTRAINT `FK_company_user_sys_user` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`id`) ON DELETE CASCADE ) COMMENT='单位用户关系' COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `department_user` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT, `user_id` BIGINT(20) NOT NULL COMMENT '用户id', `department_id` INT(11) NOT NULL COMMENT '部门id', `position_id` INT(11) NULL DEFAULT NULL COMMENT '职位id', `manager` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '部门管理员', `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', PRIMARY KEY (`id`), INDEX `FK_department_user_company_position` (`position_id`), INDEX `FK_department_user_sys_user` (`user_id`), INDEX `FK_department_user_company_department` (`department_id`), CONSTRAINT `FK_department_user_company_department` FOREIGN KEY (`department_id`) REFERENCES `company_department` (`id`) ON DELETE CASCADE, CONSTRAINT `FK_department_user_company_position` FOREIGN KEY (`position_id`) REFERENCES `company_position` (`id`) ON DELETE SET NULL, CONSTRAINT `FK_department_user_sys_user` FOREIGN KEY (`user_id`) REFERENCES `sys_user` (`id`) ON DELETE CASCADE ) COMMENT='用户、部门关联表' COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `department_lesson` ( `lesson_id` BIGINT(20) NOT NULL COMMENT '课程id', `department_id` INT(11) NOT NULL COMMENT '班级id', INDEX `lesson_id` (`lesson_id`), INDEX `department_lesson_ibfk_1` (`department_id`), CONSTRAINT `department_lesson_ibfk_1` FOREIGN KEY (`department_id`) REFERENCES `company_department` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, CONSTRAINT `department_lesson_ibfk_2` FOREIGN KEY (`lesson_id`) REFERENCES `ls_lesson` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE ) COMMENT='班级课程表' COLLATE='utf8_general_ci' ENGINE=InnoDB ; CREATE TABLE `department_exam` ( `exam_id` BIGINT(20) NOT NULL, `department_id` INT(11) NOT NULL, INDEX `exam_id` (`exam_id`), INDEX `department_exam_ibfk_2` (`department_id`), CONSTRAINT `department_exam_ibfk_1` FOREIGN KEY (`exam_id`) REFERENCES `exam_definition` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, CONSTRAINT `department_exam_ibfk_2` FOREIGN KEY (`department_id`) REFERENCES `company_department` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE ) COMMENT='学生的试卷和班级关系' COLLATE='utf8_general_ci' ENGINE=InnoDB ; -- 删除表 DROP TABLE `user_company_rel`;