标题 ## MySQL DML 知识点总结
创建一个新表:
create table studentinfo(
sno int primary key not null auto_increment,
sname varchar(20) not null,
gender char(1) not null,
age int null,
course varchar(10) null,
score float null
)
DML操纵语言 学会使用标准的SQL对数据进行CRUD增删改查
sql书写要求:
1使用英文字符,不能使用中文字符
2数值类直接填写,字符或者字符串使用引号括起来
3值要和列的约束一致,插入的列字段个数/数据类型和值的个数/数据类型必须一致
1 插入语句
语法:insert into 表名(字段名,字段名, … ) values(字段值,字段值,…);
①插入部分字段(非空字段必须填)
insert into studentinfo(sno,sname,gender) values(1,‘大力’,‘男’);
insert into studentinfo(sno,sname,gender,age) values(2,‘景斌’,‘男’,18);
insert into studentinfo(sno,sname,age,gender) values(3,‘伟奇’,18,‘男’);
②插入NULL值 空值,没有填入数据
insert into studentinfo(sno,sname,gender,age) values(4,‘苏帆’,‘男’,null);
③插入空串 有填入数据,但是数据没有内容
insert into studentinfo(sno,sname,gender,age,course) values(5,‘华彬’,‘女’,null,’’);
④插入全部字段,可以省略插入的字段列表
要求:插入的字段值的顺序必须和表结构中列的顺序完全一致。
insert into studentinfo(sno,sname,gender,age,course,score) values(6,‘于晨’,‘女’,19,‘JAVA’,99);
insert into studentinfo() values(7,‘于晨’,‘女’,19,‘JAVA’,99);
insert into studentinfo values(8,‘于晨’,‘女’,19,‘JAVA’,99);
⑤批量插入 insert into 表名(字段1,字段2,…) values (第1条记录的值1,第1条记录的值2),(第2条记录的值1,第2条记录的值2),…
insert into studentinfo
values(9,‘老师1’,‘男’,19,‘JAVA’,99)
,(10,‘老师2’,‘男’,29,‘PHP’,69)
,(11,‘老师3’,‘女’,39,‘IOS’,100);
⑥auto_increment 实现列自增,针对数值类型的列
第一步设计表:为指定整数数值列sno添加自动自增的要求
第二步:插入语句时sno字段因为设置了自动递增,可以将该字段的值使用null代替。
或者指定插入的字段列表,不对sno进行插入;
insert into studentinfo values(null,‘王嵩’,‘男’,25,‘JAVA’,79);
insert into studentinfo(sname,gender,age,course,score) values(‘王嵩’,‘男’,25,‘JAVA’,79);
2、 更新语句
语法: update 表名 set 列名=新值,列名=新值 where 子句; where表示更新的条件可以省略
①修改单个字段,不带where条件,匹配就是全表的记录
修改studentinfo表中的年龄全部为18
update studentinfo set age=18;
②修改多个字段,不带where条件,匹配就是全表的记录
修改studentinfo表中的年龄全部为19,性别为男
update studentinfo set age=19,gender=‘男’;
③修改多个字段,带where条件,限制只更新匹配的行
修改studentinfo表中大力同学的年龄为16,性别为女
update studentinfo set age=16,gender=‘女’ where sname = ‘大力’;
修改studentinfo表中女同学王嵩的年龄为36
update studentinfo set age=36 where gender=‘女’ and sname=‘王嵩’; – and 表示2个条件同时满足
修改studentinfo表中男同学王嵩的年龄为36
update studentinfo set age=36 where gender=‘男’ and sname=‘王嵩’;
3、删除数据语句
语法: delete from 表名 where 子句;
①删除studentinfo表中的所有记录,不带where
delete from studentinfo;
②带where条件,则删除匹配的行
删除女同学王嵩
delete from studentinfo where sname = ‘王嵩’ and gender = ‘女’;
数据库范式 :
第一范式:任何列不可分割,一个字段是一个元数据;
第二范式:必须有主键,不能存在部分依赖
第三范式:不能存在传递依赖
事务的特征:
数据库的事务由一个或多个DML/一个DDL/一个DCL语句组成,以begin语句的开始执行,以commint/rollback/DDL/DCL语句之一结束,或者用户会话正常结束或者系统异常终了