Is it possible to make a unique value in the database?
-
There is a field in the table whether a unique value can be added at the base level. For example, in value 1, that is, the unit with value 1 shall be 1 and the remaining values may be repeated.
Find out what a trigger could be, a question, and how to stop the request from a trigger.
TRIGGER test.trigger1 BEFORE INSERT ON test FOR EACH ROW BEGIN тут запрос END
-
You can really set up a trigger, two different triggers, one for the bet, one for the update.
DELIMITER // CREATE TRIGGER check_insert_test BEFORE INSERT ON test FOR EACH ROW BEGIN SET @block := (SELECT COUNT(*) FROM test WHERE test = 1); IF @block >= 1 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'INSERT canceled'; END IF; END// CREATE TRIGGER check_test BEFORE UPDATE ON test FOR EACH ROW BEGIN SET @block := (SELECT COUNT(*) FROM test WHERE test = 1 AND id <> NEW.id); IF @block >= 1 AND NEW.test = 1 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'UPDATE canceled'; END IF; END// DELIMITER ;
Further implementation of requests can be interrupted by the use of
SIGNAL SQLSTATE
♪ The 45000 signal is specifically designed for user errors not intercepted. If the table contains a record oftest = 1
when attempting to insert a new record with the same value, or to update another record, trying to appointtest = 1
♪, you'll get reports of a mistake ♪INSERT INTO test VALUES (1, 1); ERROR 1644 (45000): INSERT canceled
UPDATE test SET test = 1 WHERE id = 2;
ERROR 1644 (45000): UPDATE canceled