SQL

Tables and Schema

Tables

Data is organized into named tables, or relations
Table is used to store the data
Every column has its data type:

  • INTEGER, SMALLINT, TINYINT, BIGINT
  • DECIMAL(n,m) : float type
  • VARCHAR: string type
  • BOOLEAN: logic

Schemas

The names and types of the data in a table's columns are called the table's schema.

Log in shell

mysql -u root -p psw

SQL

The most common features of SQL are:

  • Projection - selecting some columns from table
  • Function application - applying a function to a value
  • Filtering - selecting rows based on some criterion
  • Grouping - aggregating rows based on the values in a column
  • Joins - combining two tables
    (回想在big data中學(xué)習(xí)的實(shí)現(xiàn)各類功能的完備數(shù)學(xué)運(yùn)算集)

Query syntax

SQL queries have the form:

SELECT expression[, expression, ...]
FROM table
[... additional clauses ...]

Projection

Projection is when you ask for some(all) of the columns of a table.
也就是抽取相應(yīng)的列礼烈,keyword 負(fù)責(zé)對(duì)抽取的列中的行進(jìn)行過濾操作弧满,
比如DISTINCT 就是抽取出某一列的不重復(fù)的所有元素(類似于數(shù)學(xué)里面的集合)婆跑。

SELECT col1, col2 
FROM table1

SELECT has many keywords like:

  • DISTINCT - return unique results
  • BETWEEN a AND b - limit the range, the values can be numbers, text, or dates
  • LIKE - pattern search within the column text
  • IN (a, b, c) - check if the value is contained among given.
SELECT DISTINCT Facility
FROM Admissions;
SELECT * 
FROM table1

Function application

在列上施加函數(shù),可以構(gòu)造新的列名

SELECT Facility,
       Lengthofstay * 24
FROM Admissions;
SELECT UPPER(Facility),
       Lengthofstay * IllnessCode
FROM Admissions;
SELECT Facility,
        Lengthofstay * 24 AS stayinhours
 FROM Admissions;

Filtering

相當(dāng)于對(duì)行施加函數(shù)犀忱,抽取出部分的行, 關(guān)鍵詞為WHERE.多個(gè)條件語(yǔ)句之間用邏輯符OR AND 隔開

SELECT *
FROM Admissions
WHERE Lengthofstay > 4;
SELECT Facility, IllnessCode
FROM Admissions
WHERE IllnessCode = 141 OR
      IllnessCode = 94;
SELECT Facility, Lengthofstay
FROM Admissions
WHERE Lengthofstay > 2 AND
              Lengthofstay < 6;

Aggregation function

Aggregate the rows according to a column, and perform operarions on the aggregated rows. 使用keyword :GROUP BY
Grouping 操作也就是匯總通常與聚合函數(shù)(共有40多種聚合函數(shù))一起使用扶关,常見的聚合函數(shù):

  • COUNT - return the number of rows
  • SUM - cumulate the values
  • AVG - return the average for the group
  • MIN / MAX - smallest / largest value
SELECT Facility,
       MAX(Lengthofstay)
FROM Admissions
GROUP BY Facility;
SELECT Facility,
       MIN(Lengthofstay),
       MAX(Lengthofstay),
       AVG(Lengthofstay)
FROM Admissions
GROUP BY Facility;
SELECT COUNT(*) FROM Admissions;

可以將過濾操作和GROUP BY 操作放到一起同時(shí)執(zhí)行,但是注意過濾操作要放到前面搀庶。

Sort the result

對(duì)結(jié)果排序,關(guān)鍵詞 :ORDER BY

SELECT Facility, Lengthofstay
FROM Admissions
ORDER BY Lengthofstay;
SELECT Facility, Lengthofstay
FROM Admissions
ORDER BY Facility, Lengthofstay

Joins

鏈接兩個(gè)或者多個(gè)表

SELECT * FROM Admissions JOIN Illnesses
ON Admissions.IllnessCode = Illnesses.Code;

Join 操作可以寫的非常的復(fù)雜,同時(shí)join分為 Left Outer Join, Inner join and right Outer join

UPDATE t1 SET a = 1
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.t1_id WHERE t1.col1 = 0 AND t2.col2 IS NULL;

LIMIT clause

從返回的結(jié)果中顯示有限的元素

SELECT Facility, IllnessCode
FROM Admissions
LIMIT 2

Database and tables

Basic operations

CREATE DATABASE database name;
DROP DATABASE database name;
SHOW DATABASES;
USE database name;
SHOW TABLES;
DESCRIBE table-name;

tables

DROP TABLE IF EXISTS plate_types;

CREATE TABLE plate_types(
  code CHAR(3) NOT NULL,
  description VARCHAR(27),
  PRIMARY KEY(code)
);

You may notice that the return of a SELECT statement is actually another table. So you may use it as a subqueries anywhere you need a table

-- create a table from another select-statement
CREATE TABLE longstays
AS SELECT facility, MAX(lengthofstay)
   FROM hospital
   GROUP BY facility;

Deleting rows and tables

DROP TABLE table-name;
# just delete the data but leave the table(as a schema)
TRUNCATE TABLE table-name;
# Delete rows
DELETE FROM table-name
WHERE conditions;

add or update tables

-- update speci c data with the WHERE clause 
UPDATE table1 SET col1 = 1 
WHERE col2 = 2

-- insert values manually   
INSERT INTO table1 (ID, FIRST_NAME, LAST_NAME) 
VALUES (1, ‘Rebel’, ‘Labs’);

-- or by using the results of a query  
INSERT INTO table1 (ID, FIRST_NAME, LAST_NAME) 
SELECT id, last_name,  rst_name FROM table2

-- load data
LOAD DATA LOCAL INFILE '/Users/xiaodiu/Google/Big Data/assignment2/data/plate-types.csv'
INTO TABLE plate_types
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n';

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末揍庄,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蚂子,更是在濱河造成了極大的恐慌,老刑警劉巖食茎,帶你破解...
    沈念sama閱讀 211,376評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件别渔,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡钠糊,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門艘刚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)截珍,“玉大人箩朴,你說(shuō)我怎么就攤上這事≌ㄅ樱” “怎么了荚斯?”我有些...
    開封第一講書人閱讀 156,966評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)滥壕。 經(jīng)常有香客問我兽泣,道長(zhǎng)绎橘,這世上最難降的妖魔是什么唠倦? 我笑而不...
    開封第一講書人閱讀 56,432評(píng)論 1 283
  • 正文 為了忘掉前任稠鼻,我火速辦了婚禮,結(jié)果婚禮上枷餐,老公的妹妹穿的比我還像新娘。我一直安慰自己毛肋,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,519評(píng)論 6 385
  • 文/花漫 我一把揭開白布诗眨。 她就那樣靜靜地躺著孕讳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪厂财。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,792評(píng)論 1 290
  • 那天与斤,我揣著相機(jī)與錄音,去河邊找鬼磷支。 笑死食寡,一個(gè)胖子當(dāng)著我的面吹牛雾狈,可吹牛的內(nèi)容都是我干的抵皱。 我是一名探鬼主播,決...
    沈念sama閱讀 38,933評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼锭弊,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼擂错!你這毒婦竟也來(lái)了樱蛤?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,701評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤爽醋,失蹤者是張志新(化名)和其女友劉穎便脊,沒想到半個(gè)月后蚂四,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體哪痰,經(jīng)...
    沈念sama閱讀 44,143評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡晌杰,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,488評(píng)論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了肋演。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,626評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡蜕乡,死狀恐怖梗夸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤扣癣,帶...
    沈念sama閱讀 34,292評(píng)論 4 329
  • 正文 年R本政府宣布憨降,位于F島的核電站,受9級(jí)特大地震影響授药,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜莱衩,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,896評(píng)論 3 313
  • 文/蒙蒙 一娇澎、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧趟庄,春花似錦、人聲如沸戚啥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)拖云。三九已至,卻和暖如春江兢,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背邑贴。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工叔磷, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人改基。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像稠腊,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子架忌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,494評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容