1. 创建测试数据库和表
CREATE DATABASE auth;
USE auth;
解释:创建一个名为 auth
的数据库,并切换到该数据库进行后续操作。
CREATE TABLE t1(id INT(10), name CHAR(20), level INT(10));
解释:在 auth
数据库中创建一个名为 t1
的表,包含三个字段:id
(整数类型,长度10)、name
(字符类型,长度20)和level
(整数类型,长度10)。
INSERT INTO t1 VALUES (10, 'sagou', 42);
INSERT INTO t1 VALUES (8, 'senoku', 45);
INSERT INTO t1 VALUES (15, 'useless', 47);
INSERT INTO t1 VALUES (27, 'guess', 52);
INSERT INTO t1 VALUES (199, 'useless', 48);
INSERT INTO t1 VALUES (272, 'Theshy', 36);
INSERT INTO t1 VALUES (298, 'leslieF', 40);
INSERT INTO t1 VALUES (30, 'shirley', 58);
INSERT INTO t1 VALUES (190, 'zhangsan', 48);
INSERT INTO t1 VALUES (271, 'lisi', 52);
INSERT INTO t1 VALUES (299, 'wangwu', 52);
INSERT INTO t1 VALUES (31, 'zhaoliu', 58);
解释:向 t1
表中插入多条记录,每条记录包含 id
、name
和 level
字段的值。
CREATE TABLE t2(id INT(10), name CHAR(20), level INT(10));
INSERT INTO t2 VALUES (10, 'sagou', 42);
INSERT INTO t2 VALUES (8, 'senoku', 45);
INSERT INTO t2 VALUES (15, 'useless', 47);
INSERT INTO t2 VALUES (27, 'guess', 52);
INSERT INTO t2 VALUES (199, 'useless', 48);
INSERT INTO t2 VALUES (272, 'Theshy', 36);
INSERT INTO t2 VALUES (298, 'leslieF', 40);
INSERT INTO t2 VALUES (30, 'shirley', 58);
INSERT INTO t2 VALUES (190, 'zhangsan', 48);
INSERT INTO t2 VALUES (271, 'lisi', 52);
INSERT INTO t2 VALUES (299, 'wangwu', 52);
INSERT INTO t2 VALUES (31, 'zhaoliu', 58);
解释:创建另一个名为 t2
的表,并插入多条记录,结构与 t1
表相同。
2. 常用查询介绍
2.1 按关键字排序
例1
SELECT id, name, level
FROM t1
WHERE level >= 45
ORDER BY level DESC;
例2
SELECT id, name, level
FROM t1
WHERE level >= 45
ORDER BY level DESC, id DESC;
2.2 对结果进行分组
例1
SELECT COUNT(name), level
FROM t1
WHERE level >= 45
GROUP BY level;
例2
SELECT COUNT(name), level
FROM t1
WHERE level >= 45
GROUP BY level
ORDER BY COUNT(name) DESC;
2.3 限制结果条目
例1
SELECT id, name, level
FROM t1
LIMIT 3;
例2
SELECT id, name, level
FROM t1
ORDER BY level DESC
LIMIT 3;
例3
SELECT id, name, level
FROM t1
LIMIT 2, 3;
2.4 设置别名
例1
SELECT COUNT(*) AS number
FROM t1;
例2
SELECT p.id, p.name
FROM t1 AS p
LIMIT 3;
例3
CREATE TABLE t3 AS
SELECT *
FROM t1;
2.5 通配符
例1
SELECT id, name, level
FROM t1
WHERE name LIKE 's%';
SELECT id, name, level
FROM t1
WHERE name LIKE '%s';
SELECT id, name, level
FROM t1
WHERE name LIKE '%es%';
s%
:从t1
表中查询id
、name
和level
字段,筛选name
以s
开头的记录。%s
:从t1
表中查询id
、name
和level
字段,筛选name
以s
结尾的记录。%es%
:从t1
表中查询id
、name
和level
字段,筛选name
包含es
字符串的记录。
例2
SELECT id, name, level
FROM t1
WHERE name LIKE '_uess';
SELECT id, name, level
FROM t1
WHERE name LIKE 'use____';
SELECT id, name, level
FROM t1
WHERE name LIKE 'shi_ley';
_uess
:从t1
表中查询id
、name
和level
字段,筛选name
第一个字符任意,接下来的字符是uess
的记录。use____
:从t1
表中查询id
、name
和level
字段,筛选name
以use
开头,后面跟随 4 个任意字符的记录。shi_ley
:从t1
表中查询id
、name
和level
字段,筛选name
以shi
开头,中间有一个任意字符,后面是ley
的记录。
例3
SELECT id, name, level
FROM t1
WHERE name LIKE '_es%';
2.6 子查询
例1
SELECT name, level
FROM t1
WHERE id IN (
SELECT id
FROM t1
WHERE level >= 45
);
例2
TRUNCATE TABLE t2;
INSERT INTO t2
SELECT *
FROM t1
WHERE id IN (
SELECT id
FROM t1
);
TRUNCATE TABLE t2;
:清空t2
表中的所有记录。INSERT INTO t2 SELECT * FROM t1 WHERE id IN (SELECT id FROM t1);
:将t1
表中id
在子查询结果中的所有记录插入到t2
表中。子查询返回t1
表中所有的id
。
3. NULL 值
例1
CREATE TABLE test (
id INT(10) NOT NULL AUTO_INCREMENT,
name VARCHAR(32) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
level INT(10) NOT NULL,
coin INT(32),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO test(name, level) VALUES ('aa', 10);
INSERT INTO test(name, level, coin) VALUES ('ab', 20, 100);
SELECT * FROM test WHERE coin IS NULL;
SELECT * FROM test WHERE coin IS NOT NULL;
4. 正则表达式
例1
SELECT id, name, level
FROM t1
WHERE name REGEXP '^zhang';
例2
SELECT id, name, level
FROM t1
WHERE name REGEXP 'ss$';
例3
SELECT id, name, level
FROM t1
WHERE name REGEXP 'an';
例4
SELECT id, name, level
FROM t1
WHERE name REGEXP 'shir.ey';
5. 运算符
5.1 算术运算符
解释:进行基本的算术运算,包括加法(1 + 2
)、减法(2 - 1
)、乘法(2 * 3
)、除法(4 / 2
)和取余(7 % 2
),并分别将结果别名为 addition
、subtraction
、multiplication
、division
和 remainder
。
5.2 比较运算符
例1
SELECT 2 = 4, 2 = '2', 'e' = 'e', (2 + 2) = (3 + 1), 'r' = NULL;
例2
SELECT 'kgc' <> 'bdqn', 1 <> 2, 3 != 3, 2.5 != 2, NULL <> NULL;
例3
SELECT 5 > 4, 'a' > 'b', 2 >= 3, (2 + 3) >= (1 + 2), 4.4 < 3, 1 < 2, 'x' <= 'y', 5 <= 5.5, 'u' >= NULL;
例4
SELECT 2 IN (1, 2, 3, 4, 5), 'c' NOT IN ('a', 'b', 'c');
5.3 逻辑运算符
例1
SELECT NOT 2, !3, NOT 0, !(4 - 4);
例2
SELECT 2 AND 3, 4 && 0, 0 && NULL, 1 AND NULL;
例3
SELECT 2 OR 3, 4 || 0, 0 OR NULL, 1 || NULL;
例4
SELECT 2 XOR 3, 0 XOR 0, 0 XOR 5, 1 XOR NULL, NULL XOR NULL;
5.4 位运算符
例1
SELECT 10 & 15, 10 | 15, 10 ^ 15, 5 & ~1;
例2
SELECT 2 << 3, 32 >> 2;
6. 集合运算符
6.1 UNION 和 UNION ALL
例1
SELECT id, name, level
FROM t1
WHERE level > 45
UNION
SELECT id, name, level
FROM t2
WHERE level < 45;
例2
SELECT id, name, level
FROM t1
WHERE level > 45
UNION ALL
SELECT id, name, level
FROM t2
WHERE level < 45;
7. 连接操作
7.1 内连接(INNER JOIN)
SELECT t1.id, t1.name, t1.level, t2.level
FROM t1
INNER JOIN t2
ON t1.id = t2.id;
7.2 左连接(LEFT JOIN)
SELECT t1.id, t1.name, t1.level, t2.level
FROM t1
LEFT JOIN t2
ON t1.id = t2.id;
7.3 右连接(RIGHT JOIN)
SELECT t1.id, t1.name, t1.level, t2.level
FROM t1
RIGHT JOIN t2
ON t1.id = t2.id;
8. 索引和性能优化
8.1 创建索引
CREATE INDEX idx_name_level
ON t1 (name, level);
8.2 删除索引
DROP INDEX idx_name_level
ON t1;
8.3 查看表的执行计划
EXPLAIN
SELECT id, name, level
FROM t1
WHERE name = 'sagou';
9. 事务处理
9.1 开始、提交和回滚事务
START TRANSACTION;
解释:开始一个事务。
INSERT INTO t1 VALUES (32, 'transaction_test', 60);
COMMIT;
ROLLBACK;
10. 视图
10.1 创建视图
CREATE VIEW view_name
AS
SELECT id, name, level
FROM t1
WHERE level > 45;
10.2 查询视图
SELECT *
FROM view_name;
10.3 删除视图
DROP VIEW view_name;
11. 存储过程和函数
11.1 创建存储过程
DELIMITER //
CREATE PROCEDURE proc_name(IN param1 INT, OUT param2 INT)
BEGIN
SELECT level INTO param2
FROM t1
WHERE id = param1;
END //
DELIMITER ;
11.2 调用存储过程
CALL proc_name(10, @result);
SELECT @result;
11.3 创建函数
DELIMITER //
CREATE FUNCTION func_name(param1 INT)
RETURNS INT
BEGIN
DECLARE result INT;
SELECT level INTO result
FROM t1
WHERE id = param1;
RETURN result;
END //
DELIMITER ;
11.4 调用函数
SELECT func_name(10);