此页面包含使用SQL语句从 Hubble中删除数据行的说明。
在阅读本页之前,请执行以下操作:
在本页的示例中,我们使用通过命令导入的示例数据。
要删除表中的行,请使用带有WHERE
子句的DELETE
语句,该子句过滤标识要删除的行的列。
在 SQL中,DELETE
语句一般采用以下形式:
DELETE FROM {table} WHERE {filter_column} {comparison_operator} {filter_value}
参数说明:
{table}
是包含要删除的行的表。{filter_column}
是要过滤的列。{comparison_operator}
是一个比较运算符,解析为TRUE
或FALSE
(例如,=
)。{filter_value}
是过滤器的匹配值。以下是删除行时要遵循的一些最佳做法:
DELETE
您执行的语句数量。使用单个语句删除多行比执行多个DELETE
语句删除单个行更有效。WHERE
子句。DELETE
如果没有WHERE
指定子句,hubble将删除指定表中的所有行。TRUNCATE
语句而不是DELETE
语句。DELETE
语句时,请确保将 SQL执行函数包装在重试循环中,以处理可能在争用下发生的事务错误。删除在非唯一列上过滤的行
假设要删除一定范围编号的历史数据,empno
要删除表中介于两个值(1-1000)之间的所有行。
delete from emp where empno BETWEEN 1 AND 1000;
// 'db' is an open database connection
tsOne := 1
tsTwo := 1000
if _, err := db.Exec("delete from emp where empno BETWEEN $1 AND $2", tsOne, tsTwo); err != nil {
return err
}
return nil
// ds is an org.postgresql.ds.PGSimpleDataSource
int tsOne = 1;
int tsTwo = 1000;
try (Connection connection = ds.getConnection()) {
PreparedStatement p = connection.prepareStatement("delete from emp where empno BETWEEN ? AND ?");
p.setInt(1, tsOne);
p.setInt(2, tsTwo);
p.executeUpdate();
} catch (SQLException e) {
System.out.printf("sql state = [%s]\ncause = [%s]\nmessage = [%s]\n", e.getSQLState(), e.getCause(),
e.getMessage());
}
# conn is a psycopg2 connection
tsOne = 1
tsTwo = 1000
with conn.cursor() as cur:
cur.execute(
"delete from emp where empno BETWEEN %s AND %s", (tsOne, tsTwo))
删除在唯一列上过滤的行
假设要删除特定编号的历史数据。
delete from emp where empno in (1001,7369,3256);
// 'db' is an open database connection
codeOne := 1001
codeTwo := 7369
codeThree := 3256
if _, err := db.Exec("delete from emp where empno in ($1, $2, $3)", codeOne, codeTwo, codeThree); err != nil {
return err
}
return nil
// ds is an org.postgresql.ds.PGSimpleDataSource
int codeOne = 1001;
int codeTwo = 7369;
int codeThree = 3256;
try (Connection connection = ds.getConnection()) {
PreparedStatement p = connection.prepareStatement("delete from emp where empno in (?, ?, ?)");
p.setInt(1, codeOne);
p.setInt(2, codeTwo);
p.setInt(3, codeThree);
p.executeUpdate();
} catch (SQLException e) {
System.out.printf("sql state = [%s]\ncause = [%s]\nmessage = [%s]\n", e.getSQLState(), e.getCause(),
e.getMessage());
}
# conn is a psycopg2 connection
codeOne = 1001
codeTwo = 7369
codeThree = 3256
with conn.cursor() as cur:
cur.execute("delete from emp where empno in (%s, %s, %s)", (codeOne, codeTwo, codeThree)),