# MySQL 基础语法

# create 创建数据库

建数据库基本语法:

create database [if not exists] database_name
[character set charset_name]
[collate collation_name];
# 如果创建的 database_name 已存在,则执行会导致错误。添加 if not exists 可以避免
# charset_name 可以指定字符集(?)
# collation_name 指定排列规则(?)

# drop 删除数据库

使用普通用户登录 MySQL 服务器,需要特定的权限才可以删除或创建数据库.

删除数据库基本语法:

drop database [if exists] database_name;

# use 选择数据库

选择数据库基础语法:

use database_name

# 基本数据类型

  • 数值类型

    1. tinyint :1 字节.
    2. smallint :2 字节
    3. mediumint :3 字节
    4. int :4 字节
    5. bigint :8 字节
    6. float :4 字节,单精度浮点数
    7. double :8 字节,双进度浮点数
    8. decimal
  • 日期和时间类型

  • 字符串类型

    1. char
    2. varchar

# create 创建数据库表

创建数据库表基本语法:

create table table_name{
	coloumn1 datatype,
	colomun2 datatype,
	...
}
[character set charset_name]
[collate collation_name]
[engine=engine_name];

字段属性:

  • Primary key :主键
  • auto_increment :自增,一般用于主键
  • not null :非空,若输入的记录该字段为空,则会报错

# drop 删除数据库表

删除数据库表基本语法:

drop table [if exists] table_name;

# alter 修改表

修改表结构基本语法

alter table table_name
add column_name datatype
drop column column_name
modify column column_name datatype

# insert 插入数据

插入数据基本语法:

insert into table_name(column1, column2, ...)
	values(values1, values2, ...)
	[values(values1, values2, ...)];

如果主键为自增的列,可使用 NULL 占位符替代

# delete 删除数据

删除记录基本语法:

delete 
from table_name
where condition;

# select 查询数据

查询语句基本语法:

select column1, column2, ...
from table_name
[where condition]
[order by column_name [ASC | DESC]]
[limit number];		# 用于限制返回的行数

# wherer 子句

where 用法:

  • 可用于 deleteupdate 操作

  • 查询语句中你可以使用一个或者多个表,表之间使用逗号 **,** 分割,并使用 WHERE 语句来设定查询条件

  • 可以在 WHERE 子句中指定任何条件

    1. 等于条件 =
    2. 不能与条件 !=<>
    3. 大于条件 >
    4. 大于等于条件 >=
    5. 小于条件 <
    6. 小于等于条件 <=
    7. 组合条件 andor
    8. 模糊匹配 like
    9. in
    10. not
    11. not in
    12. between
    13. is null
    14. is not null

# update 更新

记录更新基本语法:

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

# like 子句

模式串的语法:

  • % :表示零个或者多个字符
  • _ :表示一个字符

# union

union 用于连接两个或者两个以上的 select 语句的结果,组成一个集合(取出重复行);使用 union all 可以不去除重复的行

select column1, column2, ...
from table1
where condition1
union
select column1, column2, ...
from table2
where condition2
[order by column1, column2, ...];

# order by 语句

order by 默认升序排序

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;  # 可以使用数子替换列名(数字表示列的位置)
  • 可以使用 nulls firstnulls last 来处理 null

# group by 分组

group by 语句是根据一个或多个列对结果集进行分组,在分组的列上我们可以使用 sum、count、avg 等函数,其基本语法:

select column1, aggregate_function(column2)
from table_name
where condition
group by column1
  • with rollup 可以实现在分组统计数据基础上再进行相同的统计,例如:

    mysql> SELECT coalesce(name, '总数'), SUM(signin) as signin_count FROM  employee_tbl GROUP BY name WITH ROLLUP;
    +--------------------------+--------------+
    | coalesce(name, '总数') | signin_count |
    +--------------------------+--------------+
    | 小丽                   |            2 |
    | 小明                   |            7 |
    | 小王                   |            7 |
    | 总数                   |           16 |
    +--------------------------+--------------+
    4 rows in set (0.01 sec)

# having

having 作为 where 的补充功能, having 子句中可以使用聚集函数

# 连接

JOIN 按照功能大致分为如下几类:

  • inner join :内连接 / 等值连接,返回两个表中满足连接条件的匹配行

    select column1, column2, ...
    from table1 
    inner join table2 on table1.column_name = table2.column_name
  • left join :返回左表的所有行,并包括右表中匹配的行,如果右表中没有匹配的行,将使用 null 填充相应字段

    select column1, column2, ...
    from table1
    left join table2 on table1.column_name = table2.column_name
  • right join :返回右表的所以行,并包含左表匹配的行,如果左表中没有匹配的行,将使用 null 填充相应字段

    select column1, column2, ...
    from table1
    right join table2 on table1.column_name = table2.column_name
  • cross join :对两个表做一个笛卡尔积,最终返回的表的记录可能非常大

    SELECT olumn1, column2, ...
    FROM table1
    CROSS JOIN table2;
  • slef join :自连接

# null 值的处理

MySQLnull 表示缺失或未知的数据,处理 null 值需要特别处理,在 MySQL 中, NULL 值与任何其它值的比较(即使是 NULL )永远返回 NULL ,即 NULL = NULL 返回 NULL

MySQL 中处理 NULL 值的常见注意事项和技巧

  • is null

  • is not null

  • <=> :比较操作符(不同于 = 运算符),当比较的的两个值相等或者都为 NULL 时返回 true

  • 使用 coalesce 函数替换 null 值,它接收 多个参数 ,返回参数列表中第一个非 null 的值

    SELECT product_name, COALESCE(column1, column1, ..., 0) AS actual_quantity
    FROM products;
  • 使用 ifnull 函数处理 nullifnull 函数是 coalesce 的 MySQL 特定版本,它接受两个参数,如果第一个参数为 NULL,则返回第二个参数

  • null 排序,在使用 order by 子句进行排序时, null 会默认排在最后。如果希望 null 排在前面,可以使用 NULLS FIRST

    SELECT product_name, price
    FROM products
    ORDER BY price ASC NULLS FIRST;
  • 聚合函数 count/sum/avg 会忽略 null 值,如果希望将 null 视为 0,可以使用 coalesceifnull

# exists

# any、all

# case

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;

例子:

SELECT OrderID, Quantity,
CASE
    WHEN Quantity > 30 THEN 'The quantity is greater than 30'
    WHEN Quantity = 30 THEN 'The quantity is 30'
    ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

# 事务

# 索引

# 函数

# 参考文章

  • [1] MySQL 教程 | 菜鸟教程 (runoob.com)
  • [2] MySQL Tutorial (w3schools.com)
Edited on Views times

Give me a cup of [coffee]~( ̄▽ ̄)~*

Value WeChat Pay

WeChat Pay