MySQL的使用
所有的sql語句必須以分號;結(jié)尾
進入數(shù)據(jù)庫
退出登錄
一. 庫操作
- 查看所有數(shù)據(jù)庫
- 查看當前使用的數(shù)據(jù)庫
- 顯示當前數(shù)據(jù)庫時間
- 切換數(shù)據(jù)庫
- 創(chuàng)建新數(shù)據(jù)庫
- create database db_name charset=utf8;
- create database python charset=utf8;創(chuàng)建python數(shù)據(jù)庫
- 查看數(shù)據(jù)庫是怎么創(chuàng)建的
- show create database db_name
- 刪除數(shù)據(jù)庫(慎用!)
- drop database db_name;
- drop database python; 刪除python數(shù)據(jù)庫
- 查看數(shù)據(jù)庫版本
二. 數(shù)據(jù)的完整性
- 一個數(shù)據(jù)庫就是一個完整的業(yè)務(wù)單元,可以包含多張表,數(shù)據(jù)被存儲在表中
- 在表中為了更加準確的存儲數(shù)據(jù),保證數(shù)據(jù)的正確有效,可以在創(chuàng)建表的時候,為表添加一些強制性的驗證,包括數(shù)據(jù)字段的類型、約束
- 數(shù)據(jù)類型
- 可以通過查看幫助文檔查閱所有支持的數(shù)據(jù)類型
- 使用數(shù)據(jù)類型的原則是:夠用就行,盡量使用取值范圍小的,而不用大的,這樣可以更多的節(jié)省存儲空間
- 常用數(shù)據(jù)類型如下:
- 整數(shù):int,bit
- 小數(shù):decimal
- 字符串:varchar,char
- 日期時間: date, time, datetime
- 枚舉類型(enum)
- 特別說明的類型如下:
- decimal表示浮點數(shù),如decimal(5,2)表示共存5位數(shù),小數(shù)占2位
- char表示固定長度的字符串,如char(3),如果填充'ab'時會補一個空格為'ab '
- varchar表示可變長度的字符串,如varchar(3),填充'ab'時就會存儲'ab'
- 字符串text表示存儲大文本,當字符大于4000時推薦使用
- 對于圖片、音頻、視頻等文件,不存儲在數(shù)據(jù)庫中,而是上傳到某個服務(wù)器上,然后在表中存儲這個文件的保存路徑
- enum: 插入數(shù)據(jù)時,只能枚舉出來的選項中選擇
- 更全的數(shù)據(jù)類型可以參考http://blog.csdn.net/anxpp/article/details/51284106
- 約束
- 主鍵primary key:物理上存儲的順序
- 非空not null:此字段不允許填寫空值
- 惟一unique:此字段的值不允許重復(fù)
- AUTO_INCREMENT 自動增長
- unsigned 無符號
- 默認default:當不填寫此值時會使用默認值,如果填寫時以填寫為準
- 外鍵foreign key:對關(guān)系字段進行約束,當為關(guān)系字段填寫值時,會到關(guān)聯(lián)的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗并拋出異常
- 說明:雖然外鍵約束可以保證數(shù)據(jù)的有效性,但是在進行數(shù)據(jù)的crud(增加、修改、刪除、查詢)時,都會降低數(shù)據(jù)庫的性能,所以不推薦使用,那么數(shù)據(jù)的有效性怎么保證呢?答:可以在邏輯層進行控制
數(shù)值類型(常用)
類型 字節(jié)大小 有符號范圍(Signed) 無符號范圍(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', 實際存儲為'ab ', 輸入'abcd' 實際存儲為 'abc'
VARCHAR 0-255 類型:varchar(3) 輸 'ab',實際存儲為'ab', 輸入'abcd',實際存儲為'abc'
TEXT 0-65535 大文本
日期時間類型
類型 字節(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(重點)
- 插入數(shù)據(jù)
1.1 全字段插入
1.1.1 全字段插入單條數(shù)據(jù)
- insert into tab_name values(字段1, 字段2, 字段....);
- 查看表結(jié)構(gòu),再對應(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');
- 說明:
- 注意!插入字段的值和類型必須跟定義的一致,并且不能超過最大值,否則會報錯
- 定義表時定義了默認值(default)的時候, 在全字段插入時, 可以不用插入定義了默認值的字段,只需要用default占個位置,就會在此字段使用默認值
- 枚舉類型中的下標從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(對應(yīng)字段1, 對應(yīng)字段2, 對應(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 邏輯刪除
添加一個字段來表示這條信息是否不能再使用了
# 添加一個is_delete字段,默認為0alter table students add is_delete bit default 0;
把你要刪除的數(shù)據(jù)的is_delete修改為1, 就表示刪除了
- 查詢基本使用
4.1 查詢所有
4.1.1 查詢所有并指定顯示的列
- select name, age, gender from tab_name; 查詢所有數(shù)據(jù),顯示name, age, gender這些列
4.1.2 查詢所有并指定顯示的列,同時為顯示的列取別名
- select name as 姓名, age as 年齡, gender as 性別, addr as 地址 from students; 增強可讀性
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等字段
五. 高級查詢
- 查詢所有
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; 使用別名.字段名查詢
- 對查詢結(jié)果消除重復(fù)
- 使用邏輯運算符查詢
4.1 and 多個條件都要滿足
- 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 滿足其中一個條件
- 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 替換、
%: 替換一個或者多個
_: 替換一個
- select * from students where name like "老_"; 查詢所有以老開頭的并且只有兩個字名字
- select * from students where name like "老%"; 查詢所有以老開頭的名字,后面任意多個字符
- select * from students where name like "老%李"; 查詢所有以老開頭,以李結(jié)尾,中間可以是一個或者多個字符的名字
5.2 rlike 正則
支持正則表達式查詢
語法:select ... from tab_name where 條件 rlike "正則表達式" ;
- 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) 表示在一個非連續(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...表示在一個連續(xù)的范圍內(nèi)
6.4 not between...and...
not between...and... 表示不在一個連續(xù)的范圍內(nèi)
- select * from students where age not between 22 and 30; 查詢所有age不在20到30這個范圍內(nèi)的信息
- select * from students where age not between 22 and 30 and gender=2; 查詢所有age不在22到30這個范圍內(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
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。