<p>How do you specify that a foreign key constraint should be a 1:1 relationship in transact sql? Is declaring the column UNIQUE enough? Below is my existing code.!</p>

<pre><code>CREATE TABLE [dbo].MyTable(
    [MyTablekey] INT IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
    [OtherTableKey] INT NOT NULL UNIQUE
        CONSTRAINT [FK_MyTable_OtherTable] FOREIGN KEY REFERENCES [dbo].[OtherTable]([OtherTableKey]),
    ...
    CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED 
    (
        [MyTableKey] ASC
    ) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
</code></pre>

[{'answer_id': 32372, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 2, 'selected': False, 'text': '<p>You could declare the column to be both the primary key and a foreign key.  This is a good strategy for "extension" tables that are used to avoid putting nullable columns into the main table.</p>\n'}, {'answer_id': 32380, 'author': 'Peter Meyer', 'author_id': 1875, 'author_profile': 'https://Stackoverflow.com/users/1875', 'pm_score': 0, 'selected': False, 'text': '<p>Based on your code above, the unique constraint would be enough given that the for every primary key you have in the table, the unique constrained column is also unique.  Also, this assumes that in [OtherTable], the [OtherTableKey] column is the primary key of that table.</p>\n'}, {'answer_id': 32394, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 4, 'selected': True, 'text': '<p>A foreign key column with the UNIQUE and NOT NULL constraints that references a UNIQUE, NOT NULL column in another table creates a 1:(0|1) relationship, which is probably what you want.</p>\n\n<p>If there was a true 1:1 relationship, every record in the first table would have a corresponding record in the second table and vice-versa. In that case, you would probably just want to make one table (unless you needed some strange storage optimization).</p>\n'}, {'answer_id': 32632, 'author': 'Ivan Bosnic', 'author_id': 3221, 'author_profile': 'https://Stackoverflow.com/users/3221', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>If there was a true 1:1 relationship, every record in the first table would have a corresponding record in the second table and vice-versa. In that case, you would probably just want to make one table (unless you needed some strange storage optimization).</p>\n</blockquote>\n\n<p>This is very incorrect. Let me give you an example. You have a table CLIENT that has a 1:1 relationship with table SALES_OFFICE because, for example, the logic of your system says so. Would you really incorporate the data of SALES_OFFICE into CLIENT table? And if another tables need to relate them selfs with SALES_OFFICE? And what about database normalization best practices and patterns?</p>\n\n<blockquote>\n  <p>A foreign key column with the UNIQUE and NOT NULL constraints that references a UNIQUE, NOT NULL column in another table creates a 1:(0|1) relationship, which is probably what you want.</p>\n</blockquote>\n\n<p>The first part of your answer is the right answer, without the second part, unless the data in second table is really a kind of information that belongs to first table and never will be used by other tables.</p>\n'}, {'answer_id': 32771, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 1, 'selected': False, 'text': "<p>@bosnic:</p>\n\n<blockquote>\n  <p>You have a table CLIENT that has a 1:1 relationship with table SALES_OFFICE because, for example, the logic of your system says so. </p>\n</blockquote>\n\n<p>What your app logic says, and what your data model say are 2 different things. There is nothing wrong with enforcing that relationship with your business logic code, but it has no place in the data model.</p>\n\n<blockquote>\n  <p>Would you really incorporate the data of SALES_OFFICE into CLIENT table? </p>\n</blockquote>\n\n<p>If every CLIENT has a unique SALES_OFFICE, and every SALES_OFFICE has a singular, unique CLIENT - then yes, they should be in the same table. We just need a better name. ;)</p>\n\n<blockquote>\n  <p>And if another tables need to relate them selfs with SALES_OFFICE? </p>\n</blockquote>\n\n<p>There's no reason to. Relate your other tables to CLIENT, since CLIENT has a unique SALES_OFFICE. </p>\n\n<blockquote>\n  <p>And what about database normalization best practices and patterns?</p>\n</blockquote>\n\n<p>This <em>is</em> normalization.</p>\n\n<p>To be fair, SALES_OFFICE and CLIENT is obviously not a 1:1 relationship - it's 1:N. Hopefully, your SALES_OFFICE exists to serve more than 1 client, and will continue to exist (for a while, at least) without any clients.</p>\n\n<p>A more realistic example is SALES_OFFICE and ZIP_CODE. A SALES_OFFICE must have exactly 1 ZIP_CODE, and 2 SALES_OFFICEs - even if they have an equivalent ZIP_CODE - do not share the <em>instance</em> of a ZIP_CODE (so, changing the ZIP_CODE of 1 does not impact the other). Wouldn't you agree that ZIP_CODE belongs as a column in SALES_OFFICE?</p>\n"}]
['https://Stackoverflow.com/questions/32360', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3400/']