I'm currently designing a database structure for a warehouse for an assignment.
我目前正在为仓库设计数据库结构。
Warehouse manager (*Manager name, manager salary, ^Warehouse managed)
Warehouse (*Warehouse ID, Address, ^Warehouse Manager, Number of Rooms)
Primary Keys: *
Foreign keys: ^
In the part above I want to make it such that when the manager is first hired the warehouse managed can be null, but when he manages the warehouse no one else can manage it. So the only thing coming to mind is to make the foreign key become a primary when set.
在上面的部分中,我想说明,当管理员第一次被雇用时,管理的仓库可以为空,但是当他管理仓库时,没有其他人可以管理它。因此,唯一想到的是设置外键成为主键。
- A manager can only manage one or null warehouse.
- 经理只能管理一个或零仓库。
- A warehouse must be managed by one manager.
- 仓库必须由一位经理管理。
1 个解决方案
#1
0
Your scenario can be managed like this (I have changed names of columns a little for better understanding):
您可以像这样管理您的场景(我已经更改了一些列的名称以便更好地理解):
WarehouseManager (WarehouseManagerId PK, ManagerName, ManagerSalary)
Warehouse (WarehouseId PK, Address, WarehouseManagerId FK (WarehouseManager), Rooms
So, for a Warehouse
zero or one manager can be set. Warehouse managed
is removed, as it is not required and also can lead to inconsistencies if not set properly.
因此,对于仓库零或一个经理可以设置。仓库管理被删除,因为它不是必需的,如果设置不当也会导致不一致。
[later edit]
[后期编辑]
In order to force a warehouse manager to handle a maximum of one warehouse, a check constraint would be fit, but MySQL does not seem to handle such constructs, so a trigger may be used (not tested):
为了强制仓库经理处理最多一个仓库,检查约束适合,但MySQL似乎不处理这样的结构,因此可以使用触发器(未测试):
DELIMITER $$
CREATE TRIGGER `trgWarehouseOneManaged`
BEFORE INSERT ON `Warehouse`
BEGIN
IF EXISTS (SELECT COUNT(1) cnt FROM Warehouse GROUP BY WarehouseManagerId HAVING (cnt > 1)
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'more than one Warehouse managed';
END IF;
END$$
DELIMITER ;
#1
0
Your scenario can be managed like this (I have changed names of columns a little for better understanding):
您可以像这样管理您的场景(我已经更改了一些列的名称以便更好地理解):
WarehouseManager (WarehouseManagerId PK, ManagerName, ManagerSalary)
Warehouse (WarehouseId PK, Address, WarehouseManagerId FK (WarehouseManager), Rooms
So, for a Warehouse
zero or one manager can be set. Warehouse managed
is removed, as it is not required and also can lead to inconsistencies if not set properly.
因此,对于仓库零或一个经理可以设置。仓库管理被删除,因为它不是必需的,如果设置不当也会导致不一致。
[later edit]
[后期编辑]
In order to force a warehouse manager to handle a maximum of one warehouse, a check constraint would be fit, but MySQL does not seem to handle such constructs, so a trigger may be used (not tested):
为了强制仓库经理处理最多一个仓库,检查约束适合,但MySQL似乎不处理这样的结构,因此可以使用触发器(未测试):
DELIMITER $$
CREATE TRIGGER `trgWarehouseOneManaged`
BEFORE INSERT ON `Warehouse`
BEGIN
IF EXISTS (SELECT COUNT(1) cnt FROM Warehouse GROUP BY WarehouseManagerId HAVING (cnt > 1)
SIGNAL SQLSTATE '12345'
SET MESSAGE_TEXT := 'more than one Warehouse managed';
END IF;
END$$
DELIMITER ;