Hello,
In your case, I know a solution with TRIGGER, here's an example:
CREATE TABLE foo (
FieldA INT,
FieldB INT
);
DELIMITER //
CREATE TRIGGER InsertFieldABNotNull BEFORE INSERT ON foo
FOR EACH ROW BEGIN
IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
END IF;
END//
CREATE TRIGGER UpdateFieldABNotNull BEFORE UPDATE ON foo
FOR EACH ROW BEGIN
IF (NEW.FieldA IS NULL AND NEW.FieldB IS NULL) THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = '\'FieldA\' and \'FieldB\' cannot both be null';
END IF;
END//
DELIMITER ;
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, 10); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (10, NULL); -- OK
INSERT INTO foo (FieldA, FieldB) VALUES (NULL, NULL); -- gives error
UPDATE foo SET FieldA = NULL; -- gives error
Fiddle with the example: link
For PostgreSQL just create a Trigger that returns NULL, eg:
create table stuff (
stuff_id int primary key,
thing text
);
create or replace function stuff_inserting() returns trigger language plpgsql as $$
begin
return null;
end $$;
insert into stuff values (1, 'asdf');
select * from stuff; /* returns 1 row */
create trigger inserting before insert on stuff for each row execute procedure stuff_inserting();
insert into stuff values (2, 'fdsa');
select * from stuff; /* still returns only 1 row */