1、 DML(Data Manipulation Language – 数据操纵语言) 可以在下列条件下执行:
2、 事务是由完成若干项工作的DML语句组成的
1、INSERT 语句语法
INSERT INTO table [(column [, column...])]
VALUES (value [, value...]);
1) 使用 INSERT 语句向表中插入数据。
2)使用这种语法一次只能向表中插入一条数据。
2、插入数据
INSERT INTO departments(department_id, department_name,
manager_id, location_id)
VALUES (70, 'Pub', 100, 1700);
1) 为每一列添加一个新值。
2)按列的默认顺序列出各个列的值。
3)在 INSERT 子句中随意列出列名和他们的值。
4)字符和日期型数据应包含在单引号中。
3、向表中插入空值
1) 隐式方式: 在列名表中省略该列的值。
INSERT INTO departments (department_id,
department_name )
VALUES (30, 'Purchasing');
1 row created.
2) 显示方式: 在VALUES 子句中指定空值。
INSERT INTO departments
VALUES (100, 'Finance', NULL, NULL);
1 row created.
4、从其它表中拷贝数据
1) 在 INSERT 语句中加入子查询。
2)不必书写 VALUES 子句。
3)子查询中的值列表应与 INSERT 子句中的列名对应
INSERT INTO emp2
SELECT *
FROM employees
WHERE department_id = 90;
INSERT INTO sales_reps(id, name, salary, commission_pct)
SELECT employee_id, last_name, salary, commission_pct
FROM employees
WHERE job_id LIKE '%REP%';
5、案例实战
# 增加数据
SELECT * FROM myemp1;
#方式一:一条一条的添加
INSERT INTO myemp1
VALUES(1,'Tom',8000,'[email protected]');
# 通常添加时使用的方式:表名后的字段与values中的数据一一对应
INSERT INTO myemp1(id,salary,NAME,email)
VALUES(2,9000,'Tom','[email protected]');
# 未添加数据的字段,赋值为null
# 注意:如果email设置有not null的约束,则如下操作执行失败。
INSERT INTO myemp1(id,salary,NAME)
VALUES(3,9000,'Tom1')#,(4,10000,'Tom2'),(,,)
# 方式二:基于现有的表,导入数据
INSERT INTO myemp1(id,NAME,salary,email)
SELECT employee_id,last_name,salary,email
FROM employees
WHERE salary > 12000;
SELECT * FROM myemp1;
DESC myemp1;
DESC employees;
#此时,如果删除myemp1中的数据的话,不会影响employees中的数据。因为myemp1已经复制了employees中的数据
6、注意:values()()() 和Value()
1、UPDATE 语句语法
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];
可以一次更新多条数据。
如果需要回滚数据,需要保证在DML前,进行设置:SET AUTOCOMMIT = FALSE;
2、更新数据
UPDATE employees
SET department_id = 70
WHERE employee_id = 113;
1 row updated.
UPDATE copy_emp
SET department_id = 110;
22 rows updated.
3、更新中的数据完整性错误
UPDATE employees
SET department_id = 55
WHERE department_id = 110;
错误代码: 1452
Cannot add or update a child row: a foreign key constraint fails (`myemployees`.`employees`, CONSTRAINT `dept_id_fk` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`))
原因:不存在 55 号部门
4、实战案例:
# 修改操作 update 表名 set ... ,...,... where ....
#注意:通常修改操作都有where,否则会批量修改。
SELECT *
FROM myemp1;
UPDATE myemp1
SET email = '[email protected]'
WHERE id = 3;
#####
SET autocommit = FALSE;
COMMIT;
UPDATE myemp1
SET email = '[email protected]'
ROLLBACK;
# 有些情况下,修改会失败。
UPDATE departments
SET department_id = 700
WHERE department_id = 70;
1、DELETE 语句
使用 DELETE 语句从表中删除数据。
DELETE FROM table
[WHERE condition];
2、删除数据
DELETE FROM departments
WHERE department_name = 'Finance';
1 row deleted.
DELETE FROM copy_emp;
22 rows deleted.
3、删除中的数据完整性错误
DELETE FROM departments
WHERE department_id = 60;
错误代码: 1451
Cannot delete or update a parent row: a foreign key constraint fails (`myemployees`.`employees`, CONSTRAINT `dept_id_fk` FOREIGN KEY (`department_id`) REFERENCES `departments` (`department_id`))
原因:
You cannot delete a row that contains a primary key that is used as a foreign key in another table.
4、实战案例:
# 删除操作: delete from .... where ....
SELECT COUNT(*)
FROM employees;
SELECT *
FROM myemp1;
DELETE FROM myemp1
WHERE id = 100;
# 有些情况下,delete操作会失败。
# 由于外键约束的影响,导致删除失败。
DELETE FROM departments
WHERE department_id = 70;
SELECT *
FROM employees
WHERE department_id = 70;
1、事务:一组逻辑操作单元,使数据从一种状态变换到另一种状态。
2、 数据库事务由以下的部分组成:
3、 设置提交状态:SET AUTOCOMMIT = FALSE;
4、 以下面的其中之一作为结束:
5、COMMIT和ROLLBACK语句的优点
使用COMMIT 和 ROLLBACK语句,我们可以: