English 中文(简体)
如何创建一对一关系SQL服务器关系图
原标题:How to create one to one relationship SQL server diagram

有人知道如何从SQL Server数据库关系图创建一对一关系吗?

最佳回答

您需要在外键之上放置一个唯一键约束,因此它被限制为一对一关系。

问题回答

假设您使用的是Sql Server 2008及以后版本和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.

第二次再次单击“确定”后,SSMS设计器将显示相关表之间的键对键关系。这是一对一的关系。读作,一个表中的一条记录与另一个表的另一条记录直接相关。

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.

希望这能帮助像我这样不懂sql语法的新手,他们更喜欢通过sql数据库图功能构建整个数据库。

如果你的表是这样创建的,

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)
)

您可以使用此代码更改tableName以将tableName.fkId设置为唯一

ALTER TABLE tableName
    ADD UNIQUE (fkId)

您可以轻松使用MSSQL Management Studio。

表用户

uid(pk) username email

表_文件

pid(pk) f_name l_name user_id (fk)

  1. Right click on user_id (fk).
  2. Indexes / Keys...
  3. you can get Indexes / Keys window.

接着,

4选择“添加”(从“索引/键”窗口,它将添加一个新名称)

5在“常规部分”中选择“列”,然后选择“user_id”

6在“常规部分”中,将“是唯一的”设置为true

7在“身份部分”中为(姓名)部分命名。在这种情况下,我将提供UK_user_id_profile

8完成所有上述步骤后,关闭“索引/键”窗口

9将“uid”(来自用户表)拖放到“user_id”(源自配置文件表)中

就是这样。

这背后的理论,外键应该是独一无二的。

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

这就是阿扎姆在帖子中所说的。

[万一,我使用的是MSSQL 2012]

干杯

我相信这个问题是关于如何在SQLServerManagementStudioDiagram窗口中创建一对一关系的。我有一个过程,您可以在SQLServerManagementStudio数据库关系图中创建一对一关系。

  1. Create two tables (A and B), leave table B without a primary key.
  2. Drag a relation from table A primary key to table B s referenced column (Any matching type column, any name) (This will initially create a one-to-many relation
  3. Save the diagram (If you do not save here you will likely get a conflict in the change script)
  4. Go to table B and right click the column and set it as a primary key (This will change the relation to a one-to-one)
  5. Save the diagram

你完了!

当你使用以下内容时,它非常直接:

ALTER TABLE [Salary]
ADD CONSTRAINT FK_Salary_Employee FOREIGN KEY([EmployeeID]) 
    REFERENCES [Employee]([ID]);

这应该奏效!!!





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...