Update table so that a field has a unique value

0

How can I update a record in MySQL so that when I change a field in that record all other records change so that this field has a unique value? for example, I have a table called players and a field called artifact , which basically will receive a boolean value, just to say that player is a top scorer and not there may be more than one top scorer ... when you change some record to artilheiro = 1 all other records automatically change to artilheiro = 0

    
asked by anonymous 07.02.2018 / 15:32

1 answer

2

You can create a TRIGGER that does what is described:

CREATE TRIGGER trg_jogadores_ai AFTER UPDATE ON jogadores
FOR EACH ROW
BEGIN
  IF (NEW.artilheiro = 1) THEN
    UPDATE jogadores
       SET artilheiro = 0
     WHERE id <> NEW.id
       AND artilheiro = 1;
END;

In the above% you will verify that the record has been changed to TRIGGER and if this has occurred it will artilheiro = 1 in all records that do not have the same UPDATE and have id to artilheiro = 1 .

    
07.02.2018 / 15:51