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() 指定时间日期格式
原文地址:https://www.cnblogs.com/xihehe/p/8329841.html