前言:MySQL是一種流行的關系型數據庫管理系統,它的作用是存儲和管理數據。在Web開發中,MySQL是必備的數據庫技能之一,因為它可以幫助Web開發人員處理大量的數據,并且提供了強大的數據查詢和管理功能。
一 數據庫介紹
1.1 什么是數據庫
?1.2 數據庫產品
二 MySQL概述
2.1 下載
點開下面的鏈接:https://dev.mysql.com/downloads/mysql/
點擊Download 就可以下載對應的安裝包了, 安裝包如下:
2.2 解壓
下載完成后我們得到的是一個壓縮包,將其解壓,我們就可以得到MySQL 8.0.31 的軟件本體了(就是一個文件夾),我們可以把它放在你想安裝的位置 。
2.3 配置
2.3.1. 添加環境變量
環境變量里面有很多選項,這里我們只用到
Path
這個參數。為什么在初始化的開始要添加環境變量呢?在黑框(即CMD)中輸入一個可執行程序的名字,Windows會先在環境變量中的
Path
所指的路徑中尋找一遍,如果找到了就直接執行,沒找到就在當前工作目錄找,如果還沒找到,就報錯。我們添加環境變量的目的就是能夠在任意一個黑框直接調用MySQL中的相關程序而不用總是修改工作目錄,大大簡化了操作。
右鍵此電腦
→屬性
,點擊高級系統設置
點擊環境變量
在系統變量
中新建MYSQL_HOME
在系統變量
中找到并雙擊Path
點擊新建
?最后點擊確定。
如何驗證是否添加成功?
右鍵開始菜單(就是屏幕左下角),選擇命令提示符(管理員)
,打開黑框,敲入mysql
,回車。
如果提示Can't connect to MySQL server on 'localhost'
則證明添加成功;
如果提示mysql不是內部或外部命令,也不是可運行的程序或批處理文件
則表示添加添加失敗,請重新檢查步驟并重試。
2.3.2. 初始化MySQL
==以管理員身份,運行命令行窗口:==
在剛才的命令行中,輸入如下的指令:
mysqld --initialize-insecure
稍微等待一會,如果出現沒有出現報錯信息,則證明data目錄初始化沒有問題,此時再查看MySQL目錄下已經有data目錄生成。
tips:如果出現如下錯誤
是由于權限不足導致的,以管理員方式運行 cmd
2.3.3. 注冊MySQL服務
命令行(注意必須以管理員身份啟動)中,輸入如下的指令,回車執行:
mysqld -install
現在你的計算機上已經安裝好了MySQL服務了。
2.3.4. 啟動MySQL服務
在黑框里敲入net start mysql
,回車。
net start mysql ?// 啟動mysql服務net stop mysql ?// 停止mysql服務
?2.3.5. 修改默認賬戶密碼
在黑框里敲入mysqladmin -u root password 1234
,這里的1234
就是指默認管理員(即root賬戶)的密碼,可以自行修改成你喜歡的。
mysqladmin -u root password 1234
2.4 登錄MySQL
右鍵開始菜單,選擇命令提示符
,打開黑框。 在黑框中輸入,mysql -uroot -p1234
,回車,出現下圖且左下角為mysql>
,則登錄成功。
mysql -uroot -p1234
到這里你就可以開始你的MySQL之旅了!
退出mysql:
exit quit
登陸參數:
mysql -u用戶名 -p密碼 -h要連接的mysql服務器的ip地址(默認127.0.0.1) -P端口號(默認3306)
2.5卸載MySQL
如果你想卸載MySQL,也很簡單。
點擊開始菜單,輸入cmd,選擇 "命令提示符",選擇右側的 "以管理員身份運行"。
敲入net stop mysql
,回車。
net stop mysql
再敲入mysqld -remove mysql
,回車。
mysqld -remove mysql
最后刪除MySQL目錄及相關的環境變量。
至此,MySQL卸載完成!
2.2 數據模型
?
2.3 SQL簡介
?
?
三 數據庫設計-DDL
3.1 數據庫操作
3.2 MySQL客戶端工具
IDEA
?
?
3.2 數據類型?
3.2.1 數值類型
3.2.2 字符串類型
3.2.3 日期時間類型
??
3.2.4 案例
??
create table tb_emp
(id int auto_increment comment 'ID 主鍵'primary key,username varchar(20) not null comment '用戶名',password varchar(32) default '123456' not null comment '密碼',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別(1男 2 女)',image varchar(300) null comment '圖形url',job tinyint unsigned null comment '職位(1、班主任 2 講師 3 學生主管 4 教研主管)',entrydate date null comment '入職日期',create_time datetime not null comment '創建時間',update_time datetime not null comment '修改時間'
)comment '員工表';
3.3 表(創建、查詢、修改、刪除)
3.3.1 創建表
?
create table tb_user(id int primary key auto_increment comment 'id,唯一標識',username varchar(20) not null unique comment '用戶名',name varchar(10) not null comment '姓名',age int comment '年齡',gender char(1) default '男' comment '性別'
) comment '用戶表';
?3.3.2 查詢
?
?3.3.3 修改
?
3.3.4 刪除
?
四 數據庫操作-DML
?4.1 添加數據 - insert
4.2 修改數據 - update
4.3 刪除數據 - delete
?
?4.4 總結
五 數據庫操作-DQL
?
5.1 創建db02數據庫,并執行腳本
-- 員工管理(帶約束)
create table tb_emp (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用戶名',password varchar(32) default '123456' comment '密碼',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別, 說明: 1 男, 2 女',image varchar(300) comment '圖像',job tinyint unsigned comment '職位, 說明: 1 班主任,2 講師, 3 學工主管, 4 教研主管',entrydate date comment '入職時間',create_time datetime not null comment '創建時間',update_time datetime not null comment '修改時間'
) comment '員工表';-- 準備測試數據
INSERT INTO tb_emp (id, username, password, name, gender, image, job, entrydate, create_time, update_time) VALUES(1, 'jinyong', '123456', '金庸', 1, '1.jpg', 4, '2000-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:35'),(2, 'zhangwuji', '123456', '張無忌', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:37'),(3, 'yangxiao', '123456', '楊逍', 1, '3.jpg', 2, '2008-05-01', '2022-10-27 16:35:33', '2022-10-27 16:35:39'),(4, 'weiyixiao', '123456', '韋一笑', 1, '4.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:41'),(5, 'changyuchun', '123456', '常遇春', 1, '5.jpg', 2, '2012-12-05', '2022-10-27 16:35:33', '2022-10-27 16:35:43'),(6, 'xiaozhao', '123456', '小昭', 2, '6.jpg', 3, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:45'),(7, 'jixiaofu', '123456', '紀曉芙', 2, '7.jpg', 1, '2005-08-01', '2022-10-27 16:35:33', '2022-10-27 16:35:47'),(8, 'zhouzhiruo', '123456', '周芷若', 2, '8.jpg', 1, '2014-11-09', '2022-10-27 16:35:33', '2022-10-27 16:35:49'),(9, 'dingminjun', '123456', '丁敏君', 2, '9.jpg', 1, '2011-03-11', '2022-10-27 16:35:33', '2022-10-27 16:35:51'),(10, 'zhaomin', '123456', '趙敏', 2, '10.jpg', 1, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:53'),(11, 'luzhangke', '123456', '鹿杖客', 1, '11.jpg', 2, '2007-02-01', '2022-10-27 16:35:33', '2022-10-27 16:35:55'),(12, 'hebiweng', '123456', '鶴筆翁', 1, '12.jpg', 2, '2008-08-18', '2022-10-27 16:35:33', '2022-10-27 16:35:57'),(13, 'fangdongbai', '123456', '方東白', 1, '13.jpg', 1, '2012-11-01', '2022-10-27 16:35:33', '2022-10-27 16:35:59'),(14, 'zhangsanfeng', '123456', '張三豐', 1, '14.jpg', 2, '2002-08-01', '2022-10-27 16:35:33', '2022-10-27 16:36:01'),(15, 'yulianzhou', '123456', '俞蓮舟', 1, '15.jpg', 2, '2011-05-01', '2022-10-27 16:35:33', '2022-10-27 16:36:03'),(16, 'songyuanqiao', '123456', '宋遠橋', 1, '16.jpg', 2, '2010-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:05'),(17, 'chenyouliang', '12345678', '陳友諒', 1, '17.jpg', null, '2015-03-21', '2022-10-27 16:35:33', '2022-10-27 16:36:07'),(18, 'zhang1', '123456', '張一', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:09'),(19, 'zhang2', '123456', '張二', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:11'),(20, 'zhang3', '123456', '張三', 1, '2.jpg', 2, '2018-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:13'),(21, 'zhang4', '123456', '張四', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:15'),(22, 'zhang5', '123456', '張五', 1, '2.jpg', 2, '2016-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:17'),(23, 'zhang6', '123456', '張六', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:19'),(24, 'zhang7', '123456', '張七', 1, '2.jpg', 2, '2006-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:21'),(25, 'zhang8', '123456', '張八', 1, '2.jpg', 2, '2002-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:23'),(26, 'zhang9', '123456', '張九', 1, '2.jpg', 2, '2011-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:25'),(27, 'zhang10', '123456', '張十', 1, '2.jpg', 2, '2004-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:27'),(28, 'zhang11', '123456', '張十一', 1, '2.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:29'),(29, 'zhang12', '123456', '張十二', 1, '2.jpg', 2, '2020-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:31');
?5.2 select語法-基本查詢
-- =================== DQL: 基本查詢 ======================
-- 1. 查詢指定字段 name,entrydate 并返回
select name,entrydate from tb_emp;-- 2. 查詢返回所有字段
select id, username, password, name, gender, image, job, entrydate, create_time, update_time from tb_emp;
select * from tb_emp;-- 3. 查詢所有員工的 name,entrydate, 并起別名(姓名、入職日期)
select name as 姓名,entrydate as 入職日期 from tb_emp;
select name 姓名,entrydate 入職日期 from tb_emp;
select name '姓 名',entrydate 入職日期 from tb_emp;-- 4. 查詢已有的員工關聯了哪幾種職位(不要重復)
select distinct job from tb_emp;
?5.3 select語法-條件查詢
-- =================== DQL: 條件查詢 ======================
-- 1. 查詢 姓名 為 楊逍 的員工
select * from tb_emp where name='楊逍';-- 2. 查詢 id小于等于5 的員工信息
select * from tb_emp where id <= 5;-- 3. 查詢 沒有分配職位 的員工信息
select * from tb_emp where job is null;-- 4. 查詢 有職位 的員工信息
select * from tb_emp where job is not null;-- 5. 查詢 密碼不等于 '123456' 的員工信息
select * from tb_emp where password != '123456';
select * from tb_emp where password <> '123456';-- 6. 查詢 入職日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之間的員工信息
select * from tb_emp where entrydate >='2000-01-01' and entrydate <='2100-01-01';
select * from tb_emp where entrydate between '2000-01-01' and '2100-01-01';
-- 7. 查詢 入職時間 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之間 且 性別為女 的員工信息
select * from tb_emp where entrydate between '2000-01-01' and '2100-01-01' and gender = 2;-- 8. 查詢 職位是 2 (講師), 3 (學工主管), 4 (教研主管) 的員工信息
select * from tb_emp where job ='2'or job= '3' or job='4';
select * from tb_emp where job in(2, 3, 4);
-- 9. 查詢 姓名 為兩個字的員工信息
select * from tb_emp where name like '__';-- 10. 查詢 姓 '張' 的員工信息
select * from tb_emp where name like '張%';
?5.4?select語法-分組查詢
?
-- =================== DQL: 分組查詢 ======================
-- 聚合函數-- 1. 統計該企業員工數量
-- A 字段
select count(id) from tb_emp;
-- B 常量
select count(1) from tb_emp;-- C *
select count(*) from tb_emp;-- 2. 統計該企業員工 ID 的平均值
select avg(id) from tb_emp;-- 3. 統計該企業最早入職的員工
select min(entrydate) from tb_emp;-- 4. 統計該企業最遲入職的員工
select max(entrydate) from tb_emp;-- 5. 統計該企業員工的 ID 之和
select sum(id) from tb_emp;-- 分組
-- 1. 根據性別分組 , 統計男性和女性員工的數量
select gender,count(*) from tb_emp group by gender;-- 3. 先查詢入職時間在 '2015-01-01' (包含) 以前的員工 , 并對結果根據職位分組 , 獲取員工數量大于等于2的職位
select job,count(*) from tb_emp where entrydate <= '2015-01-01' group by job having count(*) >= 2 ;
5.5?select語法-排序查詢
-- =================== 排序查詢 ======================
-- 1. 根據入職時間, 對員工進行升序排序
select * from tb_emp order by entrydate;
select * from tb_emp order by entrydate asc;-- 2. 根據入職時間, 對員工進行降序排序
select * from tb_emp order by entrydate desc ;-- 3. 根據 入職時間 對公司的員工進行 升序排序 , 入職時間相同 , 再按照 更新時間 進行降序排序
select * from tb_emp order by entrydate,update_time desc ;
?5.6?select語法-分頁查詢
-- =================== 分頁查詢 ======================
-- 1. 從起始索引0開始查詢員工數據, 每頁展示5條記錄
select * from tb_emp limit 0,5;-- 2. 查詢 第1頁 員工數據, 每頁展示5條記錄
select * from tb_emp limit 0,5;-- 3. 查詢 第2頁 員工數據, 每頁展示5條記錄
select * from tb_emp limit 5,5;-- 4. 查詢 第3頁 員工數據, 每頁展示5條記錄
select * from tb_emp limit 10,5;-- 5467 起始索引計算公式 = (頁碼- 1) * 每頁展示記錄數select * from tb_emp limit 10,5;
?5.7 案例1
??
select *
from tb_emp
where name like '張%'and gender = 1and entrydate between '2000-01-01' and '2015-12-31'
order by update_time desc
limit 0, 10;
?5.8 案例2
-- 2.1
select if(gender=1,'男性員工','女性員工') 性別,count(*) from tb_emp group by gender;-- 2-2
select (case job when 1 then '班主任' when 2 then '講師' when 3 then '學生主管' when 4 then '教研主管' else '未分配職位' end ) as 職位,count(*) from tb_emp group by job;
?5.9 總結
六 多表設計
6.1 一對多
?
?
??
-- 部門管理
create table tb_dept(id int unsigned primary key auto_increment comment '主鍵ID',name varchar(10) not null unique comment '部門名稱',create_time datetime not null comment '創建時間',update_time datetime not null comment '修改時間'
) comment '部門表';-- 員工管理(帶約束)
create table tb_emp (id int unsigned primary key auto_increment comment 'ID',username varchar(20) not null unique comment '用戶名',password varchar(32) default '123456' comment '密碼',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別, 說明: 1 男, 2 女',image varchar(300) comment '圖像',job tinyint unsigned comment '職位, 說明: 1 班主任,2 講師, 3 學工主管, 4 教研主管',entrydate date comment '入職時間',dept_id int unsigned comment '部門ID',create_time datetime not null comment '創建時間',update_time datetime not null comment '修改時間'
) comment '員工表';insert into tb_dept (id, name, create_time, update_time) values(1,'學工部',now(),now()),(2,'教研部',now(),now()),(3,'咨詢部',now(),now()),(4,'就業部',now(),now()),(5,'人事部',now(),now());INSERT INTO tb_emp(id, username, password, name, gender, image, job, entrydate,dept_id, create_time, update_time) VALUES(1,'jinyong','123456','金庸',1,'1.jpg',4,'2000-01-01',2,now(),now()),(2,'zhangwuji','123456','張無忌',1,'2.jpg',2,'2015-01-01',2,now(),now()),(3,'yangxiao','123456','楊逍',1,'3.jpg',2,'2008-05-01',2,now(),now()),(4,'weiyixiao','123456','韋一笑',1,'4.jpg',2,'2007-01-01',2,now(),now()),(5,'changyuchun','123456','常遇春',1,'5.jpg',2,'2012-12-05',2,now(),now()),(6,'xiaozhao','123456','小昭',2,'6.jpg',3,'2013-09-05',1,now(),now()),(7,'jixiaofu','123456','紀曉芙',2,'7.jpg',1,'2005-08-01',1,now(),now()),(8,'zhouzhiruo','123456','周芷若',2,'8.jpg',1,'2014-11-09',1,now(),now()),(9,'dingminjun','123456','丁敏君',2,'9.jpg',1,'2011-03-11',1,now(),now()),(10,'zhaomin','123456','趙敏',2,'10.jpg',1,'2013-09-05',1,now(),now()),(11,'luzhangke','123456','鹿杖客',1,'11.jpg',1,'2007-02-01',1,now(),now()),(12,'hebiweng','123456','鶴筆翁',1,'12.jpg',1,'2008-08-18',1,now(),now()),(13,'fangdongbai','123456','方東白',1,'13.jpg',2,'2012-11-01',2,now(),now()),(14,'zhangsanfeng','123456','張三豐',1,'14.jpg',2,'2002-08-01',2,now(),now()),(15,'yulianzhou','123456','俞蓮舟',1,'15.jpg',2,'2011-05-01',2,now(),now()),(16,'songyuanqiao','123456','宋遠橋',1,'16.jpg',2,'2010-01-01',2,now(),now()),(17,'chenyouliang','123456','陳友諒',1,'17.jpg',NULL,'2015-03-21',NULL,now(),now());
??外鍵約束
????????
6.2 一對一
?
-- ===========================================一對一=====================================
create table tb_user(id int unsigned primary key auto_increment comment 'ID',name varchar(10) not null comment '姓名',gender tinyint unsigned not null comment '性別, 1 男 2 女',phone char(11) comment '手機號',degree varchar(10) comment '學歷'
) comment '用戶信息表';insert into tb_user values (1,'白眉鷹王',1,'18812340001','初中'),(2,'青翼蝠王',1,'18812340002','大專'),(3,'金毛獅王',1,'18812340003','初中'),(4,'紫衫龍王',2,'18812340004','碩士');create table tb_user_card(id int unsigned primary key auto_increment comment 'ID',nationality varchar(10) not null comment '民族',birthday date not null comment '生日',idcard char(18) not null comment '身份證號',issued varchar(20) not null comment '簽發機關',expire_begin date not null comment '有效期限-開始',expire_end date comment '有效期限-結束',user_id int unsigned not null unique comment '用戶ID',constraint fk_user_id foreign key (user_id) references tb_user(id)
) comment '用戶信息表';insert into tb_user_card values (1,'漢','1960-11-06','100000100000100001','朝陽區公安局','2000-06-10',null,1),(2,'漢','1971-11-06','100000100000100002','靜安區公安局','2005-06-10','2025-06-10',2),(3,'漢','1963-11-06','100000100000100003','昌平區公安局','2006-06-10',null,3),(4,'回','1980-11-06','100000100000100004','海淀區公安局','2008-06-10','2028-06-10',4);
6.3 多對多
-- ======================================多對多=============================
create table tb_student(id int auto_increment primary key comment '主鍵ID',name varchar(10) comment '姓名',no varchar(10) comment '學號'
) comment '學生表';
insert into tb_student(name, no) values ('黛綺絲', '2000100101'),('謝遜', '2000100102'),('殷天正', '2000100103'),('韋一笑', '2000100104');create table tb_course(id int auto_increment primary key comment '主鍵ID',name varchar(10) comment '課程名稱'
) comment '課程表';
insert into tb_course (name) values ('Java'), ('PHP'), ('MySQL') , ('Hadoop');create table tb_student_course(id int auto_increment comment '主鍵' primary key,student_id int not null comment '學生ID',course_id int not null comment '課程ID',constraint fk_courseid foreign key (course_id) references tb_course (id),constraint fk_studentid foreign key (student_id) references tb_student (id)
)comment '學生課程中間表';insert into tb_student_course(student_id, course_id) values (1,1),(1,2),(1,3),(2,2),(2,3),(3,4);
?
?
?6.4 總結
?6.5 綜合案例
-- 瑞吉點餐頁面原型 , 設計表結構
-- 分類表
create table category(id int unsigned primary key auto_increment comment '主鍵ID',name varchar(20) not null unique comment '分類名稱',type tinyint unsigned not null comment '類型 1 菜品分類 2 套餐分類',sort tinyint unsigned not null comment '順序',status tinyint unsigned not null default 0 comment '狀態 0 禁用,1 啟用',create_time datetime not null comment '創建時間',update_time datetime not null comment '更新時間'
) comment '菜品及套餐分類' ;-- 菜品表
create table dish(id int unsigned primary key auto_increment comment '主鍵ID',name varchar(20) not null unique comment '菜品名稱',category_id int unsigned not null comment '菜品分類ID',price decimal(8, 2) not null comment '菜品價格',image varchar(300) not null comment '菜品圖片',description varchar(200) comment '描述信息',status tinyint unsigned not null default 0 comment '狀態, 0 停售 1 起售',create_time datetime not null comment '創建時間',update_time datetime not null comment '更新時間'
) comment '菜品';-- 套餐表
create table setmeal(id int unsigned primary key auto_increment comment '主鍵ID',name varchar(20) not null unique comment '套餐名稱',category_id int unsigned not null comment '分類id',price decimal(8, 2) not null comment '套餐價格',image varchar(300) not null comment '圖片',description varchar(200) comment '描述信息',status tinyint unsigned not null default 0 comment '狀態 0:停用 1:啟用',create_time datetime not null comment '創建時間',update_time datetime not null comment '更新時間'
)comment '套餐' ;-- 套餐菜品關聯表
create table setmeal_dish(id int unsigned primary key auto_increment comment '主鍵ID',setmeal_id int unsigned not null comment '套餐id ',dish_id int unsigned not null comment '菜品id',copies tinyint unsigned not null comment '份數'
)comment '套餐菜品關系';
七 多表查詢
7.1 內連接
?
7.2? 外連接
7.3 子查詢
????????
?
?
?
????????
?
?
7.4 總結
7.5 案例
?
?
?
?
?
?
八 事務
8.1 概念
8.2 操作
?
?
?8.3 四大特性
????????
?8.4 總結?
?