尝试使用数据和现有数据操作在现有 table 中添加一个新列作为外键
try to add a new column as foreign key in existing table with data and existing data manipulation
一个非常简单的例子。我的网站 API 在数据库
中有一个 table
Employees
---------
Id
---------
Name
例如,我有 50 条记录。
现在我必须实现一项功能来添加有关部门的额外信息。因为我有一对多的关系,所以新的数据库模式与部门 ID
Employees Department
---------- -----------
Id Id
--------- -----------
Name Name
---------
DepartmentId
为此,我 运行 查询(我使用 SQL 服务器)
alter table Employees add constraint fk_employees_departmentid
foreign key (DepartmentId) references Department(Id);
但现在我有一些问题要处理
1)现在我有 50 条没有 departmentId 的现有记录。但是,我必须手动添加这个值吗?最佳做法是什么?对于 50 条记录是可能的,但对于 2000 条记录或更多?
2) 当我添加 departmentId 列时,我将此列设置为具有空值(正确吗?),但作为外键,我不想允许空值。我可以更改它或如何处理它吗?
1)Now I have the 50 existing records without departmentId. However, I must add manually this value? What is the best practice? For 50 records it is possible but for 2000 records and more?
视情况而定。您可以为 "unassigned" 设置一个新部门并将他们全部分配给该部门;您可以向 HR 发送一个价差 sheet,说 "the following employees don't have an assigned department; what department are they in? ps; don't remove the EmployeeID column from the sheet before you send it back; i need it to update the DB"。这在很大程度上是一个业务背景问题,而不是技术问题。 X 千条记录很容易处理。如果您(或其他人)手动完成,只需要一点时间即可完成。这些信息很可能在其他地方可用;您也许可以向所有部门负责人发送一份列表,说 "are any of these guys yours? Please remove all the names you don't have in your team from this spreadsheet and send it back to me" 然后根据您得到的结果更新数据库
由于这是一次性操作,因此您不需要任何特别的技巧 - 您只需取回 Excel sheet 并在空列中输入:
="UPDATE emp SET departmentID = 5 WHERE id = " & A1
并填写它以生成一堆更新语句,将文本复制到您的查询工具中并点击开始;不需要花哨地将 sheet 加载到 table 中,进行更新连接等 - 只是 hacky 风格将 excel 中的一些东西放在一起,这将为 SQL 编写你,copy/paste/run。如果 HR 发回 sheet 和部门名称列表,则将部门名称和 ID 放在 sheet 的其他地方,并使用 VLOOKUP or XLOOKUP 将名称转换为部门编号,然后根据
撰写你的 SQL
2) when I add departmentId column I set this column to have null values(is correct?), but as a foreign key, I don't want to allow null values. Can I change it or how can I handle it?
外键列允许具有 NULL 值 - 施加 "No Nulls" 限制的不是外键,而是列的可空性(将列更改为 departmantid INT NOT NULL
)那。 FK 引用主键,主键可能不为空(或者在某些数据库中,最多一个记录可以有一个 [部分] 空主键),但您可以将这些部门保留为空。如果您确实将列更改为非空,则需要先更正 NULL 值,否则更改将失败
一个非常简单的例子。我的网站 API 在数据库
中有一个 tableEmployees
---------
Id
---------
Name
例如,我有 50 条记录。
现在我必须实现一项功能来添加有关部门的额外信息。因为我有一对多的关系,所以新的数据库模式与部门 ID
Employees Department
---------- -----------
Id Id
--------- -----------
Name Name
---------
DepartmentId
为此,我 运行 查询(我使用 SQL 服务器)
alter table Employees add constraint fk_employees_departmentid
foreign key (DepartmentId) references Department(Id);
但现在我有一些问题要处理
1)现在我有 50 条没有 departmentId 的现有记录。但是,我必须手动添加这个值吗?最佳做法是什么?对于 50 条记录是可能的,但对于 2000 条记录或更多?
2) 当我添加 departmentId 列时,我将此列设置为具有空值(正确吗?),但作为外键,我不想允许空值。我可以更改它或如何处理它吗?
1)Now I have the 50 existing records without departmentId. However, I must add manually this value? What is the best practice? For 50 records it is possible but for 2000 records and more?
视情况而定。您可以为 "unassigned" 设置一个新部门并将他们全部分配给该部门;您可以向 HR 发送一个价差 sheet,说 "the following employees don't have an assigned department; what department are they in? ps; don't remove the EmployeeID column from the sheet before you send it back; i need it to update the DB"。这在很大程度上是一个业务背景问题,而不是技术问题。 X 千条记录很容易处理。如果您(或其他人)手动完成,只需要一点时间即可完成。这些信息很可能在其他地方可用;您也许可以向所有部门负责人发送一份列表,说 "are any of these guys yours? Please remove all the names you don't have in your team from this spreadsheet and send it back to me" 然后根据您得到的结果更新数据库
由于这是一次性操作,因此您不需要任何特别的技巧 - 您只需取回 Excel sheet 并在空列中输入:
="UPDATE emp SET departmentID = 5 WHERE id = " & A1
并填写它以生成一堆更新语句,将文本复制到您的查询工具中并点击开始;不需要花哨地将 sheet 加载到 table 中,进行更新连接等 - 只是 hacky 风格将 excel 中的一些东西放在一起,这将为 SQL 编写你,copy/paste/run。如果 HR 发回 sheet 和部门名称列表,则将部门名称和 ID 放在 sheet 的其他地方,并使用 VLOOKUP or XLOOKUP 将名称转换为部门编号,然后根据
撰写你的 SQL2) when I add departmentId column I set this column to have null values(is correct?), but as a foreign key, I don't want to allow null values. Can I change it or how can I handle it?
外键列允许具有 NULL 值 - 施加 "No Nulls" 限制的不是外键,而是列的可空性(将列更改为 departmantid INT NOT NULL
)那。 FK 引用主键,主键可能不为空(或者在某些数据库中,最多一个记录可以有一个 [部分] 空主键),但您可以将这些部门保留为空。如果您确实将列更改为非空,则需要先更正 NULL 值,否则更改将失败