关于删除级联的MySql

时间:2012-03-11 13:49:42

标签: mysql

我在删除级联时遇到了麻烦。

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) 
);

当我从订单中删除一个条目时,它不会从order_details表中删除。我的表定义是错误的吗?

1 个答案:

答案 0 :(得分:0)

-- 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

我已经使用我们在练习中所做的数据库完成了这项工作,我希望这对你有用。几乎逻辑很好,它的工作原理。问你是否有疑问:)