DML(Data Manipulation Language)是指数据操作语言,用来对数据库中表的数据进行增删改操作,主要包含如下内容:
- 插入:
insert
- 删除:
delete
- 更新:
update
1. Mysql插入数据
-- 语法:
-- 1. 向表中插入某些列,列明和值要一一对应
insert into 表名 (列名1,列名2,列名3...) values (值1,值2,值3...);
-- 2. 向表中插入所有列,在不指定要插入哪些列的时候,表示插入所有列
insert into 表名 values (值1,值2,值3...);
-- 示例:
insert into student(sid,name,gender,age,birth,address,score)
values(1001,'男',18,'1996-12-23','北京',83.5);
insert into student values(1001,'男',18,'1996-12-23','北京',83.5);
2. Mysql修改数据
-- 语法:
-- 1. 直接修改
update 表名 set 字段名=值,字段名=值...;
-- 2. 根据条件修改
update 表名 set 字段名=值,字段名=值... where 条件;
-- 示例
-- 1. 将所有学生的地址修改为重庆
update student set address = '重庆’;
-- 2. 将id为1004的学生的地址修改为北京
update student set address = '北京' where id = 1004;
-- 3. 将id为1005的学生的地址修改为北京,成绩修成绩修改为100
update student set address = '广州', score=100 where id = 1005;
3. Mysql删除数据
-- 语法:
-- 1. delete可以根据条件进行删除表中数据,删除内容
delete from 表名 [where 条件];
-- 2. truncate删除表是将整个表数据清空
truncate table 表名 或者 truncate 表名;
-- 示例
-- 1.删除sid为1004的学生数据
delete from student where sid = 1004;
-- 2.删除表所有数据
delete from student;
-- 3.清空表数据
truncate table student;
truncate student;
注意:delete只删除内容,而truncate类似于drop table ,可以理解为是将整个表删除,然后再创建该表;