1.3 MySQL数据操作DML
在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括使用INSERT实现数据
的插入、DELETE实现数据的删除以及UPDATE实现数据的更新。
更新数据 insert
更新数据 update
删除数据 delete
一、插入数据INSERT
1. 插入完整数据(顺序插入)
语法一:
INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES (值1,值2,值3…值n);
语法二:
INSERT INTO 表名 VALUES (值1,值2,值3…值n);
2. 指定字段插入数据
语法:
INSERT INTO 表名(字段2,字段3…) VALUES (值2,值3…);
3. 插入多条记录
语法:
INSERT INTO 表名 VALUES
(值1,值2,值3…值n),
(值1,值2,值3…值n),
(值1,值2,值3…值n);
4. 插入查询结果
语法:
INSERT INTO 表1(字段1,字段2,字段3…字段n)
SELECT (字段1,字段2,字段3…字段n) FROM 表2
WHERE …;
例子:
mysql> create table student7(id int,name varchar(20),sex enum('m','FROM'),age int(2));
mysql> desc student7;
+-------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| sex | enum('m','FROM') | YES | | NULL | |
| age | int(2) | YES | | NULL | |
+-------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into student7 values(1,'jack','m',20); \\顺序插入数据
mysql> insert into student7(name,age) values('bob',21); \\指定字段插入数据
mysql> insert into student7 values(6,'jex','m',21),(7,'bob1','FROM',22); \\插入多条记录
插入查询结果:
mysql> create table student_his(id int,name varchar(20),sex enum('m','FROM'),age int(2));
mysql> insert into student_his SELECT * from student7 WHERE name = 'bob'; \\插入查询结果
mysql> insert into student_his SELECT * from student7 WHERE age > 17; \\插入符合条件的
二、更新数据UPDATE
语法:
UPDATE 表名 SET 字段1=值1,字段2=值2 WHERE CONDITION;
示例:
mysql> update student7 set id=8; #修改全部
mysql> SELECT * from student7;
+------+------+------+------+
| id | name | sex | age |
+------+------+------+------+
| 8 | jex | m | 21 |
| 8 | bob1 | FROM | 22 |
+------+------+------+------+
2 rows in set (0.00 sec)
mysql> update student7 set id=9 WHERE name="bob1"; 指定条件
mysql> SELECT * from student7;
+------+------+------+------+
| id | name | sex | age |
+------+------+------+------+
| 8 | jex | m | 21 |
| 9 | bob1 | FROM | 22 |
+------+------+------+------+
2 rows in set (0.00 sec)
mysql> update student7 set id=10,name="newrain" WHERE name="bob1";
mysql> SELECT * from student7;
+------+----------+------+------+
| id | name | sex | age |
+------+----------+------+------+
| 8 | jex | m | 21 |
| 10 | newrain | FROM | 22 |
+------+----------+------+------+
2 rows in set (0.01 sec)
三、删除数据DELETE
语法:
DELETE FROM 表名
WHERE CONITION;
示例:
DELETE FROM mysql.user
WHERE authentication_string=’’;
delete from 表名 WHERE 条件;
mysql> delete from student7 WHERE name="jack";
delete from 表名; //删除表的全部数据
mysql> delete from student7;
Query OK, 3 rows affected (0.00 sec)
mysql> SELECT * from student7;
Empty set (0.01 sec)
作业: 更新MySQL root用户密码NewRain!@#
注:表的修改练习作为课下作业
二、MySQL数据库的查询操作
MySQL数据库查询
2.1、MySQL单表查询
准备测试表:company.employee5
| 雇员编号 | id | int | | -------- | --------------- | ------------ | | 雇员姓名 | name | varchar(30) | | 雇员性别 | sex | enum | | 雇用时期 | hire_date | date | | 雇员职位 | post | varchar(50) | | 职位描述 | job_description | varchar(100) | | 雇员薪水 | salary | double(15,2) | | 办公室 | office | int | | 部门编号 | dep_id | int |
mysql> CREATE TABLE company.employee5(
id int primary key AUTO_INCREMENT not null,
name varchar(30) not null,
sex enum('male','female') default 'male' not null,
hire_date date not null,
post varchar(50) not null,
job_description varchar(100),
salary double(15,2) not null,
office int,
dep_id int
);
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
('jack','male','20180202','instructor','teach',5000,501,100),
('tom','male','20180203','instructor','teach',5500,501,100),
('robin','male','20180202','instructor','teach',8000,501,100),
('alice','female','20180202','instructor','teach',7200,501,100),
('tianyun','male','20180202','hr','hrcc',600,502,101),
('harry','male','20180202','hr',NULL,6000,502,101),
('emma','female','20180206','sale','salecc',20000,503,102),
('christine','female','20180205','sale','salecc',2200,503,102),
('zhuzhu','male','20180205','sale',NULL,2200,503,102),
('gougou','male','20180205','sale','',2200,503,102);
mysql> SELECT 字段名称,字段名称2 from 表名 条件
mysql> SELECT column_name,column_2 from table WHERE ...
简单查询:
mysql> SELECT * from t3;
mysql> SELECT name, salary, de