SQL陳述句大全
- 一、查詢操作
- 資料操作
- 支持函式
- 表、欄位屬屬性函式
- 系統函式
- 表操作
- 二、視圖
- 三、存盤程序函式
- 存盤程序示例
- 游標
- 四、事件
- 五、報表
- 六、備份
一、查詢操作
資料操作
| 序列 | 關鍵字 | 注解 | 實體 | 備注 |
|---|---|---|---|---|
| 1. | select | 檢索資料 | select * from test_name; | |
| 2. | insert | 添加資料行 | insert into test_name(id,name,age,sex) values(1,‘aaa’,12,‘女’); insert into table_name(id,name,age,sex) values(1,‘aaa’,12,‘女’); insert into new_table_name select * from table_name; | |
| 3. | delete | 洗掉資料行 | delete from table_name where age=12; | |
| 4. | truncate | 洗掉所有資料行 | truncate table_name; | |
| 5. | update | 更新資料行 | update table_name set age=13 where id=1; | |
| 6. | distinct | 回傳唯一不同值 | select distinct sex,age from table_name; | |
| 7. | where | 條件陳述句 | select * from table_name where age=12; | |
| 8. | and | 與 | select * from table_name where age=11 and sex=‘男’; | |
| 9. | or | 或 | select * from table_name where age=11 or sex=‘男’; | |
| 10. | order by | 排序 | select * from table_name order by sex; | |
| 11. | group by | 組合 | select * from table_name group by sex; | |
| 12. | having | 聚合條件類似where | select sex,avg(age) from table_name group by sex having avg(age)>10; | |
| 12. | top | 選擇開始資料行 | select top 10 sex,age from table_name; | sqlserver支持陳述句 |
| 13. | limit | 選擇資料行 | select * from table_name limit 2; | mysql支持陳述句 (limit 偏移行數,行數)(limit 行數) |
| 14. | like % _ | 模糊條件、通配符 | select * from table_name where name like ‘%a%’; | %一個或多個字符,_一個字符 [!xx]不包含 |
| 15. | ln | 包含 | select * from table_name where name in (‘aaa’); | |
| 16. | between | 兩者之間 | select * from table_name where age between 10 and 12; | |
| 17. | left join | 左全集 | right join | 右全集 |
| 18. | full outer join | 全集 | select * from table_name left join new_table_name on table_name.id=new_table_name.id | |
| 19. | union/union all | 表的串接 | select * from table_name union select * from new_table_name; | union 不重復 union all 全部 |
| 20. | as | 別名 | select * from table_name as a; | |
| 21. | exists | 正在否 | select * from table_name where exists (select age from new_table_name where table_name.id=id and age>12); | |
| 22. | mid | 字符提取 | select mid(name,1,1) from table_name; | mysql支持 |
| 23. | substring | 提取字符 | select substring(name,1,1) from table_name; | |
| 24. | regexp | 正則運算式 | select * from table_name where name regexp ‘^a’; | |
| 25. | over | 開窗函式 | select *,row_number() over(order by sex) as a from table_name; | msyql8.0以上及sqlserver支持 |
支持函式
| 函式 | 注解 | 函式 | 注解 | 函式 | 注解 |
|---|---|---|---|---|---|
| 取合函式 | |||||
| avg() | 平均值 | count() | 計數 | first() | 第一個 |
| last() | 最后一個 | max() | 最大值 | min() | 最小值 |
| sum() | 求和 | ||||
| Scalar函式 | |||||
| ucase() | 轉大寫 | lcase() | 轉小寫len() | 長度 | |
| round() | 四舍五入 | format() | 格式化 | ||
| mysql時間函式 | |||||
| now() | 當前日期時間 | curdate() | 當前日期 | curtime() | 當前時間 |
| date() | 當前日期 | extract() | date_add() | 后幾天 | |
| date_sub() | 前幾天 | datediff() | 日期間天數 | date_format() | 自定義格式 |
| sql server 時間函式 | |||||
| getdate() | 當前日期時間 | datepart() | 日期、時間 | dateadd() | 幾天前或后 |
| datediff() | 日期間天數 | convert() | 字定義格式 | ||
| 視窗函式 | |||||
| partition by | 磁區 | order by | 分組 | frame | 當前子集 |
| row_number | 分配序列號 | rank() | 排名 | dense_rank() | 等級序列 |
| percent_rank() | 百分數等級 | cume_dist() | 累積分布 | ||
| first_value() | 第一行值 | last_value() | 最后一行 | lag() | 前N行值 |
| nth_value() | 框架n行回傳引數值 | ntile() | 視窗指定排名 |
滑動視窗:
| 函式 | 注解 | 函式 | 注解 | 函式 | 注解 |
|---|---|---|---|---|---|
| expr preceding | 前幾行 | expr following | 后幾行 | ||
| unbounded preceding | 磁區第一行 | unbounded following | 磁區最后一行 | ||
| current row | 邊界是當前行 |
示例,前一行與后一行記錄的平均數 rows between frame_start and frame_end
| 函式 | 注解 | 示例 |
|---|---|---|
| rows | 行物件 | select * ,avg(age) over(partition by sex order by id rows between 1 preceding and 1 following ) avg_age from table_name; |
| range | 值物件 | select * ,avg(age) over(partition by sex order by create_date range between interval 1 day preceding and interval 1 day following) avg_age from table_name; |
更多支持函式參考:函式大全 如:數學函式,統計函式 等
表、欄位屬屬性函式
| 序列 | 關鍵字 | 注解 | 實體 | 備注 |
|---|---|---|---|---|
| 1. | primary key | 主鍵 | ||
| 2. | unique | 約束唯一性 | ||
| 3. | foreign key | 外鍵 | foreign key(new_col) references table_name(id) | |
| 4. | check | 檢查約束 | CHECK(salary>0 AND salary<100) | |
| 5. | default | 默認值 | column varchar(20) default ‘xiamen’ | |
| 6. | not null | 非空 | ||
| 7. | auto increment | 自增主鍵 |
系統函式
| 序列 | 關鍵字 | 注解 | 實體 | 備注 |
|---|---|---|---|---|
| 1. | show databases; | 查看資料庫表 | ||
| 2. | show tables; | 查看當前資料庫所有表 | ||
| 3. | show create table table_name; | 查看構造表函式 | ||
| 4. | show procedure status where db=‘sql’; | 查看資料庫函式 | ||
| 5. | show create procedure sql.delete_name; | 查看存盤程序函式 | ||
| 6. | app_name() | 當前應用程式名稱 | ||
| 7. | coalesce() |
表操作
| 序列 | 關鍵字 | 注解 | 實體 | 備注 |
|---|---|---|---|---|
| 1. | create table | 創建表 | create table test_name (id int,name varchar(20),age smallint ,sex char(2)); | |
| 2. | create table | 復制表 | create table new_table_name select * from table_name; mysql支持陳述句 | |
| 3. | select into | 復制表 | select * into new_table_name from table_name; sqlserver支持陳述句 | |
| 4. | drop table | 洗掉表 | drop table test_name; | |
| 5. | select into | 備份表 | select * into new_table_name from table_name; | |
| 6. | create temporary table | 臨時表 | ||
| 7. | alter table | 修改表結構 | alter table test_name add height FLOAT(2); alter table test_name drop age; | |
| 8. | create index | 創建索引 | create [qnique] index id on table_name(id); | 可選引數qnique 唯一索引 |
| 9. | drop index | 洗掉索引 | drop index id on table_name; | |
| 10. | create view | 創建視圖 | create view table_name_view as select sex,avg(age) from table_name group by sex; | |
| 11. | drop view | 洗掉視圖 | drop view table_name_view; | |
| 12. | create procedure | 創建存盤程序 | ||
| 13. | drop procedure | 洗掉存盤程序 | ||
| 14. | create trigger | 創建解發器 | ||
| 15. | drop trigger | 洗掉解發器 | ||
| 16. | create schema | 資料庫添加新模式 | ||
| 17. | drop schema | 洗掉模式 | ||
| 18. | create domain | 創建資料值域 | ||
| 19. | drop domain | 洗掉域值 |
二、視圖
| 函式 | 代碼 | 示例 |
|---|---|---|
| create view | 創建視圖 | create view table_name_view as select sex,avg(age) from table_name group by sex; |
| drop view | 洗掉視圖 | drop view table_name_view; |
三、存盤程序函式
| 序列 | 函式 | 代碼 | 示例 |
|---|---|---|---|
| 1. | create procedure | 創建存盤程序 | create procedure p_name() begin select * from table_name; end ; |
| 2. | alter procedure | 修改存盤程序 特征 | alter procedure p_name comment ‘test’; |
| 3. | drop procedure | 洗掉存盤程序 | |
| 函式 | |||
| 4. | if…then… else… end if | 條件 | |
| 5. | case …when…then…when…then…end case | 條件 | |
| 6. | while… do…end while | 條件回圈 | |
| 7. | repeat…until …end repeat | 條件回圈 | |
| 8. | loop…end loop | 回圈 | |
| 9. | iterate | 參考復合陳述句 |
存盤程序示例
delimiter $$ //定義結束字符
//宣告存盤程序
create [definer={user | current_user}] --定義函式人員,默認登入用戶
procedure sp_name ( [proc_parameter[,...]]) --定義名稱
[characteristic ...] routine_body
proc_parameter:
[ in|out|inout] param_name type --程式引數,輸出,輸出
characteristic:
comment 'string'
| language sql
| [not] deterministic
| { contains sql | no sql | reads sql data | modifies sql data }
| sql security { definer | invoker }
routine_body:
valid sql routine statement
//存盤程序開始與結束
[begin_lable:] BEGIN --陳述句的開始,可以給陳述句賦予標簽,增加可讀性
[statement_list]
...
END [end_lable]
delimiter; --將陳述句的結束符恢復
輸出函式實體:洗掉給定人員所有資訊
create procedure delete_name(in p_name varchar(20))
begin
delete from table_name where name=p_name;
end
call delete_name('aaa'); --呼叫函式
輸出函式實體:查看平均年齡
create procedure out_avg_age(out p_avg_age integer)
begin
declare test_val_1 int; --宣告變數方式一 區域變數
set test_val_1=100;
set @test_val_2=200; --宣告變數方式二 (類似全域變數)
set p_avg_age=(select avg(age) from table_name);
set p_avg_age=p_avg_age+test_val_1+@test_val_2;
end;
call out_avg_age(@test);
select @test;
alter procedure 存盤程序名 [ 特征 … ]
| 序列 | 代碼 | 解釋 |
|---|---|---|
| 1. | CONTAINS SQL | 表示子程式包含 SQL 陳述句,但不包含讀或寫資料的陳述句, |
| 2. | NO SQL | 表示子程式中不包含 SQL 陳述句, |
| 3. | READS SQL DATA | 表示子程式中包含讀資料的陳述句, |
| 4. | MODIFIES SQL DATA | 表示子程式中包含寫資料的陳述句, |
| 5. | SQL SECURITY { DEFINER | INVOKER } |
| 6. | DEFINER | 表示只有定義者自己才能夠執行, |
| 7. | INVOKER | 表示呼叫者可以執行, |
| 8. | COMMENT ‘string’ | 表示注釋資訊, |
游標
構造游標 declare cursor_name cresor for select_statement;
打開游標 open cursor_name;
使用游標 fetch cursor_name into var_name[,var_name]…
關閉游標 close cursor_name;
示例
drop procedure if exists p_cursor; --洗掉函式
create procedure p_cursor(in num int(11),out result varchar(100)) --創建函式
begin
declare p_name varchar(10);
declare p_age int(11);
declare done int default 0;
declare count int(11) default 0;
declare cur_demo cursor for select name,age from table_name; --定義游標
-- declare continue handler for sqlstate '02000' set done=1; --資訊狀態
open cur_demo; --打開游標
repeat --回圈
fetch cur_demo into p_name,p_age; -- 提取游標值
if num=count then
select concat_ws(',',result,p_name,p_age) into result;
set done=true;
else
set count=count+1;
end if;
until done -- 回圈條件
end repeat; --關倍訓圈
close cur_demo; -- 關閉游標
end;
call p_cursor(2,@test); --呼叫函式
select @test;
四、事件
五、報表
六、備份
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/196472.html
標籤:區塊鏈
