简体   繁体   English

要删除的正确SQL语法是什么?

[英]What is the correct sql syntax to delete?

I'm making a simple board. 我正在做一个简单的板子。 I want to delete an article to connect in DB one access after the user has input password. 用户输入密码后,我想删除一篇文章以连接数据库一次访问。

So, I made a method in a DAO class. 因此,我在DAO类中创建了一个方法。 This is the query part. 这是查询部分。

        sql.append("DELETE FROM article ");
        sql.append("WHERE ? = (SELECT pwd FROM article WHERE no = ?) and no = ?");
        pstmt = conn.prepareStatement(sql.toString());
        pstmt.setString(1, pwd);
        pstmt.setInt(2, no);
        pstmt.setInt(3, no);

Someone told me it is incorrect syntax. 有人告诉我这是不正确的语法。 But, it is working. 但是,它正在工作。

Is it incorrect syntax? 语法不正确吗?

I think you have unnecessary subqueries in your statement. 我认为您的陈述中有不必要的子查询。 Why don't you simplify your query: 为什么不简化查询:

sql.append("DELETE FROM article WHERE no = ?");
pstmt = conn.prepareStatement(sql.toString());
pstmt.setInt(1, no);

Q: Is it incorrect syntax? 问:语法不正确吗?

The syntax of the SQL DELETE statement looks valid. SQL DELETE语句的语法看起来有效。 There's nothing "incorrect" about the syntax, in terms of it being a valid SQL statement to prepare and execute. 关于语法,没有什么“不正确”的方面,因为它是准备和执行的有效SQL语句。


However, it does look like the subquery is not necessary. 但是,看起来确实不需要子查询。 I believe an equivalent result could be achieved without a subquery. 我相信没有子查询也可以实现相同的结果。 We would tend to write a simpler statement, without the subquery. 我们倾向于编写一个没有子查询的简单语句。 But that doesn't make the syntax of your statement "incorrect". 但这不会使您的语句的语法“不正确”。

What we don't see is the reasoning for including the subquery. 我们看不到包含子查询的原因。 Why wouldn't this statement achieve an equivalent result? 为什么该语句不能达到等效结果?

   DELETE FROM article WHERE pwd = ? AND no = ?

Most (all?) relational databases are going to throw an error if (when) the subquery returns more than one row. 如果(当)子查询返回多行时,大多数(全部?)关系数据库将引发错误。 (We don't see any guarantee that no is unique in the article table.) (我们看不到article表中no唯一的保证。)

If the value in the pwd column is NULL, then the equality comparison " pwd = ? " in the WHERE clause is going to evaluate to NULL. 如果pwd列中的值为NULL,则WHERE子句中的相等比较“ pwd = ? ”将计算为NULL。 So it's not possible for this statement to delete a row that has a NULL value for pwd . 因此,该语句不可能删除pwd具有NULL值的行。 (The statement would need to be modified to allow deletion of rows with NULL values for pwd column.) (需要修改该语句以允许删除pwd列的值为NULL的行。)

We don't see any Java code that initializes sql . 我们没有看到任何可初始化sql Java代码。 We're going to assume that there's a previous line that sets the contents of sql to a zero length string, or whitespace and/or valid SQL comment. 我们将假设存在上一行,将sql的内容设置为零长度的字符串,空白和/或有效的SQL注释。 Why not just 为什么不只是

  sql = "DELETE FROM article"
        + " WHERE pwd = ?"
        + " AND no = ?";

On a style note, when I'm dynamically generating a statement, appending additional SQL syntax to an existing string, I tend to include the whitespace at the beginning of the string I'm adding, not at the end of what I'm going to add to. 在样式说明上,当我动态生成一条语句时,将其他SQL语法附加到现有的字符串上时,我倾向于在要添加的字符串的开头而不是在要执行的操作的末尾包括空格添加到。


If the pwd column is a "password", we're assuming that what's being stored is a hashed value, and not plaintext. 如果pwd列是“密码”,则假定存储的是散列值,而不是明文。

And we're assuming that this block is within a "try/catch/finally" block. 并且我们假定此块位于“ try / catch / finally”块中。

We can have some quibbles with the code. 我们可以对代码进行一些改动。

But as far as the syntax of the SQL DELETE statement, it looks valid to me. 但是就SQL DELETE语句的语法而言 ,它对我来说似乎是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM