2013-01-15 84 views
0

我有數據庫與5桌違反MySql約束條件。插入

students PK : ID -> anum, first, last 
studentinfo PK/FK : ID -> why, student_commenets, finished, aidyear 
Times  PK/FK : ID -> signintime, counselor_start_time, 
          additional_time, finish_time 
counselor PK/FK : ID -> firstcounselor, secondcounselor, thirdcounselor 
Comments PK/FK : ID -> counselorcomments, additional_commenets 

我有一個名爲signinpage.php

該網頁我有寫信給三個不同的表(學生,studentinfo和時間)

上頁

我的代碼休耕:

if (empty($errors) === true) 
{ 
include('core/queries/inserts.updates.selects/students.php'); 
include('core/queries/inserts.updates.selects/studentinfo.php'); 
include('core/queries/inserts.updates.selects/signintime.php'); 

$dbh = null;  
header('location: signedin.php'); 
exit(); 
} 
每個文件的

是實際插入查詢。 (如果你亞勒需要看到他們,我會更新這個帖子)

我遇到的錯誤是:

SQLSTATE [23000]:完整性約束違規:1452不能添加或 更新子行:一個外鍵約束失敗(testtimes, 約束times_ibfk_2外鍵(id)參考文獻studentsid)ON DELETE CASCADE ON UPDATE CASCADE)

要添加到此,第一個查詢(students.php和第二個查詢studentinfo.php) 插入就好了。相同的ID,登錄時間插入到表中會出現問題:時間。

在phpmyadmin中,兩個表(studentinfo和times)都配置爲相同,都有從學生開始會話(這是PK ID)開始會話後刪除並更新到原始表(學生)的級聯。

我該如何解決這個錯誤?

編輯:

<?php 
require('core/init.php'); 

try 
{ 
    $null = NULL; 
    $query = $dbh->prepare("INSERT INTO `times` (signintime) VALUES (:signintime)"); 
    $query->bindParam(':signintime' , $null); 
    $query->execute(); 
} 

     catch (PDOException $e) 
     { 
       error_log($e->getMessage()); 
       die($e->getMessage()); 
     } 
?> 
+2

嗨,這是不相關的問題,但你可以從閱讀受益:http://support.microsoft.com/kb/283878 – user1909426

+0

向你表示感謝。隨着這項業務的需要,他們必須存儲多餘的數據(anum = student id),因爲多名學生多次進來。除此之外,我覺得這是一個很好的形式。然而tyvm的閱讀材料! – RaGe10940

回答

0

你的表設計看起來我錯了。我假設times表中students表中的每一行都可以有多個條目。在這種情況下,你會需要以下列times

id - PK 
student_id - FK 
signintime 
counselor_start_time 
additional_time 
finish_time 

然後每行一個特定的學生也有同樣的student_id值,但不同的id值。

+0

已經有學生證其ANUM(對不起,我沒有提一句),並與我期望多個學生來回到辦公室多次。所以使用anum作爲主鍵/ FK會導致違規?沒有? – RaGe10940

+0

我說它應該只用作'times'表的外鍵,它需要自己的主鍵分開了這一點。 –

+0

這也沒有工作。我不斷收到約束 – RaGe10940

0

以下聲明和示例與您提到的表格不同,但的想法仍然相同

爲什麼產生錯誤的原因是因爲你試圖插入一個孩子表中,該值沒有出現在表中的值。 孩子表意味着它依賴於另一個表(,它是父母)。

爲了進一步解釋,考慮以下架構,

CREATE TABLE StudentList 
(
    ID INT PRIMARY KEY, 
    NAme VARCHAR(50) 
); 

CREATE TABLE AddressList 
(
    StudentID INT, 
    Address VARCHAR(50), 
    CONSTRAINT tb_fk FOREIGN KEY (StudentID) 
    REFERENCES StudentList(ID) 
); 

INSERT INTO StudentList VALUES (1, 'Jon'); 
INSERT INTO StudentList VALUES (2, 'Skeet'); 

INSERT INTO AddressList VALUES (1, 'Hello'); 
INSERT INTO AddressList VALUES (2, 'World'); 
INSERT INTO AddressList VALUES (1, 'Other Address'); 

有兩個表:StudentListAddressList。該表Address是子表,並且依賴於表StudentList也稱爲父表)。要在柱插入被允許的唯一值表AddressListStudentID1 and 2,因爲這些都是在表StudentList發現的唯一ID秒。

當您嘗試插入記錄比1 and 2上表Address其它ID,例如

INSERT INTO AddressList VALUES (1, 'Other Address'); 

它會產生一個錯誤,告訴:

不能添加或更新子行:一外鍵約束失敗 (db_2_ec2e8addresslist,CONSTRAINT tb_fk FOREIGN KEY (StudentID)參考文獻studentlistID)):

因爲列StudentID的值被插入在桌子上不提供對母表(StudentList)。

所以,我希望這將有助於你現在明白了。

+0

我使用相同的php腳本在同一時間插入student,studentinfo和時間。此時sessionid或PK:ID是除學生表之外的所有表的FK。 – RaGe10940

+0

另外需要包含的一件事是沒有保存學生信息的數據庫。學生自己將記錄插入學生表。這是我的問題。我一次插入學生表,學生信息表和時間表。 – RaGe10940