mysql資料庫基礎學習總結2(DML)
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 查詢語名
注意:這種語法只會複製結構及資料,並不會複製任何約束;