How to create one to one relationship SQL server diagram

You need to put a unique key constraint on top of the foreign key, so its restricted to one-one relationship.


Assuming you are using Sql Server 2008 onwards and SSMS.

  1. The easiest way to do this is simply grab the key from the primary table in the designer surface and drop the key over the secondary table, directly over the primary key.
  2. A second later, two dialog boxes will pop up, in the active dialog confirm that the two keys involved in the drag and drop process, are actually P.Ks in their respective tables and press O.K.
  3. Finally, confirm in the second tables any constraints and any other settings that you would want applied like cascading features and etc.

Once you click O.K again for the second time, SSMS Designer will show a key to key relationship between the tables involved. This is a one-one relationship. Read as, one record in one table directly relates to another record in another table.

i.e A ResolutionsTable - 1 : 1 - Resolution Types Read as one resolution has one resolution type applied to it, i.e, case closed or case ongoing, case pending law change, case denied? Of course people have different DB design skills so what works for one may not work for another db developer. Never the less the example is clear enough to understand.

Hopefully this will help any non sql syntax savvy noobies out there like me who prefer to do build an entire database via the SQL Database Diagram feature.


if your table is created like this,

CREATE TABLE tableName (
   id INT NOT NULL IDENTITY(1,1) CONSTRAINT[PK:tableName] PRIMARY KEY(id)
      , fkId INT NOT NULL
           CONSTRAINT[FK:tableName:tableFk]
           FOREIGN KEY(fkId)
           REFERENCES tableFk(id)
)

CREATE TABLE tableFk (
   id INT NOT NULL IDENTITY(1,1) CONSTRAINT[PK:tableFk] PRIMARY KEY(id)
)

you can alter the tableName with this code to set tableName.fkId as unique

ALTER TABLE tableName
    ADD UNIQUE (fkId)