DML是对表中的数据进行增、删、改操作:insert update delete
小知识:1)在mysql中,字符串类型和日期类型都要用单引号括起来,'tom' '2015-09-04'
2)空值类型有两种 : null==不占内存 " "==占内存
3)查看数据库编码的具体信息 Show variables like 'CHARACTER%';
4)临时更改客户端和服务器结果集的编码
SET character_set_client=utf8;
Set character_set_results=utf8;
一、准备工作(一)
1.创建数据库
CREATE DATABASE mydb1;
2.创建emp表:
create table emp(
id int,
name varchar(10),
gender varchar(10),
birthday date,
salary float(10,2),
entry_date date,
resume text
);
3.插入数据
INSERT INTO emp(id,name,gender,birthday,salary,entry_date,resume)
VALUES(1,'zhangsan','female','1990-5-10',10000,'2015-5-5-','good girl');
INSERT INTO emp(id,name,gender,birthday,salary,entry_date,resume)
VALUES(2,'lisi','male','1995-5-10',10000,'2015-5-5','good boy');
INSERT INTO emp(id,name,gender,birthday,salary,entry_date,resume)
VALUES(3,'你好','male','1995-5-10',10000,'2015-5-5','good boy');
二、DML具体操作(重要)
1.查看表中的所有数据
select * from emp;
2.修改操作(update)
语法:UPDATE 表名 SET 列名1=列值1,列名2=列值2 。。。 WHERE 列名=值
1)将所有员工薪水修改为5000元。
update emp set salary=5000;
2)将姓名为’zhangsan’的员工薪水修改为3000元。
update emp set salary=3000 where name='zhangsan';
3)将姓名为’你好’的员工薪水修改为4000元,resume改为very good。
update emp set salary=3000,resume='very good' where name='你好';
4)将lisi的薪水在原有基础上增加1000元。
update emp set salary=salary+1000 where name='lisi';
3.删除操作(delete)
语法 : DELETE FROM 表名 【WHERE 列名=值】
1)删除表中名称为’zhangsan’的记录。
delete from emp where name='zhangsan';
2)删除表中所有记录。
delete from emp;
3)使用truncate删除表中记录。
truncate table emp;
注:delete和truncate的区别
DELETE 删除表中的数据,表结构还在;删除后的数据可以找回
TRUNCATE 删除是把表直接DROP掉,然后再创建一个同样的新表。
删除的数据不能找回。执行速度比DELETE快。
三、准备工作(二)
1.创建学生stu表并插入数据
CREATE TABLE stu (
sid CHAR(6),
sname VARCHAR(50),
age INT,
gender VARCHAR(50)
);
INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');
INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');
INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');
INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');
INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');
INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');
INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');
INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');
INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');
INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');
INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);
2.创建雇员emp表并插入数据
CREATE TABLE emp(
empno INT,
ename VARCHAR(50),
job VARCHAR(50),
mgr INT,
hiredate DATE,
sal DECIMAL(7,2),
comm decimal(7,2),
deptno INT
);
INSERT INTO emp values(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);
INSERT INTO emp values(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO emp values(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO emp values(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO emp values(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO emp values(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO emp values(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO emp values(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);
INSERT INTO emp values(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO emp values(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO emp values(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);
INSERT INTO emp values(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO emp values(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO emp values(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);
3.创建部门表dept并插入数据
CREATE TABLE dept(
deptno INT,
dname varchar(14),
loc varchar(13)
);
INSERT INTO dept values(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept values(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept values(30, 'SALES', 'CHICAGO');
INSERT INTO dept values(40, 'OPERATIONS', 'BOSTON');
四、DQL的具体操作(重要)
数据库执行DQL语句不会对数据进行改变,而是让数据库发送结果集给客户端。
查询返回的结果集是一张虚拟表。
查询关键字:SELECT
语法: SELECT 列名 FROM 表名
【WHERE --> BROUP BY -->HAVING--> ORDER BY】
语法:
SELECT selection_list /*要查询的列名称*/
FROM table_list /*要查询的表名称*/
WHERE condition /*行条件*/
GROUP BY grouping_columns /*对结果分组*/
HAVING condition /*分组后的行条件*/
ORDER BY sorting_columns /*对结果分组*/
LIMIT offset_start, row_count /*结果限定*/
1.基础查询
1.1查询所有列
select * from stu;
1.2查询指定列
select sid,sname,gender from stu;
2.条件查询介绍
2.1 条件查询就是在查询时给出WHERE子句,在WHERE子句中可以使用如下运算符及关键字:
=、!=、<>、<、<=、>、>=;
BETWEEN…AND;
IN(set);
IS NULL;
AND;
OR;
NOT;
2.2 查询性别为女,并且年龄小于50的记录
select * from stu where gender='female' and age<50;
2.3 查询学号为S_1001,或者姓名为liSi的记录
select * from stu where sid='S_1001' or sname='lisi';
2.4 查询学号为S_1001,S_1002,S_1003的记录
select * from stu where sid='S_1001' or sid='S_1002' or sid='S_1003';
或者
select * from stu where sid in('S_1001','S_1002','S_1003');(常用)
2.5 查询学号不是S_1001,S_1002,S_1003的记录
select * from stu where sid not in('S_1001','S_1002','S_1003');(常用)
或者
select * from stu where sid!='S_1001' and sid!='S_1002' and sid!='S_1003';
2.6 查询年龄为null的记录
select * from stu where age is null;
2.7 查询年龄在20到40之间的学生记录
select * from stu where age >=20 and age<=40;
或者
select * from stu where age between 20 and 40;
2.8 查询性别非男的学生记录
select * from stu where gender!='male';
或者
select * from stu where gender<>'male';
或者
select * from stu where gender='female';
2.9 查询姓名不为null的学生记录
select * from stu where sname is not null;(常用)
或者
select * from stu where not sname is null;
如有错误,还请指正哦~