Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6594519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:49:21+00:00 2026-05-25T17:49:21+00:00

I am trying to create three tables: Users username userID Categories category categoryID userID

  • 0

I am trying to create three tables:

Users

  • username
  • userID

Categories

  • category
  • categoryID
  • userID (references user table)

Expenses

  • amount
  • date
  • expenseID
  • categoryID (references category table) NOT WORKING!
  • userID (references user table) NOT WORKING!

Here is the code of my create statements. Everything worked fine until I added the “ON DELETE…” clauses to the expenses table.

private final static String createUsersTable = 
"CREATE TABLE users ("+
     "userID INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "+
     "username VARCHAR(255) NOT NULL UNIQUE " +
")";
private final static String createCategoriesTable = 
    "CREATE TABLE categories ("+
    "categoryID INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "+
    "category VARCHAR(255) NOT NULL, " +
    "userID INTEGER NOT NULL, " +
    "FOREIGN KEY (userID) REFERENCES users (userID) ON DELETE CASCADE"+
")";
private final static String createExpensesTable =
    "CREATE TABLE expenses ("+
    "expenseID INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "+
    "amount INTEGER NOT NULL, "+
    "date VARCHAR(255) NOT NULL, "+
    "categoryID INTEGER, "+
    "userID INTEGER NOT NULL, "+
    "FOREIGN KEY (categoryID) REFERENCES categories (categoryID) ON DELETE SET NULL, "+
    "FOREIGN KEY (userID) REFERENCES users (userID) ON DELETE CASCADE"+
")";

This is the exception that I get when I try to create the table:

java.sql.SQLSyntaxErrorException: Foreign Key ‘SQL110915104641092’ is invalid because ‘The table cannot be defined as a dependent of table ME.USERS because of delete rule restrictions. (The relationship causes the table to be delete-connected to the indicated table through multiple relationships and the delete rule of the existing relationship is SET NULL.). ‘.

I must be missing something fundamental about SQL databases. What?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:49:22+00:00Added an answer on May 25, 2026 at 5:49 pm

    expenses refers to categories, which refers to users. So having a foreign key for users in expenses is redundant. I believe that the error comes from conflicting actions between SET NULL and CASCADE.

    When you delete a user, the categories referring to it are deleted (because of the CASCADE) and the categories foreign key is set to NULL in expenses (because of the SET NULL). But, at the same time, when you delete a user, the expenses referring to it are deleted (because of the CASCADE).

    One liner: Remove a users foreign key in either expenses or categories, depending on what you’re trying to do with them…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
So I have three tables: CREATE TABLE tblUser ( [pkUserID] [int] IDENTITY(1,1) NOT NULL,
I'm trying to set up the following tables using JPA/Hibernate: User: userid - PK
I'm trying to create a small html fragment (a div element) consisted of three
I'm trying to expect an error in an rspec test. lambda {Participant.create!({:user_id => three.id,
I'm trying to create a subset of a table (as a materialized view), defined
I'm creating a Ruby on Rails application, and I'm trying to create/login/logout users. This
Imagine a database table that looks like this: create table [dbo].[user] ( id int
I would like to create user in my user table when new user have
I'm trying to create a Django app where the user can make a list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.