提交 9adba7b6 编写于 作者: M Mars Liu

insert

上级 a426a398
{
"type": "code_options",
"author": "Mars",
"author": "ccat",
"source": "numbers.md",
"notebook_enable": false,
"exercise_id": "0093549d885b4eec8f556fc86cc60431"
......
{
"type": "code_options",
"author": "Mars",
"author": "ccat",
"source": "datetime.md",
"notebook_enable": false,
"exercise_id": "9f51c5816b1047368bdab70cb20d5116"
......
{
"type": "code_options",
"author": "Mars",
"author": "ccat",
"source": "description.md",
"notebook_enable": false,
"exercise_id": "772f9b0947074197bbeb414fd5a094b0"
......
{
"type": "code_options",
"author": "Mars",
"author": "ccat",
"source": "binary.md",
"notebook_enable": false,
"exercise_id": "b3cdc6ba96d544969c08fe697df32dde"
......
......@@ -2,7 +2,9 @@
"node_id": "mysql-fcca525ab0f04f16834ded9b2b3f38a4",
"keywords": [],
"children": [],
"export": [],
"export": [
"insert.json"
],
"keywords_must": [],
"keywords_forbid": [],
"group": 0
......
{
"type": "code_options",
"author": "ccat",
"source": "insert.md",
"notebook_enable": false,
"exercise_id": "81e312020bb04761800918d54a7a5851"
}
\ No newline at end of file
# 插入
Goods 数据库中有一个表:
```mysql
create table book(
id int primary key auto_increment,
title varchar(200) not null ,
description varchar(1000) default '',
price decimal(12, 4),
isbn char(16) not null ,
publish_at date not null
);
create unique index idx_book_isbn on book(isbn);
```
那么下列哪个选项的代码可以执行成功?
## 答案
```mysql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1';
insert into book(title, price, isbn, publish_at) select 'a other book title', 25.4, 'yy-yyyy-xxxx', '2019-12-1';
```
## 选项
### 唯一键冲突
```mysql
insert into book(title, price, isbn, publish_at) select 'a book title', 25.4, 'xx-xxxx-xxxx', '2019-12-1';
insert into book(title, price, isbn, publish_at) select 'a other book title', 35.4, 'xx-xxxx-xxxx', '2019-12-1';
```
### 缺少必要的列
```mysql
insert into book(price, isbn, publish_at) select 25.4, 'xx-xxxx-xxxx', '2019-12-1';
insert into book(price, isbn, publish_at) select 35.4, 'yy-yyyy-xxxx', '2019-12-1';
```
### 类型错误
```mysql
insert into book(title, price, isbn, publish_at) select 'a book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1';
insert into book(title, price, isbn, publish_at) select 'a other book title', 'unknown', 'xx-xxxx-xxxx', '2019-12-1';
```
### 违反非空约束
```mysql
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1';
insert into book(title, price, isbn, publish_at) select null, 'unknown', 'xx-xxxx-xxxx', '2019-12-1';
```
......@@ -157,7 +157,13 @@
{
"文本字符串类型": {
"node_id": "mysql-27ff66e31d3d4118977cbbc04da6887e",
"keywords": [],
"keywords": [
"text",
"varchar",
"char",
"字符串",
"文本"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......@@ -166,7 +172,11 @@
{
"二进制字符串类型": {
"node_id": "mysql-ec8f22f2c63a4f27bd12815644d0f3db",
"keywords": [],
"keywords": [
"binary",
"blob",
"二进制"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册