赞
踩
插入语句
不指定列名的插入
INSERT INTO 表名称 VALUES (值1, 值2,....)
- # 如果表中已有1号部门,则出错。因为dept_id是主键,不允许重复
- mysql> insert into departments values(1, '行政部');
- ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
-
- # mysql> insert into departments values(10, '行政部');
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into employees values
- -> (134, '张三', '2019-5-10', '2000-10-12', 'zhangsan@guodong.com', '15088772354', 9),
- -> (135, '李四', '2020-8-20', '1999-6-23', 'lisi@guodong.com', '13323458734', 9);
- Query OK, 2 rows affected (0.01 sec)
- Records: 2 Duplicates: 0 Warnings: 0
指定列名的插入
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
- mysql> insert into departments (dept_name, dept_id) values ('售后部', 11);
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into departments (dept_name) values ('咨询部');
- Query OK, 1 row affected (0.00 sec)
- mysql> insert into employees
- -> (name, hire_date, birth_date, email, phone_number, dept_id)
- -> (
- -> select name, hire_date, birth_date, email, phone_number, dept_id
- -> from employees
- -> where name='张三'
- -> );
- Query OK, 1 row affected (0.00 sec)
- Records: 1 Duplicates: 0 Warnings: 0
使用set语句
INSERT INTO 表名 SET 列名1=列值1, 列名2=列值2, ...
- mysql> insert into departments set dept_name='采购部';
- Query OK, 1 row affected (0.00 sec)
修改单表记录
UPDATE 表名称 SET 列名称=新值, 列名称=新值, ... WHERE 筛选条件
- # 修改人事部的名称为人力资源部
- mysql> update departments set dept_name='人力资源部'
- -> where dept_name='人事部';
- Query OK, 1 row affected (0.00 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
修改多表记录
- UPDATE 表1 AS 表1别名
- INNER | LEFT | RIGHT JOIN 表2 AS 表2别名
- ON 连接条件
- SET 列=值, 列=值, ...
- WHERE 连接条件
- # 修改李四所在部门为企划部
- mysql> update departments as d
- -> inner join employees as e
- -> on d.dept_id=e.dept_id
- -> set d.dept_name='企划部'
- -> where e.name='李四';
删除单表记录
DELETE FROM 表名 WHERE 筛选条件;
- # 删除重复的员工张三,只保留一个张三的信息
- # 查询张三信息
- mysql> select * from employees where name='张三';
-
- # 根据员工编号删除重复的张三
- mysql> delete from employees where employee_id=136;
- Query OK, 1 row affected (0.00 sec)
删除多表记录
- DELETE 表1别名, 表2别名
- FROM 表1 AS 表1别名
- INNER | LEFT | RIGHT JOIN 表2 AS 表2别名
- ON 连接条件
- WHERE 筛选条件
- # 删除9号部门中所有的员工
- mysql> delete e
- -> from employees as e
- -> inner join departments as d
- -> on e.dept_id=d.dept_id
- -> where d.dept_id=9;
- Query OK, 2 rows affected (0.00 sec)
TRUNCATE TABLE 表名
- # 清空wage_grade表
- mysql> truncate table wage_grade;
- Query OK, 0 rows affected (0.01 sec)
作者:一个小运维
链接:https://www.jianshu.com/p/b876ea9a40eb
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。