Click here to Skip to main content
14,454,912 members
Rate this:
Please Sign up or sign in to vote.
See more:
Here i have a project table and a module table. Now i want to delete project A but the project A is foreign key of module table. How should i delete the project which is not integrated with module table. Only project that doesn't attach with any module can delete

What I have tried:

sb_sql.append("DELETE FROM ");
sb_sql.append(DB_SCHEMA);
sb_sql.append(".PROJECT ");
sb_sql.append("WHERE PROJECTID = '" + Id + "' ");
Posted
Updated 13-Feb-20 23:22pm
v2
Comments
Richard Deeming 14-Feb-20 8:40am
   
sb_sql.append("WHERE PROJECTID = '" + Id + "' ");

Don't do it like that!

Your code is almost certainly vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If i understand you well...

You need to delete all modules related to the project A, then delete that project.

Depending on relationship between tables and value of option "ON DELETE ...", you can use JOIN statement to delete related data from both tables.

See:
MySQL :: MySQL 8.0 Reference Manual :: 13.1.20.6 FOREIGN KEY Constraints[^]
MySQL ON DELETE CASCADE: Deleting Data from Related Tables Automatically[^]
   
Comments
CPallini 14-Feb-20 10:29am
   
5.
Maciej Los 15-Feb-20 15:23pm
   
Thank you, Carlo!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100