SQL學習筆記:高級教程


SQL語法

  • LIMIT
    select col from table limit number
    select * from table limit number
  • LIKE
    select * from table where col LIKE '%in%'
    select * from table where col NOT LIKE '%in%'
  • 通配符
    • 通配符必須與LIKE一起使用
    • % -- 替代一個或者多個字符
    • _ -- 替代一個字符
    • [charlist] -- 字符列中的任何單一字符
    • [!charlist] -- 不在字符列中的任何單一字符
    • select * from table where col LIKE '[abc]d_e%'
  • IN 在where中選取多個值
    select * from table where col IN ('zhi1', 'zhi2')
  • BETWEEN ... AND ...
    select * from table where col BETWEEN 'a' AND 'c'
    select * from table where col NOT BETWEEN 'a' AND 'c'
  • Alias
    select A.col, B.col1 from table1 AS A, table2 AS B where A.col = 'zhi1' AND B.col = 'zhi2'
    select col1 AS name1, col2 AS name2 from table
  • JOIN
    select A.col1, B.col2 from tablea AS A, tableb AS B where A.col = B.col
  • INNER JOIN
    select tablea.col1, tableb.col2 from tablea INNER JOIN tableb ON tablea.col = tableb.col
  • LEFT JOIN
    結果會返回左表中所有的行,即使右表中沒有匹配的行
    select tablea.col1, tableb.col2 from tablea LEFT JOIN tableb ON tablea.col = tableb.col
  • RIGHT JOIN
    結果返回右表中所有的行,即使右表中沒有匹配的行
    select tablea.col1, tableb.col2 from tablea RIGHT JOIN tabelb ON tablea.col = tableb.col
  • FULL JOIN
    結果返回兩表中所有的行,即使沒有匹配
    select tablea.col1, tableb.col2 from tablea FULL JOIN tableb ON tablea.col = tableb.col
  • UNION 與 UNION ALL
    union 需要有相同數量的列同時數據類型相似,列的順序也要一致
    union 輸出唯一的值; union all 輸出所有的值
    select col1, col2 from tablea
    UNOIN ALL
    select col1, col2 from tableb
  • SELECT INTO 創建備份
    select * into tablea_backup from tablea
  • CREATE DATABASE 創建數據庫
    create database name
  • CREATE TABLE 創建表
    cerete tbale name (id int, name varchar(255), adress varchar(255))
  • 數據類型
數據類型 描述
int(size) 僅容納整數
括號內為數字的最大位數
decimal(size,d)
numeric(size, d)
容納帶有小數的數字
size規定數字的最大位數
d規定小數點右側的最大位數
char(size) 容納固定長度的字符串
size為規定字符串的長度
varchar(size) 容納可變長度的字符串
字符串的最大長度
data(yyyymmdd) 容納日期
  • sql約束可以加在 create table 語句也可以用在alter table語句
  • NOT NULL 不接受空值
    crete table name (id int NOT NULL, name varchar(255) NOT NULL, city varchar(255))
  • UNIQUE 約束唯一標識數據庫表中的每條記錄
  • 每個表可以有多個unique,但只有一個primary key 約束
  • 創建表時
    crete table name (id int NOT NULL, name varchar(255) NOT NULL, city varchar(255), UNIQUE(id))
    crete table name (id int NOT NULL, name varchar(255) NOT NULL, city varchar(255), CONSTRAINT uc_name UNIQUE (id, city))
  • 更改表時
    alter table name ADD UNIQUE(id)
    alter table name ADD CONSTRAINT uc_name UNIQUE (id, city)
  • 撤銷UNIQUE約束
    alter table name DROP INDEX uc_name
  • primry key = unique + not null
  • 每個表中都應該有一個主鍵,並且只有一個主鍵
  • 創建表時
    crete table name (id int NOT NULL, name varchar(255) NOT NULL, city varchar(255), PRIMARY KEY (id))
    crete table name (id int NOT NULL, name varchar(255) NOT NULL, city varchar(255), CONSTRAINT pk_name PRIMARY KEY (id, city))
  • 更改表時
    alter table name ADD PRIMARY KEY (id)
    alter table name ADD CONSTRAINT pk_name PRIMARY KEY (id, city)
  • 撤銷表時(注意與撤銷unique的不同)
    alter table name DROP PRIMARY KEY
  • FOREIGN KEY
    crete table tablename1(id1 int not null, things varchar(255), id2 int, primary key (id1), FOREIGN KEY (id2) REFERENCES tablename2 (id))
    crete table tablename1(id1 int not null, things varchar(255), id2 int, primary key (id1), CONSTRAINT fk_name FOREIGN KEY (id2) REFERENCES tablename2 (id))
  • 更改表時
    alter table name ADD CONSTRAIN fk_name FOREIGN KEY (id) REFERENCES tablename2 (id1)
  • 撤銷foreign key
    alter table name DROP FOREIGN KEY fk_name
  • CHECK
    crete table tablename1(id1 int not null, things varchar(255), id2 int, primary key (id1), CONSTRAINT chk_name CHECK (id > 0 AND city = "abc"))
    alter table name ADD CONSTRAINT chk_name CHECK (id > 0 and city = 'abc')
    alter table name DROP CHECK chk_name
  • DEFAULT
    crete table tablename1(id1 int not null, things varchar(255), id2 int, shijian date DEFAULT GETDATE())
    alter table name ALTER city SET DEFAULT 'abc'
    alter table name ALTER city DROP default
  • INDEX
    CREATE INDEX indexname ON tablename (city, id DESC)
  • DROP
    DROP table tablename
    TRUNCATE table tablename
    truncate僅僅刪除數據不刪除表本身
  • ALTER 添加列
    alter table tablename ADD colname datatype
  • 修改列數據格式
    alter table tablename ALTER COLUMN colname datatype
  • 刪除列
    alter table tablename DROP COLUMN colname
  • AUTO_INCREMENT
    在每次加入新的數據后,會自動創建主鍵的值
    crete table tablename1(id1 int not null AUTO_INCREMENT, things varchar(255), id2 int, primary key (id1))
  • VIEW 視圖
    CREATE VIEW viewname AS
    select * from tablename
  • 撤銷視圖
    DROP VIEW viewname
  • NULL
    select * from tablename where col1 IS NOT NULL
  • IFNULL
    select IFNULL(COL1, 0) from table1
  • DATE函數
函數 描述
NOW() 返回當前日期和時間
CURDATE() 返回當前日期
CURTIME() 返回當前時間
DATE() 提取日期部分
EXTRACT() 提取日期時間的單獨部分
DATE_ADD() 給日期加上指定的時間間隔
DATE_SUB() 給日期減去指定的時間間隔
DATEDIFF() 返回兩日期之間的天數
DATE_FORMAT() 指定時間日期格式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM