Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/09/2009, 09:10
Avatar de huesos52
huesos52
Colaborador
 
Fecha de Ingreso: febrero-2009
Ubicación: Manizales - Colombia
Mensajes: 5.980
Antigüedad: 16 años, 1 mes
Puntos: 360
Respuesta: ¿Cómo saber si registro tiene referencias a través de FK?

Con information_schema puedes obtener algunos datos. Mira algunos ejemplos.

Código MYSQL:
Ver original
  1. mysql> desc cobros;
  2. +----------+--------------+------+-----+---------+----------------+
  3. | Field    | Type         | Null | Key | Default | Extra          |
  4. +----------+--------------+------+-----+---------+----------------+
  5. | id       | int(11)      | NO   | PRI | NULL    | auto_increment |
  6. | fecha    | date         | NO   |     |         |                |
  7. | unidad   | varchar(3)   | NO   | MUL |         |                |
  8. | cuotadm  | float        | NO   |     |         |                |
  9. | parq     | float        | NO   |     |         |                |
  10. | otros    | float        | NO   |     |         |                |
  11. | concepto | varchar(120) | NO   |     |         |                |
  12. +----------+--------------+------+-----+---------+----------------+
  13. 7 rows in set (0.00 sec)
  14.  
  15. mysql> desc unidad;
  16. +-----------+-------------+------+-----+---------+-------+
  17. | Field     | Type        | Null | Key | Default | Extra |
  18. +-----------+-------------+------+-----+---------+-------+
  19. | uniresi   | varchar(3)  | NO   | PRI |         |       |
  20. | estado    | varchar(10) | NO   |     |         |       |
  21. | arriendo  | varchar(2)  | NO   |     |         |       |
  22. | numhab    | int(2)      | NO   |     |         |       |
  23. | parq      | varchar(2)  | NO   |     |         |       |
  24. | numparq   | int(10)     | NO   |     |         |       |
  25. | mascota   | varchar(2)  | NO   |     |         |       |
  26. | matricula | varchar(20) | YES  |     | NULL    |       |
  27. +-----------+-------------+------+-----+---------+-------+
  28. 8 rows in set (0.00 sec)
  29.  
  30. mysql> select constraint_name,constraint_type,table_name,table_schema from information_schema.table_constraints where table_name = 'cobros' and table_schema='sgh';
  31. +-----------------+-----------------+------------+--------------+
  32. | constraint_name | constraint_type | table_name | table_schema |
  33. +-----------------+-----------------+------------+--------------+
  34. | PRIMARY         | PRIMARY KEY     | cobros     | sgh          |
  35. | cobros_ibfk_1   | FOREIGN KEY     | cobros     | sgh          |
  36. +-----------------+-----------------+------------+--------------+
  37. 2 rows in set (0.00 sec)
  38.  
  39. mysql> select constraint_name,constraint_type,table_name,table_schema from information_schema.table_constraints where table_name = 'unidad' and table_schema='sgh';
  40. +-----------------+-----------------+------------+--------------+
  41. | constraint_name | constraint_type | table_name | table_schema |
  42. +-----------------+-----------------+------------+--------------+
  43. | PRIMARY         | PRIMARY KEY     | unidad     | sgh          |
  44. +-----------------+-----------------+------------+--------------+
  45. 1 row in set (0.00 sec)
  46.  
  47. mysql> select count(*) conteo from information_schema.table_constraints where table_name = 'cobros' and table_schema='sgh' and constraint_type='FOREIGN KEY';
  48. +--------+
  49. | conteo |
  50. +--------+
  51. |      1 |
  52. +--------+
  53. 1 row in set (0.00 sec)
  54.  
  55. mysql>
__________________
Without data, You are another person with an opinion.
W. Edwads Deming