Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign Key constraints missing after phpmyadmin export

I create a table in mysql using the following script:

  CREATE TABLE IF NOT EXISTS users_x_activities(
  id int NOT NULL auto_increment, 
  id_user int unsigned NOT NULL,
  id_attivita int unsigned NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (id_user) REFERENCES utente(id),
  FOREIGN KEY (id_attivita) REFERENCES attivita(id)
  ) ENGINE = INNODB;

When I export the created table from phpMyAdmin, I obtain the following script

CREATE TABLE IF NOT EXISTS `users_x_activities` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_user` int(10) unsigned NOT NULL,
  `id_attivita` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `id_user` (`id_user`),
  KEY `id_attivita` (`id_attivita`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

So the question are: where is my foreign key constraints? does KEY refer to FK? Seems that the two tables utente and attivita are no longer referenced in the new generated script. where am I doing wrong?

EDIT

In phpMyAdmin, configuring the export of the table I found the option "Display Foreign Key Relationship" If I flag this option I otain also this code in the script

--
-- RELATIONS FOR TABLE `users_x_activity`:
--   `id_user`
--       `utente` -> `id`
--   `id_attivita`
--       `attivita` -> `id`
--

--
-- Constraints for dumped tables
--

--
-- Constraints for table `users_x_activity`
--
ALTER TABLE `users_x_activity`
  ADD CONSTRAINT `users_x_activities_ibfk_1` FOREIGN KEY (`id_user`) REFERENCES `utente` (`id`),
  ADD CONSTRAINT `users_x_activities_ibfk_2` FOREIGN KEY (`id_attivita`) REFERENCES `attivita` (`id`);

This means that if I add the option "Display Foreign Key Relationship" I obtain also the FK constrains? in other case not?

like image 793
GVillani82 Avatar asked Jul 26 '13 20:07

GVillani82


People also ask

How do I display foreign keys in phpMyAdmin?

To see FKs of a table first select table from the object explorer, then go to Structure tab and then select Relation view. Please note that in different versions it might be in different locations. On the Relation view screen you will see all foreign keys defined for this table (as a foreign table).

Why can't I add a foreign key constraint?

The usual cause are generally a mismatch in the type of the column of the primary table and the foreign table. It can also be a mismatch in the Engine type of two tables i.e. MyISAM or InnoDB. Datatype both columns should have same datatype. int(11) on one table and smallint(5) on another will cause problem.


2 Answers

So the question are: where is my foreign key constraints?

They are defined in the database. The output from SHOW CREATE TABLE users_x_activities will include the foreign key constraint definitions.

The definitions of the foreign key constraints likely appear in separate ALTER TABLE statements at the end of the generated script.

does KEY refer to FK?

No. KEY id_user (id_user) here refers to an index.

Seems that the two tables utente and attivita are no longer referenced in the new generated script.

Yes, you are correct. The foreign key constraints are not included in the CREATE TABLE statement.

where am I doing wrong?

A MySQL SHOW CREATE TABLE users_x_activities will include the foreign key constraints.

The foreign key constraints are likely included in the script generated by phpMyAdmin, but at the end of the script, in separate ALTER TABLE statements.

like image 51
spencer7593 Avatar answered Nov 14 '22 04:11

spencer7593


There are two type of constraints when you managing your tables with phpmyadmin:

  • internal: when you set constraints with phpmyadmin designer for example the constraints stored as internal,that will not be included in export.
  • innoDB: these constraints included in export check out linked video about it

Setting up a foreign key constraint

like image 31
Michael Sebastian Avatar answered Nov 14 '22 03:11

Michael Sebastian