Skip to content

Commit c921d74

Browse files
committed
added transaction
1 parent c93df9b commit c921d74

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ select avg(population) from city group by population;
228228

229229
#### Count function
230230
```sql
231-
select count(name) from city group by name;
231+
select district, count(district) from city group by district;
232232
```
233233

234234
#### Maximum function
@@ -262,4 +262,35 @@ show databases;
262262
#### Calling procedure
263263
```sql
264264
call display_dbs();
265-
```
265+
```
266+
267+
## Transaction
268+
269+
#### Begin transaction
270+
```sql
271+
start transaction;
272+
```
273+
274+
#### Create savepoint
275+
```sql
276+
savepoint sv_pt;
277+
```
278+
279+
```sql
280+
delete from city; -- changing data in table
281+
```
282+
283+
#### Rollback
284+
```sql
285+
rollback to sv_pt;
286+
```
287+
288+
#### Releasing savepoint
289+
```sql
290+
release savepoint sv_pt;
291+
```
292+
293+
#### Commiting changes
294+
```sql
295+
commit;
296+
```

transaction.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
-- creating basic stuff to work on them
3+
create database cheatsheet;
4+
use cheatsheet;
5+
6+
create table city
7+
(
8+
id int primary key,
9+
name varchar(17),
10+
countrycode varchar(3),
11+
district varchar(20),
12+
population int
13+
);
14+
15+
insert into city values (6, "Rotterdam", "NLD", "Zuid-Holland", 593321);
16+
insert into city values (3878, "Scottsdale", "USA", "Arizona", 202705);
17+
insert into city values (3965, "Corona", "USA", "California", 124966);
18+
insert into city values (3973, "Concord", "USA", "California", 121780);
19+
insert into city values (3977, "Cedar Rapids", "USA", "Iowa", 120758);
20+
insert into city values (3982, "Coral Springs", "USA", "Florida", 117549);
21+
insert into city values (4054, "Fairfield", "USA", "California", 92256);
22+
insert into city values (4058, "Boulder", "USA", "Colorado", 91238);
23+
insert into city values (4061, "Fall River", "USA", "Massachusetts", 90555);
24+
25+
-- begin transaction
26+
start transaction;
27+
28+
-- create savepoint
29+
savepoint sv_pt;
30+
31+
delete from city; -- changing data in table
32+
33+
-- rollback
34+
rollback to sv_pt;
35+
36+
-- releasing savepoint
37+
release savepoint sv_pt;
38+
39+
-- commiting changes
40+
commit;

0 commit comments

Comments
 (0)