MySQL constraint to prevent combinations of columns
you can use Triggers for that.


CREATE TABLE files_dir (
  `dir` VARCHAR(5),
  `file` VARCHAR(6)
);

INSERT INTO files_dir
  (`dir`, `file`)
VALUES
  ('dir_A', 'file_0'),
  ('dir_A', 'file_1'),
  ('dir_A', 'file_1'),
  ('dir_B', 'file_2'),
  ('dir_C', 'file_3');
  




CREATE TRIGGER before_file_insert 
    BEFORE INSERT ON files_dir
    FOR EACH ROW 
 BEGIN
 IF EXISTS(SELECT 1 FROM files_dir WHERE `file`= NEW.`file` AND `dir` = NEW.`dir`) then
  BEGIN
  END;
 ELSE
    IF EXISTS(SELECT 1 FROM files_dir WHERE `file`= NEW.`file`) THEN
          SIGNAL SQLSTATE '45000' 
      SET MESSAGE_TEXT = &quot;file has another directory&quot;;
    END IF;
   END IF;
 END;




 INSERT INTO files_dir
  (`dir`, `file`)
VALUES
('dir_A', 'file_1');


✓




 INSERT INTO files_dir
  (`dir`, `file`)
VALUES
('dir_B', 'file_1');


file has another directory




SELECT * FROM files_dir;


dir   | file  
:---- | :-----
dir_A | file_0
dir_A | file_1
dir_A | file_1
dir_B | file_2
dir_C | file_3
dir_A | file_1


db&lt;&gt;fiddle here
For an BEFORE UPDATE TRIGGER you can do something eual.
Of course you can always change your insert statement with a join like ypercubeᵀᴹ already said.