資料庫知識點

来源:http://www.cnblogs.com/zpfbuaa/archive/2016/04/06/5358035.html
-Advertisement-
Play Games

WHY SQL? 1.SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be nec ...


WHY SQL? 1.SQL is a very-high-level language, in which the programmer is able to avoid specifying a lot of data-manipulation details that would be necessary in languages like C++. 2.What makes SQL viable is that its queries are “optimized” quite well, yielding efficient query executions. Queries :
1. Single-relation queries
2. Multi-relation queries
3. Subqueries
4. Grouping and Aggregation

(1)SELECT - FROM - WHERE statements 

  SELECT ... (desired attributes)

  From ... (one or more tables)

  WHERE ...( condition about tuples of the tables)

EX: Using Beers(name, manf), what beers are made by Busch?

SELECT name,

FROM Beers,

WHERE manf = 'Busch'

(2)When there is one relation in the FROM clause, SELECT * clause stands for “all attributes of this relation.”

(3)If you want the result to have different attribute names, use “AS <new name>” to rename an attribute.

EX:Example based on Beers(name, manf):

SELECT name AS beername, manf
FROM  Beers
WHERE manf = ‘Busch’

(4)SQL allows duplicates in relations as well as in query results.

       To force the elimination of duplicates, insert the keyword distinct  after select.

        Find the names of all branches in the loan relations, and remove duplicates

select distinct branch_name
from loan 

  The keyword all specifies that duplicates not be removed.          

select all branch_name
from loan

 (5)Any expression that makes sense can appear as an element of a SELECT clause.

  EX: from Sells(bar, beer, price): 
SELECT bar, beer,  price * 6 AS priceInYuan

FROM Sells;

(6)function 查詢全體學生的姓名、出生年份和所有系,要求用小寫字母表示所有系名。

SELECT Sname,2006-Sage AS 'Year of Birth: 'LOWER(Sdept)

FROM Student;

(7)What we can use in select clause :

      expressions 

      constants 

      functions 

      Attribute alias

(8)What you can use in WHERE: 

 

            attribute names of the relation(s) used in the FROM.

 

            comparison operators:  =, <>, <, >, <=, >=, between, in           

 

            apply arithmetic operations:  stockprice*2

 

            operations on strings (e.g., “||”  for concatenation).

 

            Lexicographic order on strings.

 

            Pattern matching:    s LIKE p

 

            Special stuff for comparing dates and times. 

(9)Range comparison: between

謂詞:   BETWEEN …  AND  …    NOT BETWEEN  …  AND  …

 查詢年齡在20~23歲(包括20歲和23歲)之間的學生的姓名、系別和年齡。

SELECT Sname,Sdept,Sage

FROM   Student

WHERE Sage BETWEEN 20 AND 23

(10)Set operator: in

使用謂詞: IN <值表>,  NOT IN <值表>                

<值表>:  用逗號分隔的一組取值

[例]查詢信息系(IS)、數學系(MA)和電腦科學系(CS)學生的姓名和性別。

SELECT Sname,Ssex

FROM  Student

WHERE Sdept IN ( 'IS''MA''CS' );

 (11)Patterns

 

WHERE clauses can have conditions in which a string is compared with a pattern, to see if it matches.

 

General form:    <Attribute> LIKE <pattern>  or  <Attribute> NOT LIKE <pattern>

 

Pattern is a quoted string with % = “any string”;  _  = “any character.” (12)The LIKE operator s LIKE p:  pattern matching on strings p may contain two special symbols: %  = any sequence of characters _   = any single character (13)ESCAPE character When the string contains ‘%’ or ‘_’, you need to use ESCAPE character (14)Ordering the Display of Tuples Use ‘Order by’ clause to specify the alphabetic order of the query result
select distinct customer_name    
from    borrower        
order by customer_name

We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default.

Example:  order by customer_name desc Note: Order by can only be used as the last part of select statement (15)Order by 

[例]  查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系中的學生按年齡降序排列。

SELECT  *
FROM  Student
ORDER BY Sdept,Sage DESC

(16)Null Values

Three-Valued Logic

To understand how AND, OR, and NOT work in 3-valued logic, think of TRUE = 1, FALSE = 0, and UNKNOWN = ½. AND = MIN; OR = MAX, NOT(x) = 1-x. Example:

TRUE AND (FALSE OR NOT(UNKNOWN)) = MIN(1, MAX(0, (1 - ½ ))) = MIN(1, MAX(0, ½ ) = MIN(1, ½ ) = ½.

(17)If x=Null then 4*(3-x)/7 is still NULL

If x=Null   then x=“Joe”    is UNKNOWN Three boolean values: FALSE             = 0 UNKNOWN      = 0.5 TRUE               = 1

 (18)Unexpected behavior:

SELECT *

FROM   Person

WHERE  age < 25  OR  age >= 2

Some Persons are not included !

(19)Testing for Null

Can test for NULL explicitly:

x IS NULL x IS NOT NULL
SELECT *
FROM     Person
WHERE  age < 25  OR  age >= 25 OR age IS NULL

Now it includes all Persons

 (20)Aggregations

SUM, AVG, COUNT, MIN, and MAX can be applied to a column in a SELECT clause to produce that aggregation on the column. Also, COUNT(*) counts the number of tuples. 計數 COUNT([DISTINCT|ALL] *)   COUNT([DISTINCT|ALL] <列名>) 計算總和 SUM([DISTINCT|ALL] <列名>)  計算平均值 AVG([DISTINCT|ALL] <列名>)

求最大值 MAX([DISTINCT|ALL] <列名>) 

求最小值 MIN([DISTINCT|ALL] <列名>) 

–DISTINCT短語:在計算時要取消指定列中的重覆值 –ALL短語:不取消重覆值 –ALL為預設值

 EX:From Sells(bar, beer, price), find the average price of Bud:

SELECT AVG(price)

FROM Sells

WHERE beer = ‘Bud’;

(21)Eliminating Duplicates in an Aggregation

DISTINCT inside an aggregation causes duplicates to be eliminated before the aggregation. Example: find the number of different prices charged for Bud:
SELECT COUNT(DISTINCT price)
FROM Sells
WHERE beer = ‘Bud’;

(22)NULL’s Ignored in Aggregation

NULL never contributes to a sum, average, or count, and can never be the minimum or maximum of a column. But if there are no non-NULL values in a column, then the result of the aggregation is NULL. (23)Grouping We may follow a SELECT-FROM-WHERE expression by GROUP BY and a list of attributes. The relation that results from the SELECT-FROM-WHERE is grouped according to the values of all those attributes, and any aggregation is applied only within each group. EX: From Sells(bar, beer, price), find the average price for each beer:
SELECT beer, AVG(price)
FROM Sells
GROUP BY beer;

(24)Restriction on SELECT Lists With Aggregation

If any aggregation is used, then each element of the SELECT list must be either: 1.Aggregated, or 2.An attribute on the GROUP BY list. (25)Illegal Query Example You might think you could find the bar that sells Bud the cheapest by:
SELECT bar, MIN(price)

FROM Sells

WHERE beer = ‘Bud’;
But this query is illegal in SQL. Why? Note bar is neither aggregated nor on the GROUP BY list. (26)HAVING Clauses HAVING <condition> may follow a GROUP BY clause. If so, the condition applies to each group, and groups not satisfying the condition are eliminated.
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先是控制項輪換 一.創建主佈局 1.用到的控制項是 TextSwitcher (文本輪換) 那麼其他對應的也就是 ImageSwitcher (圖片輪換) 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
  • 昨天發佈的Xcode7.3,用了一天的時間終於裝上了(網路不給力),突然發現原來所使用的插件不能用了,當時表情如下: 記得在更新7.2的時候也是這樣的,當時重新下載的插件安裝成功,但是未免有些麻煩,經過一番研究,發現是原來的插件UUID證書變了,蘋果要求必須要加入UUID才能使用,保證插件的穩定性。 ...
  • 原文http://www.cnblogs.com/cnjava/archive/2013/02/28/2937699.html 講解的oracle資料庫面對大數據如何優化查詢。 ...
  • 預處理 預先處理(準備好),讓DBMS先對重覆執行的SQL語句進行預先的編譯。之後,再調用編碼好sql,同時傳遞數據。 第1步:準備預處理 語法: prepare 預處理名 from 'sql語句'; 示例: 第2 步:執行預處理 語法: execute 預處理名; 預處理的使用: 1、固定的sql... ...
  • 插入一個記錄需要的時間由下列因素組成,其中的數字表示大約比例: 連接:(3) 發送查詢給伺服器:(2) 分析查詢:(2) 插入記錄:(1x記錄大小) 插入索引:(1x索引) 關閉:(1) 這不考慮打開表的初始開銷,每個併發運行的查詢打開。 表的大小以logN (B樹)的速度減慢索引的插入。 加快插入 ...
  • Oracle 中的 TO_DATE 和 TO_CHAR 函數oracle 中 TO_DATE 函數的時間格式,以 2008-09-10 23:45:56 為例 格式 說明 顯示值 備註 Year(年): yy two digits(兩位年) 08 yyythree digits(三位年) 008 y ...
  • 最終解決方式:更換字元集utf8-->utf8mb4 上周有開發人員反饋一個問題:前臺應用抓取微博信息,每天總有幾條數據插入不成功。應用日誌顯示: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xAA",...' for c ...
  • 子查詢 一個select中還包含另一個select,其中最裡面的select語句稱之為子查詢 根據select出現的位置可以將子查詢分為以下幾類: from子查詢 where子查詢 exists子查詢 從select返回的結果,那麼子查詢又可以分為: 標量子查詢 查詢的結果只有一個值。 示例: 需求... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...