MySQL的使用
所有的sql語句必須以分號(hào);結(jié)尾
進(jìn)入數(shù)據(jù)庫(kù)
退出登錄
一. 庫(kù)操作
- 查看所有數(shù)據(jù)庫(kù)
- 查看當(dāng)前使用的數(shù)據(jù)庫(kù)
- 顯示當(dāng)前數(shù)據(jù)庫(kù)時(shí)間
- 切換數(shù)據(jù)庫(kù)
- 創(chuàng)建新數(shù)據(jù)庫(kù)
- create database db_name charset=utf8;
- create database python charset=utf8;創(chuàng)建python數(shù)據(jù)庫(kù)
- 查看數(shù)據(jù)庫(kù)是怎么創(chuàng)建的
- show create database db_name
- 刪除數(shù)據(jù)庫(kù)(慎用!)
- drop database db_name;
- drop database python; 刪除python數(shù)據(jù)庫(kù)
- 查看數(shù)據(jù)庫(kù)版本
二. 數(shù)據(jù)的完整性
- 一個(gè)數(shù)據(jù)庫(kù)就是一個(gè)完整的業(yè)務(wù)單元,可以包含多張表,數(shù)據(jù)被存儲(chǔ)在表中
- 在表中為了更加準(zhǔn)確的存儲(chǔ)數(shù)據(jù),保證數(shù)據(jù)的正確有效,可以在創(chuàng)建表的時(shí)候,為表添加一些強(qiáng)制性的驗(yàn)證,包括數(shù)據(jù)字段的類型、約束
- 數(shù)據(jù)類型
- 可以通過查看幫助文檔查閱所有支持的數(shù)據(jù)類型
- 使用數(shù)據(jù)類型的原則是:夠用就行,盡量使用取值范圍小的,而不用大的,這樣可以更多的節(jié)省存儲(chǔ)空間
- 常用數(shù)據(jù)類型如下:
- 整數(shù):int,bit
- 小數(shù):decimal
- 字符串:varchar,char
- 日期時(shí)間: date, time, datetime
- 枚舉類型(enum)
- 特別說明的類型如下:
- decimal表示浮點(diǎn)數(shù),如decimal(5,2)表示共存5位數(shù),小數(shù)占2位
- char表示固定長(zhǎng)度的字符串,如char(3),如果填充'ab'時(shí)會(huì)補(bǔ)一個(gè)空格為'ab '
- varchar表示可變長(zhǎng)度的字符串,如varchar(3),填充'ab'時(shí)就會(huì)存儲(chǔ)'ab'
- 字符串text表示存儲(chǔ)大文本,當(dāng)字符大于4000時(shí)推薦使用
- 對(duì)于圖片、音頻、視頻等文件,不存儲(chǔ)在數(shù)據(jù)庫(kù)中,而是上傳到某個(gè)服務(wù)器上,然后在表中存儲(chǔ)這個(gè)文件的保存路徑
- enum: 插入數(shù)據(jù)時(shí),只能枚舉出來的選項(xiàng)中選擇
- 更全的數(shù)據(jù)類型可以參考http://blog.csdn.net/anxpp/article/details/51284106
- 約束
- 主鍵primary key:物理上存儲(chǔ)的順序
- 非空not null:此字段不允許填寫空值
- 惟一unique:此字段的值不允許重復(fù)
- AUTO_INCREMENT 自動(dòng)增長(zhǎng)
- unsigned 無符號(hào)
- 默認(rèn)default:當(dāng)不填寫此值時(shí)會(huì)使用默認(rèn)值,如果填寫時(shí)以填寫為準(zhǔn)
- 外鍵foreign key:對(duì)關(guān)系字段進(jìn)行約束,當(dāng)為關(guān)系字段填寫值時(shí),會(huì)到關(guān)聯(lián)的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗并拋出異常
- 說明:雖然外鍵約束可以保證數(shù)據(jù)的有效性,但是在進(jìn)行數(shù)據(jù)的crud(增加、修改、刪除、查詢)時(shí),都會(huì)降低數(shù)據(jù)庫(kù)的性能,所以不推薦使用,那么數(shù)據(jù)的有效性怎么保證呢?答:可以在邏輯層進(jìn)行控制
數(shù)值類型(常用)
類型 字節(jié)大小 有符號(hào)范圍(Signed) 無符號(hào)范圍(Unsigned)
TINYINT 1 -128 ~ 127 0 ~ 255
SMALLINT 2 -32768 ~ 32767 0 ~ 65535
MEDIUMINT 3 -8388608 ~ 8388607 0 ~ 16777215
INT/INTEGER 4 -2147483648 ~2147483647 0 ~ 4294967295
BIGINT 8 -9223372036854775808 ~ 9223372036854775807 0 ~ 18446744073709551615
字符串
類型 字節(jié)大小 示例
CHAR 0-255 類型:char(3) 輸入 'ab', 實(shí)際存儲(chǔ)為'ab ', 輸入'abcd' 實(shí)際存儲(chǔ)為 'abc'
VARCHAR 0-255 類型:varchar(3) 輸 'ab',實(shí)際存儲(chǔ)為'ab', 輸入'abcd',實(shí)際存儲(chǔ)為'abc'
TEXT 0-65535 大文本
日期時(shí)間類型
類型 字節(jié)大小 示例
DATE 4 '2020-01-01'
TIME 3 '12:29:59'
DATETIME 8 '2020-01-01 12:29:59'
YEAR 1 '2017'
TIMESTAMP 4 '1970-01-01 00:00:01' UTC ~ '2038-01-01 00:00:01' UTC
三. 表操作
- 查看所有數(shù)據(jù)表
- 查看表結(jié)構(gòu)
創(chuàng)建表
create table tab_name(
字段名 字段數(shù)據(jù)類型 約束,
字段名 字段數(shù)據(jù)類型 約束,
字段名 字段數(shù)據(jù)類型 約束,
字段名 字段數(shù)據(jù)類型 約束,
...........................
);
3.1. 創(chuàng)建表案例
create table students( num_id int unsigned primary key auto_increment not null, name varchar(30) not null, age tinyint(3) unsigned, gender enum('男', '女', '中性', '保密') default '男', addr varchar(255));
- 查看表示如何創(chuàng)建的
- show create table tab_name;
- show create table students;
- 修改表名稱
- alter table old_tab_name rename to/as new_tab_name;
- 刪除表
- 修改表結(jié)構(gòu)
7.1 添加字段
7.2 修改字段名稱和類型
- alter table tab_name change 原字段名 新字段名 類型和約束;
- alter table students change birthday birth date default "1990-01-01";
7.3 刪除字段
- alter table tab_naem dorp 字段名;
- alter table students drop birthday;
四. 表的CRUD(重點(diǎn))
- 插入數(shù)據(jù)
1.1 全字段插入
1.1.1 全字段插入單條數(shù)據(jù)
- insert into tab_name values(字段1, 字段2, 字段....);
- 查看表結(jié)構(gòu),再對(duì)應(yīng)插入數(shù)據(jù)
mysql> desc students; ---------- ------------------------------------- ------ ----- ------------ ---------------- | Field | Type | Null | Key | Default | Extra | ---------- ------------------------------------- ------ ----- ------------ ---------------- | num_id | int(10) unsigned | NO | PRI | NULL | auto_increment || name | varchar(30) | NO | | NULL | || age | tinyint(3) unsigned | YES | | NULL | || gender | enum('男','女','中性','保密') | YES | | 男 | || addr | varchar(255) | YES | | NULL | || birthday | date | YES | | 1990-01-01 | | ---------- ------------------------------------- ------ ----- ------------ ----------------
- insert into students values(0, '老李', 99, 1, '地球南美', "2017-01-01");
- insert into students values(null, 'Gavin', 22, "男", '貴州貴陽', '1997-09-24');
- 說明:
- 注意!插入字段的值和類型必須跟定義的一致,并且不能超過最大值,否則會(huì)報(bào)錯(cuò)
- 定義表時(shí)定義了默認(rèn)值(default)的時(shí)候, 在全字段插入時(shí), 可以不用插入定義了默認(rèn)值的字段,只需要用default占個(gè)位置,就會(huì)在此字段使用默認(rèn)值
- 枚舉類型中的下標(biāo)從1開始
1.1.2 全字段插入多條
insert into students values(default, '老楊', 22, "男", '貴州六盤水', "1997-09-24"), (default, '老馮', 20, "女", '貴州金沙', "1998-11-20"), (default, '老張', 21, "女", '貴州貴陽', "1997-12-12"), (default, '老朱', 22, "女", '貴州六盤水', "1996-05-23");
1.2 指定字段
1.2.1指定字段插入單條數(shù)據(jù)
- insert into students (字段1, 字段2, 字段...) values(對(duì)應(yīng)字段1, 對(duì)應(yīng)字段2, 對(duì)應(yīng)字段...);
- insert into students (name, age, gender) values("老老王", 88, 4);
1.2.2 指定字段插入多條數(shù)據(jù)
insert into students (name, age, gender) values('aaa', 21, 1),('bbb', 22, 2),('ccc', 20, 3),('ddd', 23, 4),('eee', 28, 1),('xxx', 29, 2);
- 修改數(shù)據(jù)
2.1 修改全部
- update tab_name set 列1=值1, 列2=值2...where 條件
- update students set age=20;把所有人的age都改成20
2.2 指定條件修改
- update students set age=22 where name="Gavin";把所有name="Gavin"的行的age都修改成22
- update students set addr="地球", age=99 where addr is NULL;把所有addr是NULL的行的addr修改成地球,age修改成99
- 刪除數(shù)據(jù)
3.1 物理刪除
3.1.1 刪除全部
- delete from tab_name where 1; 刪除表中的所有數(shù)據(jù)
- delete from yyy where 1;刪除yyy表中的所有數(shù)據(jù)
3.1.2 指定條件刪除
- delete from students where name="aaa"; 刪除name為aaa的行
3.2 邏輯刪除
- 查詢基本使用
4.1 查詢所有
4.1.1 查詢所有并指定顯示的列
- select name, age, gender from tab_name; 查詢所有數(shù)據(jù),顯示name, age, gender這些列
4.1.2 查詢所有并指定顯示的列,同時(shí)為顯示的列取別名
- select name as 姓名, age as 年齡, gender as 性別, addr as 地址 from students; 增強(qiáng)可讀性
4.2 指定條件查詢
4.2.1 指定條件查詢,顯示所有信息
- select * from students where gender="女"; 查詢所有g(shù)ender="女"的行
4.2.2 指定條件查詢,顯示指定字段
- select name, age, gender from students where gender="男" 查詢gender="男"的所有行,并顯示name, age, gender等字段
五. 高級(jí)查詢
- 查詢所有
1.1 查詢所有字段
1.2 查詢指定字段
- select name, age, gender from students;
1.3 查詢指定字段, 指定別名
- select name as 姓名, age as 年齡, gender as 性別 from students;
- 指定條件查詢
2.1 指定條件查詢,顯示所有信息
- select * from students where gender="女"; 查詢所有g(shù)ender="女"的行
2.2 指定條件查詢,顯示指定字段
- select name, age, gender from students where gender="男" 查詢gender="男"的所有行,并顯示name, age, gender等字段
- select students.name, students.age, students.gender from students;通過表名.字段查詢
2.3 給表取別名
- select s.name, s.age, s.gender from students as s; 使用別名.字段名查詢
- 對(duì)查詢結(jié)果消除重復(fù)
- 使用邏輯運(yùn)算符查詢
4.1 and 多個(gè)條件都要滿足
- select * from students where age>23 and age<27; 查詢age大于23 并且age小于27的所有滿足條件的行
- select name, age, gender, birthday where age>22 and age<50; 查詢所有age大于等于22,并且小于等于50的所有行,并顯示指定字段
- select * from students where age < 27 and gender="女"; 查詢年齡小于27的所有女生
4.2 or 滿足其中一個(gè)條件
- select * from students where age<22 or age>27; 查詢age小于22或者age大于27的數(shù)據(jù)
- select * from students where (age<22 or age>27) and gender="女"; 查詢age小于22或者age大于27的女生 信息
4.3 not
- select * from students where (not age<22) and gender="女"; 查詢所有age不在22以上的女生
- select * from students where not (age<22 and gender="男");查詢所有age不小于22的男生
- 模糊查詢
5.1 like 替換、
%: 替換一個(gè)或者多個(gè)
_: 替換一個(gè)
- select * from students where name like "老_"; 查詢所有以老開頭的并且只有兩個(gè)字名字
- select * from students where name like "老%"; 查詢所有以老開頭的名字,后面任意多個(gè)字符
- select * from students where name like "老%李"; 查詢所有以老開頭,以李結(jié)尾,中間可以是一個(gè)或者多個(gè)字符的名字
5.2 rlike 正則
支持正則表達(dá)式查詢
語法:select ... from tab_name where 條件 rlike "正則表達(dá)式" ;
- select * from students where name rlike "^老"; 查詢所有以老開頭的名字
- select * from students where name rlike "王$"; 查詢所有以王結(jié)尾的名字
- select * from students where addr rlike "貴.*"; 查詢所有以貴開頭的地址
- 范圍查詢
6.1 in
in(22, 26, 30) 表示在一個(gè)非連續(xù)的范圍內(nèi)
- select * from students where age=21 or age=25 or age=30;
- select * from students where age in(21, 25, 30); 查詢所有age為21,25,30的所有信息
6.2 not in
not in 不在非連續(xù)范圍內(nèi)
- select * from students where age not in(21, 25, 30); 查詢所有age不是21,25,30的所有信息
6.3 between...and...
between...and...表示在一個(gè)連續(xù)的范圍內(nèi)
6.4 not between...and...
not between...and... 表示不在一個(gè)連續(xù)的范圍內(nèi)
- select * from students where age not between 22 and 30; 查詢所有age不在20到30這個(gè)范圍內(nèi)的信息
- select * from students where age not between 22 and 30 and gender=2; 查詢所有age不在22到30這個(gè)范圍內(nèi)的信息
- 判斷空
7.1 is null: 判斷是空
- select * from students where addr is null;
- select * from students where addr is NuLl; 查詢addr是空的信息
說明:null不區(qū)分大小寫
7.2 is not null 判斷不是空
- select * from students where addr is not null; 查詢addr不是空的信息
- 排序
來源:
http://www.icode9.com/content-2-217101.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。