From da725e7906b55f08519d380cfc9c7ecb78d4b29b Mon Sep 17 00:00:00 2001 From: Mars Liu Date: Thu, 4 Nov 2021 16:20:55 +0800 Subject: [PATCH] add dml and commit exercise --- .../4.INSERT/config.json" | 4 +- .../4.INSERT/insert.json" | 5 +++ .../4.INSERT/insert.md" | 30 ++++++++++++++ .../5.UPDATE/config.json" | 4 +- .../5.UPDATE/update.json" | 5 +++ .../5.UPDATE/update.md" | 29 +++++++++++++ .../6.DELETE/config.json" | 3 +- .../6.DELETE/delete.json" | 5 +++ .../6.DELETE/delete.md" | 30 ++++++++++++++ .../7.REPLACE/config.json" | 3 +- .../7.REPLACE/replace.json" | 5 +++ .../7.REPLACE/replace.md" | 41 +++++++++++++++++++ .../config.json" | 3 +- .../transaction.json" | 5 +++ .../transaction.md" | 41 +++++++++++++++++++ 15 files changed, 208 insertions(+), 5 deletions(-) create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.json" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.md" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.json" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.md" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.json" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.md" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.json" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.md" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.json" create mode 100644 "data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.md" diff --git "a/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/config.json" "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/config.json" index 0ef4a24..c382fba 100644 --- "a/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/config.json" +++ "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/config.json" @@ -1,5 +1,7 @@ { "keywords": [], "node_id": "oceanbase-4d176da86e544f98acdb7b473d14902a", - "title": "INSERT" + "title": "INSERT", + "notebook_enable": false, + "export": ["insert.json"] } \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.json" "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.json" new file mode 100644 index 0000000..e33023f --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "insert.md" +} \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.md" "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.md" new file mode 100644 index 0000000..8b1a346 --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/4.INSERT/insert.md" @@ -0,0 +1,30 @@ +# 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 diff --git "a/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/config.json" "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/config.json" index 2850440..c346a7d 100644 --- "a/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/config.json" +++ "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/config.json" @@ -1,5 +1,7 @@ { "keywords": [], "node_id": "oceanbase-331c385ebc2448198f48564851555c06", - "title": "UPDATE" + "title": "UPDATE", + "notebook_enable": false, + "export": ["update.json"] } \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.json" "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.json" new file mode 100644 index 0000000..61174dd --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "update.md" +} \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.md" "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.md" new file mode 100644 index 0000000..f71b45c --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/5.UPDATE/update.md" @@ -0,0 +1,29 @@ +# 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 diff --git "a/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/config.json" "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/config.json" index 5dcccf8..0ccc501 100644 --- "a/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/config.json" +++ "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/config.json" @@ -1,5 +1,6 @@ { "keywords": [], "node_id": "oceanbase-704cfa03b69449b4bed5dc753bf5b7e7", - "title": "DELETE" + "title": "DELETE", + "export": "delete.json" } \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.json" "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.json" new file mode 100644 index 0000000..440c13c --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "delete.md" +} \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.md" "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.md" new file mode 100644 index 0000000..09540fc --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/6.DELETE/delete.md" @@ -0,0 +1,30 @@ +# 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'); +``` + diff --git "a/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/config.json" "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/config.json" index 03b8165..d30c426 100644 --- "a/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/config.json" +++ "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/config.json" @@ -1,5 +1,6 @@ { "keywords": [], "node_id": "oceanbase-2884a0055c714346ba751c2f653e8445", - "title": "REPLACE" + "title": "REPLACE", + "export": ["replace.json"] } \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.json" "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.json" new file mode 100644 index 0000000..533de3b --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "replace.md" +} \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.md" "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.md" new file mode 100644 index 0000000..2a76886 --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/7.REPLACE/replace.md" @@ -0,0 +1,41 @@ +# 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 diff --git "a/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/config.json" "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/config.json" index 46f2cc2..ec51219 100644 --- "a/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/config.json" +++ "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/config.json" @@ -1,5 +1,6 @@ { "keywords": [], "node_id": "oceanbase-8526e5c5374b4081b9a5c698b9748856", - "title": "提交事务" + "title": "提交事务", + "export": "transaction.json" } \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.json" "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.json" new file mode 100644 index 0000000..7efcf48 --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.json" @@ -0,0 +1,5 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "transaction.md" +} \ No newline at end of file diff --git "a/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.md" "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.md" new file mode 100644 index 0000000..e320d3e --- /dev/null +++ "b/data/2.OceanBase\344\270\255\351\230\266/8.\346\217\220\344\272\244\344\272\213\345\212\241/transaction.md" @@ -0,0 +1,41 @@ +# 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 -- GitLab