我现在对mysql有一个可怕的问题。我正在尝试创建一个数据库并将学生表中的 SUP_ID 链接到主管表。我得到“错误
SQL查询:
;
CREATE TABLE STUDENT(
STU_ID SMALLINT( 5 ) NOT NULL ,
STU_NAMECHAR( 30 ) NOT NULL ,
EMAILCHAR( 25 ) ,
PHONECHAR( 10 ) NOT NULL ,
ENROLLEDCHAR( 1 ) ,
SUP_ID SMALLINT( 5 ) ,
PRIMARY KEY ( STU_ID ) ,
FOREIGN KEY ( SUP_ID ) REFERENCES SUPERVISOR( SUP_ID )
) ENGINE = InnoDB;
MySQL 说:1215 - 尝试创建此数据库时无法添加外键约束。我该怎么办?
set foreign_key_checks=0;
CREATE DATABASE INT_EDIT;
USE INT_EDIT;
CREATE TABLE SUPERVISOR
(
SUP_ID SMALLINT(5) NOT NULL,
LOGIN_ID CHAR(10) NOT NULL,
SUP_NAME VARCHAR(30) NOT NULL,
PASSWORD CHAR(40) NOT NULL,
PRIMARY KEY (LOGIN_ID, SUP_ID)
)
ENGINE = InnoDB;;
INSERT INTO SUPERVISOR VALUES ('user@example.org','11111111','Joe User',sha1('temppassword'));
CREATE TABLE TEACHER
(
T_NAME CHAR(30),
T_ROOM CHAR(30),
SCHOOL CHAR(40) NOT NULL,
PHONE CHAR(10),
GRADE CHAR(4) NOT NULL,
PRIMARY KEY (T_NAME, T_ROOM)
)
ENGINE = InnoDB;;
CREATE TABLE STUDENT
(
STU_ID SMALLINT(5)NOT NULL,
STU_NAME CHAR(30) NOT NULL,
EMAIL CHAR(25),
PHONE CHAR(10) NOT NULL,
ENROLLED CHAR(1),
SUP_ID SMALLINT(5),
PRIMARY KEY (STU_ID),
FOREIGN KEY (SUP_ID) REFERENCES SUPERVISOR (SUP_ID)
)
ENGINE = InnoDB;;
CREATE TABLE ASSIGNMENT
(
A_ID SMALLINT(10) NOT NULL,
TYPE CHAR(4) NOT NULL,
YEAR CHAR(2) NOT NULL,
SEMESTER CHAR(2) NOT NULL,
OBSERVED CHAR(1) NOT NULL,
T_NAME CHAR(30) NOT NULL,
T_ROOM CHAR(30) NOT NULL,
STU_ID SMALLINT(5) NOT NULL,
PRIMARY KEY (A_ID),
FOREIGN KEY (T_NAME) REFERENCES TEACHER (T_NAME),
FOREIGN KEY (T_ROOM) REFERENCES TEACHER (T_ROOM),
FOREIGN KEY (STU_ID) REFERENCES STUDENT (STU_ID),
)
ENGINE = InnoDB;;
CREATE TABLE COMMENT
(
A_ID CHAR(10) NOT NULL,
C_DATE DATETIME() NOT NULL,
T_COMMENT CHAR(4) NOT NULL,
S_COMMENT CHAR(4) NOT NULL,
SUP_ID SMALLINT(5),
T_XTRA_CMT TEXT,
S_XTRA_CMT TEXT,
PRIMARY KEY (A_ID),
FOREIGN KEY (A_ID) REFERENCES ASSIGNMENT (A_ID),
PRIMARY KEY (C_DATE),
FOREIGN KEY (SUP_ID) REFERENCES SUPERVISOR (SUP_ID)
FOREIGN KEY (T_COMMENT) REFERENCES COMMENT_TYPE (T_COMMENT),
FOREIGN KEY (S_COMMENT) REFERENCES COMMENT_TYPE (S_COMMENT)
)
ENGINE = InnoDB;;
CREATE TABLE COMMENT_TYPE
(
T_COMMENT CHAR(4),
S_COMMENT CHAR(4),
T_COMMENT_TEXT TEXT,
S_COMMENT_TEXT TEXT,
PRIMARY KEY(T_COMMENT,S_COMMENT)
)
ENGINE = InnoDB;;
CREATE UNIQUE INDEX Sup_login_indx
ON SUPERVISOR (LOGIN_ID);
CREATE UNIQUE INDEX Stu_id_indx
ON STUDENT (STU_ID);
INSERT INTO COMMENT_TYPE VALUES(0001,,'Great job!',);
INSERT INTO COMMENT_TYPE VALUES(,0001,,'Worked great with children!');
GRANT SELECT ON INT_EDIT.SUPERVISOR
TO LOGINAUTH
IDENTIFIED BY LOGINAUTH;
GRANT INSERT, UPDATE, DELETE
ON INT_EDIT.STUDENT,INT_EDIT.TEACHER,INT_EDIT.ASSIGNMENT,INT_EDIT.COMMENT,INT_EDIT.COMMENT_TYPE,
TO EDITAUTH
IDENTIFIED BY EDITAUTH;
GRANT SELECT
ON INT_EDIT.STUDENT,INT_EDIT.TEACHER,INT_EDIT.ASSIGNMENT,INT_EDIT.COMMENT,INT_EDIT.COMMENT_TYPE,
TO VIEWAUTH
IDENTIFIED BY VIEWAUTH;
FLUSH PRIVILEGES;
任何帮助都有帮助!