La clave foránea falla al crear


Quiero una clave foránea entre 2 tablas, así que lo intento como siempre. Ahora el problema que estoy teniendo es que no crea, y por lo que parece no crea porque ya hay una clave pero no la hay.

- Unable to create relationship 
 'FK_tbl_Paramed_RegistratieBehandelingen_Users'.  
  The ALTER TABLE statement conflicted with the 
  FOREIGN KEY constraint "FK_tbl_Paramed_RegistratieBehandelingen_Users". 
  The conflict occurred in database "Nestor_Server", 
  table "dbo.Users", column 'UserID'.

He comprobado si tienen el mismo tipo , lo hacen(bigint) así que no entiendo por qué no lo creará

Author: cjk, 2012-07-04

3 answers

Es posible que tenga registros en RegistratieBehandelingen(No está seguro sobre el nombre de la tabla) que no está presente en la tabla de usuarios.

select * from RegistratieBehandelingen a where UserID IS NULL or
not exists (select 1 from Users b where b.UserID= a.UserID)
 49
Author: praveen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-04 07:47:40

Esto significa que tiene datos secundarios sin ID de padre coincidente.

Ejecute lo siguiente para ver si obtiene algún resultado:

SELECT * 
FROM tbl_Paramed_RegistratieBehandelingen r
LEFT JOIN Users u on r.UserID = u.UserID
WHERE u.UserID IS NULL

(cambiar los nombres de las tablas y columnas cuando proceda)

Si obtiene algún resultado, debería mostrar qué registros contienen ID de usuario que no coinciden con los Usuarios.

 11
Author: cjk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-04 07:50:17

Después de la consulta anterior, es posible que desee eliminar el ID de usuario no existente de la tabla tbl_Paramed_RegistratieBehandelingen o insertarlos en usuarios de la tabla .

 0
Author: konkass,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-28 09:22:23