0
 CREATE DATABASE IF NOT EXISTS UNIVERSITY; 

USE UNIVERSITY; 

CREATE TABLE IF NOT EXISTS STUDENT 
  ( 
     STUDENT_NO    VARCHAR(25) NOT NULL UNIQUE, 
     STUDENT_NAME  VARCHAR(255), 
     ADDRESS       VARCHAR(255), 
     YEAR_STUDY    INT, 
     SEX           VARCHAR(5), 
     DATE_OF_BIRTH DATE, 
     COURSE_NO     VARCHAR(10), 
     PRIMARY KEY(STUDENT_NO) 
  ) 
ENGINE=INNODB; 

CREATE TABLE IF NOT EXISTS SUBJECT 
  ( 
     SUBJECT_NO   VARCHAR(10) NOT NULL UNIQUE, 
     SUBJECT_NAME VARCHAR(255), 
     DEPT_NAME    VARCHAR(255), 
     PRIMARY KEY(SUBJECT_NO) 
  ) 
ENGINE=INNODB; 

CREATE TABLE IF NOT EXISTS RESULTS 
  ( 
     STUDENT_NO VARCHAR(25) NOT NULL UNIQUE, 
     SUBJECT_NO VARCHAR(25) NOT NULL UNIQUE, 
     YEAR_EXAM  INT, 
     MARKS      INT, 
     PRIMARY KEY(STUDENT_NO, SUBJECT_NO), 
     FOREIGN KEY(STUDENT_NO) REFERENCES STUDENT(STUDENT_NO), 
     FOREIGN KEY(SUBJECT_NO) REFERENCES SUBJECT(SUBJECT_NO) 
  ) 
ENGINE=INNODB; 

CREATE TABLE IF NOT EXISTS COURSE 
  ( 
     COURSE_NO VARCHAR(10) NOT NULL UNIQUE, 
     TITLE     VARCHAR(255), 
     DURATION  INT, 
     PRIMARY KEY(COURSE_NO) 
  ) 
ENGINE=INNODB; 

CREATE TABLE IF NOT EXISTS COURSE_SUB 
  ( 
     COURSE_NO  VARCHAR(10) NOT NULL UNIQUE, 
     SUBJECT_NO VARCHAR(10) NOT NULL UNIQUE, 
     PRIMARY KEY(COURSE_NO, SUBJECT_NO), 
     FOREIGN KEY(COURSE_NO) REFERENCES COURSE(COURSE_NO), 
     FOREIGN KEY(SUBJECT_NO) REFERENCES SUBJECT(SUBJECT_NO) 
  ) 
ENGINE=INNODB; 

我正在创建一个数据库,但它有一些错误我找不到什么错误

请帮忙

4

3 回答 3

0

用这个替换你的“结果”表查询

create table IF NOT EXISTS results( Student_No varchar(25) not null unique, Subject_No varchar(25)not null unique, Year_Exam int, Marks int,
primary key(Student_No,Subject_No),
foreign key(Student_No) references Student(Student_No),foreign key(Subject_No) references Subject(Subject_No) )engine=innodb;
于 2013-08-28T05:33:02.127 回答
0

尝试这个

 CREATE DATABASE IF NOT EXISTS UNIVERSITY; 

 USE UNIVERSITY; 

 CREATE TABLE IF NOT EXISTS STUDENT 
 ( 
 STUDENT_NO    VARCHAR(25) NOT NULL UNIQUE, 
 STUDENT_NAME  VARCHAR(255), 
 ADDRESS       VARCHAR(255), 
 YEAR_STUDY    INT, 
 SEX           VARCHAR(5), 
 DATE_OF_BIRTH DATE, 
 COURSE_NO     VARCHAR(10), 
 PRIMARY KEY(STUDENT_NO) 
 ) 
 ENGINE=INNODB; 

 CREATE TABLE IF NOT EXISTS SUBJECT 
 ( 
 SUBJECT_NO   VARCHAR(10) NOT NULL UNIQUE, 
 SUBJECT_NAME VARCHAR(255), 
 DEPT_NAME    VARCHAR(255), 
 PRIMARY KEY(SUBJECT_NO) 
 ) 
 ENGINE=INNODB; 

 CREATE TABLE IF NOT EXISTS RESULTS 
 ( 
 STUDENT_NO VARCHAR(25) NOT NULL UNIQUE, 
 SUBJECT_NO VARCHAR(25) NOT NULL UNIQUE, 
 YEAR_EXAM  INT, 
 MARKS      INT, 
 PRIMARY KEY(STUDENT_NO, SUBJECT_NO), 
 FOREIGN KEY(STUDENT_NO) REFERENCES STUDENT(STUDENT_NO), 
 FOREIGN KEY(SUBJECT_NO) REFERENCES SUBJECT(SUBJECT_NO), 
 ) 
 ENGINE=INNODB; 

 CREATE TABLE IF NOT EXISTS COURSE 
 ( 
 COURSE_NO VARCHAR(10) NOT NULL UNIQUE, 
 TITLE     VARCHAR(255), 
 DURATION  INT, 
 PRIMARY KEY(COURSE_NO) 
 ) 
 ENGINE=INNODB; 

 CREATE TABLE IF NOT EXISTS COURSE_SUB 
 ( 
 COURSE_NO  VARCHAR(10) NOT NULL UNIQUE, 
 SUBJECT_NO VARCHAR(10) NOT NULL UNIQUE, 
 PRIMARY KEY(COURSE_NO, SUBJECT_NO), 
 FOREIGN KEY(COURSE_NO) REFERENCES COURSE(COURSE_NO), 
 FOREIGN KEY(SUBJECT_NO) REFERENCES SUBJECT(SUBJECT_NO) 
 ) 
 ENGINE=INNODB; 
于 2013-08-28T05:49:58.747 回答
0

您在Year_Exam int and 和andMarks int之间缺少逗号foreign key(Student_No) references Student(Student_No)foreign key(Subject_No) references Subject(Subject_No)

就像是

create table IF NOT EXISTS 
results( 
    Student_No varchar(25) not null unique, 
    Subject_No varchar(25)not null unique, 
    Year_Exam int, 
    Marks int, 
    primary key(Student_No,Subject_No), 
    foreign key(Student_No) references Student(Student_No) ,
    foreign key(Subject_No) references Subject(Subject_No) 
)engine=innodb;
于 2013-08-28T05:23:13.313 回答