创建表时出现mysql错误

时间:2013-08-28 05:17:55

标签: mysql sql database

 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; 

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

请帮助

3 个答案:

答案 0 :(得分:0)

您在Year_Exam intMarks 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;

答案 1 :(得分: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;

答案 2 :(得分: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;