mysql - "key column doesn't exist in table" -
i'm making database in mysql in class , i'm having trouble adding foreign keys tables. added of foreign keys tables when created tables couldn't add of them in creation process. i'm trying add remaining foreign keys below method.
alter table orders add foreign key (customer_sid) references customer(sid); for reason error message listed in title keeps popping up. code below. can see tables coming out bit funny if read them left right can see says. think?
mysql> show columns games; +------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +------------+-------------+------+-----+---------+----------------+ | gameid | int(11) | no | pri | null | auto_increment | | videogames | varchar(30) | no | | null | | | year | int(11) | no | | null | | | genreid | int(11) | no | mul | null | | | companyid | int(11) | no | mul | null | | | directorid | int(11) | no | mul | null | | +------------+-------------+------+-----+---------+----------------+ 6 rows in set (0.01 sec) mysql> show columns consoles; +--------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +--------------+-------------+------+-----+---------+----------------+ | consoleid | int(11) | no | pri | null | auto_increment | | console | varchar(20) | no | | null | | | yearreleased | int(11) | no | | null | | +--------------+-------------+------+-----+---------+----------------+ 3 rows in set (0.01 sec) mysql> alter table games -> add foreign key(consoleid) references consoles(consoleid); error 1072 (42000): key column 'consoleid' doesn't exist in table
there no field consoleid in table games. need create before trying add constraint on it. add consoleid , create constraint:
alter table games add column consoleid integer not null; alter table games add foreign key (consoleid) references consoles(consoleid); you should think adding third table associate consoles games because games multiplateform. :)
Comments
Post a Comment