DML
数据操纵语言(Data Manipulation Language, DML)是用于数据库操作,对数据库其中的对象和数据运行访问工作的编程语句,通常是数据库专用编程语言之中的一个子集,简单来说就是增删改查。
INSERT 实现数据的 插入
DELETE 实现数据的 删除
UPDATE 实现数据的 更新。
- 插入数据insert
完整插入:
语法:insert into 表名 values(字段1 类型1,字段2 类型2);
部分插入:
语法:insert into 表名 (列名,列名) values(字段1 类型1,字段1 类型1) - 更新数据update
语法:update 表名 set 列名=数值 where 条件
修改MySQL数据库管理员root账户的密码
mysql> Update mysql.user set authentication_string=password(‘ZhongGuo@123’) where user=’root’
- 删除数据库delete
语法:delete from 表名 where id=2
DQL
在MySQL管理软件中,可以通过SQL语句中的DQL语言来实现数据的
Select查询操作,互联网用户查询余额,查询装备,查询商品的操作。
Mysql查询
- 准备环境
- 创建库:
mysql> create database company
- 创建表:
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> desc employee5;
- 插入数据:
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),
('aofa','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);
2. 简单查询
查看所有列:select * from 表名;
查看部分列:select 列1,列2,列3, from 表名;
注意:前提是需要进入数据库,不进入数据库,就输入库名。
例:看年薪:
mysql> select name,salary,salary*14 from employee5;
- 单条件查询where:
查询hr部门的员工名字
mysql> Select name,post from employee5 where post=’hr’;
- 多条件查询and/or
查询hr部门员工名字,工资大于1000
mysql> Select name,salary from employee5 where post=’hr’ and salary>1000;
查询部门所有员工,工资是6000或8000的员工
mysql> Select name,salary from employee5 where salary=6000 or salary=8000;
- 关键字between and 在什么之间
查询工资在5000~15000的员工
mysql> Select name,salary from employee5 where salary between 5000 and 15000;
工资不在5000~15000范围 使用not between 5000 and 15000;
- 关键字in集合查询
工资4000,或5000,或9000的员工
mysql> Select name,salary from employee5 where salary in(4000,5000,9000);
- 关键字is null
没有岗位描述的员工
mysql> Select name,job_description from emloyee5 where job_description is null;
- 关键字like 模糊查询
查询叫a的员工
mysql> Select name from employee5 where name like ‘a%’;
- 查询排序
工资升序排列
Select * from 表名 order by 工资列名 asc;
工资降序排列
Select * from 表名 order by 工资列名 desc;
工资最高的前五名
Select * from 表名 order by 工资列名 desc limit 5;