MySQL入門介紹
DML-資料操作語言
是指對錶中的資料的增,刪,改
- 插入-insert
語法:insert into 表名(列名,列名,…)values(值1,值2,…)
注意:
列名順序與原表無關,但是列要在表中存在
要插入的列,不需要是全表列.可只列出需要插入的欄位名
值與列的順序,個數,型別一致
字串值,日期值需要使用單引號’’,例如’張三’ ‘2021-01-07’
可以插入空值null(前提是列允許null值)
插入全表時,可以省略列名,值的賦值是按照原表列順序賦值
insert into student (id,username,birthday)
values (2,'李四','2021-01-07'); insert into student (id,username,birthday) values ('3','李四','2021-01-07'); insert into student (id,username,birthday) values (4,4,'2021-01-07'); insert into student (id,username,birthday) values (5,'王五',null); INSERT INTO student ( id,username,age,sex,birthday,score) VALUES(6,'趙六',19,'男','2021-01-7',99.0); -- 省略列名,要與原表列順序一致 insert into student values (7,'趙六7',19,'男','2021-01-07',98.0); -- 資料長度要與建立時一致 insert into student values (8,'趙六8',20,'男','2021-01-07',999.99); -- insert into student values (8,'趙六8',20,'男','2021-01-07',9999.99);
- 更新-update
語法:update 表名 set 欄位1= 值1,欄位2=值2,··· 【where 欄位=值】;
-- 更新 -- 將學號為1的學生年齡改為20,成績改為100 update student set age = 20,score = 100 where id = 1; -- 將所有女生的年齡改為18 update student set age = 18 where sex = '女' -- 將學號為7的學生的成績加2分 update student set score = score + 2 where id = 7; -- 不加條件,全表更新 update student set birthday = '2021-01-06';
- 刪除
語法:delete from 表名 【where 欄位=值】;
根據需求加上條件,只刪除符合條件的資料,如果不加,則會刪除全表資料
-- 刪除學生表姓名為張三的資料 delete from student where username = '張三'; delete from student where username = '李四'; -- 如果不加條件,則會刪除全表,清空表中資料 delete from student; truncate TABLE stu; ps: 工作中,不能真的刪除資料,資料很重要,後期可能會做資料收集,處理,分析等等工作。那麼,如何實現“刪除”? 設計表時,給表設計多設計一個欄位:狀態欄位status 一般定義狀態碼:1正常 2不正常 或者 1普通使用者 2普通會員 3黃金會員 7已登出 在點選按鈕刪除時,其實是更新欄位的狀態
- 約束
約束:對插入資料的約束。一般在建立表時指定約束,然後在插入值時按照約束賦值。
約束型別:主鍵約束,唯一性約束,預設值約束,非空約束,外來鍵約束(引用完整型約束)
資料型別 自增約束
create table 表名(
欄位 資料型別(長度) 約束,
欄位 資料型別(長度)約束1 約束2,
欄位 資料型別(長度)
);
主鍵約束+自增約束 primary key 主鍵 + auto_increment 自動增長
主鍵約束:
- 主鍵列值不能重複,即唯一
- 主鍵值不能為空
- 主鍵是索引(index)
一張表中可以設定兩個或多個主鍵,它們是一個整體,叫聯合主鍵
注意: 聯合主鍵的所有列值重複,才算重複
一般主鍵都會設定為自增. 如果不給主鍵設定值,則會根據上條資料主鍵值,自增1
create table stu1( id int(11) primary key, -- 給id新增主鍵約束 name varchar (10) ); create table stu2( id int(11), age int(11), primary key(id,age) -- 聯合主鍵 ); create table stu3( id int primary key auto_increment, -- 給id新增主鍵約束+自增約束 name varchar(10) ); insert into stu1 (id) values (1);-- 沒有主鍵的列值,可以重複 # Duplicate entry '1' for key 'PRIMARY' insert into student (id) values (1); insert into student (id) values (1); -- 主鍵不能為空 -- insert into stu1 (name) values ('張三'); -- 主鍵自增,不插入值,會自動增長 insert into stu3 (name) values ('張三'); insert into stu3 (name) values ('李四'); -- 刪除一行資料,自增的資料會按照刪除之前繼續自增 delete from stu3 where id = 2; insert into stu3 (name) values ('王五'); 刪除主鍵 ALTER TABLE stu DROP PRIMARY KEY; 新增主鍵 ALTER TABLE stu MODIFY id INT PRIMARY KEY; 刪除自動增長 ALTER TABLE stu MODIFY id INT; 新增自動增長 ALTER TABLE stu MODIFY id INT AUTO_INCREMENT;
唯一約束 unique 使該列的值不能重複
create table stu4( id int , name varchar(10) unique );
資料型別約束 資料型別約束即,限制該列儲存的資料型別,不符合不能存入!
非空約束 not null 在插入時該列值不能為空
create table stu5( id int, name varchar(10) not null );
預設值約束
預設值 default xx 當該列不賦值時,預設賦值為xx.
DBMS如果建立時不指定約束,那麼每個列的值預設為null
create table stu6( id int, name varchar(10), sex char(1) default '男' );
註釋
ps: 建表時在欄位後 使用comment ‘註釋’,可以對該列註釋
create table stu7( id int comment '主鍵', -- 註釋為主鍵 name varchar(10) comment '姓名', -- 註釋為姓名 sex char(1) default '男' );
引用完整性約束 —>外來鍵
建立學生表stu,欄位sid,sname,age
建立課程表course 欄位cid,cname,sid
假設一個學生可以有多個課程,所以在課程表中設計了stu表的主鍵列sid
create table stu( sid int primary key auto_increment comment ‘學生主鍵’, sname varchar(10) comment ‘學生姓名’, age int comment ‘學生年齡’ ); create table course( cid int primary key auto_increment comment ‘課程主鍵’, cname varchar(10) comment ‘課程名稱’, sid int comment ‘關聯學生表id’ constraint fk_course_stu foreign key (sid) references stu(sid) );
設計了外來鍵後,課程表中的sid欄位就與學生表的sid欄位有聯絡.
父表: stu表 (被引用的表)
子表: course表 (引用表)
- 插入時:
- 學生表可以隨意插入值
- 課程表插入值時,sid欄位只能插入學生表中有的sid值
- 刪除或更新時:
- RESTRICT: 如果想要刪除/更新父表的記錄,子表中有關聯該父表記錄的,則不允許刪
- 除父表記錄
1. NO ACTION: 同restrict一樣,刪除時先檢查外來鍵.如果有引用,不能刪除
2. CASCADE: 級聯,父表刪除/更新時,如果有子表有關聯,那麼子表父表資料一起刪除/更 新
3. SET NULL: 置空,父表刪除/更新時,如果子表有關聯,那麼子表該條記錄置為null
引用完整性約束-外來鍵(foreign key)
建立外來鍵的方式:
在建表時直接指定 ,在引用了別的表字段的表中新增外來鍵約束
constraint 外來鍵名 foreign key(當前表字段) references 其他表(欄位)
建好表後再指定
alter table 表名 add constraint 外來鍵名 foreign key (欄位) references 其他表(欄位)
create table stu( sid int primary key auto_increment comment '學生主鍵', sname varchar(10) comment '學生姓名', age int comment '學生年齡' ); create table course( cid int primary key auto_increment comment '課程主鍵', cname varchar(10) comment '課程名稱', sid int comment '關聯學生表id', constraint fk_course_stu foreign key(sid) references stu(sid) ); alter table course add constraint fk_course_stu foreign key (sid) references stu(sid);
DQL
DQL是指查詢語言,從資料表中通過一系列條件將符合條件查出.
DQL查詢語句只是檢視資料,不會對原表有任何影響.
查詢的結果是一張虛擬表.
查詢的關鍵字: select
語法: select 欄位1,欄位2,… from 表名【where… group by… having… order by… limit…】
語法:select selection_list --要查詢的列 from table_name -- 要查詢的表名 where condition -- 過濾行條件 group by grouping_clumns -- 對結果按照列進行分組 having condition -- 分組後再過濾 order by sort_column -- 排序 limit offset,row_count -- 對結果限制
- 基礎查詢
-- ========= 基本查詢 ========= -- 查詢所有列,將所有列名寫出即可 select sname,sex,sid,score,age,cid,groupLeaderId from stu; -- 查詢所有列,可以使用* 代替所有列名 select * from stu; -- 查詢指定 列 -- 查詢學生學號,姓名,年齡 select sid,sname,age from stu; select sname from stu;
- 條件查詢
– ============== 條件查詢 =================
條件查詢就是查詢時在基礎查詢的後面使用
where語句,where後使用運算子將符合條件的資料保留
= 相等
!= <> 不等
<小於 >大於 <= >=
and 和/與 兩邊的條件同時成立才行
or 或 兩邊的條件有一個成立即可
in(set) 範圍 在set集合範圍內
between 值1 and 值2 在…之間… (包含臨界值,從小到大)
not in 取反
-- 查詢學號為1001的學生資訊 select * from stu where sid = 1001 -- 查詢學生成績大於60的學生id 姓名,成績 select sid,sname,score from stu where score > 60; -- 查詢學生性別為女,並且年齡小於50的記錄 select * from stu where sex = '女' and age < 50 -- 查詢學生學號為1001,或者姓名為李四的記錄 select * from stu where sid = 1001 or sname = '李四' -- 查詢學號為1001,1002,1003的記錄 -- select * from stu where sid = 1001 or sid = 1002 or sid = 1003 select * from stu where sid in (1001,1002,1003) -- 查詢學號不是1001,1002,1003的記錄 select * from stu where sid not in (1001,1002,1003) select * from stu where sid != 1001 and sid != 1002 and sid != 1003 -- 查詢學生年齡在20到40之間的學生記錄 select * from stu where age >= 20 and age <= 40; select * from stu where age between 20 and 40; -- 查詢性別非男的學生記錄 select * from stu where sex != '男' -- select * from stu where sex = '女'
- 模糊查詢
模糊查詢需要放在where後使用like關鍵字
例如 where 欄位 like ‘%關鍵字’
例如 where 欄位 like ‘_關鍵字’
萬用字元:
% ,可以匹配任何個數任意字元
_ ,可以匹配一個任意字元
– 查詢姓名以“張”開頭的學生記錄
select * from stu where sname like '張%'; select * from stu where sname like '張_'; select * from stu where sname like '張__'; -- 查詢姓名中包含“三”的學生記錄 select * from stu where sname like '%三%'
- 排序查詢
排序查詢:將查詢到的的結果按照一定的順序排序
order by 欄位 [desc|asc] ==> desc 降序,asc升序
例如: order by 欄位 desc
例如: order by 欄位 asc
-- 查詢所有學生記錄,按年齡升序排序 select * from stu order by age asc -- 查詢所有學生記錄,按年齡降序排序 select * from stu order by age desc -- 不寫排序型別,預設是升序 select * from stu order by age -- 查詢所有學生記錄,按年齡升序排序,如果年齡相同時,按編號降序排序 select * from stu order by age asc,sid desc -- 查詢成績大於60的學生id,姓名,成績,並根據成績降序 select sid,sname,score from stu where score > 60 order by score desc;
- 聚合函式
聚合函式
count(列名) 統計指定列不為null的行數
max(列名) 獲得指定列中最大值,如果是字串,按照字元順序
min(列名) 獲得最小
sum(列名) 計算指定列的和值,計算非數字,結果是0
avg(列名) 計算指定列的平均值,計算非數字,結果是0
語法: select 聚合函式 from 表名 【where…】
查詢stu表中記錄數: select count(sid) from stu; update stu set score =null where sid =1001; select count(sex) from stu; -- 在列名後使用as 給列取別名,as可以省略 select count(sid) as '記錄數' from stu; select count(sid) '記錄數' from stu; -- 查詢stu表中有成績的人數: select count(score) '人數' from stu; -- 查詢stu表中成績大於60的人數: select count(*) from stu where score > 60; -- 查詢所有學生成績和: select sum(score) from stu; select sum(score) '總成績' from stu; -- 統計所有學生平均成績 select avg(score) from stu; -- 統計stu表中成績大於60的平均分 select avg(score) from stu where score > 60 -- 查詢最高成績和最低成績: select max(score) '最高分',min(score) '最低分' from stu; -- 統計stu表中成績大於60的最高成績和最低成績: select max(score) '最高分',min(score) '最低分' from stu where score > 60; -- 查詢最高成績,以及學生姓名 /* 與聚合函式一同出現的列名,必須出現在group by後 反之,如果出現了一個沒有在groupby後出現的欄位,那麼查詢結果不正常! */ select max(score) '最高分',sname from stu; select sum(age) from stu; -- 490 select sum(score) from stu; -- 742 -- 計算總年齡與總成績的和 select sum(age)+sum(score) from stu; select sum(age + score) from stu;
- 去重
去重函式 distinct(列) 將指定列的資料去重
-- 不重複的年齡 select distinct(age) from stu; -- 查詢年齡不重複的共有多少人 select count(distinct age) from stu;