问题
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