提交 da725e79 编写于 作者: M Mars Liu

add dml and commit exercise

上级 bcadc0f1
{
"keywords": [],
"node_id": "oceanbase-4d176da86e544f98acdb7b473d14902a",
"title": "INSERT"
"title": "INSERT",
"notebook_enable": false,
"export": ["insert.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "insert.md"
}
\ No newline at end of file
# Insert
下列哪一个选项的插入语句有错误?
## 答案
```sql
insert into cust set first_name = 'Mars', last_name = 'Liu', city = 'Tian Jin';
```
## 选项
### 标准插入语句
```sql
insert into cust(first_name, last_name, city) values('Mars', 'Liu', 'Tian Jin');
```
### insert 可以使用 select 子句
```sql
insert into cust(first_name, last_name, city) select 'Mars', 'Liu', 'Tian Jin';
```
### insert 可以插入多条 values
```sql
insert into cust(first_name, last_name, city)
values ('Mars', 'Liu', 'Tian Jin'), ('Milly', 'Lee', 'Tian Jin');
```
\ No newline at end of file
{
"keywords": [],
"node_id": "oceanbase-331c385ebc2448198f48564851555c06",
"title": "UPDATE"
"title": "UPDATE",
"notebook_enable": false,
"export": ["update.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "update.md"
}
\ No newline at end of file
# Update
下面选项中,有错的是:
## 答案
```sql
update cust(first_name) values('mars');
```
## 选项
### 生产环境下不带 where 的update 通常是极度危险的,但是合法
```sql
update cust set first_name = 'Mars';
```
### 标准 sql
```sql
update cust set first_name='Mars' where id = 1234;
```
### Update 多个字段也是常用操作
```sql
update cust set first_name='Mars', last_name='Liu' where id = 1234;
```
\ No newline at end of file
{
"keywords": [],
"node_id": "oceanbase-704cfa03b69449b4bed5dc753bf5b7e7",
"title": "DELETE"
"title": "DELETE",
"export": "delete.json"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "delete.md"
}
\ No newline at end of file
# Delete
下列删除语句中,有错误的是:
## 答案
```sql
delete from cust(id, last_name, first_name, city) where id = 5;
```
## 选项
### 在生产环境不带 where 的 delete 是极度危险的操作,但是它仍然合法
```sql
delete from cust;
```
### 标准的删除操作
```sql
delete from cust where id=2341;
```
### 删除操作可以带有子查询
```sql
delete from cust where id in (select id from cust where city='Tian Jin');
```
{
"keywords": [],
"node_id": "oceanbase-2884a0055c714346ba751c2f653e8445",
"title": "REPLACE"
"title": "REPLACE",
"export": ["replace.json"]
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "replace.md"
}
\ No newline at end of file
# Replace
假设有数据表如下
```sql
create table cust(
id integer primary key ,
first_name varchar(256),
last_name varchar(256),
city varchar(256)
);
```
下列replace语句中,有错误的是:
## 答案
```sql
replace into cust(id) set first_name = 'Mars', last_name = 'Liu' where id = 6;
```
## 选项
### 标准的 replace
```sql
REPLACE INTO cust(id, first_name, last_name, city) values(1,'Mars', 'Liu', 'Tian Jin');
```
### 合法的插入操作
```sql
REPLACE INTO cust(first_name, last_name, city) values('Mars', 'Liu', 'Tian Jin');
```
### 合法的插入操作
```sql
REPLACE INTO cust(first_name, last_name, city)
SELECT first_name, last_name, city FROM book;
```
\ No newline at end of file
{
"keywords": [],
"node_id": "oceanbase-8526e5c5374b4081b9a5c698b9748856",
"title": "提交事务"
"title": "提交事务",
"export": "transaction.json"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "transaction.md"
}
\ No newline at end of file
# Transaction
下列选项中,哪个可以正确的保证 channel 表和 posts 表的同步修改?
## 答案
```sql
begin;
insert into posts(title, content) select ?, ?;
update channel set posts = posts +1 where channel_id = ?;
commit;
```
## 选项
### 没有 commit
```sql
begin;
insert into posts(title, content) select ?, ?;
update channel set posts = posts +1 where channel_id = ?;
end;
```
### 没有创建事务
```sql
insert into posts(title, content) select ?, ?;
update channel set posts = posts +1 where channel_id = ?;
commit;
```
### 多次提交
```sql
begin;
insert into posts(title, content) select ?, ?;
commit;
update channel set posts = posts +1 where channel_id = ?;
commit;
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册