3 votes

Clé étrangère SQL Server

Je suis en train de créer une base de données d'étudiants dans SQL Server 2005 mais cela me donne une erreur concernant les clés étrangères, merci de vérifier quel est le problème.

 CREATE TABLE STUDENT
(
STUDENT_REGNO  INT  NOT NULL PRIMARY KEY,
STUDENT_FIRSTNAME VARCHAR(30) NOT NULL,
STUDENT_LASTNAME VARCHAR(20) NOT NULL,
STUDENT_FATHER_NAME VARCHAR(30) NOT NULL,
STUDENT_EMAIL VARCHAR(20),
STUDENT_MOBILE_NO VARCHAR(15),
STUDENT_PHONE_NO VARCHAR(15),
STUDENT_ADDRESS VARCHAR (40),
DEPARTMENT_ID INT   CONSTRAINT DEP_1 FOREIGN KEY
 REFERENCES DEPARTMENT(DEPARTMENT_ID),
ACADEMIC_ID INT CONSTRAINT ACDEMIC FOREIGN KEY
REFERENCES ACADEMIC(ACADEMIC_ID),
CLASS_NAME VARCHAR(20) CONSTRAINT CLASS_1 FOREIGN KEY 
REFERENCES CLASS(CLASS_NAME)
)
CREATE TABLE FACULTY
(
FACULTY_ID INT  NOT NULL PRIMARY KEY,
FACULTY_NAME VARCHAR(20) NOT NULL,
FACULTY_FATHER_NAME VARCHAR(30) NOT NULL,
FACULTY_EMAIL VARCHAR(20),
FACULTY_ADDRESS VARCHAR(40) NOT NULL,
FACULTY_MOBILE_NO VARCHAR(30),
FACULTY_PHONE_NO VARCHAR(20),
FACULTY_QUALIFICATION VARCHAR(20),
DEPARTMENT_ID INT CONSTRAINT DEPARTEMNT_2 FOREIGN KEY 
REFERENCES DEPARTMENT(DEPARTMENT_ID),
ACADEMIC_ID INT CONSTRAINT ACADEMIC_1 FOREIGN KEY
REFERENCES ACADEMIC(ACADEMIC_ID)
)
CREATE TABLE USER1
(
USERNAME VARCHAR(20) NOT NULL,
USER_PASSWORD VARCHAR(6) NOT NULL,
USER_TYPE VARCHAR(20) NOT NULL,

)
CREATE TABLE DEPARTMENT
(
DEPARTMENT_ID int NOT NULL PRIMARY KEY,
DEPARTMENT_NAME VARCHAR(20) NOT NULL,
DEAPRTMENT_OFFICE_NO VARCHAR(20),
)
CREATE TABLE CLASS
(
CLASS_NAME VARCHAR(20) NOT NULL,
CLASS_STRENGTH INT NOT NULL,
)
CREATE TABLE ANNOUNCMENT
(
ANNOUNCMENT_ID int NOT NULL PRIMARY KEY,
ANNOUNCMENT_DESCRIPTION VARCHAR(50),
ANNOUNCMENT_TITLE VARCHAR(45)
)
CREATE TABLE FORUM
(
FORUM_ID int NOT NULL PRIMARY KEY,
FORUM_NAME VARCHAR(35) NOT NULL,
FORUM_DESCRIPTION VARCHAR(333),
FORUM_DISCUSSION VARCHAR(22),
POSTED_BY VARCHAR(34)
)
CREATE TABLE GRADE
(
LETTER_GRADE CHAR NOT NULL ,
PERCENTAGE FLOAT NOT NULL,
STUDENT_REGNO int CONSTRAINT STUDENT_5 FOREIGN KEY(STUDENT)
REFERENCES STUDENT(STUDENT_REGNO),
COURSE_ID int CONSTRAINT COURSE_1 FOREIGN KEY(COURSE)
REFERENCES COURSE(COURSE_ID)
)
CREATE TABLE COURSE
(
COURSE_NAME VARCHAR(20) NOT NULL,
COURSE_CREDIT_HR INT NOT NULL,
COURSE_DESCRIPTION VARCHAR(99),
)
CREATE TABLE TERM
(
TERM_START_DATE DATETIME NOT NULL,
TERM_END_DATE DATETIME NOT NULL,
student_regno int CONSTRAINT STUDENT_2 FOREIGN KEY(STUDENT)
REFERENCES STUDENT(STUDENT_REGNO)
)
CREATE TABLE PREVIOUS_ACADEMIC_INFO
(
ACADEMIC_ID int NOT NULL PRIMARY KEY,
DEGREE_DESCRIPTION VARCHAR(50),
CERTIFICATION VARCHAR(30),
DURATION DATETIME
)
CREATE TABLE TEACHER_COURSE
(
AREA_OF_SPECILIZATION VARCHAR(30),
faculty_id int CONSTRAINT FACULTY_1 FOREIGN KEY(FACULTY)
REFERENCES FACULTY(FACULTY_ID)
)

et la première erreur est

Msg 1769, Level 16, State 1, Line 65
La clé étrangère 'STUDENT_5' fait référence à une colonne invalide 'STUDENT' dans la table référencée 'GRADE'.
Msg 1750, Level 16, State 0, Line 65
Impossible de créer la contrainte. Veuillez consulter les erreurs précédentes

4voto

Martin Smith Points 174101
CREATE TABLE GRADE
(
LETTER_GRADE CHAR NOT NULL ,
PERCENTAGE FLOAT NOT NULL,
STUDENT_REGNO int CONTRAINTE STUDENT_5 REFERENCES STUDENT(STUDENT_REGNO),
COURSE_ID int CONTRAINTE COURSE_1 REFERENCES COURSE(COURSE_ID)
)

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X