mysql - Issue with adding Foreign Key Constraint -
i have built following tables in mysql using v.5.6.18. tried create table foreign key constraint didn't work. trying alter. looks far:
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): have error in sql syntax; check manual corresponds mysql server version right syntax use near '' @ 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 | | +-------------+-------------+------+-----+---------+-------+ | 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 | | +---------------+-------------+------+-----+---------+-------+ | 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 | | +----------+-------------+------+-----+---------+-------+ | 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)
you have foreign key in wrong table. foreign key should exist in referencing table (many) , should reference referenced (one) table. should be:
alter table campaign_overviews add foreign key (petition) references petitions (petition);
also need index (key) on field in petition table.
alter table petition add key petition (petition);
for reference: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
Comments
Post a Comment