2013-04-06 59 views
0

我創建了具有外鍵如下表:我怎麼看到它確實是一個外鍵和父表?

CREATE TABLE interests 
(
    int_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    an_interest VARCHAR(50) NOT NULL, 
    contact_id INT NOT NULL, 
    CONSTRAINT my_contacts_contact_id_fk 
    FOREIGN KEY (contact_id) 
    REFERENCES my_contacts (contact_id) 
); 

當我做DESC我看到:

mysql> desc interests; 
+------------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+------------+-------------+------+-----+---------+----------------+ 
| int_id  | int(11)  | NO | PRI | NULL | auto_increment | 
| interest | varchar(50) | NO |  | NULL |    | 
| contact_id | int(11)  | NO | MUL | NULL |    | 
+------------+-------------+------+-----+---------+----------------+ 
3 rows in set (0.02 sec) 

我知道什麼MUL是,但我怎麼會明確地看到,contact_id是定義爲外鍵,哪個是通過CLI的父表?
另外爲什麼我不能使用my_contacts_contact_id_fk來刪除外鍵約束?

UPDATE

mysql> ALTER TABLE interests DROP CONSTRAINT my_contacts_contact_id_fk; 
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n 
ear 'CONSTRAINT my_contacts_contact_id_fk' at line 2 
mysql> 
+0

你的表名是利益或my_interests? – 2013-04-06 12:09:48

+1

@MoyedAnsari:Sorry.Copy-paste problem.Fixed OP – Cratylus 2013-04-06 12:14:48

+0

'SHOW CREATE TABLE interests;' – Travesty3 2013-04-06 12:17:20

回答

0

約束是對自己的數據庫對象,所以當你運行 desc [interests]它不會讓我感到吃驚的是沒有顯示約束的完整描述。 嘗試desc [my_contacts_contact_id_fk]?您可以執行alter table [interests] drop constraint [my_contacts_contact_id_fk]。如果您在刪除時遇到問題,是否可以發佈您的alter table SQL和來自服務器的響應?

+0

查看更新在OP – Cratylus 2013-04-06 12:23:46

+0

'desc my_contacts_contact_id_fk;'也不工作 – Cratylus 2013-04-06 12:24:41

+0

語法 'ALTER TABLE interests DROP CONSTRAINT my_contacts_contact_id_fk;' 似乎對我有效。嘗試限定表名和約束條件 名稱與反標記(如MSSQL服務器中的方括號): 'ALTER TABLE \'interest \'DROP CONSTRAINT \'my_contacts_contact_id_fk \'; ' – 2013-04-06 13:13:09

相關問題