douzhenggui8171 2017-03-06 17:06
浏览 18
已采纳

映射ER图的专业化,插入子表时重复

I have a user table that he can be either a teacher or headmaster onlyMy ER diagram. I think that disjoint specialization it is the appropriate for it in ER. I'm using MySQL dbms and after I created the tables I created a user in User table(for example UserId=31). But the problem is when I'm inserting a new record to teacher table with a UserId =31 and inserting the same UserId in headmaster table it doesn't give any error. I think this is considered a violation for disjoint property.

I have a user table with this attributes

User(UserId, Username, Password, gender, name, address, education, university, mobile).

and teacher table that it comes after mapping like this

teacher(UserId,courses).

and headmaster table. headmaster(UserId,Years_Of_Service).

When I had created teacher and headmaster tables I created them without a primary key, then I added an index for UserId attribute in each table and make it reference to UserId in User table.

My Question: How I can implement the disjoint for my tables using MySQL?

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douhe6255 2017-03-07 05:55
    关注

    One way of doing it is to add type indicators to each of your tables, as well as FK constraints that include the type indicators.

    I'm just going to show the supertype as well as one subtype table.

    CREATE TABLE `User` (
      `UserID` bigint(21) NOT NULL,
      `UserType` enum('H', 'T') NOT NULL,
      ...
      PRIMARY KEY (`UserID`),
      UNIQUE KEY (`UserID`,`UserType`)
    ) ENGINE=InnoDB;
    
    CREATE TABLE `Teacher` (
      `UserID` bigint(21) NOT NULL,
      `UserType` enum('H', 'T') NOT NULL,
      ...
      PRIMARY KEY (`UserID`),
      FOREIGN KEY (`UserID`, `UserType`) REFERENCES `User` (`UserID`, `UserType`) ON UPDATE CASCADE
    ) ENGINE=InnoDB;
    

    Note that the UserType column must be defined the same way in supertype and subtype tables in order to create an FK constraint. I added a unique key to the User table to support a composite FK constraint in the subtypes. To restrict the type indicator in each subtype table, we then create triggers:

    DELIMITER ;;
    
    CREATE TRIGGER check_teacher_insert BEFORE INSERT ON Teacher
        FOR EACH ROW
        BEGIN
            IF new.UserType != 'T' THEN
                SIGNAL SQLSTATE '45000'   
                SET MESSAGE_TEXT = 'Invalid UserType in Teacher';
            END IF;
        END;
    ;;
    
    CREATE TRIGGER check_teacher_update BEFORE UPDATE ON Teacher
        FOR EACH ROW
        BEGIN
            IF new.UserType != 'T' THEN
                SIGNAL SQLSTATE '45000'   
                SET MESSAGE_TEXT = 'Invalid UserType in Teacher';
            END IF;
        END;
    ;;
    
    DELIMITER ;
    

    An alternative approach is to leave out the type indicators, and just use triggers:

    DELIMITER ;;
    
    CREATE TRIGGER check_teacher_insert BEFORE INSERT ON Teacher
        FOR EACH ROW
        BEGIN
            IF EXISTS (
                SELECT 1
                FROM Headmaster
                WHERE UserID = new.UserID
            ) THEN
                SIGNAL SQLSTATE '45000'   
                SET MESSAGE_TEXT = 'Invalid UserID in Teacher - already exists in HeadMaster';
            END IF;
        END;
    ;;
    
    CREATE TRIGGER check_teacher_update BEFORE UPDATE ON Teacher
        FOR EACH ROW
        BEGIN
            IF EXISTS (
                SELECT 1
                FROM Headmaster
                WHERE UserID = new.UserID
            ) THEN
                SIGNAL SQLSTATE '45000'   
                SET MESSAGE_TEXT = 'Invalid UserID in Teacher - already exists in HeadMaster';
            END IF;
        END;
    ;;
    
    DELIMITER ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)