DML是指数据操作语言,英文全称是Data Manipulation Language,用来对数据库中表的数据记录进行更新。
关键字:
insert into 表 (列名1,列名2,列名3...) values (值1,值2,值3...); //向表中插入某些 insert into 表 values (值1,值2,值3...); //向表中插入所有列
这里的两种方式,第一种需要指定插入的列,有时候,我们需要把某些值赋值为NULL,不需要插入这些为空的数据,那么此时第一个方式就比较好。
insert into student(sid,name,gender,age,birth,address,score) values(1001,'王小王-123','男',18,'1996-12-23','北京',83.5);
如果要使用第二种方式就需要,将所有的数据全部填充,少一个也不可以的
首先我们准备数据:
update 表名 set 字段名=值,字段名=值...; update 表名 set 字段名=值,字段名=值... where 条件;
第一种方法会将所有的列进行修改,而第二种只会通过where条件进行匹配到对应的数据进行修改
-- 将所有学生的地址修改为重庆 update student set address = '重庆'; -- 讲id为1004的学生的地址修改为北京 update student set address = '北京' where id = 1004 -- 讲id为1005的学生的地址修改为北京,成绩修成绩修改为100 update student set address = '广州',score=100 where id = 1005
delete from 表名 [where 条件]; truncate table 表名 或者 truncate 表名
注意这里的两种语法虽然都是对数据进行删除,但是二者的内部方法却是不一样的;
truncate和delete只删除数据不删除表的结构;drop删除表的结构被依赖的约束(constrain),触发器(trigger),索引(index),依赖于该表的存储过程/函数将保留,但是变为invalid(无效的)状态
-- 1.删除sid为1004的学生数据 delete from student where sid = 1004; -- 2.删除表所有数据 delete from student; -- 3.清空表数据 truncate table student; truncate student;