2017-02-17 45 views
1

我已經看過這篇文章的其他主題,並試圖使我的腳本適應他們,但無濟於事。所以我在這裏張貼。SQL Noob。 「外鍵約束錯誤地形成」

我有兩張桌子,科目和學生。在這些內部,有兩個主鍵(subject_id和student_id),請參考full view of the two tables的圖像(在phpmyadmin中完成)。我想要創建第三個表,其中包含entry_id(主鍵),subject_id(FK),subject_name(FK),subjectexamboard(FK),student_id(FK)和studentfirstname(FK)。

這裏是我試過的phpMyAdmin的運行代碼:

CREATE TABLE entries3(
entry_id INT NOT NULL AUTO_INCREMENT, 
subject_id INT NOT NULL, 
subject_name VARCHAR(20) NOT NULL, 
subjectExamBoard VARCHAR(10) NOT NULL, 
student_id INT UNSIGNED NOT NULL, 
studentFirstName VARCHAR(20) NOT NULL, 
studentLastName VARCHAR(40) NOT NULL, 
PRIMARY KEY (entry_id), 
CONSTRAINT FOREIGN KEY (subject_id) REFERENCES subjects(subject_id), 
CONSTRAINT FOREIGN KEY (subject_name) REFERENCES subjects(subject_name), 
CONSTRAINT FOREIGN KEY (subjectExamBoard) REFERENCES subjects(exam_board), 
CONSTRAINT FOREIGN KEY (student_id) REFERENCES students(student_id), 
CONSTRAINT FOREIGN KEY (studentFirstName) REFERENCES students(first_name), 
CONSTRAINT FOREIGN KEY (studentLastName) REFERENCES students(last_name)); 

感謝您的幫助,請寬容我,因爲我很新的SQL。

+1

上的名稱的限制是不必要的,因爲是列。 –

+0

我刪除了[tag:sql-server]標籤。 MySQL具有'AUTO_INCREMENT'語法,但Microsoft SQL Server不支持。 –

+0

正如@DanBracuk所說,你通過擁有'studentFirstName','studentLastName','subjectExamBoard'和'subject_name'來創造不必要的冗餘。所有這些列可以通過連接獲得。你可能想重溫你的設計 –

回答

0

檢查每一列對您的FK約束,他們應該是相同的數據類型

+0

好的,會做的。非常感謝! – chunkydumpling

0

也就是說你想要做什麼。你想要一個查詢或一個視圖。

你在上面試圖做什麼打破任何一種database normalization

你的條目表是這樣的:

CREATE TABLE entries3 (
    entry_id INT NOT NULL AUTO_INCREMENT, 
    subject_id INT NOT NULL, 
    student_id INT UNSIGNED NOT NULL, 
    PRIMARY KEY (entry_id), 
    CONSTRAINT fk_entry_subjects FOREIGN KEY (subject_id) REFERENCES subjects(subject_id), 
    CONSTRAINT fk_entry_students FOREIGN KEY (student_id) REFERENCES students(student_id) 
); 

然後你

select sub.*, stu.* 
from entries3 as ent 
    inner join subjects as sub 
    on ent.subject_id = sub.subject_id 
    inner join students as stu 
    on ent.student_id = stu.student_id 
+0

啊我明白了。我不明白,但非常感謝你的幫助。我會稍後再嘗試! :) – chunkydumpling

+0

由於你不明白,我聽說過這本書,數據庫設計爲單純的凡人的好東西。相關的陳詞濫調是,做事比做兩次更容易。 –

0

查詢應該是約束name_of_constrainT FOREIGN KEY

假設你的名字是有效的

CREATE TABLE entries3(
    entry_id INT NOT NULL AUTO_INCREMENT, 
    subject_id INT NOT NULL, 
    subject_name VARCHAR(20) NOT NULL, 
    subjectExamBoard VARCHAR(10) NOT NULL, 
    student_id INT UNSIGNED NOT NULL, 
    studentFirstName VARCHAR(20) NOT NULL, 
    studentLastName VARCHAR(40) NOT NULL, 
    PRIMARY KEY (entry_id), 
    CONSTRAINT subject_id_fk  FOREIGN KEY (subject_id)  REFERENCES subjects(subject_id), 
    CONSTRAINT subject_name_fk FOREIGN KEY (subject_name)  REFERENCES subjects(subject_name), 
    CONSTRAINT subjectExamBoar_fk FOREIGN KEY (subjectExamBoar) REFERENCES subjects(exam_board), 
    CONSTRAINT student_id_fk  FOREIGN KEY (student_id)  REFERENCES students(student_id), 
    CONSTRAINT studentFirstName_fk FOREIGN KEY (studentFirstName) REFERENCES students(first_name), 
    CONSTRAINT studentLastName_fk FOREIGN KEY (studentLastName) REFERENCES students(last_name)); 
+0

我明白了,感謝您的幫助。沒有意識到這一點! – chunkydumpling

+0

@chunkydumpling如果我的回答是正確的,請將其標記爲已接受...看到這裏如何 http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – scaisEdge

+0

你忘了指定FOREIGN KEY後面的列 – LONG

0

外鍵約束名稱不能與表名稱的屬性相同。

0

不知道你到底想從第三個表中獲得什麼。但是,如果目標只是將兩個表格放在一起,那麼只需創建一個視圖即可。

CREATE view entries3 AS 
SELECT 
su.subject_id AS 'Subject_ID', 
su.subject_name AS 'Subject_Name', 
su.subjectexamboard AS 'Subject_Exam_Board' 
st.student_id AS 'Student_ID', 
st.studentfirstname AS 'Student_First_Name' 
FROM subject su, student st; 

然後查詢作爲

SELECT * from entries;