DML(Data Manipulation Language),数据操作语言,用来对数据库中表的数据进行更新。
增删改查(CRUD)是数据库操作的基本功能,分别对应创建(Create)、读取(Read)、更新(Update)和删除(Delete)数据
- 创建(creat)—插入新数据
insert into <tablename>(column1, column2, column3, ...)
values(value1, value2, value3, ...);
eg.往员工数据表中添加一名员工信息,包括id,姓名,部门,工号,年龄,性别:
insert into staff(id,name,department,num,age,gender)
values(1,zhangsan,tp,dj0001,29,male)
id设置了自增,因此可以不插入id,默认或找到之前的最大值在+1
insert into staff(name,department,num,age,gender)
values(lisi,hr,dj0021,39,female)
---李四的id为2
也可以不指定字段,直接插入值,此时表中的每个字段都需要有给定的值
inser into staff values(5,wangwu,hr,dj0007,27,female)
(6,zhanglong,tp,dj0008,29,male);
- 读取(read)—查询数据
select <column1, column2, column3, ...> from <tablename> --一般配合where或having来进行条件判断
select name from staff where name="zhangsan"
select * from <tablename> ---查询所有字段
- 更新(update)—修改数据
update <tablename> set column1=value1,column2=value2,... where condition
update staff set id=2,age=40
update employee set salary = salary + 1000 where name='王五';
-
删除(delete)—删除数据
delete from <tablename> where condition; delete from staff where id=3 ---删除staff表中id=3的数据 delete from staff; ---删除staff表中所有数据 truncate [table] <tablename>; ---清空staff数据,相当于把原staff删除再建一个新的空staff表