Creating and Bridge tables in MySQL query

萝らか妹 提交于 2021-02-04 19:26:28

问题


I am working with a basic table design for a MySQL database. This database project was given as an idea, mainly aimed for an educational purpose. There are a total of 11 tables which 2 are used for bridging purposes. I tried to properly set primary and foreign keys. I am not sure how to write the query that will create the 11 tables and bridge them all at once. HERE I have attached a visual diagram of these tables.

Through basic learning I can create one table but not sure how to advance from here:

CREATE TABLE Course (
          Course_ID INT,
          Course_Abbreviation VARCHAR(5),
      Course_Number INT,
      Section_Number INT,
      Professor_ID INT,
      Status VARCHAR(10)        
        ) TYPE=innodb;

回答1:


You're almost there, you have to define PRIMARY and FOREIGN keys for the attributes within the tables, an example with the Students, Course, and Course_Br_Students table is:

CREATE TABLE Students(
PRIMARY KEY Student_ID SERIAL INTEGER,
Username VARCHAR(255),
First_name VARCHAR(255),
Email VARCHAR(255),
Phone number INTEGER,
Beginning_Date TIME,
Ending_Date TIME,
Max_hours INTEGER,
)

CREATE TABLE Student_Br_Course(
FOREIGN KEY Student_ID REFERENCES Students(Student_ID),
FOREIGN KEY Course_ID REFERENCES Courses(Course_ID),
Role VARCHAR(255),
Status BOOLEAN,
)

CREATE TABLE Courses(
PRIMARY KEY Course_ID INTEGER,
Course_Abbreviation VARCHAR(255),
Course_Number INTEGER,
Section_number INTEGER,
Professor_ID INTEGER,
)

Here's a reference for understanding how to create tables with PRIMARY and FOREIGN keys:

http://www.w3schools.com/sql/sql_foreignkey.asp



来源:https://stackoverflow.com/questions/15398859/creating-and-bridge-tables-in-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!