Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to delete from two different tables which are linked by the primary key.

My tables are as follows..

Table Name: StudentCourse

PK: StudentID
StudentName
StudentPassword
CourseCode


Table Name: StudentModuleResult

PK: StudentID
ModuleCode
ResultCode


I have the following function:

C#
public String deleteStudent(String studentid) {
        int noOfUpdates = 0;
        String message = "";
        try {
            String strQuery = "DELETE FROM StudentCourse WHERE StudentID = " + studentid
                            + " DELETE FROM StudentModuleResult WHERE StudentID = " + studentid;

            PreparedStatement stmt = conn.prepareStatement(strQuery);
            stmt.setString(1, studentid);
            noOfUpdates = stmt.executeUpdate();
        }
        catch (SQLException e) {
            message = "SQL Exception: " + e.getMessage();
        }
        if (message.length() ==0 && noOfUpdates == 0)
            message = "Record was not updated";

        return message;
    }





However I am receiving the following error:

SQL Exception: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'StudentID = 10198765 DELETE FROM StudentModuleResult WHERE StudentID = 10198765'.
Posted

While you can do this with a single I would suggest that you either:
- create a stored procedure to do the deletions and pass the key as parameter
- run the commands from client side separately.

Few other notes:
- use parameters instead of literals in the SQL statements
- Consider if you really need 2 tables with the same primary key (1:1 relation)
 
Share this answer
 
Comments
Manfred Rudolf Bihy 29-Mar-11 14:19pm    
Absolutely! 5+
Wendelius 29-Mar-11 14:26pm    
Thanks :)
Monjurul Habib 29-Mar-11 14:23pm    
nice advice.5+
Wendelius 29-Mar-11 14:27pm    
Thank you :)
Why are you using stmt.setString? Your SQL statement does not contain a parameter and secondly you are trying to execute two SQL DELETE statements. If the syntax allows for this there
is probably a separator missing.
Try executing the two DELETE statments separately. You have not indicated the usage of a foreign key, but if one exists you have to delete from the table that has the foreign key constraint first.

Best Regards,
 
Share this answer
 
v2
Comments
Monjurul Habib 29-Mar-11 14:23pm    
nice advice.5+
Manfred Rudolf Bihy 29-Mar-11 14:27pm    
Thank you!
Wendelius 29-Mar-11 14:37pm    
Good advice. my 5.
Manfred Rudolf Bihy 29-Mar-11 14:39pm    
Thanks Mika!
http://stackoverflow.com/questions/1509949/deleting-from-2-tables-at-the-same-time[^]

cascade delete

If i misunderstand your question, please feel free to correct me.
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 29-Mar-11 14:32pm    
While agree with you (my 5+) OP hasn't mentioned a foreign key relation ship between the two tables. It would make sense of course to have a composite PK in table StudentModuleResult where the foreign key part would be StudentID to table StudentCourse. The whole model doesn't make any sense as a student would only be able to have one course and only one module, but maybe OP can enlighten us on this.
Monjurul Habib 29-Mar-11 14:42pm    
thanx and yes you are right.
Wendelius 29-Mar-11 14:38pm    
Good advice. +5
Monjurul Habib 29-Mar-11 14:42pm    
thank you.
Dalek Dave 29-Mar-11 16:15pm    
Good Call

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900