MySQL筆記
撰寫時間 : 2021/09/20 - 09/21
教學影片
下載後配置
-
關閉自動開機後自動啟動,減少資源

-
配置環境變數mysql.exe
在C:\Program Files\MySQL\MySQL Server 8.0\bin

命令行開啟程式
- 在
cmd
鍵入,開啟程式
關閉則為net start mysql80
- 開啟數據庫服務主機
-h
host(數據庫服務主機) -P
port(端口號) -u
user(用戶)
表格與鍵值
代碼 |
功能 |
primary key |
設定這個屬性可以區分每一筆資料 |
foreign key |
可以對應到自己或是其他表格的primary key |
創建資料庫與表格
SQL
語法規範
- 不區分大小寫,但建議關鍵字大寫、表名列名小寫
- 命令結束用
;
隔開
- 表名列名習慣上用``避免被判定為關鍵字
代碼 |
功能 |
show databases; |
查看當前數據庫 |
use {database_name}; |
打開指定的數據庫 |
show tables; |
顯示表單訊息 |
show tables {other_database_name}; |
查看其他數據庫 |
select database(); |
顯示當前數據庫 |
create table {table_name} name1 type1 ,
name2 type2); |
創建表 |
drop database {database_name} |
刪除資料庫 |
select version(); |
登入mysql時查看版本 |
mysql --version
mysql -V |
非登入mysql時查看版本 |
#單行註釋 、-- 單行註釋 、/*多行註釋*/ |
註釋 |
數據類型
型態 |
解釋 |
INT |
整數 |
DECIMAL(m, n) |
浮點數(m位數,小數點占n位) |
VARCHAR(m) |
最多能存放m個字元 |
BLOB |
Binary Large Object,存放二進制的資料,影片、圖片… |
DATE |
日期,'YYYY-MM-DD' |
TIMESTAMP |
時間,'YYYY-MM-DD HH:MM:SS' |
新增資料
代碼 |
功能 |
insert into {table_name} values(value1, value2) |
插入資料 |
insert into {table_name} ({name1}. {name2})values(value1, value2) |
自定義順序插入資料 |
約束(constraints)
代碼 |
功能 |
not null |
不可空白 |
unique |
不可重複 |
dafault {name} |
默認值 |
auto_increment |
每加一筆數據時自動累加1 |
修改與刪除
- 先把預更新模式關閉
set sql_safe_updates = 0;
- 修改
變動表格,當id是2或3時,把該row的name
更新為"sella"、major
更新為"english"。
成績低於60分刪除資料。>
大於、<
小於、=
等於、<>
不等於。
取得資料
代碼 |
功能 |
select * from {table_name} |
回傳表格所有資料 |
select {name1}, {name2} from {table_name} |
回傳符合該屬性的資料 |
代碼 |
功能 |
order by {name1}, {name1} asc |
預設由低到高排序 |
order by {name} desc |
由高到低排序 |
limit 3 |
回傳前3筆資料 |
where {name1} <> 60 |
回傳不等於60分資料 |
where in({name1}, {name2}, {name3}) |
等於三筆OR 的條件 |
公司資料案例
創建

新增
INSERT INTO `branch` VALUES(1, '研發', NULL);
INSERT INTO `branch` VALUES(2, '行政', NULL);
INSERT INTO `branch` VALUES(3, '資訊', NULL);
INSERT INTO `employee` VALUES(206, '小黃', '1998-10-08', 'F', 50000, 1, NULL);
INSERT INTO `employee` VALUES(207, '小綠', '1985-09-16', 'M', 29000, 2, 206);
INSERT INTO `employee` VALUES(208, '小黑', '2000-12-19', 'M', 35000, 3, 206);
INSERT INTO `employee` VALUES(209, '小白', '1997-01-22', 'F', 39000, 3, 207);
INSERT INTO `employee` VALUES(210, '小蘭', '1925-11-10', 'F', 84000, 1, 207);
UPDATE `branch`
SET `manager_id` = 206
WHERE `branch_id` = 1;
UPDATE `branch`
SET `manager_id` = 207
WHERE `branch_id` = 2;
UPDATE `branch`
SET `manager_id` = 208
WHERE `branch_id` = 3;
INSERT INTO `client` VALUES(400, '阿狗', '254354335');
INSERT INTO `client` VALUES(401, '阿貓', '25633899');
INSERT INTO `client` VALUES(402, '旺來', '45354345');
INSERT INTO `client` VALUES(403, '露西', '54354365');
INSERT INTO `client` VALUES(404, '艾瑞克', '18783783');
INSERT INTO `works_with` VALUES(206, 400, 70000);
INSERT INTO `works_with` VALUES(207, 401, 24000);
INSERT INTO `works_with` VALUES(208, 402, 9800);
INSERT INTO `works_with` VALUES(208, 403, 24000);
INSERT INTO `works_with` VALUES(210, 404, 87940);
取得
聚合函數(aggregate function)
萬用字元(wildcards)
Symbol |
Description |
Example |
% |
Represents zero or more characters |
bl% finds bl, black, blue, and blob |
_ |
Represents a single character |
h_t finds hot, hat, and hit |
[] |
Represents any single character within the brackets |
h[oa]t finds hot and hat, but not hit |
^ |
Represents any character not in the brackets |
h[^oa]t finds hit, but not hot and hat |
- |
Represents a range of characters |
c[a-b]t finds cat and cbt |
聯集(union)
- 員工id、名字 union 客戶id、名字,並重新命名表格
交集(join)
子查詢(subquery)
python連線MySQL
commit()
意義
MySQLConnection.commit()
method sends a COMMIT statement to the MySQL server, committing the current transaction. After the successful execution of a query make changes persistent into a database using the commit() of a connection class.
ref : Use Commit and Rollback to Manage MySQL Transactions in Python