R
R
raury2021-07-04 23:44:25
MySQL
raury, 2021-07-04 23:44:25

Why can't I drop parent MYSQL table?

To drop the towns table, I first have to drop the citizens. What's the point then in ON DELETE CASCADE?

CREATE TABLE IF NOT EXISTS towns
(
    name VARCHAR(24) DEFAULT 0 NOT NULL,
    mayor VARCHAR(24) DEFAULT 0 NOT NULL,
    `timestamp` BIGINT DEFAULT 0 NOT NULL,
    PRIMARY KEY(name)
);

CREATE TABLE IF NOT EXISTS citizens
(
  town VARCHAR(24),
    name VARCHAR(24) DEFAULT 0 NOT NULL,
    uuid CHAR(36) DEFAULT 0 NOT NULL,
    PRIMARY KEY(name),
    FOREIGN KEY (town) REFERENCES towns(name) ON DELETE CASCADE
);

insert into towns (name, mayor, timestamp) values ('Town', 'Name', 1625428916539);
insert into citizens (town, name, uuid) values ('Town', 'Name', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-07-05
@raury

What's the point then in ON DELETE CASCADE?

The fact that child (from the citizens table) records are deleted when the parent (towns) is deleted.
There is DROP TABLE ... CASCADE for dropping tables, but (surprise surprise) it doesn't work in mysql.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question