007.DML之insert
1.DML
数据操作语言;
DML操作的主体是表中的数据(记录),操作分为四种(CURD)
insert,update,delete,select;
2.insert 语句
语法:
insert [into] table_name [列名] values(值...);
3.insert语句的小结
使用insert语句每次必须插入一条完整的记录,不能只插入部分列的值
插入任何记录,都必须满足这个表中的所有完整性约束(pk,fk,unique,not nlll)
可以使用insert插入指定字段的值,但其它的字段必须可以为空或有default值;
使用insert也可以一次插入多个记录;
008.DML之select
1.select查询;
简单语法
select distinct 列名|* from 表
where 条件语句;
2.案例
select * from tb_student //查询tb_student 表中的所有的数据;
select studentname,degree,salary*14 as salary from tb_student //只列出学生姓名,学历和年薪这三个列的值
select distinct degree from tb_student: 列出有多少种学历;使用distinct时行剔重
select * from tb_student where salary>1000; 条件查询
select * from tb_student where studentName like '%玉%';
select * from tb_student where studentName REGEXP '^[A-Za-z]{3,4}$'; :使用正则
3.where中的语法
比较运算符 > ,<, >=, <= ,!= ,=
逻辑运算符 and,or,not
between and :在某个闭区间范围内;
in,not in:在不连续的范围内
is null:不为空
like :模糊查询; _ :任意一个字符 %:任意多个字符; sname like '%玉%';
regexp:正则表达式 字段 regexp ‘正则’
009.select2
1.聚合函数
聚合函数也叫集合函数或分组函数;
一共有5个;
count():计数
sum():求和
avg():平均值
max():最大值
min():最小值
2.分组
group by 列或表达式;
3.统计出每种学历的平均工资和人数;
selec degree,count(*),avg(salary) from tb_student group by degree;
010.select3
1.where 与 having的区别?
在一个语句如果同时存在where 与 having,那么区别如下
where是在group by 之前执行,而having是在group by之后执行;
where中不能使用分组函数;
2.对查询结果进行排序可以使用order by
ordery by 表达式 [asc|desc]
3.limit:获得部分查询结果
limit start,size;
011.DML修改和删除
1.update的语法
update 表名 set 列名=值,列名2=值。。。
[where 条件 ]
2.delete 语法
delete from 表名
[where 条件]
3.表复制
根据查询结果创建一个新表;
create table 表名 select 查询语名
注意:这种语法只会复制结构及数据,并不会复制任何约束;