简体   繁体   中英

MySql on delete cascade

Im having trouble with on delete cascade.

drop table orders;
CREATE TABLE orders(
    o_id        INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
    c_id        INTEGER NOT NULL REFERENCES Customer(c_id),
    a_id        INTEGER NOT NULL REFERENCES Address(a_id),
    order_date  DATE NOT NULL,
    value       DECIMAL(10,2) NOT NULL CHECK (value >= 0.00),
    method      VARCHAR(20),
    order_type      VARCHAR(30),
    order_interval  VARCHAR(20),
    start_date  DATE NOT NULL,
    admin       VARCHAR(30),
    active      INTEGER NOT NULL,
    notes       VARCHAR(300)
);

drop table order_details;
CREATE TABLE order_details(
    o_id            INTEGER NOT NULL REFERENCES orders(o_id) ON DELETE CASCADE,
    wp_id           INTEGER NOT NULL REFERENCES wp_details(wp_id),
    pack_revision   INTEGER NOT NULL,
    pack_order      INTEGER NOT NULL,
    delivery_date   DATE NOT NULL,
    dispatched_date DATE NOT NULL,
    qty             INTEGER NOT NULL,
    f_id            INTEGER REFERENCES freebies(f_id),
    notes           VARCHAR(300),
    CONSTRAINT  PRIMARY KEY (wp_id, o_id) 
);

When i delete an entry from orders, it does not delete from the order_details table. Are my table definitions wrong?

-- Eliminar la red social Facebook en forma de cascada
--Aqui agrego la posibilidad de On Update Cascade y On Delete Cascade a las
-- reglas generales de la tabla y sus claves foraneas
ALTER TABLE Usuarios WITH CHECK ADD CONSTRAINT 
usuarios_redessociales_fk FOREIGN KEY(red_social)
REFERENCES RedesSociales (Id_red_social)
ON UPDATE CASCADE
ON DELETE CASCADE
-- Efectivamente se borra
DELETE FROM RedesSociales
WHERE Id_red_social = 1
-- Simboliza modificacion (drop) en cierta forma de la estructura de la BD
DROP TABLE Usuarios
DROP TABLE RedesSociales

I've already done this with the DB that We're doing at a practice work, I hope this work for you. Almost the logic is fine, it works. Ask me if you have doubts :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM