SQL Many-to-Many Relationship Between Multiple Tables

后端 未结 3 1192

I have a database I\'m trying to create on SQL and I am trying to connect the relationships together. There are three tables: superhero, power, and superheroPower. The tables su

3条回答
  •  没有蜡笔的小新
    2021-01-24 02:29

    Yes, everything there looks okay. But...


    A few notes:

    We'd use a shorter datatype for the gender column; I don't see that we'd need 255 characters to express that. (There is a limit on the maximum size of a row which is enforced.) If there only a few values for that, we'd consider ENUM datatype.

    We'd also likely add NOT NULL constraints on several of those columns, such as heroname, firstname, lastname. We'd also likely add DEFAULT ''. Sometimes, we really do need to allow NULL values for some reason, but we use NOT NULL wherever we can.

    I'm hesitant about the TEXT columns. There's nothing wrong with using TEXT datatype, but I'm just suspicious that those may be "hiding" some information that might better be stored in additional columns.

    For the foreign keys, we'd assign a name to the constraints, following the pattern we use, and also likely add ON UPDATE CASCADE ON DELETE CASCADE

    CONSTRAINT FK_superheroPower_power FOREIGN KEY (powerID) 
      REFERENCES power(id) ON UPDATE CASCADE ON DELETE CASCADE
    

    A note about identifiers (table names and column names)

    The way we do it, all table name are lower case. (We have a MySQL option set that forces all table names to lower case.) We do this to avoid incompatibility issues for different operating systems/filesystems (some of which are case sensitive, and some are not).

    Also, table names are singular. The name of the table names what one row of the table represents. We also don't include _table as part of the name.

    Column names in MySQL are never case sensitive, but we always use lower case for the column names as well. We don't "camelCase" our column names, we use underscore character as separators, e.g. power_id vs. powerID, hero_name vs. heroName.


    FOLLOWUP

    My "notes" above aren't specific rules that must be followed; those are just patterns we use.

    Following these patterns does not guarantee that we'll have a successful software, but it does help us.

    For your reference, I'll show how these tables would look as a "first cut" from our shop, as an illustration of another pattern; this is not "the right way", it's just "a way" that we've settled on as a team.

    CREATE TABLE superhero
    ( id               INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'pk'
    , hero_name        VARCHAR(255) NOT NULL                COMMENT ''
    , first_name       VARCHAR(255) NOT NULL DEFAULT ''     COMMENT ''
    , last_name        VARCHAR(255) NOT NULL DEFAULT ''     COMMENT ''
    , first_appearance DATE                                 COMMENT 'date superhero first appeared'
    , gender           ENUM('female','male','other')        COMMENT 'female,male or other'
    , biography_text   TEXT                                 COMMENT ''
    , universe         VARCHAR(255)                         COMMENT ''
    , PRIMARY KEY(id)
    , UNIQUE KEY superhero_UX1 (hero_name) 
    ) ENGINE=InnoDB;
    
    CREATE TABLE power
    ( id               INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'pk'
    , name             VARCHAR(255) NOT NULL                COMMENT ''  
    , description_text TEXT NOT NULL                        COMMENT '' 
    , PRIMARY KEY(id)
    , UNIQUE KEY power_UX1 (name)
    ) ENGINE=InnoDB;
    
    CREATE TABLE superheropower
    ( superhero_id   INT UNSIGNED NOT NULL         COMMENT 'pk, fk ref superhero'
    , power_id       INT UNSIGNED NOT NULL         COMMENT 'pk, fk ref power'
    , PRIMARY KEY(superhero_id, power_id)
    , CONSTRAINT FK_superheropower_superhero 
         FOREIGN KEY(superhero_id) REFERENCES superhero(id)
         ON UPDATE CASCADE ON DELETE CASCADE
    , CONSTRAINT FK_superheropower_power
         FOREIGN KEY (power_id) REFERENCES power(id) 
         ON UPDATE CASCADE ON DELETE CASCADE
    ) ENGINE=InnoDB;
    

提交回复
热议问题