CREATE TABLE student
(
s_id INTEGER ,
s_name VARCHAR2(80) ,
s_email VARCHAR (80) ,
FOREIGN KEY (s_id) REFERENCES classes (s_id)
);
CREATE TABLE teacher
(
t_id INTEGER ,
t_name VARCHAR2(80) ,
t_email VARCHAR2 (80) ,
FOREIGN KEY (t_id) REFERENCES classes (t_id)
);
CREATE TABLE courses
(
c_id INTEGER ,
c_title VARCHAR2 (25) ,
c_describetion VARCHAR2 (25) ,
FOREIGN KEY (c_id) REFERENCES classes (c_id)
);
CREATE TABLE classes
(
cl_name VARCHAR2 (30) ,
cl_number INTEGER ,
t_id INTEGER ,
s_id INTEGER ,
c_id INTEGER ,
PRIMARY KEY (cl_number , t_id , s_id , c_id)
);
答案 0 :(得分:1)
您要从其他三个表中引用表“类”。这是应该引用这些的类。将FK放在引用相应表的“ id”字段的类的列中。另外,我认为您需要将它们定义为PK / UNIQUE。
答案 1 :(得分:1)
您正在以相反的方式创建外键。您希望classes
具有引用其他表的外键。您还需要在3个主表中定义主键,以便可以将它们称为外键。
此外,MySQL中没有VARCHAR2
数据类型(这是Oracle特定的数据类型):请改用VARCHAR。
考虑:
CREATE TABLE student (
s_id INTEGER PRIMARY KEY,
s_name VARCHAR(80),
s_email VARCHAR(80)
);
CREATE TABLE teacher (
t_id INTEGER PRIMARY KEY,
t_name VARCHAR(80),
t_email VARCHAR(80)
);
CREATE TABLE courses (
c_id INTEGER PRIMARY KEY,
c_title VARCHAR(25),
c_description VARCHAR(25)
);
CREATE TABLE classes (
cl_name VARCHAR(30),
cl_number INTEGER,
t_id INTEGER,
s_id INTEGER,
c_id INTEGER,
PRIMARY KEY (cl_number , t_id , s_id , c_id),
FOREIGN KEY (s_id) REFERENCES student (s_id),
FOREIGN KEY (t_id) REFERENCES teacher (t_id),
FOREIGN KEY (c_id) REFERENCES courses (c_id)
);
如果您运行的是Oracle(如问题中的错误消息所示)而不是MySQL(为您的问题加了标签),那么您想要的是VARCHAR2
而不是VARCHAR
(已弃用) 。脚本的其余部分保持不变:
CREATE TABLE student (
s_id INTEGER PRIMARY KEY,
s_name VARCHAR2(80),
s_email VARCHAR2(80)
);
CREATE TABLE teacher (
t_id INTEGER PRIMARY KEY,
t_name VARCHAR2(80),
t_email VARCHAR2(80)
);
CREATE TABLE courses (
c_id INTEGER PRIMARY KEY,
c_title VARCHAR2(25),
c_description VARCHAR2(25)
);
CREATE TABLE classes (
cl_name VARCHAR2(30),
cl_number INTEGER,
t_id INTEGER,
s_id INTEGER,
c_id INTEGER,
PRIMARY KEY (cl_number , t_id , s_id , c_id),
FOREIGN KEY (s_id) REFERENCES student (s_id),
FOREIGN KEY (t_id) REFERENCES teacher (t_id),
FOREIGN KEY (c_id) REFERENCES courses (c_id)
);