我有两个表,一个触发器自动填充第二个表如何获取数据以便自动避免属性的空值?

3ks5zfa0  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(235)

我有两张table,一个自动填充第二张table的触发器。如何获取数据以便自动避免属性的空值?

CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (department_id),
UNIQUE (department_name));

CREATE TABLE `designations_of_departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
designation_1 VARCHAR(30),
designation_2 VARCHAR(30),
designation_3 VARCHAR(30),
designation_4 VARCHAR(30),
designation_5 VARCHAR(30),
FOREIGN KEY (department_id)
    REFERENCES departments (department_id)
    ON DELETE CASCADE);

delimiter $$
create trigger trigger_1 after insert on departments
for each row begin 
insert into designations_of_departments(department_id, designation_1, designation_2, designation_3, designation_4, designation_5) 
values (new.department_id, null, null, null, null, null);
end$$ delimiter ;

我尝试了这个查询,但它仍然显示空值。

select * from designations_of_departments
where not (designation_1 <=> null and designation_2 <=> null and
designation_3 <=> null and designation_4 <=> null and designation_5 <=>
null) and department_id=1;

例子:

INSERT INTO `departments` VALUES (1,'HRM'), (2,'Accounting')(3,'Engineering');

UPDATE designations_of_departments SET 
designation_1 = 'Senior HR',
designation_2 = 'HR',
designation_3 = 'Junior HR' WHERE department_id = 1;

这是我根据评论中的建议尝试的:

select * from designations_of_departments
where designation_1 is not null
 and designation_2 is not null
 and designation_3 is not null
 and designation_4 is not null
 and designation_5 is not null
 and department_id=1;

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题