insert.md 2.7 KB
Newer Older
M
insert  
Mars Liu 已提交
1 2 3 4
# 插入

Goods 数据库中有一个表:

fix bug  
张志晨 已提交
5
```sql
M
insert  
Mars Liu 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
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);
```

那么下列哪个选项的代码可以执行成功?

M
Mars Liu 已提交
19 20
<hr/>

F
feilong 已提交
21
点击进入[MySQL实战练习环境](https://mydev.csdn.net/product/pod/new?image=cimg-centos7-skilltreemysql&connect=auto&create=auto&utm_source=skill){target="_blank"}。
F
feilong 已提交
22

F
feilong 已提交
23 24
* `show databases;` 列出所有数据库
* `show tables;` 列出所有表
M
Mars Liu 已提交
25

M
insert  
Mars Liu 已提交
26 27
## 答案

fix bug  
张志晨 已提交
28
```sql
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
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'; 
M
insert  
Mars Liu 已提交
47 48 49 50 51 52
```

## 选项

### 唯一键冲突

fix bug  
张志晨 已提交
53
```sql
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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'; 
M
insert  
Mars Liu 已提交
71 72 73 74
```

### 缺少必要的列

fix bug  
张志晨 已提交
75
```sql
76 77 78 79 80 81 82 83 84 85 86 87 88
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';
M
insert  
Mars Liu 已提交
89 90 91 92
```

### 类型错误

fix bug  
张志晨 已提交
93
```sql
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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';
M
insert  
Mars Liu 已提交
111 112 113 114
```

### 违反非空约束

fix bug  
张志晨 已提交
115
```sql
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
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';
M
insert  
Mars Liu 已提交
133
```