MySQL - 一个表上的行删除不会更新其他表

时间:2017-09-01 12:45:47

标签: mysql database foreign-keys constraints

我有2个表,一个用于员工,另一个用于部门。部门可以有多个/不同的员工,但一个员工只能在一个部门工作。他们的关系是一对多的。

表格创作:

CREATE TABLE IF NOT EXISTS department(
    id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    name VARCHAR(255) NOT NULL UNIQUE, 
    street VARCHAR(255) NOT NULL, 
    employees_count INT(20) DEFAULT '0') 
ENGINE=INNODB;

CREATE TABLE IF NOT EXISTS employee(
    id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(255) NOT NULL, 
    last_name VARCHAR(255) NOT NULL, 
    email VARCHAR(255) NOT NULL, 
    born INT(20) NOT NULL, 
    country VARCHAR(255) NOT NULL, 
    department_name VARCHAR(255) NOT NULL, 
    FOREIGN KEY (department_name) REFERENCES department(name) 
    ON UPDATE CASCADE ON DELETE CASCADE) 
ENGINE=INNODB;

表格插入:

INSERT INTO department(name,street) VALUES ('Alexandroupoli', 'Leoforos Dimokratias21');
INSERT INTO department(name,street) VALUES ('Athens','Basilisis Sofias 111');
INSERT INTO department(name,street) VALUES ('Patras','Smurnis 34');
INSERT INTO department(name,street) VALUES ('Kalamata','Leoforos Fountas 241');
INSERT INTO department(name,street) VALUES ('Heraklion','Leoforos Enetwn 132');
INSERT INTO department(name,street) VALUES ('Thessaloniki','Karolou 45');
INSERT INTO department(name,street) VALUES ('Xanthi','Agia Barbasa 68');
INSERT INTO department(name,street) VALUES ('Larisa','Hroon Polutexneiou 12');

INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Vaggelis','Michos','vagg7@gmail.com','1995','Greece','Athens');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('James','Gunn','james8@gmail.com','1970','USA','Athens');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('George','McMahon','george95@gmail.com','1978','Usa','Patras');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('John','Jones','john13@gmail.com','1992','England','Patras');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Marinos','Kuriakopoulos','marin_kur@gmail.com','1986','Greece','Alexandroupoli');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Dimitris','Nikolaou','dimitis8@yahoo.gr','1984','Greece','Larisa');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Soufiane','El Kaddouri','sofiane@yahoo.com','1974','France','Xanthi');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Maria','Apostolou','mariamaria1@gmail.com','1997','Greece','Larisa');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Ioannis','Marinou','ioannis_ap@yahoo.gr','1982','Greece','Kalamata');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Thanasis','Athanasiou','thanos89@gmail.com','1989','Cyprus','Heraklion');

UPDATE department SET employees_count = (SELECT COUNT(*) FROM employee WHERE department.name = employee.department_name);

enter image description here

enter image description here

在创建2个表并在它们之间创建外键约束时,当我从department表中删除Department时,每个相关员工也会被删除,就像我需要的那样。

但是,当我从员工表中删除员工时,部门中的employees_count行不会更改。例如,如果我有2名使用department_name = Athens的员工,那么在删除其中一个员工时,当我转到departments表时,employees_count将保持等于2而不是1。

更新employees表中的department_name时会发生同样的事情。如果对于一名员工,我更新他的部门名称来自"雅典"让我们说" Patras",部门表保持不变。

所以我在想,在删除员工之后,也可能执行此命令

UPDATE department SET employees_count = (SELECT COUNT(*) FROM employee WHERE department.name = employee.department_name);

这肯定会解决我的问题。

但是,部门表中的employees_count列是否可以自动分配给在那里工作的员工数量,而不仅仅是整数字段? 有没有更实用的方法来解决这个问题,而不是在每次删除后添加UPDATE命令?

1 个答案:

答案 0 :(得分:2)

我说你不​​需要将部门计数存储在表格中作为单独的属性;它是您可以随时通过查询找到的值。

此外,删除记录可能非常绝对。这绝不是最佳做法,但我更喜欢将记录标记为已删除(可能带有时间戳)。这是我猜想的可能感兴趣的元数据。这样就可以保留所有员工信息。值得深思。

希望这很有帮助。