MySQL 基本文法

來源:互聯網
上載者:User

標籤:handler   art   engine   lstat   主鍵   tar   過程   方式   刪除索引   

1.說明: 建立預設資料庫
create database 庫名;


2.說明: 建立指定字元編碼的資料庫
create database 庫名 character set 字元編碼格式;


3.說明:刪除資料庫
drop database 庫名;


4.說明: 建立新表
create table 表名 (列名1 列類型 列約束[是否為空白,是否為主鍵,是否主鍵自動增加...],
列名2 列類型 列約束[是否為空白,是否為主鍵,是否主鍵自動增加...],
...
列名n列類型 列約束[是否為空白,是否為主鍵,是否主鍵自動增加...]
);


5.說明: 刪除表
drop table 表名;


6.說明: 修改表結構
①說明: 添加列
alter table 表名 add 列名 列類型 [約束];
②說明: 修改列
alter table 表名 change 列名 列新名 新列類型 [約束];
③說明: 刪除列
alter table 表名 drop 列名;
④說明: 重新命名表
alter table 表名 rename 新表名;
⑤說明: 修改表的儲存引擎
alter table 表名 engine = ‘儲存引擎名‘;


7.說明: 查看當前資料庫伺服器下所有的資料庫
show databases;


8.說明: 查看當前資料庫的編碼格式
show variables like ‘character%‘;


9.說明: 設定當前用戶端字元集
set character_set_client = 字元格式設定;


10.說明: 設定串連字元集
set character_set_connection = 字元格式設定;


11.說明: 設定結果字元集
set character_set_results = 字元格式設定;


12.說明: 使用指定資料庫
use 庫名;


13.說明: 查看當前資料庫下所有的表
show tables;


14.說明: 查看錶結構
desc 表名;/describe 表名;


15.說明: 查看之前所定義的表的結構的文法
show create table 表名;


16.說明: 查詢表中所有資料
show * from 表名;


17.說明: 查詢表中指定條件的資料
show * from 表名 where 篩選條件;


18.說明: 插入數值
insert into 表名(需要插入資料的列1,列2...列n)  values(對應列的數值1,數值2...數值n);


19.說明: 修改表中資料
update 表名 set 列=‘新值’where 限定修改條件;


20.說明: 清空表中資料(可恢複)
delete from 表名;


21.說明: 清空表中資料(不可恢複)
truncate table 表名;


22.說明: 刪除符合指定條件的資料
delete from 表名 where 指定條件;


23.說明: 增加指定名稱的主鍵
alter table 表名 add constraint 指定名稱(PK) primary key (需要設為主鍵的列名);


24.說明: 刪除表中主鍵
alter table 表名 drop primary key [主鍵名];


25.說明: 添加指定名稱的外鍵
alter table 從表名 add constraint 指定名稱(FK) foreign key (從表中列名) references 主表(主鍵列);


26.說明: 刪除表中指定名稱的外鍵
alter table 表名 drop foreign key [外鍵名];


27.說明: 查詢表中指定列的值(消除結果中的重複值)
select distinct 列名 from 表名;


28.說明: 分頁查詢
select 列名 from 表名 where 條件 limit 開始位置,表示行數;


29.說明: select 文法
select [all|distinct]
*|表名.*|表名.列名 [as 列別名]
from 
表名[as 表別名]
[left|right|inner join 表名2]-- 聯集查詢
[where 指定條件]-- 指定結果需要滿足的條件
[group by 進行分組的列名]-- 指定結果按照哪幾個欄位進行分組
[having 指定分組結果的次要條件]-- 過濾分組的記錄必須滿足的次要條件
[order by 進行排序的列名 排序方式]-- 指定查詢記錄按一個或多個條件排序
[limit 限制條件];


30.說明: 範圍查詢
select * from 表名 where 列名 between 小值 and 大值;


31.說明: 模糊查詢
select * from 表名 where 列名 like ‘模糊查詢條件‘;


32.說明: 查詢模糊查詢條件中帶逸出字元的資料
select * from 表名 where 列名 like ‘逸出字元符號_%‘ escape ‘逸出字元符號‘;


33.說明: 包含查詢
select * from 表名 where 列名 in (範圍);


34.說明: 不包含
select * from 表名 where not in (範圍);


35.說明: 資料為空白查詢
select * from 表名 where 列名 is null;


36.說明: 資料不為空白查詢
select * from 表名 where 列名 is not null;


37.說明: 多表查詢
select * from 表1,表2,...表n where 多表中的關聯準則;


38.說明: 分組查詢
select * from 表名 [where 篩選條件] group by 分組列名 [having 分組結果篩選條件];


39.說明: 內串連查詢
①隱式內串連
select 要查詢的列 from 表1,表2...表n where 多表關聯準則;
②顯示內串連
select 要查詢的列 from 表1 inner join 表2 on 多表關聯準則;


40.說明: 外串連查詢
①左串連
select 要查詢的列 from 表1 left join 表2 on 多表關聯準則;
②右串連
select 要查詢的列 from 表2 right join 表2 on 多表關聯準則;


41.說明: 變數賦值使用
① set @變數名 = 賦值; 或 set @變數名 := 賦值;
② select @變數名 := 賦值; 或 select @變數名 := 欄位名 from 表名 where 條件;


42.說明: if 條件陳述式
select if(運算式1,運算式2,運算式3) from 表名 [where 篩選條件...]; 判斷運算式1,成立返回運算式2結果,否則返回運算式3結果。


43.說明: case 條件陳述式
① select case when 條件1 then 結果1 when 條件2 then 結果2 ... else 其他結果 end [別名] from 表名 [where 篩選條件...];
② select case 要判斷的欄位或運算式 when 常量1 then 結果1 ... else 其他結果 end [別名] from 表名 [where 篩選條件...];


44.說明: 查詢 session 變數
show session variables;


45.說明: 查詢 global 變數
show global variables;


46.說明: 設定系統變數新值
set @@變數類型.變數名 = 新值;


47.說明: 主鍵索引的添加
①建立表時添加: create table 表名 (列名 類型 primary key);
或: create table 表名 (列名 類型 ...,primary key (索引列));
②建立表後添加: alter table 表名 add primary key (索引列);


48.說明: 唯一索引的添加
①建立表時添加: create table 表名 (列名 類型 unique);
或: create talbe 表名 (列名 類型...,unique key [索引名] (索引列));
②建立表後添加: alter table 表名 add unique key [索引名] (索引列);


49.說明: 常規索引的添加
①建立表時添加: create table 表名 (列名 類型 ..., index/key [索引名] (索引列1,索引列2,..));
②建立表後添加: alter table 表名 add index [索引名] (索引列1,索引列2...);


50.說明: 全文索引的添加 (只能用於MyISAM 類型的資料表)(用於char,varchar,text資料列類型)
①建立表時添加: create table 表名 (列名 類型 ...,fulltext(索引列))engine = MyISAM;
②建立表後添加: alter table 表名 add fulltext [索引名] (索引列);


51.說明: 刪除索引
① drop index 索引名 on 表名;
② alter table 表名 drop index 索引名;
③ alter table 表名 drop primary key;


52.說明: 查看索引
show index[或keys] from 表名;


53.說明: 分析SQL語句的執行效能
explain 表名; <==> desc 表名
explain select 語句;


54.說明: 建立視圖
create view 視圖名 as <select語句>;


55.說明: 刪除視圖
drop view 視圖名;


56.說明: 查看視圖
select * from 視圖名;


57.說明: 查看所有視圖
use information_schema;
select * from views;


58.說明: 建立自訂函數
create function 函數名(參數列表)
returns 傳回值類型
【begin】 函數主體 【end】;


59.說明: 調用函數
select 函數名;


60.說明: 刪除函數
drop function 函數名;


61.說明: 自訂函數中的變數
declare 變數名【,變數名2...】 VARIANT 資料型別 【default 預設值】;


62.說明: 給變數賦值
select 要賦的值 into 要賦值的變數名;
<==> select 要賦值的變數名 := 要賦的值;
<==> set 要賦值的變數名 = 要賦的值;


63.說明: 流程式控制制語句文法:
① if 條件判斷結構
if 條件判斷語句1 then 滿足條件1輸出的結果;
elseif 條件判斷語句2 then 滿足條件2輸出的結果;
...
else 都不滿足上述條件輸出的結果;
end if;
② case 分支結構
Ⅰ: 等值判斷
case 判斷欄位/列/變數
when 值1 then 等於值1輸出的結果;
when 值2 then 等於值2輸出的結果;
...
else 都不滿足上述條件輸出的結果;
end case;
Ⅱ: 區間判斷
case 
when 判斷條件1 then 滿足條件1輸出的結果;
when 判斷條件2 then 滿足條件2輸出的結果;
...
else 都不滿足上述條件輸出的結果;
end case;


64.說明: Loop 迴圈(死迴圈,一般需要跟leave、iterate)
[begin_label:] Loop
迴圈語句
end Loop [end_label];


65.說明: Leave 跳出迴圈控制
leave label;


66.說明: iterate 跳出本次迴圈
iterate label;


67.說明: repeat 迴圈 (滿足條件跳出迴圈)
[begin_label:] repeat
迴圈語句;
until 結束迴圈條件
end repeat [end_label];


68.說明: while 迴圈 (滿足條件時,執行迴圈內的條件)
[begin_label:] while 參與迴圈執行的條件 DO
迴圈語句;
end while [end_label];


69.說明: 建立預存程序
create procedure 預存程序名稱 ([預存程序參數IN\out\inout] 參數名稱 參數類型)
begin
預存程序的主體;
end;


70.說明: 預存程序調用
call 預存程序名稱(參數);


71.說明: 刪除預存程序
drop procedure 預存程序名稱;


72.說明: 查看庫內所有預存程序
show procedure status;


73.說明: 定義異常
declare 異常名 condition for 異常類型;(sqlstate_value 例: sqlstate ‘42000‘或 mysql_error_code 例: 1148)


74.說明: 異常捕獲
declare 錯誤處理方式 handler for 異常名[,...] 錯誤碼(處理方式:continue,exit,undo)


75.說明: 關閉自動認可模式
set autocommit = 0;


76.說明: 開啟自動認可模式
set autocommit = 1;


77.說明: 開始事務
start transaction;


78.說明: 提交事務
commit;


79.說明: 資料復原
rollback;

MySQL 基本文法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.