跳到主要内容

创建操作

准备数据


CREATE TABLE `film` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT '主键标识',
`title` VARCHAR(100) NOT NULL COMMENT '电影名称',
`release_year` INT COMMENT '发行年份',
`director` VARCHAR(50) COMMENT '导演',
`duration` INT COMMENT '片长(分钟)',
`_openid` VARCHAR(64) COMMENT '权限字段',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='电影表';

1. 创建单条数据,无返回值

Request


curl -i -X POST http://{{host}}/v1/rdb/rest/v1/film \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <This is a fake token, please use yours>' \
-d '{
"title": "Inception",
"release_year": 2010,
"director": "Christopher Nolan",
"duration": 148
}'

Response

HTTP/1.1 201 Created

Header:
content-length: 0
content-range: */1
preference-applied: return=minimal

Body: (Empty)

说明:

  • content-range: */1,表示受影响行数,此时标表示插入了 1 条数据
  • preference-applied: return=minimal,表示执行写数据时,采用了 return=minimal 的返回策略(写操作默认行为,即不产生返回体)

2. 创建单条数据,插入返回

Request


curl -i -X POST 'https://{{host}}/v1/rdb/rest/film?select=*' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <This is a fake token, please use yours>' \
-H 'Prefer: return=representation' \
--data '{
"title": "The Shawshank Redemption",
"release_year": 1994,
"director": "Frank Darabont",
"duration": 142
}'

Response

HTTP/1.1 201 Created

Header:
content-length: 0
content-range: */1
preference-applied: return=representation

Body:
[
{
"_openid": "1977683311217119233",
"director": "Frank Darabont",
"duration": 142,
"id": 2,
"release_year": 1994,
"title": "The Shawshank Redemption"
}
]

说明:

  • 如果表中存在自增字段,则返回值中会包含自增字段的值
  • 请求中指定 Header Prefer: return=representation,表示执行新增时,返回新增后的数据,不会额外产生一次查询, 默认返回所有字段数据,可配合/table?select=*使用
  • 单独使用 /table?select=* 时,不会生效
  • 指定/table?select=*时,默认返回插入的所有字段数据;指定/table?select=id,title,director时,返回插入的指定字段数据

3. 创建单条数据,插入返回,指定返回字段

Request


curl -i -X POST 'https://{{host}}/v1/rdb/rest/film?select=title,director' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <This is a fake token, please use yours>' \
-H 'Prefer: return=representation' \
--data '{
"title": "The Godfather",
"release_year": 1972,
"director": "Francis Ford Coppola",
"duration": 175
}'

Response

HTTP/1.1 201 Created

Header:
content-length: 0
content-range: */1
preference-applied: return=representation

Body:
[
{
"director": "Francis Ford Coppola",
"title": "The Godfather"
}
]

  • 指定 Header Prefer: return=representation时,默认插入返回所有字段数据 select=*

4. 批量创建数据

Request


curl -i -X POST 'https://{{host}}/v1/rdb/rest/film?select=id,title,director,duration' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <This is a fake token, please use yours>' \
-H 'Prefer: return=representation' \
--data '[
{
"title": "The Godfather: Part II",
"release_year": 1974,
"director": "Francis Ford Coppola",
"duration": 202
},
{
"title": "The Godfather: Part III",
"release_year": 1990,
"director": "Francis Ford Coppola",
"duration": 194
}
]'

Response

HTTP/1.1 201 Created

Header:
content-length: 0
content-range: */2
preference-applied: return=representation

Body:
[
{
"director": "Francis Ford Coppola",
"duration": 202,
"id": 4,
"title": "The Godfather: Part II"
},
{
"director": "Francis Ford Coppola",
"duration": 194,
"id": 5,
"title": "The Godfather: Part III"
}
]

说明:

  • content-range: */2 表示本次请求创建了2条数据
  • 批量创建数据时,整体在一个事务中,如果有失败,则整体回滚

注意: 在执行批量创建时,自增字段回填可能不准确,请不要依赖批量创建时返回的自增字段!

原因:受 MySQL 数据库特性影响,创建数据时 MySQL 仅能知道受影响行数,以及最终插入的自增值是多少,从而推算出这一批数据的自增值。但是自增值分配并非连续的,可能遇到"手动指定"、"INSERT IGNORE"、"INSERT ON DUPLICATE KEY UPDATE"等情况,导致自增值分配不连续。