SQL Many-to-Many Relationship Between Multiple Tables

后端 未结 3 1161

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:32

    Edit[1]: This is a version of SQL code on how I would do it!!

    CREATE TABLE superhero
    ( 
    Superheo_id INT NOT NULL AUTO_INCREMENT, 
    heroName VARCHAR(255) NOT NULL, 
    firstName VARCHAR(255)NULL,
    lastName VARCHAR(255)NULL, 
    firstAppearance DATE NULL, 
    gender VARCHAR(255) NULL, 
    bio TEXT NULL, 
    universe VARCHAR(255) NULL, 
    CONSTRAINT SUPERHERO_PK PRIMARY KEY(SUPERHERO_id)
    );
    
    CREATE TABLE power
    ( 
    POWER_id INT NOT NULL AUTO_INCREMENT, 
    name VARCHAR(255) NOT NULL, 
    description TEXT NOT NULL,
    CONSTRAINT POWER_PK PRIMARY KEY(POWER_id)
    );
    
    CREATE TABLE superheroPower
    ( 
    superheroID INT DEFAULT(0) NOT NULL, 
    powerID INT DEFAULT(0) NOT NULL, 
    CONSTRAINT SUPERHEROPOWERS_SUPERHERO_FK FOREIGN KEY(superheroID) REFERENCES superhero(id), 
    CONSTRAINT SUPERHEROPOWERS_POWERS_FK FOREIGN KEY(powerID) REFERENCES power(id) 
    );
    

    edit[2]:You are able to change null to not null and vise versa depending on if you want a user to move past with out installing the other information. I have never used the Auto_increment before in my sql tables, so for me that is something new I just learned from you

提交回复
热议问题