From 2b1ace4e1a8bdcc637f59172a3109eb23f9d1a0a Mon Sep 17 00:00:00 2001 From: Zhang Peng Date: Fri, 4 May 2018 15:50:13 +0800 Subject: [PATCH] =?UTF-8?q?:bookmark:=20mysql=20=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/mysql/dml_demo.sql | 69 +++++++++++++++-------- codes/mysql/select_demo.sql | 109 ++++++++++++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 22 deletions(-) create mode 100644 codes/mysql/select_demo.sql diff --git a/codes/mysql/dml_demo.sql b/codes/mysql/dml_demo.sql index cf5be89..1595ea2 100644 --- a/codes/mysql/dml_demo.sql +++ b/codes/mysql/dml_demo.sql @@ -16,51 +16,76 @@ CREATE TABLE user ( password varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码', email varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱', PRIMARY KEY (id) -) COMMENT='用户表'; +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表'; + + +-- 添加测试数据 +INSERT INTO user(username, password, email) VALUES ('张三', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('李四', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('王五', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('赵六', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('柳七', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('英八', '123456', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('雷九', '123456', 'xxxx@163.com'); ############################################################# -# 向表中插入新记录 +# 插入数据 ############################################################# -- 不指定列名方式插入记录 -INSERT INTO `user` -VALUES (1, 'root', 'root', 'xxxx@163.com'); +INSERT INTO user +VALUES (10, 'root', 'root', 'xxxx@163.com'); -- 指定列名方式插入记录 -INSERT INTO `user`(`username`, `password`, `email`) +-- 注意自增ID数值,由于当前最大的ID值为10,所以插入本条记录时自增ID为11 +INSERT INTO user(username, password, email) VALUES ('admin', 'admin', 'xxxx@163.com'); ############################################################# -# 更新表中的记录 +# 更新数据 ############################################################# -- 更新记录 -UPDATE `user` -SET `username`='robot', `password`='robot' -WHERE `username` = 'root'; +UPDATE user +SET username='robot', password='robot' +WHERE username = 'root'; ############################################################# -# 查询表中的记录 +# 查询数据 ############################################################# --- 查询表中的记录 -SELECT `username`, `password` FROM `user` -WHERE `id` = 1; +-- 查询单列 +SELECT username FROM user; --- 查询表中的所有记录 -SELECT * FROM `user`; +-- 查询多列 +SELECT username, password FROM user; --- 查询表中的不重复记录 -SELECT DISTINCT `username` -FROM `user`; +-- 查询所有列 +SELECT * FROM user; + +-- 查询不同的值 +SELECT DISTINCT password FROM user; +SELECT DISTINCT username, password FROM user; + +-- 限制结果 +-- 返回前 5 行 +SELECT * FROM user LIMIT 5; +-- 返回前 5 行 +SELECT * FROM user LIMIT 0, 5; +-- 返回第 3 ~ 5 行 +SELECT * FROM user LIMIT 2, 3; + +-- 简单的过滤查询 +SELECT username, password FROM user +WHERE id = 1; ############################################################# -# 删除表中的记录 +# 删除数据 ############################################################# -- 删除符合条件的记录 -DELETE FROM `user` -WHERE `username` = 'robot'; +DELETE FROM user +WHERE username = 'robot'; -- 清空数据表 -TRUNCATE TABLE `user`; +TRUNCATE TABLE user; diff --git a/codes/mysql/select_demo.sql b/codes/mysql/select_demo.sql new file mode 100644 index 0000000..b1c952e --- /dev/null +++ b/codes/mysql/select_demo.sql @@ -0,0 +1,109 @@ +/** + * Mysql 触发器(TRIGGER)创建、使用示例 + * @author Zhang Peng + * @date 2018/5/4 + */ + +############################################################# +# 运行本例的预置操作 +############################################################# + +-- 新建数据表 user +DROP TABLE IF EXISTS user; +CREATE TABLE user ( + id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Id', + username VARCHAR(64) NOT NULL DEFAULT '' COMMENT '用户名', + password VARCHAR(64) NOT NULL DEFAULT '' COMMENT '密码', + email VARCHAR(64) DEFAULT NULL COMMENT '邮箱', + date TIMESTAMP NOT NULL DEFAULT NOW() COMMENT '日期', + PRIMARY KEY (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表'; + +-- 添加测试数据 +INSERT INTO user(username, email) VALUES ('叶开', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('傅红雪', '444444', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('张三丰', '333333', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('陆小凤', '777777', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('王小虎', '555555', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('张飞', '222222', ''); +INSERT INTO user(username, password, email) VALUES ('李寻欢', '444444', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('楚留香', '999999', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('段 誉', '888888', 'xxxx@163.com'); +INSERT INTO user(username, password) VALUES ('萧 峰', '444444'); +INSERT INTO user(username, password, email) VALUES ('李逍遥', '666666', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('sb', '444444', 'xxxx@163.com'); +INSERT INTO user(username, password, email) VALUES ('Joe', '666666', 'xxxx@163.com'); + +############################################################# +# 查询排序 +############################################################# + +-- 查询结果排序 +SELECT * FROM user +ORDER BY date DESC, username ASC; + +############################################################# +# 过滤查询 +############################################################# + +-- 查询 email 为 NULL 的记录 +SELECT * FROM user +WHERE email IS NULL; + +-- 查询 email 为 '' 的记录 +SELECT * FROM user +WHERE email=''; + +############################################################# +# 过滤查询中使用通配符 +############################################################# + +-- 以张开头的任意文本 +SELECT * FROM user +WHERE username LIKE '张%'; + +-- 以张开头的两字文本 +SELECT * FROM user +WHERE username LIKE '张_'; + +-- 不以张开头的任意文本 +SELECT * FROM user +WHERE username NOT LIKE '张%'; + +-- 查询2个字姓名的记录 +SELECT * FROM user +WHERE username LIKE '__' ; + +-- 查询3个字姓名的记录 +SELECT * FROM user +WHERE username LIKE '___' ; + +############################################################# +# 查询中使用计算字段 +############################################################# + +-- 查询3个字姓名的记录 +SELECT CONCAT(TRIM(username), ' (', password, ')') AS '用户名密码' FROM user; + +############################################################# +# 查询分组 +############################################################# + +-- 分组就是把具有相同的数据值的行放在同一组中 +-- 指定的分组字段除了能按该字段进行分组,也会自动按按该字段进行排序 +SELECT password, COUNT(*) AS num +FROM user +GROUP BY password; + +-- GROUP BY 按分组字段进行排序,ORDER BY 也可以以汇总字段来进行排序 +SELECT password, COUNT(*) AS num +FROM user +GROUP BY password +ORDER BY num DESC; + +-- WHERE 过滤行,HAVING 过滤分组,行过滤应当先于分组过滤 +SELECT password, COUNT(*) AS num +FROM user +WHERE password != '' +GROUP BY password +HAVING num >= 2;