寫在前面
關于MySQL的下載安裝和其圖形化軟件Navicat的下載安裝,網上已經有了很多的教程,這里就不再贅述了,本文主要是介紹了關于MySQL數據庫
的基礎知識。
MySQL數據庫
MySQL數據庫基礎
MySQL數據庫概念
MySQL 數據庫: 是一個關系型數據庫管理系統 。
支持SQL語句的數據庫:
① MySQL數據庫; ② SQL Server數據庫; ③ Oracle數據庫; ④ Sybase數據庫; ⑤ DB2數據庫; ⑥ PostgreSQL數據庫; ⑦ 人大金倉數據庫(國產)。
MySQL數據庫的邏輯結構
計算機數據容量計算方式
1、1PB = 1024TB
2、1TB = 1024GB
3、1GB = 1024MB
4、1MB = 1024KB
5、1KB = 1024B
6、1B = 8bits(比特)
PB:拍字節,TB:太字節,GB:千兆字節;MB:兆字節,KB:千字節,B:字節
MySQL數據庫的數據類型
① 整數類型 (bool、 int 、 big int ) ; ② 浮點數類型 ( float 、 double 、 decimal ) ; ③ 字符串類型 ( varchar 、 text 、 blob ) ; ④ 日期類型 ( DateTime(年-月-日 時:分:秒) 、 TimeStamp(時間戳) ) ; ⑤ 其他數據類型 ( enum 等) 。
編寫SQL語句的順序,解釋器編譯SQL語句的順序
編寫SQL語句順序與解釋器編譯SQL語句順序(重點):
如上圖所示,SQL語句的編寫順序為:Select distinct 字段名 from 表名 join 表名 on 連接條件 where 查詢條件 group by 分組字段 having 分組條件 order by 排序條件 limit 查詢位置 查詢條數
編譯器解釋SQL語句的順序為:
from 表名 on 連接條件 join 表名 where 查詢條件 group by 分組字段 having 分組條件 Select distinct 字段名 order by 排序條件 limit 查詢位置 查詢條數
exists與in、內表與外表的區別
內表與外表的區別:
- 建表默認就是內表,如:
create table 指定表名;
- 加
external
就是外表,如:create table external 指定表名;
- 刪掉內表,
HDFS(分布式文件系統)
上的數據被刪掉了;- 刪除外表,
HDFS
上的數據是存在的。
exists 與 in的區別:
exists
的效率比in
查詢要高,因為in
不走索引。
in
適合于外表數據量大而內表數據小的情況;exists
適合于外表小而內表大的情況。
數據庫常用命令
查看函數幫助的、查看警告的命令
- 查看函數說明命令:
help 函數名
,如:help abs;----查看abs()函數幫助 - 顯示數據庫警告的命令:
show warnings
數據庫常用的函數
avg(expression)
:返回字段expression的平均值;max(expression)
: 返回字段expression中的最大值;min(expression)
:返回字段expression中的最小值;sum(expression)
:返回字段expression的總和;count(expression)
: 返回字段expression的行數;replace(string, from_string, new_string)
: string:原始字符串;from_string:要替換的子字符串;new_string:新的替換子字符串;group_concat(expression)
:用于將group by產生的同一個分組中的值連接起來,返回一個字符串結果user()
:返回當前用戶名:select USER();database()
: 返回當前數據庫名:select DATABASE();version()
:返回數據庫的版本號:select VERSION();
新建、選擇、刪除數據庫與新建、顯示、刪除表的命令
- 新建數據庫命令:
create database 數據庫名 character set utf8 collate utf8_general_ci;
注意:
character set
設置編碼格式,collate
校驗編碼格式
-
選擇數據庫命令:
use 數據庫名;
-
刪除數據庫命令:
drop database 數據庫名;
-
新建表命令:
create table 表名;
-
顯示表命令:
show tables;
-
刪除指定表的命令:
drop table 指定表名;
查看指定表的結構的命令
- 查看表的字段與字段值命令:
describe 指定表名;
為指定表添加、修改、重命名、刪除字段的命令
-
為指定表添加一個字段:
alter table 指定表名 add 字段名 字段類型;
-
為指定表修改字段類型:
alter table 指定表名 modify 字段名 字段類型;
-
重命名指定表的字段:
alter table 指定表名 change 原字段名 新字段名 字段類型;
-
為指定表刪除字段:
alter table 指定表名 drop 字段名;
新建表并為表添加一條數據
- 為指定表添加一條數據:
insert into 指定表名(字段名1,字段名2,字段名3,字段名4)values(數據1,數據2,數據3,數據4);
使用MySQL語句的例子
自動插入錄入時間
--新建一個自動記錄時間的消息表
create table test_msg( id int primary key auto_increment comment "主鍵",message longtext comment "消息",cre_time timestamp not null default current_timestamp comment "錄入時間")comment="記錄消息的表";-- 沒有輸入時間,會自動記錄插入數據時系統時間
insert into test_msg(message) values("這是一個重大的新聞");
查詢其他數據庫的表
--編寫一個查詢其他數據庫數據時數據的SQL語句
select from database_name.表名;-- 其中database_name不是當前選擇的數據庫
單表查詢(重點)
-- 新建學生表并插入相應的數據
create table student(
s_id int(10) primary key default 0 comment'用戶ID',
sname varchar(10) comment'用戶姓名',
sex varchar(1) comment'用戶性別',
age int(3) comment'用戶年齡',
s_time DateTime comment'記錄時間',
s_info longtext comment'用戶簡介'
)engine=InnoDB default charset=utf8;-- 查詢學生表中所有數據
select * from student;
select s_id,sname,sex,age,s_time,s_info from student;-- 查詢學生姓名為張浩的信息
select * from student where sname="張浩";
select * from student where sname in("張浩");-- 查詢學生姓名,重復的不要顯示
select distinct(sname) from student;-- 查詢年齡為20-40的學生信息
select * from student where age between 20 and 40;
select * from student where age>=20 and age<=40;-- 查詢以年齡進行(小到大,大到小)排序顯示學生信息
-- 小到大排序:
select * from student where age order by age asc;-- 大到小排序:
select * from student where age order by age desc;-- 查詢姓名以A開頭、以A結尾、包含A、A開頭并且后面跟一個字符的學生信息
-- A開頭:
select * from student where sname like'A%';-- A結尾:
select * from student where sname like'%A';-- 包含A:
select * from student where sname like'%A%';-- A開頭并且后面跟一個字符:
select * from student where sname like'A_';-- 查詢性別不為女的學生信息
select * from student where sex not in ('女');
select * from student where sex !='女';-- 查詢年齡是22、55歲的學生信息
select * from student where age in (22,55);
select * from student where age=22 or age=55;-- 查詢年齡由小到大,在第二到第五的所有學生信息
select * from student where age order by age desc limit 1,4;-- 查詢最新的學生信息
select * from student order by s_time desc limit 1;-- 查詢相同年齡等于2的人數,并顯示其姓名
select age as "年齡",count(age) as "人數",group_concat(sname)
from student where age group by age having count(age)=2;-- 根據學生的年齡查詢顯示學生處于的年齡段
select * ,
case when t1.age between 0 and 18 then 'young'
when t1.age between 19 and 45 then 'middle'
else 'old' end as 'test_age'
from student t1;-- 將李麗的年齡更新100歲
update student set sex='女',age="100" where s_id='97';-- 刪除李麗的數據
delete from student where sname="李麗";-- 清除學生表所有的數據
truncate table student;
笛卡爾積
連接查詢沒有建外鍵時會出現笛卡爾積:如集合A={a,b}, 集合B={1,2} 兩個集合則為笛卡爾積 {(a,1),(a,2),(b,1),(b,2)}。
MySQL 的連接查詢
左連接查詢:
left join 表名 on 連接條件……
-----左表全部保留,右表關聯不上用null
表示;右連接查詢:
right join 表名 on 連接條件……
-----右表全部保留,左表關聯不上的用null
表示;內連接查詢:
inner join 表名 on- 連接條件
(join 表名 on 連接條件
)……-----兩表關聯保留兩表中交集的記錄;全連接查詢:
左連接+ union all +右連接
-----兩表關聯查詢它們的所有記錄;
三表查詢(重點)
-- 創建員工表
create table employee(
id int primary key auto_increment,
name varchar(10) unique,
age int,
salary double,
check(salary>7000 and salary<=15000),
relation_department_name varchar(20));-- 創建部門表
create table department(
id int primary key auto_increment,
department_name varchar(20) unique,
department_person_num int);-- 創建項目表
create table project(
id int primary key auto_increment,
project_name varchar(20),
relation_department_name varchar(20));-- 1、要求-查詢姓名為張三的員工,所在部門的名稱、部門人數、關聯項目的數量、關聯項目的具體名稱select t1.name as "姓名", t2.department_name as "部門名",
t2.department_person_num as "部門人數",count(t3.project_name) as "關聯項目個數",
group_concat(t3.project_name) as "關聯項目名稱"
from employee t1, department t2, project t3
where t1.relation_department_name = t2.department_name
and t2.department_name = t3.relation_department_name
and t1.name = "張三" group by t2.department_name,t2.department_person_num ;select t1.name as "姓名",t2.department_name as "部門名", t2.department_person_num as "部門人數",
count(t3.project_name) as "關聯項目個數",group_concat(t3.project_name) as "關聯項目名稱"
from employee t1 left join department t2 on t1.relation_department_name = t2.department_name
left join project t3 on t2.department_name = t3.relation_department_name
where t2.department_name is not null
and t3.relation_department_name is not null
and t1.name = "張三" group by t2.department_name,t2.department_person_num ;-- 2、要求-查詢所有的員工,所在部門的人數與關聯的項目數,具體項目的名稱?select t1.name as "姓名",t2.department_person_num as "部門人數",
count(t3.project_name) as "關聯項目個數",group_concat(t3.project_name) as "關聯項目名稱"
from employee t1, department t2, project t3
where t1.relation_department_name = t2.department_name
and t2.department_name = t3.relation_department_name
group by t1.name,t2.department_person_num ;select t1.name as "姓名",t2.department_person_num as "部門人數",
count(t3.project_name) as "關聯項目個數",group_concat(t3.project_name) as "關聯項目名稱"
from employee t1 left join department t2 on t1.relation_department_name = t2.department_name
left join project t3 on t2.department_name = t3.relation_department_name
where t2.department_name is not null
and t3.relation_department_name is not null
group by t1.name,t2.department_person_num ;-- 3、要求-查詢出每個人工程師所在的部門、薪資、負責項目名稱select t1.name as "姓名",t2.department_name as "部門名",t1.salary as "薪資",
group_concat(t3.project_name) as "關聯項目名稱"
from employee t1, department t2, project t3
where t1.relation_department_name = t2.department_name
and t2.department_name = t3.relation_department_name
group by t1.name,t1.salary;select t1.name as "姓名",t2.department_name as "部門名",t1.salary as "薪資",
group_concat(t3.project_name) as "關聯項目名稱"
from employee t1 left join department t2 on t1.relation_department_name = t2.department_name
left join project t3 on t2.department_name = t3.relation_department_name
where t2.department_name is not null
and t3.relation_department_name is not null
group by t1.name,t1.salary;-- 4、要求-查詢出每個人工程師所在的部門、薪資、負責項目名稱,并增加一個工資階段財富類型字段select t1.name as "姓名",t2.department_name as "部門名",t1.salary as "薪資",
group_concat(t3.project_name) as "關聯項目名稱",case when t1.salary between 0 and 4000 then "低收入"
when t1.salary between 4001 and 8000 then "中收入"
else "高收入" end as "財富類型"from employee t1, department t2, project t3
where t1.relation_department_name = t2.department_name
and t2.department_name = t3.relation_department_name
group by t1.name,t1.salary;select t1.name as "姓名",t2.department_name as "部門名",t1.salary as "薪資",
group_concat(t3.project_name) as "關聯項目名稱" ,case when t1.salary between 0 and 4000 then "低收入"
when t1.salary between 4001 and 8000 then "中收入"
else "高收入" end as "財富類型"from employee t1 left join department t2 on t1.relation_department_name = t2.department_name
left join project t3 on t2.department_name = t3.relation_department_name
where t2.department_name is not null
and t3.relation_department_name is not null
group by t1.name,t1.salary;-- 5、要求-查詢項目中學生管理系統最高薪資的工程師姓名、所在項目、薪資select t1.name as "姓名",t3.project_name as "項目名",t1.salary as "薪資"
from employee t1, department t2, project t3
where t1.relation_department_name = t2.department_name
and t2.department_name = t3.relation_department_name
and t3.project_name = "學生管理系統"
order by t1.salary desc limit 1;select t1.name as "姓名",t3.project_name as "項目名",t1.salary as "薪資"
from employee t1 left join department t2 on t1.relation_department_name = t2.department_name
left join project t3 on t2.department_name = t3.relation_department_name
where t2.department_name is not null
and t3.relation_department_name is not null
and t3.project_name = "學生管理系統"
order by t1.salary desc limit 1;
MySQL視圖
什么是視圖
MySQL數據庫視圖:視圖(View)是一種虛擬存在的表。
視圖的特點
- 視圖的列可以來自不同的表,是表的抽象和在邏輯意義上建立的新關系
- 視圖是由基本表(實表)產生的表(虛表)
- 視圖的建立和刪除不影響基本表
- 對視圖內容的更新(增刪改)直接影響基本表
- 當視圖來自多個基本表時,不允許添加、刪除、更新數據。
視圖的優缺點
優點:
1、
安全
:一些數據表有著重要的信息,有些字段是保密的,不能讓用戶直接看到;2、
性能
:視圖建立在服務器上,直接調用在服務器上運行,可以提高性能;3、
靈活
:建立一張視圖,視圖中的數據直接映射到新建的表,這樣,就可以少做很多改動,也達到了升級數據表的目的。缺點:如果實際數據表的結構變更了,就需要及時對相關的視圖進行相應的維護。
視圖相關的SQL語句
- 聲明視圖:
create view 視圖名 as SQL查詢語句;
- 查看視圖:
show tables;
- 刪除視圖:
drop view 視圖名;
MySQL存儲過程
什么是存儲過程
存儲過程:是一組為了完成特定功能的SQL語句
集合。
存儲過程的優缺點
優點:
- 封裝性:封裝成一個沒有返回值函數;
- 可增強SQL語句的功能和靈活性:針對特定的功能編寫存儲過程;
- 高性能:存儲過程編寫成功后,就存儲在數據庫服務器中,以后客戶端可以直接調用,從而提高性能;
- 提高數據庫的安全性:存儲過程作為接口提供給外部程序,外部程序無法直接操作數據庫表,可以提高數據安全性。
缺點:
DBMS(DBMS是數據庫管理系統)
中的存儲過程語法有所不同,所以可移植性差。
存儲過程的語法
create procedure 存儲過程名(in 參數名 參數類型,out 參數名 參數類型)
-- in:表示輸入,out:表示輸出
begin
-- 實現功能的SQL語句(一般是查詢語句的封裝);
end;-- 調用存儲過程:
call 存儲過程名();-- 刪除存儲過程:
drop procedure 存儲過程名;
-- 例子如下:-- 新建存儲過程test_add:create procedure test_add(in a int,in b int,out c int)
begin
set c=a+b;
end-- 調用存儲過程:
call test_add(2,3,@a);-- 查看輸出
select @a;-- 刪除存儲過程:
drop procedure test_add;
MySQL 數據庫導入導出
將表導出:
-- 將表test_csv導出到指定路徑下面,導成csv類型文件select id,name from test_csvinto outfile 'D:/test_as/test(utf8).csv'character set utf8fields terminated by ',';-- 導入前可以先查看導入的路徑:show variables like 'secure_file_priv';-- 將into outfile 'D:/test_as/test(utf8).csv'中路徑改為對應路徑-- character set utf8 表示字符編碼為utf-8-- fields terminated by ','; 表明數據與數據直接用','分隔開
將表導入:
load data local infile 'D:/test_as/test(utf8).csv'into table test_csvcharacter set utf8fields terminated by ',';-- 將infile 'D:/test_as/test(utf8).csv'中路徑改為對應路徑-- character set utf8 表示字符編碼為utf-8-- fields terminated by ','; 表明數據與數據直接用','分隔開
數據庫整體導入導出(以Navicat
為例):
- 導出:右鍵數據庫,選擇
“存儲SQL文件”
,選擇對應的存儲位置; - 導入:連接中新建數據,選中新建數據庫右鍵
“運行SQL文件”
;
MySQL 中事務的基本使用
事務概念:是一個操作序列,不可分割的工作單位,這些操作要么都執行,要么都不執行。
事務的語法:
1、
begin
或start transaction
:顯式地開啟一個事務;2、
commit
或commit work
:提交事務,對數據庫進行的修改是永久性的;3、
rollback
或rollback work
:回滾結束用戶事務,并撤銷正在進行的所有未提交的修改事務;4、
savepoint S1
:在事務中創建一個回滾點(savepoint
),一個事務中可以有多個回滾點,S1
代表回滾點名稱;5、
rollback to [savepoint] S1
:把事務回滾到標記點,S1
代表回滾點名稱。