Mysql浅析
Mysql淺析
SQL:
- Structured Query Language(結(jié)構(gòu)化查詢語言)
SQL分為三個部分:
- DDL(Data Definition Language)
- 數(shù)據(jù)定義語言,用來維護(hù)存儲數(shù)據(jù)的結(jié)構(gòu)(數(shù)據(jù)庫、數(shù)據(jù)表),代表指令: create、drop、alter 等。
- DML(Data Manipulation Language)
- 數(shù)據(jù)操作語言,用來對數(shù)據(jù)進(jìn)行操作(數(shù)據(jù)表中的內(nèi)容),代表指令: insert、delete、update 等。
- 其中DML內(nèi)部又單獨(dú)進(jìn)行了一個分類DQL(Date Query Language),數(shù)據(jù)查詢語言。如: select 。
- DCL(Data Control Language)
- 數(shù)據(jù)控制語言,主要負(fù)責(zé)權(quán)限管理(用戶)。代表指令:grant、revoke。
SQL基本操作(crud)
庫操作
- 新增數(shù)據(jù)庫:
- 基本語法:create database 數(shù)據(jù)庫名稱 [庫選項(xiàng)];
- 庫選項(xiàng):用來約束數(shù)據(jù)庫,氛圍兩個選項(xiàng):
- charset/character set,具體字符集(數(shù)據(jù)庫的編碼格式),例:GBK、UTF8...
- 校對集設(shè)定:collate具體校對集。
- 例如:create database my_database charset utf8;
- 注意點(diǎn):
- 如果想用保留字或者關(guān)鍵字當(dāng)做庫名,需要使用反引號(Tab上面那個鍵)。
- 如果想用中文當(dāng)庫名,需要加一行set name gbk;
- 每個數(shù)據(jù)庫下都有一個opt文件:保留了庫選項(xiàng)。
- 查看數(shù)據(jù)庫:
- 查看所有數(shù)據(jù)庫:show databases;
- 查看指定部分的數(shù)據(jù)庫:模糊查詢
- show database like 'pattern';
- pattern:% 表示匹配多個字符, _ 表示匹配單個字符 。
- 例如:查看以information_開頭的數(shù)據(jù)庫名:
- show databases like 'information\_%';(_需要轉(zhuǎn)義,用?)
- 查看數(shù)據(jù)庫創(chuàng)建語句
- show create database 數(shù)據(jù)庫名;
- 更新數(shù)據(jù)庫:
- 基本語法:alter database 數(shù)據(jù)庫名 charset/character [=] 字符集;
- 數(shù)據(jù)庫名字不可修改,數(shù)據(jù)庫的修改僅限庫選項(xiàng):字符集、校對集(依賴于字符集)
- 修改字符集后,校對集自動修改
- 刪除數(shù)據(jù)庫:
- drop database 數(shù)據(jù)庫名;
表操作
- 新增數(shù)據(jù)表:
- 基本語法:
create table [if not exists] 表名 ( 字段名 數(shù)據(jù)類型, ... 字段名 數(shù)據(jù)類型 )[表選項(xiàng)]; - if not exists:可選項(xiàng),如果表不存在就創(chuàng)建,否則不執(zhí)行創(chuàng)建代碼。
- 表選項(xiàng):
- 字符集:charset / character set 具體字符集。
- 校對集:collate 具體校對集。
- 存儲引擎:engine 具體存儲引擎(常見為innodb和myisam)。
- 注意:任何一個表都需要制定數(shù)據(jù)庫。
- 方案一:create table 數(shù)據(jù)庫名.表名...
- 方案二:use 數(shù)據(jù)庫名,再創(chuàng)建表。
- 基本語法:
- 查看數(shù)據(jù)表(跟查看數(shù)據(jù)庫類似):
- show tables;
- show tables like 'pattern';
- show create table 表名 \g或者\(yùn)G或者;
- \g:;(分號)。
- \G:將查到的表旋轉(zhuǎn)90度查看。
- desc 表名; == describe 表名; == show columns from 表名;
- 修改數(shù)據(jù)表(分兩類修改):
- 修改表本身:表名和表選項(xiàng)
- 表名:rename table 舊表名 to 新表名;
- 表選項(xiàng):alter table 表名 表選項(xiàng) [=] 值;
- 例:
- 修改字段(新增、修改、重命名、刪除)
- 新增字段:
- 基本語法:alter table 表名 add [column] 字段名 數(shù)據(jù)類型 [列屬性][位置];
- 位置:first:第一個位置、after 字段名:在指定字段之后,默認(rèn)是在最后一個字段之后。
- 例:
- 修改字段(修改列屬性或者數(shù)據(jù)類型):
- 基本語法:alter table 表名 modify 字段名 數(shù)據(jù)類型 [列屬性][位置];
- 例:alter table my_class modify name varchar(30) first;
- 重命名字段:
- 基本語法:alter table 表名 change 舊字段名 新字段名 數(shù)據(jù)類型 [列屬性][位置];
- 例:alter table my_class change name new_name varchar(30) after id;
- 刪除字段:
- 基本語法:alter table 表名 drop 字段名;
- 新增字段:
- 修改表本身:表名和表選項(xiàng)
- 刪除數(shù)據(jù)表:
- 基本語法:drop table 表名1,表名2,...表名n;
- 可以一次性刪除多張表。
- 基本語法:drop table 表名1,表名2,...表名n;
數(shù)據(jù)操作
- 新增數(shù)據(jù):
- 給全字段插入數(shù)據(jù),不需要指定字段列表:要求插入的字段與表中字段順序一致。非數(shù)值數(shù)據(jù),建議使用單引號。
- 基本語法:insert into 表名 values (值列表)[,(值列表)...];
- 例: insert into my_student values ('zhangsan', 'male', 22, 160, 1);
- 給部分字段插入數(shù)據(jù)。
- 基本語法:insert into 表名 (字段列表) values (值列表)[,(值列表)...];
- 例:insert into my_student (name, gender, age, height, c_id) values ('zhangsan', 'male', 22, 160, 1);
- 給全字段插入數(shù)據(jù),不需要指定字段列表:要求插入的字段與表中字段順序一致。非數(shù)值數(shù)據(jù),建議使用單引號。
- 查看數(shù)據(jù):
- 基本語法:select */字段列表 from 表名 [where條件];
- 例:select * from my_student;
- 更新數(shù)據(jù):
- 基本語法:update 表名 set 字段名=字段值 [where條件];
- 例:update my_student set gender='female' where id=1;
- 刪除數(shù)據(jù):
- 基本語法:delete from 表名 [where條件];
- 例:delete from my_student where id=1;
轉(zhuǎn)載于:https://www.cnblogs.com/ronaldo-coder/p/ronaldo.html
總結(jié)
- 上一篇: git修改文件权限方式
- 下一篇: linux cmake编译源码,linu