mysql---安裝、常用命令、建表、查詢、主鍵、外來鍵、表關係、約束、group by、order by、where
@
目錄-
mysql的安裝
安裝 community server 5.6 64位,一定是這個版本,高版本的可能下邊的配置無效。
防止你們找不到直接點選這個連結:https://dev.mysql.com/downloads/installer/
下載好後直接解壓到D盤根目錄,建議D盤根目錄,如果不是,下邊程式碼的
設定mysql的安裝目錄
和設定mysql資料庫的資料的存放目錄
中的路徑需要自己手動改變一下。把裡邊的一個my-default.ini的檔案複製到pycharm,重新命名為my.ini然後開啟,刪除所有裡邊的內容,把下邊的這段程式碼寫到裡邊,內容什麼都不要變,除非你沒有解壓到D盤根目錄。
[mysql] # 設定mysql客戶端預設字符集 default-character-set=utf8 [mysqld] #設定3306埠 port = 3306 # 設定mysql的安裝目錄 basedir=D:\mysql-5.6.50-winx64 # 設定mysql資料庫的資料的存放目錄 datadir=D:\mysql-5.6.50-winx64\data # 允許最大連線數 max_connections=200 # 服務端使用的字符集預設為8位元編碼的latin1字符集 character-set-server=utf8 # 建立新表時將使用的預設儲存引擎 default-storage-engine=INNODB
ctrl+s儲存後,把my.ini檔案copy到D:\mysql-5.6.50-winx64下邊。
然後新增系統環境變數:此電腦-->屬性-->高階系統變數-->環境變數。
雙擊系統變數裡的path,點選新建,把D:\mysql-5.6.50-winx64\bin新增上,然後一路點確定就好。
前邊都完成後,搜尋框找到命令列並以管理員的身份執行。
執行命令安裝MySQL服務 ,MySQL服務就被註冊到你的作業系統中:
mysqld install
看到安裝完成,然後啟動MySQL服務:
net start mysql
看到服務已啟動,證明安裝成功,否則一定是你前邊什麼步驟搞錯了。
-
mysql
mysqld install # 安裝MySQL服務 MySQL服務就被註冊到你的作業系統中 net start mysql # 啟動MySQL服務 net stop mysql # 關閉MySQL服務 mysql -uroot -p # 啟動客戶端連線server,預設無密碼.就是管理員身份登入 set password = password('123'); # 給當前賬戶設定密碼 create user 'xiaoli'@'10.14.206.%' identified by '123'; # 建立一個其他使用者 # create user '使用者名稱'@'IP地址/域名' identified by '密碼'; select user(); # 檢視當前登入使用者 create database 資料庫名; # 建立一個數據庫 show databases # 檢視所有的資料庫 grant all/select/insert on 資料庫名 to 使用者名稱; # 給一個使用者授權 use 資料庫名 # 切換到這個庫下 create table student(name char(12), age int); # 建立一張表 desc student; # 查看錶結構 insert into student values ('alex', 78); # 插入資料 insert into student values ('alex', 78), ('wang', 12); insert into student (name, age) values ('alex', 78); select * from student; # 查詢資料 update student set age=85 where name='alex'; # 修改資料 delete from student where name='alex'; # 刪除資料 show variables like '%engin%'; # 檢視預設儲存引擎
-
儲存引擎
儲存引擎:儲存資料的方式
1.資料儲存在硬碟上,資料持久化
一張表
資料
表的結構
索引(查詢的時候用的一個目錄結構)
資料和索引存在一起 2個檔案
Innodb儲存引擎: 支援事務 行級鎖 表級鎖 支援外來鍵 mysql5.6之後預設的
資料和索引不存在一起 3個檔案
Myisam儲存yinq 表級鎖 mysql5.5之前預設的
2.資料儲存在記憶體中,資料斷電消失.
只儲存表結構
Memory儲存引擎
面試題:
你瞭解mysql的儲存引擎嗎?
瞭解一些.
你的專案用了什麼儲存引擎,為什麼?
Innodb儲存引擎
多個使用者操作的過程中對同一張表的資料同時做修改, 行級鎖
innodb支援行級鎖,所以我們使用了改儲存引擎
未來適應程式未來的擴充套件性,擴充套件新功能的時候可能會用到.
專案中有兩張表 之間的外來鍵關係 怕出問題 做了外來鍵約束檢視mysql是否支援各種引擎
show engines; -
mysql中的資料型別
數字
整數
TINTYINT 1位元組 預設有符號 加unsigned約束 無符號
SMALLINT 2位元組
MEDIUMINT 3位元組
INT/INTEGER 4位元組
BIGINT 8位元組
浮點型
FLOAT 4位元組
DOUBLE 8位元組
DECIMAL double(65, 30) 預設10位整數create table t1( id int, age tinyint unsigned ); insert into t1 values (1, 89); insert into t1 values (-2, 93); create table t2( f1 float(5,2), # 保留兩位小數,並四捨五入 f2 float, f4 double(5,2) f3 double );
時間
datetime 20201022165023
date 20201022
time 165023
timestamp
year 1901/2155create table t4 ( dt datetime not null default current timestamp on update current timestamp, # 非空約束,預設為當前時間並且更新時自動更新為當前時間 y year, t time, ts timestamp # 預設為當前的時間 ); insert into t4 values(now(), now(), now(), now());
字串
預設都是1個位元組長度
char 0-255位元組 定長儲存 'alex' 存 'alex ' 定長
varchar 0-65535位元組 邊長儲存 'alex' 存'alex4'
適合使用cahr
身份證號 手機號碼 qq號 username password 銀行卡號
適合使用varchar
評論 朋友圈 微博enum/set
enum 單選行為
set 多選行為create table t6( c1 char, # char 可以不寫長度 v1 varchar(1), # varchar 必須寫 c2 char(5), v2 varchar(10) ); create table t8( id int, name char(18), gender enum('male', 'female') ); insert int t8 values(1, 'alex', 'male'); create table t9( id int, name char(18), hobby set('抽菸', '喝酒', '燙頭') ); insert into t9 values(1, '太白', '抽菸,喝酒,打遊戲'); # 不在範圍內的會自動去除
-
完整性約束
約束一個欄位不能為空 not null
唯一約束 unique 值不能重複,連續兩個null可以
無符號的 int unsigned
預設值是什麼 default
自增 auto_increment 只能對資料有效 自帶非空約束 至少時unique約束之後才能使用
主鍵 primary key
外來鍵 foreign keycreate table t10( id int unsigned not mull unique, # 非空不生效的話,修改配置檔案 name char(18) not null, gender enum('male', 'female') not null default 'male' ); create table t11( id int, server_name char(12), ip char(10), port char(10), unique(ip, port) # 聯合唯一 unique 兩列不能一i重複 單個的可以重複 ); create table t12( id int not null unique, # 第一個被定義為非空+唯一的那一列會成為這張表的primary key主鍵 主鍵時唯一的 username not null unique ) create table t13( id int not null unique, username primary key # 也可以自定義 ); create table t14( id int, server_name char(12), ip char(10), port char(10), primary key(ip, port) # 聯合主鍵 ); create table t15( id int primary key auto_increment, name char(12) ); insert into t15(name) values('alex'); insert into t15(name) values('小明'); # 外來鍵 # 班級表 create table class( cid int primary key auto_increment, cname char(12) not null, start date ); # 學生表 create table student( id int primary key auto_increment, name char(12) not null, gender enum('male', 'female') default 'male', class_id int, foreign key(class_id) references class(cid) ); insert into class values(1, 'py231','2019-3-19'); # 此時必須先寫class insert into student values(1, '小明', 'male',1);
-
修改表
alter table 表名; alter table t2 add age int notnull; # 自動新增到最後 alter table t2 add age int notnull after id; # 新增到id後 alter table t2 modify name char(15) not null; # 修改型別,不能欄位改名 alter table t2 change name sname char(15) not null; # 修改型別,不能欄位改名 # 刪除表 drop table 表名;
-
表與表之間的關係
多對一 學生 班級 多個學生是一個班級的 學生表有一個外來鍵 關聯班級表 書籍 作者 多本書可以都是一個作者寫的 書記表有一個外來鍵 關聯作者表 多對多 出現第三張表 這張表關聯兩個外來鍵 一對一 客戶 學生 學生表建立外來鍵 兩個欄位必須都是unique
練習:
根據表結構合理設計表與表之間的主外來鍵關係和約束,並完成表結構的建立。
注意建表的順尋和向表中插入資料的順序,需要建立外來鍵的表在後邊建立外來鍵。插入的時候也是。
create table teacher( tid int primary key auto_increment, tname char(10) not null ); create table course( cid int unique auto_increment, cname char(10) not null, teacher_id int, foreign key(teacher_id) references teacher(tid) ); create table class( cid int unique auto_increment, caption char(10) not null ); create table student( sid int unique auto_increment, sname char(10) not null, gender enum('男', '女') not null default'男', class_id int, foreign key(class_id) references class(cid) ); create table score( sid int unique auto_increment, student_id int, course_id int, number int, foreign key(student_id) references student(sid), foreign key(course_id) references course(cid) ); insert into teacher(tname) values('波多野結衣'); insert into teacher(tname) values('蒼井空'); insert into teacher(tname) values('俞布麻衣'); select * from teacher; # +-----+-----------------+ # | tid | tname | # +-----+-----------------+ # | 1 | 波多野結衣 | # | 2 | 蒼井空 | # | 3 | 俞布麻衣 | # +-----+-----------------+ # 3 rows in set (0.00 sec) insert into course values(1, '生物', 1); insert into course values(2, '體育', 1); insert into course values(3, '物理', 2); select * from course; # +------+--------+------------+ # | cid | cname | teacher_id | # +------+--------+------------+ # | 1 | 生物 | 1 | # | 2 | 體育 | 1 | # | 3 | 物理 | 2 | # +------+--------+------------+ # 3 rows in set (0.00 sec) insert into class(caption) values('三年二班'); insert into class(caption) values('一年三班'); insert into class(caption) values('三年一班'); select * from class; # +-----+--------------+ # | cid | caption | # +-----+--------------+ # | 1 | 三年二班 | # | 2 | 一年三班 | # | 3 | 三年一班 | # +-----+--------------+ # 3 rows in set (0.00 sec) insert into student(sname,gender,class_id) values('鋼蛋','女',1); insert into student(sname,gender,class_id) values('鐵錘','女',1); insert into student(sname,class_id) values('山炮',2); select * from student; # +-----+--------+--------+----------+ # | sid | sname | gender | class_id | # +-----+--------+--------+----------+ # | 1 | 鋼蛋 | 女 | 1 | # | 2 | 鐵錘 | 女 | 1 | # | 3 | 山炮 | 男 | 2 | # +-----+--------+--------+----------+ # 3 rows in set (0.00 sec) insert into score(student_id, course_id, number) values(1,1,60); insert into score(student_id, course_id, number) values(1,2,59); insert into score(student_id, course_id, number) values(2,2,100); select * from score; # +-----+------------+-----------+--------+ # | sid | student_id | course_id | number | # +-----+------------+-----------+--------+ # | 1 | 1 | 1 | 60 | # | 2 | 1 | 2 | 59 | # | 3 | 2 | 2 | 100 | # +-----+------------+-----------+--------+ # 3 rows in set (0.00 sec)
-
資料庫的增刪改和select的操作
create table t1( id int primary key auto_increment, username char(12) not null, sex enum('male','female') default 'male', hobby set('抽菸', '喝酒','燙頭') not null ); # 增加: insert into t1 values(1, '大壯', 'male','抽菸,燙頭'); insert into t1 values(2, 'b哥', 'male','燙頭'),(3, 'alex', 'male','抽菸'); insert into t1(username, hobby) values('小李', '喝酒'); create table t2(id int, username char(12)); insert into t2 select id, username from t1; # 注意沒有values # 刪除 delete from t2; # 清空這張表,表結構還在.不會清空自增的偏移量.就是設定了auto_increment 你新增到了3 delete之後再新增會從4開始 truncate table t2; # 會把偏移量也清空 drop table t2; # 表結構都沒了,這張表就沒了 delete from t1 where id = 3; # 刪除某一條資料 # 修改 update t1 set hobby = '喝酒,燙頭' where id = 1; # 查詢 # 資料準備 company.employee 員工id id int 姓名 emp_name varchar 性別 sex enum 年齡 age int 入職日期 hire_date date 崗位 post varchar 職位描述 post_comment varchar 薪水 salary double 辦公室 office int 部門編號 depart_id int #建立表 create table employee( id int not null unique auto_increment, emp_name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) unsigned not null default 28, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, #一個部門一個屋子 depart_id int ); #插入記錄 #三個部門:教學,銷售,運營 insert into employee(emp_name,sex,age,hire_date,post,salary,office,depart_id) values ('egon','male',18,'20170301','老男孩駐沙河辦事處外交大使',7300.33,401,1), #以下是教學部 ('alex','male',78,'20150302','teacher',1000000.31,401,1), ('wupeiqi','male',81,'20130305','teacher',8300,401,1), ('yuanhao','male',73,'20140701','teacher',3500,401,1), ('liwenzhou','male',28,'20121101','teacher',2100,401,1), ('jingliyang','female',18,'20110211','teacher',9000,401,1), ('jinxin','male',18,'19000301','teacher',30000,401,1), ('成龍','male',48,'20101111','teacher',10000,401,1), ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是銷售部門 ('丫丫','female',38,'20101101','sale',2000.35,402,2), ('丁丁','female',18,'20110312','sale',1000.37,402,2), ('星星','female',18,'20160513','sale',3000.29,402,2), ('格格','female',28,'20170127','sale',4000.33,402,2), ('張野','male',28,'20160311','operation',10000.13,403,3), #以下是運營部門 ('程咬金','male',18,'19970312','operation',20000,403,3), ('程咬銀','female',18,'20130311','operation',19000,403,3), ('程咬銅','male',18,'20150411','operation',18000,403,3), ('程咬鐵','female',18,'20140512','operation',17000,403,3) ; # 查詢結果對欄位重新命名 # select 欄位 新名字,... from 表; # select 欄位 as 新名字, ... from 表; select id i,emp_name name from employee; # +----+------------+ # | i | name | # +----+------------+ # | 1 | egon | # | 2 | alex | # | 3 | wupeiqi | # | 4 | yuanhao | # | 5 | liwenzhou | # | 6 | jingliyang | # | 7 | jinxin | # | 8 | 成龍 | # | 9 | 歪歪 | # | 10 | 丫丫 | # | 11 | 丁丁 | # | 12 | 星星 | # | 13 | 格格 | # | 14 | 張野 | # | 15 | 程咬金 | # | 16 | 程咬銀 | # | 17 | 程咬銅 | # | 18 | 程咬鐵 | # +----+------------+ # 18 rows in set (0.00 sec) # 避免重複 distinct select post from employee; # +-----------------------------------------+ # | post | # +-----------------------------------------+ # | 老男孩駐沙河辦事處外交大使 | # | teacher | # | teacher | # | teacher | # | teacher | # | teacher | # | teacher | # | teacher | # | sale | # | sale | # | sale | # | sale | # | sale | # | operation | # | operation | # | operation | # | operation | # | operation | # +-----------------------------------------+ # 18 rows in set (0.00 sec) select distinct post from employee; # +-----------------------------------------+ # | post | # +-----------------------------------------+ # | 老男孩駐沙河辦事處外交大使 | # | teacher | # | sale | # | operation | # +-----------------------------------------+ # 4 rows in set (0.01 sec) # 聯合去重,age和sex都不同的留下 select distinct age,sex from employee; # mysql> select age,sex from employee; # +-----+--------+ # | age | sex | # +-----+--------+ # | 18 | male | # | 78 | male | # | 81 | male | # | 73 | male | # | 28 | male | # | 18 | female | # | 18 | male | # | 48 | male | # | 48 | female | # | 38 | female | # | 18 | female | # | 18 | female | # | 28 | female | # | 28 | male | # | 18 | male | # | 18 | female | # | 18 | male | # | 18 | female | # +-----+--------+ # 18 rows in set (0.00 sec) # # mysql> select distinct age,sex from employee; # +-----+--------+ # | age | sex | # +-----+--------+ # | 18 | male | # | 78 | male | # | 81 | male | # | 73 | male | # | 28 | male | # | 18 | female | # | 48 | male | # | 48 | female | # | 38 | female | # | 28 | female | # +-----+--------+ # 10 rows in set (0.00 sec) # 四則運算 select emp_name, salary*12 year_salary from employee; # 計算年薪 # mysql> select emp_name, salary*12 year_salary from employee; # +------------+-------------+ # | emp_name | year_salary | # +------------+-------------+ # | egon | 87603.96 | # | alex | 12000003.72 | # | wupeiqi | 99600.00 | # | yuanhao | 42000.00 | # | liwenzhou | 25200.00 | # | jingliyang | 108000.00 | # | jinxin | 360000.00 | # | 成龍 | 120000.00 | # | 歪歪 | 36001.56 | # | 丫丫 | 24004.20 | # | 丁丁 | 12004.44 | # | 星星 | 36003.48 | # | 格格 | 48003.96 | # | 張野 | 120001.56 | # | 程咬金 | 240000.00 | # | 程咬銀 | 228000.00 | # | 程咬銅 | 216000.00 | # | 程咬鐵 | 204000.00 | # +------------+-------------+ # 18 rows in set (0.00 sec) # concat() 拼接 select concat(emp_name,':',salary) info from employee; # +-----------------------------+ # | info | # +-----------------------------+ # | egon:7300.33 | # | alex:1000000.31 | # | wupeiqi:8300.00 | # | yuanhao:3500.00 | # | liwenzhou:2100.00 | # | jingliyang:9000.00 | # | jinxin:30000.00 | # | 成龍:10000.00 | # | 歪歪:3000.13 | # | 丫丫:2000.35 | # | 丁丁:1000.37 | # | 星星:3000.29 | # | 格格:4000.33 | # | 張野:10000.13 | # | 程咬金:20000.00 | # | 程咬銀:19000.00 | # | 程咬銅:18000.00 | # | 程咬鐵:17000.00 | # +-----------------------------+ # 18 rows in set (0.01 sec) # concat_ws('|', 'alex', '74'); 第一個是分隔符 # 練習 # 1 查出所有員工的名字,薪資,格式為 # <名字:egon> <薪資:3000> select concat('<','名字',emp_name,'>'), concat('<','薪資',salary,'>') from employee; # 2 查出所有的崗位(去掉重複) select distinct post from employee; # 3 查出所有員工名字,以及他們的年薪,年薪的欄位名為annual_year select emp_name,salary*12 annual_salary from employee; # where約束 篩選所有符合條件的行 # where可以使用: # 1.比較運算子:> < = >= <= != # 2.範圍: # between and [10000, 20000] 在之間的所有的 # in in [10000, 20000] 在裡面的選一個 # 3.like 'e%' 萬用字元 # %表示任意多個 # _表示一個 # 4.邏輯運算子:and or not select * from employee where sex = 'male'; select * from employee where salary >= 10000; select * from employee where salary between 10000 and 20000; # [10000, 20000] # 練習 # . 檢視崗位是teacher的員工姓名、年齡 select emp_name, age from employee where post = 'teacher'; # . 檢視崗位是teacher且年齡大於30歲的員工姓名、年齡 select emp_name, age from employee where post = 'teacher' and age > 30; # . 檢視崗位是teacher且薪資在9000-10000範圍內的員工姓名、年齡、薪資 select emp_name, age, salary from employee where post = 'teacher' and salary between 9000 and 10000; # . 檢視崗位描述不為NULL的員工資訊 select * from employee where post_comment is not null; # 判斷NUL必須用is # . 檢視崗位是teacher且薪資是10000或9000或30000的員工姓名、年齡、薪資 select emp_name,age,salary from employee where post = 'teacher' and salary in (10000,9000,30000); # . 檢視崗位是teacher且薪資不是10000或9000或30000的員工姓名、年齡、薪資 select emp_name,age,salary from employee where post = 'teacher' and salary not in (10000,9000,30000) # . 檢視崗位是teacher且名字是jin開頭的員工姓名、年薪 select emp_name,salary*12 from employee where post = 'teacher' and emp_name like 'jin%'; # 分組聚合 group by count max min sum avg 求出的值之和這個組對應 select sex,count(id) from employee group by sex; # +--------+-----------+ # | sex | count(id) | # +--------+-----------+ # | male | 10 | # | female | 8 | # +--------+-----------+ # 2 rows in set (0.02 sec) # 練習 # . 查詢崗位名以及崗位包含的所有員工名字 select post,group_concat(emp_name) from employee group by post; # +-----------------------------------------+---------------------------------------------------------+ # | post | group_concat(emp_name) | # +-----------------------------------------+---------------------------------------------------------+ # | operation | 程咬鐵,程咬銅,程咬銀,程咬金,張野 | # | sale | 格格,星星,丁丁,丫丫,歪歪 | # | teacher | 成龍,jinxin,jingliyang,liwenzhou,yuanhao,wupeiqi,alex | # | 老男孩駐沙河辦事處外交大使 | egon | # +-----------------------------------------+---------------------------------------------------------+ # 4 rows in set (0.00 sec) # . 查詢崗位名以及各崗位內包含的員工個數 select post,count(id) from employee group by post; # . 查詢公司內男員工和女員工的個數 select sex,count(id) from employee group by sex; # . 查詢崗位名以及各崗位的平均薪資 select post,avg(salary) from employee group by post; # . 查詢崗位名以及各崗位的最高薪資 select post,max(salary) from employee group by post; # . 查詢崗位名以及各崗位的最低薪資 select post,min(salary) from employee group by post; # . 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資 select sex,avg(salary) from employee group by sex; # having 過濾 在having條件中可以使用聚合函式,在where中不可以 select post,avg(salary) from employee group by post having avg(salary) > 10000; # 練習 # 1. 查詢各崗位內包含的員工個數小於2的崗位名、崗位內包含員工名字、個數 select post,group_concat(emp_name),count(id) from employee group by post having count(id)<2; # 3. 查詢各崗位平均薪資大於10000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary)>10000; # 4. 查詢各崗位平均薪資大於10000且小於20000的崗位名、平均工資 select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000; # order by 根據什麼排序 預設(asc)從小到大 desc 從大到小 select * from employee order by age desc; select * from employee order by age,salary desc; 優先根據age從小到大排,在age相同的情況下,再根據薪資從大到小排 # limit m,n 從第m+1開始取n項,等同於 limit m offset n select * from employee order by salary desc limit 2, 2;