SQL Agregar clave externa a la columna existente


Si estoy usando el siguiente comando SQL en SQL Server 2008 para actualizar una tabla con una restricción de clave foránea:

ALTER TABLE Employees
ADD FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id)

UserID siendo mi columna FK en la tabla Employees. Estoy tratando de hacer referencia a la UserID en mi tabla ActiveDirectories. Recibo este error:

Referencias de la clave foránea 'userId' columna no válida 'userId' en referencia cuadro "Empleados".

Author: DROP TABLE users, 2012-04-30

6 answers

El error indica que no hay ninguna columna userId en la tabla Employees. Intente agregar la columna primero y luego vuelva a ejecutar la instrucción.

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);
 161
Author: BluesRockAddict,
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-04-30 19:37:34

Tal vez tienes tus columnas al revés??

ALTER TABLE Employees
ADD FOREIGN KEY (UserID)           <-- this needs to be a column of the Employees table
REFERENCES ActiveDirectories(id)   <-- this needs to be a column of the ActiveDirectories table

¿Podría ser que la columna se llame ID en la tabla Employees, y UserID en la tabla ActiveDirectories?

Entonces su orden debe ser:

ALTER TABLE Employees
ADD FOREIGN KEY (ID)                   <-- column in table "Employees"
REFERENCES ActiveDirectories(UserID)   <-- column in table "ActiveDirectories" 
 16
Author: marc_s,
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-04-30 19:39:31

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)

Para permitir el nombramiento de una restricción de CLAVE FORÁNEA, y para definir una restricción de CLAVE FORÁNEA en varias columnas, utilice la siguiente sintaxis SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
 2
Author: Venkatesh Bandarapu,
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
2015-06-23 14:48:26

Forma de creación de clave foránea correcta para ActiveDirectories (id), creo que el error principal es que no mencionó la clave primaria para id en la tabla de ActiveDirectories

 0
Author: Siva Ramakrishna,
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
2016-12-01 10:59:55
ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyBook
FOREIGN KEY FacId
REFERENCES Book Book_Id

ALTER TABLE Faculty 
WITH CHECK ADD  CONSTRAINT FKFacultyStudent 
FOREIGN KEY FacId
REFERENCES Student StuId
 0
Author: Sandy bhardwaj,
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
2017-09-21 04:27:34

En el futuro.

ALTER TABLE Employees
ADD UserID int;

ALTER TABLE Employees
ADD CONSTRAINT FK_ActiveDirectories_UserID FOREIGN KEY (UserID)
    REFERENCES ActiveDirectories(id);
 0
Author: Krishneil,
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
2018-05-24 00:50:11