Question

I'm stumped on whether I have written the correct syntax to create a foreign key. I used SQL Server 2012 Express.

If I run a ALTER query to set a foreign key relationship between two table, it works fine, no errors occured. However, if I right-click the table where the FK was created, I don't see any relationships.

This is the ALTER query I have wrote. It creates a relationship between Employers and Employees with EmployerID as a FK.

USE demodemo;

BEGIN TRAN t1
ALTER TABLE Employees
WITH check
ADD CONSTRAINT Employees_EmployerID_FK FOREIGN KEY 
    (EmployerID) REFERENCES Employers(ID);
GO

The command was executed 'successfully'.

However, if I right click the table, Employees, and select 'Relationships'.

No foreign keys relationships can be seen.

I thought writing the above ALTER query would be the equivalent of creating a FK relationship via the 'Relationships' gui.

Despite having no issues in creating foreign key relationships, I just cannot see them at all.

  • What could I be doing wrong?
  • Is my ALTER query correct?
  • What is the ALTER syntax equivalent to allow me to view the "selected relationships"?
Était-ce utile?

La solution

Your DML is missing COMMIT. Also, right click and refresh after executing the SQL

Raj

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top