简体   繁体   English

MySQL删除级联

[英]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. 当我从订单中删除条目时,它不会从order_details表中删除。 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 :) 问我是否有疑问:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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