Data Manipulation and Controls in SQL
Last updated: 2026-07-21
DCL (Data Control Language): DCL is used to confirm or cancel changes made to the data in the database. Additionally, it can be used to set whether users of the RDBMS have the permissions to operate on objects within the database (such as database tables).
Commands to manipulate data
- Alter
<Table_name>- add <col> <col_type>
- ADD PRIMARY KEY <col>
- ADD FROEIGN KEY ... references ...
- drop <col>
- change <old_col> <new_col>
- modify <col> <col_type>
- UPDATE <table> SET <col> = ... WHERE ...
- DELETE FROM <table> WHERE ...
Transactions
- Automicity
- Consistency: maintain consistency for every roll back
- Isolation: not affect by other transactions
- Durability
Transactions is supported by Oracle, some engines in MySQL(SHOW ENGINES)
Commands
- START TRANSACTION or BEGIN
- explicitly start a transaction - multiple sentences go toghether
- COMMIT
- ROLLBACK [TO <savepoint>]
- SAVEPOINT
- RELEASE SAVEPOINT
- SET TRANSACTION
- different isolation levels
auto commit
supported by MySQL
SQL
mysql> set autocommit =0; //关闭自动提交
mysql> set autocommit =1; //开启自动提交Isolation level
in SQL 92, Isolation
- dirty read: read uncommited transactions
- nonrepeatable read: other transactions changed/deleted data between two reads
- phantom read: more data was added between reads
| Isolation level | dirty read | nonrepeatable read | phantom read |
|---|---|---|---|
| READ UNCOMMITED | T | T | T |
| READ COMMITED | F | T | T |
| REPEATABLE READ | F | F | T |
| SERIALIZABLE | F | F | F |
cursor
SQL
// 1.)
// MySQL, SQL Server, DB2, Maria DB
DECLARE cursor_name CURSOR FOR select_statement
// Oracle, PostgreSQL
DECLARE cursor_name CURSOR IS select_statement
// 2.)
OPEN cursor_name
// 3.)
FETCH cursor_name INTO var_name ...
// 4.)
CLOSE cursor_name
// 5.)
DEALLOCATE cursor_namec- cursor overflow: reached EOF
SQL
dd
CREATE PROCEDURE `alter_attack_growth`()
BEGIN
-- 创建接收游标的变量
DECLARE temp_id INT;
DECLARE temp_growth, temp_max, temp_start, temp_diff FLOAT;
-- 创建结束标志变量
DECLARE done INT DEFAULT false;
-- 定义游标
DECLARE cur_hero CURSOR FOR SELECT id, attack_growth, attack_max, attack_start FROM heros;
-- 指定游标循环结束时的返回值
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
OPEN cur_hero;
FETCH cur_hero INTO temp_id, temp_growth, temp_max, temp_start;
REPEAT
IF NOT done THEN
SET temp_diff = temp_max - temp_start;
IF temp_growth < 5 THEN
IF temp_diff > 200 THEN
SET temp_growth = temp_growth - 1.1;
ELSEIF temp_diff >= 150 AND temp_diff <=200 THEN
SET temp_growth = temp_growth - 1.08;
ELSEIF temp_diff < 150 THEN
SET temp_growth = temp_growth - 1.07;
END IF;
ELSEIF temp_growth >=5 AND temp_growth <=10 THEN
SET temp_growth = temp_growth - 1.05;
END IF;
UPDATE heros SET attack_growth = ROUND(temp_growth,3) WHERE id = temp_id;
END IF;
FETCH cur_hero INTO temp_id, temp_growth, temp_max, temp_start;
UNTIL done = true END REPEAT;
CLOSE cur_hero;
ENDConnect with Python
API Standards
Follow Python DB API Standards
- mysql-connector
- MySQLdb
- mysqlclient
- PyMySQL
ORM Framework
- SQLAlchemy
- session
- datatypes
- Django
- models
- peewee
Mapping
- Column <--> Table
- Column instance -> row
- Attributes -> SQL fields