MySQL 5 資料庫基礎語句總結

qx1415926發表於2017-05-27

近來開始接觸MySQL,為了方便學習使用,將一些常用的基本語句做了總結。以“庫-表-欄位-資料”的結構框架為順序,以“增改查刪”為線索,總結了如下這些語句。


資料型別

整數型:

型別 大小 範圍(無符號)
tinyint 1B 0 - 255
smallint 2B 0 - 65 535
mediumint 3B 0 - 16 777 215
int 4B 0 - 4 294 967 295
bigint 8B 0 - 18 446 744 073 709 551 615

浮點型:

型別 大小
float 4B
double 8B
decimal

字串型別:

型別 大小
char 255B
varchar 255B
tinyblob 255B
tinytext 255B
blob 65 535B
text 65 535B
mediumblob 16 777 215B
mediumtext 16 777 215B
longblob 4 294 967 295B
longtext 4 294 967 295B

日期型別:

型別 大小 範圍 格式
date 3 1000-01-01/9999-12-31 YYYY-MM-DD
time 3 -838:59:59/838:59:59 HH:MM:SS
year 1 1901/2155 YYYY
datetime 8 1000-01-01-00:00:00/9999-12-31-23:59:59 YYYY-MM-DD HH:MM:SS
timestamp 8 1970-01-01-00:00:00/2037年某時 YYYYMMDDHHMMSS

操作語句:

庫:

新增

create database [if not exists] 庫名;

查詢

show databases;   
use庫名;select database();
show create database庫名[\G]

修改

alter database 庫名;

刪除

drop database [if exists] 庫名;

表:

建立

use 庫名; create table 表名(欄位名1 欄位型別 欄位約束, 欄位名2 欄位型別 欄位約束...);
create table 庫名.表名(欄位名1 欄位型別 欄位約束, 欄位名2 欄位型別 欄位約束...);

新增

insert into 表名[(欄位,欄位...)] values (值1,值2...),(值1,值2...)...;

查詢

describe 表名;簡單
show create table 表名 \G詳細
show tables;查詢庫所有表

修改

update 表名 set c=新值... where 條件;
alter table 表名 rename 表名;修改表名

刪除

delete from 表名;刪除表資料
drop table 表名;刪除表

欄位:

新增

alter table 表名 add 欄位名 type [約束] [first|after old_欄位名];

查詢

select * from 表名;

修改

alter table 表名 modify 欄位名 type;修改型別
alter table 表名 modify 欄位_name type first|after 欄位名;修改位置

刪除

alter table 表名 drop 欄位名;

約束語句:

主鍵約束:

建立

create table 表名(欄位名 型別 primary);
create table 表名(欄位名1 型別,欄位名2 型別,欄位名3 型別,primary key(欄位名1,欄位名2));

新增

alter table 表名 add primary key(欄位名1,欄位名2);

修改

alter table 表名 modify 欄位名 型別 primary key;

刪除

alter table 表名 drop primary key;

外來鍵約束:

新增

alter table 表名1 add [constraint 外來鍵名] foreign key(外來鍵欄位名) references 表名2(引用欄位名);

查詢

show create table 表名;

刪除

alter table 表名 drop foreign key 外來鍵名;

預設值約束:

新增

alter table 表名 alter 欄位名 set default 預設值;
create table 表名(欄位名 型別 not null default 預設值);

刪除

alter table表名 alter 欄位名 drop default;     

唯一性約束:

建立

create table 表名(欄位名1 型別 not null unique,欄位名2 型別 not null, unique(欄位名2));

新增

alter table 表名 add unique(欄位1,欄位2…);

刪除

alter table 表名 drop index 欄位名;

自新增約束:

建立

create table 表名(欄位名 型別 primary key auto_increment);

新增

alter table 表名 modify欄位名 型別 not null auto_increment;

刪除

alter table 表名 modify欄位名 型別 not null

資料操作語句:

select(查詢):

全部

select * from 表名;

查詢任意欄位(按列表序輸出)

select 欄位列表 from表名; 

條件

select 欄位列表 from 表名 where 條件表示式;
條件 符號或關鍵字
比較 =、<、<=、>、>=、!=、!<、!>、<>
指定範圍 between and、not between and
指定集合 in、not in
匹配字元 like、not like(%任意字元、_一個字元)
是否為空值 is null、is not null
多個查詢條件 and、or

分組

select欄位列表from 表名 group by 欄位列表 [having 條件表示式] [with rollup];

排序

select欄位列表from 表名 order by 欄位列表 [asc | desc];

限制

select欄位列表from 表名 limit [初始位置(從0開始計數)], 查詢記錄數量;

避免重複

select distinct(欄位名),欄位列表 from 表名;

總條數

select [欄位名] count(*) from 表名;

平均數

select [欄位名] avg(*) [as ‘平均組名’] from 表名;

求和值

select [欄位名] sum(*) [as ‘求和組名’] from 表名;

最大最小值

select [欄位名] max[min](*) [as ‘極值組名’] from 表名;

內連線查詢

select 欄位列表 from 表名1  [inner] join 表名2  on 表名1.欄位名=表名2.欄位名;
select a.欄位,b.欄位 from 表名1 as a inner join 表名2 as b on a.欄位=b.欄位;
select 欄位列表 from1,表2  where1.欄位=表2.欄位;
select a.欄位,b.欄位 from 表名1 as a,表名2 as b where a.欄位=b.欄位;

insert(新增):

insert into 表名[(欄位列表)] values(欄位值列表),(欄位值列表)…;
insert into 表名1(欄位列表1) select欄位列表2 from 表名2 where 條件表示式;

update(修改):

update 表名 set 欄位1=值1,…欄位n=值n  where 條件表示式;

刪除(delete):

delete from 表名 [where 條件表示式];

其他命令:

查詢當前時間:

select now();

查詢搜尋引擎:

show engines;

執行文字檔案命令:

source filename
\. filename
shell>mysql [庫名] < text_file

相關文章