---------------------------------------------------------------------------
創建表
CREATE TABLE 表名 (屬性名 數據類型 [完整性約束條件].
屬性名 數據類型 [完整性約束條件].
.
.
屬性名 數據類型 [完整性約束條件]
);
約束條件 | 說明 |
PRIMARY KEY | 標識該屬性為該表的主鍵,可以唯一標識的對應的記錄 |
FOREIGN KEY | 標識該屬性為該表的外鍵,與某表的主鍵關聯 |
NOT NULL | 標識該屬性不能為空 |
UNIQUE | 標識該屬性的值是唯一的 |
AUTO_INCREMENT | 標識該屬性的值自動增加 |
DEFAULT | 為屬性設置默認值 |
---------------------------------------------------------------------------
查看表
show tables; ----查看當前數據庫中的表
插入數據
insert into `表名` (`字段1`,`字段2`,`字段3`...) values ('字段1','字段2','字段3'...);
添加新屬性
alter table 表名 add 字段 字段類型;
---------------------------------------------------------------------------
查看表結構
desc 表名; ----查看基本表結構
show create table 表名; ----查看表詳細結構
---------------------------------------------------------------------------
修改表
alter table 舊表名 rename 新表名; ----修改表名
alter table 表名 change 舊屬性名 新屬性名 新數據類型; ----修改字段
alter table 表名 add 屬性名1 數據類型 [完整性約束條件] [FIRST|AFTER 屬性名2]; ----增加字段
alter table 表名 drop 屬性名; ----刪除字段
---------------------------------------------------------------------------
刪除表
drop table 表名; ----刪除表
---------------------------------------------------------------------------
單表查詢
select 字段1,字段2,字段3,...from 表名; ----查詢所有字段
select * from 表名; ----查詢所有字段
select 字段1,字段2,... from 表名; ----查詢指定字段
select 字段1,字段2,... from 表名 where 條件表達式; ----where條件查詢
select 字段1,字段2,... from 表名 where 字段 [not] in (元素1,元素2,元素3); ----選擇[不]在括號內元素(部分字段)
select 字段1,字段2,... from 表名 where 字段 [not] between a and b; ----選擇[不]在ab范圍內元素(部分字段)
select 字段1,字段2,... from 表名 where 字段 is NULL; ----空值查詢
select 字段1,字段2,... from 表名 where 字段 like '%字符a%'; ----模糊查詢字段中包含字符a元素
select 字段1,字段2,... from 表名 where 字段 like '字符a_'; ----模糊查詢字段中字符a后還有1個字的元素(1個_表示一個字符)
select 字段1,字段2,... from 表名 where 條件表達式 and 條件表達式; ----與條件查詢
select 字段1,字段2,... from 表名 where 條件表達式 or 條件表達式; ----或條件查詢
select distinct 字段 from 表名; ----去重復查詢
select 字段1,字段2,... from 表名 order by 字段 (desc); ----按字段升序(降序)排序
select 字段1,group_concat(字段2) from 表名 group by 字段1; ----按字段1分組輸出字段2
select 字段1,count(字段2) from 表名 group by 字段1; ----按字段1分組輸出字段2個數
select 字段1,count(字段2) from 表名 group by 字段1 having count(字段2)+條件表達式; ----按字段1分組輸出條件內字段2個數(having對查詢進行篩選)
select 字段1,count(字段2) from 表名 group by 字段1 with rollup; ----with rollup將結果綜合
select 字段1,字段2... from 表名 limit a,b; ----輸出從a開始b條數據
select 字段1,count(字段2) as 命名 from 表名 group by 字段1; ----按字段1分組重命名輸出字段2個數
---------------------------------------------------------------------------
使用聚合函數查詢
select count(*) from 表名; ----計算表中有幾組數據
select 字段1,count(*) from 表名 group by 字段1; ----統計字段數量
select stuName,sum(字段) from 表名 where 條件表達式; ----求和函數
select stuName,sum(字段) from 表名 group by 字段; ----求和函數
select 字段,avg(字段) from 表名 where 條件表達式; ----平均函數
select 字段,avg(字段) from 表名 group by 字段; ----平均函數
select 字段,sum(字段) from 表名 where 條件表達式; ----最大值函數
select 字段,sum(字段) from 表名 group by 字段; ----最大值函數
select 字段,sum(字段) from 表名 where 條件表達式; ----最大值函數
select 字段,min(字段) from 表名 where 條件表達式; ----最小值函數
---------------------------------------------------------------------------
連接查詢
內連接查詢
select * from 表1,表2...; ----連接兩個表
select * from 表1,表2... where 條件表達式; ----符合條件的兩個表屬性
select 字段1,字段2... from 表1,表2... where 條件表達式; ----符合條件的兩個表屬性
外連接查詢
select 字段1,字段2... from 表名 left join 表名 on 條件表達式; ----左連接
select 重命名1.字段1,重命名2.字段2... from 表1 重命名1 left join 表2 重命名2 on 條件表達式; ----左連接
select 重命名1.字段1,重命名2.字段2... from 表1 重命名1 right join 表2 重命名2 on 條件表達式; ----右連接
多條件查詢
select 字段1,字段2... from 表1,表2... where 條件表達式 and 條件表達式; ----多條件查詢
---------------------------------------------------------------------------
子查詢
select * from 表名 where 字段 not in (select 字段 from 表名); ----帶in關鍵字查詢(條件落在另一個select查詢結果中)
select * from 表名 where 字段 條件表達式 (select查詢); ----條件查詢判斷select結果
select * from 表名 where 字段>= any (select查詢); ----條件判斷符合任意select查詢結果
select * from 表名 where 字段>= all (select查詢); ----條件判斷符合所有select查詢結果
---------------------------------------------------------------------------
合並查詢
select 字段 from 表名 union select 字段 from 表名; ----將查詢到的記錄合並(去重復)
select 字段 from 表名 union all select 字段 from 表名; ----將查詢到的記錄合並(不去重復)
---------------------------------------------------------------------------
取別名
select a.字段 from 表名 重命名a where 條件表達式; ----表重命名
select a.字段 (as) 重命名b from 表名 重命名a where 條件表達式; ----字段重命名
---------------------------------------------------------------------------
插入數據
insert into 表名 values(數據1,數據2,數據3...); ----插入數據(沒有的用NULL)
insert into 表名(字段1,字段2...) values (數據1,數據2...); ----給指定字段插入數據
---------------------------------------------------------------------------
更新數據庫
update 表名 set 字段1=新數據1,字段2=新數據2... where 條件表達式; ----更新數據
update 表名 set 字段1=新數據1,字段2=新數據2... where 字段 like 數據; ----like查詢並更新數據
delete from 表名 where 條件表達式; ----刪除指定條件的數據
---------------------------------------------------------------------------
數據索引
create table 表名(屬性名 數據類型[約束條件],
屬性名 數據類型[約束條件],
...
unique|fulltext|spatial index|key 別名 (屬性名(長度) asc|desc)
); ----在創建表的時候創建索引
unique|fulltext|spatial index|key 別名 (屬性名1,屬性名2... asc|desc) ----創建表時的多列索引
create unique|fulltext|spatial index 別名 on 表名(屬性名); ----在已經存在的表中創建索引
alter table 表名 add unique|fulltext|spatial index 別名(屬性名);
create index 別名 on 表名(屬性名1,屬性名2...); ----在已經存在的表中創建多列索引
alter table 表名 add index 別名(屬性名1,屬性名2...);
drop index 索引名 in 表名; ----刪除索引
---------------------------------------------------------------------------
視圖
create view 視圖名 as select 別名1.字段1,別名2.字段2... from 表1 別名1,表2 別名2... where 條件表達式; ----創建視圖
insert into 視圖名 values(字段1,字段2...); ----視圖中插入數據
update 視圖名 set 字段1=新數據,字段2=新數據... where 條件表達式; ----視圖中更新數據
delete from 視圖名 where 條件表達式; ----刪除符合條件的數據
drop view (if exists) 視圖名 (restrict|cascade); ----刪除視圖
---------------------------------------------------------------------------
觸發器
create trigger 觸發器名 after insert
on 表名 for each row
update 表名 set 字段表達式 where 條件表達式;
delimiter |
create trigger 觸發器名 after delete
on 表名 for each row
begin
update 表名 set 字段表達式 where 條件表達式;
insert into 表名 values(字段1,字段2...);
delete form 表名 where 條件表達式;
......
end
|
delimiter; ----多執行語句觸發器
drop trigger 觸發器名;
---------------------------------------------------------------------------
常用函數
curdate() ----返回當前日期(年月日)
curtime() ----返回當前時間
now() ----返回當前精確時間(年月日時分秒)
year() ----返回date數據類型的年
month() ----返回date數據類型的月
day() ----返回數據date數據類型的天
char_length() ----計算字符個數
upper() ----將字符大寫
lower() ----將字符小寫
abs() ----求絕對值
sqrt() ----求平方根
mod(a,b) ----a除b求余
md5('字符串') ----密碼加密(不可逆)
aes_encrypt('密碼','秘鑰') ----秘鑰加密
aes_encrypt('密文','秘鑰') ----秘鑰解密
----在使用aes_encrypt加密時,如果要將加密的密碼存入varchar類的列中需要hex()函數將密文轉化成十六進制,解密時將密文用unhex()取出
---------------------------------------------------------------------------
創建存儲函數和存儲過程
delimiter &&
create procedure 存儲過程名 (in 變量 數據類型,out 變量 數據類型,inout 變量 數據類型)
reads sql data
begin
select count(*) from 表名 where 條件表達式;
end
&&
delimiter;
delimiter &&
create procedure 存儲過程名()
begin
declare 變量 數據類型;
set 變量='變量值';
insert into 表名 values(變量1,變量2....);
end
&&
delimiter ;
delimiter &&
create procedure 存儲過程名()
begin
declare a,b 數據類型;
select 字段1,字段2 into a,b from where id=1;
insert into 表名 values(null,a,b);
end
&&
delimiter;
delimiter &&
create procedure 存儲過程名()
begin
declare a,b 數據類型;
declare 游標名 cursor for select 字段1,字段2... from 表名;
open 游標名;
fetch 游標名 into a,b;
insert into 表名 values(a,b);
close 游標名;
end
&&
delimiter;
declare cursor_name cursor for select_statement; ----定義游標
open cursor_name; ----打開游標
close cursor_name; ----關閉游標
fetch cursor_name into varname; ----使用游標
delimiter &&
create procedure 存儲過程名(in 字段 數據類型)
begin
select count(*) into @num from 表名 where 條件表達式
if @num>0 then update 表名 set 字段='字符串' where 條件表達式;
else
insert into 表名 values(字段1,字段2...);
end if;
end
&&
delimiter;
delimiter &&
create procedure 存儲過程名(in 字段 數據類型)
begin
select count(*) into @num from 表名 where 條件表達式;
case @num
when 1 then update t_user1 set 字段1='字符'... where 條件表達式;
when 2 then insert into t_user1 values();
else insert into t_user1 values();
end case;
end
&&
delimiter ;
delimiter &&
create procedure 存儲過程名(in totalNum(變量名) int)
begin
字段:loop
set totalNum = totalNum-1;
if totalNum = 0 then leave aaa;
elseif totalNum = 3 then iterate aaa; ----iterate 當totalNum=3時候跳過(類似continue);
end if;
insert into 表名 values(totalNum,'23123123','123125123');
end loop 字段;
end
&&
delimiter ;
delimiter &&
create procedure 存儲過程名(in totalNum(變量名) int)
begin
repeat
set totalNum=totalNum-1;
insert into t_user3 values(totalNum,'2123123','1235135');
until totalNum=2
end repeat;
end
&&
delimiter
delimiter &&
create procedure 存儲過程名(in totalNum(變量名) int)
begin
while totalNum>3 do
insert into t_user3 values(totalNum,'1231576y24','12312315');
set totalNum=totalNum-1;
end while;
end
&&
delimiter
show procedure|function status like '存儲過程名|函數名';
show create procecdure|function sp_name;
alter procedure | function sp_name characteristic; ----修改存儲過程和函數(sp_name表示函數名或過程名)
characteristic{
{contains sql} no sql|reads sql data|modifies sql datta}
|sql security{definer|invoker}
|comment 'sting' ----后面加注釋語句
drop procedure|function sp_name;
---------------------------------------------------------------------------
數據庫備份與還原
mysqldump -u username -p dbname table1 table2 ...>BackupName.sql ----備份數據庫(將數據庫導出為.sql文件)
dbname----數據庫名
table----表名可以不加
BackupName----導出數據庫名(>后面可以用絕對路徑)
mysql -u root -p (dbname) <backup.sql ----dbname表示數據庫名稱(可選),輸入時是新建一個數據庫導入已有的表,不輸入則是導入已有的庫(>后面可以用絕對路徑)