MySQL 學習筆記01
一:MySQL簡介
MySQL是一種中型的、開放原始碼的、關係型資料庫管理系統(DBMS)。
進入MySQL環境的方法:mysql -h 主機地址 -u 使用者名稱 -p
檢視所有資料庫的命令:show databases;
進入資料庫命令:use 資料庫名;
檢視資料庫中所有表的命令:show tables;
查詢表中所有的資料:select * from 表名稱;注意:如果要使查詢結果格式化,則在SQL後面加上\G, 即:select * from 表名稱\G;
查詢當前所在的資料庫:select database();
檢視資料庫建立語句:show create database 資料庫名;
查看錶建立語句:show create table 表名稱;
查看錶結構:desc 表名稱;
二:基本SQL:
SQL(Structed Query Language):結構化查詢語言,專門用來操作關係型資料庫的 語言。
1. 建立資料庫
create database 資料庫名稱 [default character set utf8];
1.建立mydb資料庫 create database mydb default character set utf8; 2.進入mydb資料庫 use mydb; 3.檢視建立mydb的語法 show create database mydb;
2.建立表
create table 表名稱( 欄位名1 資料型別 [約束], 欄位名2 資料型別 [約束], ...... 欄位名n 資料型別 [約束],);
4.建立學生表 create table students( stuid integer primary key auto_increment, name varchar(20) not null, age int, sex char(5), score double not null );
3.插入記錄:
方式一:(指定欄位名)insert into 表名稱(欄位名1,欄位名2,...欄位名n)values(值1,值2,...值n);
方式二:(未指定欄位名)insert into 表名稱 values(值1,值2,...值n);
方式三:(指定欄位名,一條SQL插入多條記錄)insert into 表名稱(欄位名1,欄位名2,...欄位名n)values (值1,值2,...值n),(值1,值2,...值n)...(值1,值2,...值n);
方式四:(未指定欄位名,一條SQL插入多條記錄)insert into 表名稱 values(值1,值2,...值n), (值1,值2,...值n)...,(值1,值2,...值n);
5.插入記錄: (指定欄位名) insert into students(name,age,sex,score)values('風清揚',20,'男',93.5); (未指定欄位名) insert into students values(10,'隆美爾',25,'男',83); (指定欄位名,一條SQL插入多條記錄) insert into students(name,age,sex,score)values('張學良',22,'男',60), ('蔣介石',25,'男',85); (未指定欄位名,一條SQL插入多條記錄) insert into students values(16,'孫悟空',25,'男',89), (19,'豬八戒',23,'男',69);
4.刪除記錄
delete from 表名稱 [where 條件];
1. 刪除成績小於70的記錄 delete from students where score<70;
刪除年齡為25歲,且成績為85的記錄 delete from students where age=25 and score=85;
5.修改記錄
update 表名稱 set 欄位名=欄位值[,欄位名=欄位值,...][where 條件]; 3. 將名字為'孫悟空'的記錄的成績修改為60,年齡修改為35 update students set score=60,age=35 where name='孫悟空';
6.查詢記錄
select 欄位名1,欄位名2,.. from 表名稱 [where 條件];
4. 查詢成績在80到90之間的記錄 方式一: select * from students where score>=80 and score<=90; 方式二: select * from students where score between 80 and 90;
7.sql資料型別
MySQL常用資料型別:
varchar(n) 可變長度的字串型別
char(n) 定長字串
integer/int 整數型別
double 雙精度小數型別
date 日期型別 在資料庫表中的格式為'YYYY-MM-DD'
datetime 日期時間型別 在資料庫表中的格式為'YYYY-MM-DD HH:MM:SS'
注意:date型別與datetime型別演示
create table temp( id int primary key auto_increment, d date not null, dt datetime not null ); 插入記錄: insert into temp(d,dt)values('','1937-07-07 02:32:58'); insert into temp(d,dt)values('20180910','19370707023258'); insert temp(d,dt)values('1937-07-07 02:32:58','-09-10'); insert into temp(d,dt)values(current_date(),now()); insert into temp(d,dt)values(current_date(),sysdate());
三:MySQL中的聚合函式
應用場景:分組查詢
1. max(欄位名) 查詢某個欄位的最大值 2. min(欄位名) 查詢某個欄位的最小值 3. avg(欄位名) 查詢某個欄位的平均值 4. sum(欄位名) 查詢該欄位對應值的和 5. count(欄位名) 查詢某個欄位中不為null的記錄數 6. count(*) 查詢總記錄數
四:分組查詢
說明:根據分組欄位,對不同的組進行資料統計查詢 語法: select 欄位名1,欄位名2...,[聚合函式] from 表名稱 group by 分組欄位 [having 條件];
一:先建立product產品表 create table product( proid int primary key auto_increment, name varchar(20) not null, price double not null, type varchar(20) not null, note text ); ``` 插入記錄 insert into product(name,price,type,note)values ('辣條',3.5,'零食',null), ('洗衣粉',12.5,'日用品',null), ('旺旺雪餅',10,'零食','旺旺雪餅最近銷量不好'), ('鉛筆',3,'學習用品',null), ('臉盆',15,'日用品',null), ('文具盒',5,'學習用品','文具盒深受小學生喜愛'); 二:編寫分組查詢語句 按照產品型別進行分組,查詢每一組的平均價格 select type as 產品型別, avg(price) as 平均價格 from product group by type; 查詢“零食”組的平均價格 select type as 產品型別, avg(price) as 平均價格 from product group by type having type='零食';
注意:查詢欄位(select後的欄位)必須包含在分組欄位中; having用來篩選某個分組
五:模糊查詢
語法: select 欄位名1,欄位名2... from 表名稱 where 欄位名 like 模糊條件;
模糊查詢的萬用字元:
% 匹配任意多個任意字元
_ 匹配一個任意字元
1. 查詢所有姓張的學生 select * from students where name like '張%'; 2. 查詢名字中含有'空'的學生 select * from students where name like '%空%'; 3. 查詢名字中包含三個字,並且最後一個是'空'的學生記錄 select * from students where name like '__空';
六:排序查詢
語法: select 欄位名1,欄位名2... from 表名稱 [where 條件] order by 欄位名[desc,欄位名...];
1. 根據成績從高到低將所有學生排序 select * from students order by score desc; 2. 根據成績從高到低(降序)將所有學生排序,在成績相等的情況下 根據年齡從低到高(升序)。 select * from students order by score desc,age;
七:限制查詢
1. 限制數量的查詢
語法: select 欄位名1,欄位名2... from 表名稱 [where 條件] [其他條件] limit 最多記錄數;
查詢前三名的學生記錄 select * from students order by score desc limit 3;
2.通過偏移量和記錄數查詢某一部分記錄
語法:select 欄位名1,欄位名2... from 表名稱 [where 條件] [其他條件] limit 起始偏移量,最多記錄數;注意:第一條記錄的起始偏移量是0;
1.查詢源表中第4~6條記錄 select * from students limit 3,3; 2.查詢第二名與第三名的學生記錄 select * from students order by score desc limit 1,2;
3.分頁查詢
已知:頁碼pagenum與每一頁的最大顯示記錄數pageSize
語法:select 欄位名1,欄位名2... from 表名稱 [where 條件]
[其他條件] limit (pagenum-1)*pageSize,pageSize;
已知每頁最多顯示3條記錄,查詢第二頁應該顯示的記錄 select * from students limit 3,3;
八:修改、刪除表
1. 新增列
語法: alter table 表名稱 add column 欄位名 資料型別 [約束]; 1. 給students表新增home列 alter table students add column home varchar(20) not null;
2.刪除列
語法:alter table 表名稱 drop column 欄位名; 1. 將students表中的home列的約束去掉 alter table students modify column home varchar(20);
3.修改列
語法:alter table 表名稱 modify column 欄位名 資料型別 [約束]; 將students表中的home列刪除 alter table students drop column home;
4.刪除資料表
語法:drop table 表名稱; 刪除students表 drop table students;
5.刪除資料庫
drop database 資料庫名;