我有一個使用MySQL 2005的數據庫。我有兩個表,Enrollment和AlertMsg。MySQL - 無法添加外鍵到表
註冊的主鍵是兩列,UnitCode和StudentID。這兩列都是另一個表的外鍵。
AlertMsg的主鍵是三列UnitCode,StudentID和AlertNo。
當我嘗試做與UnitCode外鍵在ALERTMSG表,就像這樣:
ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment(UnitCode)
我收到以下錯誤:
SQL Execution Error.
Executed SQL statement: ALTER TABLE AlertMsg
ADD FOREIGN KEY (UnitCode)
REFERENCES Enrolment (UnitCode)
Error Source: .Net SqlClient Data Provider
Error Message: There are no primary or candidate keys in the referenced table 'Enrolment' that match the referencing column list in the foreign key 'FK_AlertMsg_UnitCo_571DF1D5'.
Could not create constraint. See previous errors.
經過一番搜索,似乎這是因爲UnitCode不是註冊的主鍵。但Enrollment的表格定義似乎表明它是。我在SQL方面有點新手,所以我假設如果表定義的最左邊的列中有一個鍵,就意味着它是表的主鍵。
誰能幫助?提前致謝。
MySQL的2005? SQL Server 2005? – llazzaro