色偷偷91综合久久噜噜-色偷偷成人-色偷偷尼玛图亚洲综合-色偷偷人人澡久久天天-国内精品视频一区-国内精品视频一区二区三区

Hello! 歡迎來到小浪云!


SQL 快速參考:簡化數據庫管理


avatar
小浪云 2024-12-22 171

sql 備忘單

本博客全面指導最重要的sql命令和操作。它涵蓋了基本查詢、連接、子查詢、索引和更高級的概念。

目錄

  1. sql 基礎知識
  2. 數據定義語言(ddl)
  3. 數據操作語言(dml)
  4. 數據查詢語言(dql)
  5. 數據控制語言(dcl)
  6. 加入
  7. 子查詢
  8. 索引
  9. 聚合函數
  10. 分組和排序
  11. 交易
  12. 高級 sql
  13. 最佳實踐

sql 基礎知識

sql 查詢的結構

select column1, column2 from table_name where condition order by column limit n; 

在 sql 中注釋

  • 單行評論: — 這是一條評論
  • 多行評論
  /* this is a       multi-line comment */ 

數據定義語言(ddl)

創建表

create table table_name (     column1 datatype [constraints],     column2 datatype [constraints],     ... ); 

示例:

create table employees (     id int primary key,     name varchar(100),     age int,     hire_date date ); 

修改表格

添加列

alter table table_name add column_name datatype; 

刪除一列

alter table table_name drop column column_name; 

修改列

alter table table_name modify column column_name datatype; 

重命名表

alter table old_table_name rename to new_table_name; 

刪除一個表

drop table table_name; 

創建索引

create index index_name on table_name (column_name); 

刪除索引

drop index index_name; 

數據操作語言 (dml)

將數據插入表中

insert into table_name (column1, column2, ...) values (value1, value2, ...); 

示例:

insert into employees (id, name, age, hire_date) values (1, 'john doe', 30, '2022-01-01'); 

更新表中的數據

update table_name set column1 = value1, column2 = value2, ... where condition; 

示例:

update employees set age = 31 where id = 1; 

從表中刪除數據

delete from table_name where condition; 

示例:

delete from employees where id = 1; 

數據查詢語言 (dql)

從表中選擇數據

select column1, column2, ... from table_name where condition order by column limit n; 

示例:

select * from employees; select name, age from employees where age > 30; 

通配符

  • *:選擇所有列
  • %:零個或多個字符的通配符(在 like 子句中)
  • _:僅代表一個字符的通配符(在 like 子句中)

示例:

select * from employees where name like 'j%'; 

數據控制語言(dcl)

授予權限

grant permission on object to user; 

示例:

grant select, insert on employees to 'user1'; 

撤銷權限

revoke permission on object from user; 

示例:

revoke select on employees from 'user1'; 

加入

內連接

當兩個表中存在匹配項時返回行。

select columns from table1 inner join table2 on table1.column = table2.column; 

左連接(或左外連接)

返回左表中的所有行以及右表中的匹配行。如果不匹配,右表中的列將顯示 null 值。

select columns from table1 left join table2 on table1.column = table2.column; 

右連接(或右外連接)

返回右表中的所有行以及左表中的匹配行。如果不匹配,左表中的列將顯示 null 值。

select columns from table1 right join table2 on table1.column = table2.column; 

全外連接

當其中一個表中有匹配項時返回行。

select columns from table1 full outer join table2 on table1.column = table2.column; 

子查詢

select 中的子查詢

select column1, (select column2 from table2 where condition) as alias from table1; 

where 中的子查詢

select column1 from table1 where column2 in (select column2 from table2 where condition); 

from 中的子查詢

select alias.column1 from (select column1 from table2 where condition) as alias; 

索引

創建索引

create index index_name on table_name (column1, column2); 

刪除索引

drop index index_name; 

唯一索引

確保一列(或一組列)中的所有值都是唯一的。

create unique index index_name on table_name (column_name); 

聚合函數

數數

計算符合特定條件的行數。

select count(*) from table_name where condition; 

返回列中值的總和。

select sum(column_name) from table_name; 

平均電壓

返回列中值的平均值。

select avg(column_name) from table_name; 

最小值和最大值

返回列中的最小值和最大值。

select min(column_name), max(column_name) from table_name; 

分組和排序

分組依據

將具有相同值的行分組為匯總行。

select column1, count(*) from table_name group by column1; 

擁有

應用 group by 后過濾組。

select column1, count(*) from table_name group by column1 having count(*) > 5; 

訂購依據

按升序或降序對結果集進行排序。

select column1, column2 from table_name order by column1 desc; 

交易

開始交易

begin transaction; 

進行交易

commit; 

回滾事務

rollback; 

高級sql

案例當

查詢中的條件邏輯。

select column1,        case            when condition then 'result 1'            when condition then 'result 2'            else 'default'        end as alias from table_name; 

聯合和聯合全部

  • union:合并兩個或多個查詢的結果集(刪除重復項)。
  • union all:合并結果集(保留重復項)。
select column from table1 union select column from table2;  select column from table1 union all select column from table2; 

最佳實踐

  • 盡可能使用 join 而不是子查詢以獲得更好的性能。
  • 對經常搜索的列建立索引以加快查詢速度。
  • 避免 select * 并僅指定您需要的列。
  • 對大型結果集使用 limit 限制返回的行數。
  • 標準化您的數據以避免冗余并提高一致性。
  • 使用where子句而不是在聚合之前過濾數據。
  • 測試查詢性能,特別是對于大型數據集。
  • 使用事務來保證數據的一致性,尤其是涉及多個dml語句的操作。

結論

此 sql 備忘單涵蓋了使用關系數據庫所需的所有基本 sql 命令和技術。無論您是查詢、插入、更新還是連接數據,本指南都將幫助您更有效地使用 sql。

  

相關閱讀

主站蜘蛛池模板: 久久综合狠狠色综合伊人 | 国产欧美一区二区三区久久 | 不卡一级aaa全黄毛片 | 国产精品亚洲综合一区在线观看 | 国产揄拍国内精品对白 | 欧美激情性 | 香港经典三级在线免播放观看 | 日本www免费 | 国产成人精品影院狼色在线 | 色偷偷888欧美精品久久久 | 国产一区二区三区日韩欧美 | 99精品免费 | 欧美性久久 | 日本免费高清 | 伊人久久中文字幕久久cm | 日本黄一级日本黄二级 | 国产一级久久免费特黄 | 一级特黄a 大片免费 | 国产三级精品在线 | 久久国产高清一区二区三区 | 国产精品亚洲玖玖玖在线靠爱 | 日本jizzjizz | 狠狠色狠色综合曰曰 | 我要看免费一级毛片 | 999国产精品999久久久久久 | 六月婷婷在线观看 | 美女在线视频观看影院免费天天看 | 色亚洲一区 | 国产日韩欧美精品在线 | 久久精品网| 亚洲国产精品日韩高清秒播 | 日本免费高清 | 78m成人亚洲 | 亚洲欧美综合国产精品一区 | 91精品免费久久久久久久久 | 色噜噜噜视频 | 天天艹天天操 | 国产欧美日韩网站 | 国产欧美在线一区二区三区 | 噜噜噜在线观看 | 无遮挡很爽很污很黄的网站w |