添加外键约束的问题

时间:2014-06-03 16:44:30

标签: mysql sql foreign-keys

我使用v.5.6.18在MySQL中构建了以下表格。最初尝试使用外键约束创建表但它没有工作。所以现在我只是想做一个改变。到目前为止,这就是一切:

Create table Campaign_Overviews(
    -> Campaign varchar(45),
    -> Petition varchar(45),
    -> DateStarted date,
    -> PRIMARY KEY(Campaign,Petition,DateStarted);
ERROR 1046 (3D000): No database selected
mysql> USE ReclaimEmailAcq;
Database changed
mysql> Create table Campaign_Overviews(
    -> Campaign varchar(45),
    -> Petition varchar(45),
    -> DateStarted date,
    -> PRIMARY KEY(Campaign,Petition,DateStarted);
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 near '' at
line 5
mysql> Create table Campaign_Overviews(
    -> Campaign varchar(45) NOT NULL,
    -> Petition varchar(45) NOT NULL,
    -> DateStarted date NOT NULL,
    -> PRIMARY KEY(Campaign, Petition, DateStarted)
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE Petitions(
    -> Petition varchar(45) NOT NULL,
    -> Medium varchar(45),
    -> AdID varchar(45) NOT NULL,
    -> URL varchar(45),
    -> Cost float,
    -> PRIMARY KEY(AdID)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE Email_Addresses(
    -> Email varchar(45) NOT NULL,
    -> Date_Acquired DATETIME,
    -> Petition varchar(45),
    -> AdID varchar(45) NOT NULL,
    -> PRIMARY KEY (Email, AdID)
    -> );
Query OK, 0 rows affected (0.04 sec)

mysql> ALTER TABLE Petitions
    -> ADD FOREIGN KEY (Petition)
    -> REFERENCES Campaign_Overviews (Petition);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> show tables;
+---------------------------+
| Tables_in_reclaimemailacq |
+---------------------------+
| campaign_overviews        |
| email_addresses           |
| petitions                 |
+---------------------------+
3 rows in set (0.00 sec)

mysql> DESCRIBE campaign_overviews;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| Campaign    | varchar(45) | NO   | PRI | NULL    |       |
| Petition    | varchar(45) | NO   | PRI | NULL    |       |
| DateStarted | date        | NO   | PRI | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> DESCRIBE email_addresses;
+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| Email         | varchar(45) | NO   | PRI | NULL    |       |
| Date_Acquired | datetime    | YES  |     | NULL    |       |
| Petition      | varchar(45) | YES  |     | NULL    |       |
| AdID          | varchar(45) | NO   | PRI | NULL    |       |
+---------------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

DESCRIBE petitions;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Petition | varchar(45) | NO   |     | NULL    |       |
| Medium   | varchar(45) | YES  |     | NULL    |       |
| AdID     | varchar(45) | NO   | PRI | NULL    |       |
| URL      | varchar(45) | YES  |     | NULL    |       |
| Cost     | float       | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

1 个答案:

答案 0 :(得分:0)

您在错误的表格中有外键。外键应该存在于引用表(很多)中,并且应该引用引用的(一个)表。它应该是:

ALTER TABLE Campaign_Overviews
ADD FOREIGN KEY (Petition)
REFERENCES Petitions (Petition);

此外,您还需要在请愿表中的该字段上使用索引(Key)。

ALTER TABLE Petition
ADD KEY Petition (Petition);

供参考: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html