Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
Why do I get a DELETE ON cascade syntax error

I am trying to make the groups in my messaging system delete the group, the messages and the members using delete on cascade error. My code :

"CREATE TABLE `groups` (
          groups_id int AUTO_INCREMENT PRIMARY KEY,
          owner_id int NOT NULL,
          group_name varchar(255)
        )",
  "CREATE TABLE `groups_members` (
          groups_members_id int AUTO_INCREMENT PRIMARY KEY,
          groups_id int NOT NULL,
          user_id int,
          FOREIGN KEY (groups_id)
              REFERENCES groups(groups_id)
              DELETE ON CASCADE
        )",
  "CREATE TABLE `groups_contents` (
          groups_contents_id int AUTO_INCREMENT PRIMARY KEY,
          user_id int,
          groups_id int,
          date_time DATETIME,
          message varchar(1000),
          FOREIGN KEY (groups_id)
              REFERENCES groups(groups_id)
              DELETE ON cascade
        )"

The error it gives me: ERROR ON STATEMENT [CREATE TABLE groups_members ( groups_members_id int AUTO_INCREMENT PRIMARY KEY, groups_id int NOT NULL, user_id int, CONSTRAINT banana FOREIGN KEY (groups_id) REFERENCES groups(groups_id) DELETE ON CASCADE )] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DELETE ON CASCADE )' at line 8

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have some minor bugs in your code

Groups is a reserved word, so it has to be in backticks.

The syntax is ON DELETE and not DELETE ON

CREATE TABLE `groups` (
    groups_id INT AUTO_INCREMENT PRIMARY KEY,
    owner_id INT NOT NULL,
    group_name VARCHAR(255)
);
CREATE TABLE `groups_members` (
          groups_members_id int AUTO_INCREMENT PRIMARY KEY,
          groups_id int NOT NULL,
          user_id int,
          FOREIGN KEY (groups_id)
              REFERENCES `groups` (groups_id)
              ON DELETE  CASCADE
        );
CREATE TABLE `groups_contents` (
          groups_contents_id int AUTO_INCREMENT PRIMARY KEY,
          user_id int,
          groups_id int,
          date_time DATETIME,
          message varchar(1000),
          FOREIGN KEY (groups_id)
              REFERENCES  `groups` (groups_id)
              ON DELETE  cascade
        );        
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!