ALTER TABLE stock_prices MODIFY COLUMN code varchar(50) COLLATE utf8mb4_0900_as_cs NOT NULL;
-- borra los registros duplicados para poder hacer el unique index y no tener datos dobles
DELETE FROM stock_prices
WHERE id IN (
    SELECT id FROM (
                       WITH duplicados AS (
                           SELECT code, currency, date, COUNT(*) AS 'DUPLICADO code/currency/date'
                           FROM stock_prices
                           GROUP BY 1,2,3
                           HAVING(count(*)) > 1
                           ORDER BY 1, 2, 3
                       )
                       SELECT MIN(sp.id) AS id
                       FROM stock_prices sp
                                JOIN duplicados d ON d.code = sp.code
                           AND d.currency = sp.currency
                           AND d.date = sp.date
                       GROUP BY sp.code, sp.currency, sp.date
                       ORDER BY 1
                   ) t
);
CREATE UNIQUE INDEX unico_por_dia ON stock_prices (code, currency,date);
