删除数据
通过 HTTP RESTful API 对 PostgreSQL 数据库进行删除操作。具体接口详情请参考 HTTP API/PostgreSQL 数据库
基础语法
DELETE https://your-envId.api.tcloudbasegateway.com/v1/rdb/rest/:table
Authorization: Bearer <access_token>
💡 提示:access_token 请参考 获取 AccessToken
基础删除
# 删除单条数据
curl -X DELETE 'https://{{host}}/v1/rdb/rest/film?id=eq.1' \
-H 'Authorization: Bearer <access_token>'
💡 注意:默认情况下删除操作不返回数据,仅返回状态码和受影响行数
Response
content-range: */1,表示受影响行数,此时表示删除了 1 条数据preference-applied: return=minimal,表示执行删除时,采用了return=minimal的返回策略(删除操作默认行为,即不产生返回体)
请求返回示例
HTTP/1.1 204 No Content
Header:
content-length: 0
content-range: */1
preference-applied: return=minimal
Body: (Empty)
删除并返回数据
# 删除数据并返回被删除的记录
curl -X DELETE 'https://{{host}}/v1/rdb/rest/film?select=*&id=eq.2' \
-H 'Authorization: Bearer <access_token>' \
-H 'Prefer: return=representation'
Request
- 请求中指定 Header
Prefer: return=representation,表示执行删除时,返回被删除的记录数据。底层通过DELETE ... RETURNING语法在单次查询中完成删除并返回结果
请求返回示例
HTTP/1.1 200 OK
Header:
content-length: 266
content-range: */1
preference-applied: return=representation
Body:
[
{
"_openid": "1977683311217119233",
"director": "Frank Darabont",
"duration": 142,
"id": 2,
"release_year": 1994,
"title": "The Shawshank Redemption"
}
]
复杂条件删除
# 使用复杂条件删除数据
curl -X DELETE 'https://{{host}}/v1/rdb/rest/film?id=in.(7,8,9)&duration=gte.150&limit=2&order=id.desc' \
-H 'Authorization: Bearer <access_token>' \
-H 'Prefer: return=representation'
Request
id=in.(7,8,9):删除 id 在 7、8、9 中的记录duration=gte.150:且时长大于等于 150 分钟limit=2:最多删除 2 条记录order=id.desc:按 id 降序排列后删除
请求返回示例
HTTP/1.1 200 OK
Header:
content-length: 156
content-range: */2
preference-applied: return=representation
Body:
[
{
"id": 9,
"title": "The Dark Knight",
"director": "Christopher Nolan",
"duration": 152
},
{
"id": 8,
"title": "Pulp Fiction",
"director": "Quentin Tarantino",
"duration": 154
}
]