mirror of https://github.com/dunwu/db-tutorial.git
🔖 调整 mysql 示例
parent
13c2f91097
commit
8520b5045b
|
@ -8,12 +8,12 @@
|
||||||
# 数据库定义
|
# 数据库定义
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
|
-- 撤销数据库 test
|
||||||
|
DROP DATABASE IF EXISTS test;
|
||||||
|
|
||||||
-- 创建数据库 test
|
-- 创建数据库 test
|
||||||
CREATE DATABASE test;
|
CREATE DATABASE test;
|
||||||
|
|
||||||
-- 撤销数据库 test
|
|
||||||
DROP DATABASE test;
|
|
||||||
|
|
||||||
-- 选择数据库 test
|
-- 选择数据库 test
|
||||||
use test;
|
use test;
|
||||||
|
|
||||||
|
@ -21,48 +21,51 @@ use test;
|
||||||
# 数据表定义
|
# 数据表定义
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
|
-- 撤销表 user
|
||||||
|
DROP TABLE IF EXISTS user;
|
||||||
|
DROP TABLE IF EXISTS vip_user;
|
||||||
|
|
||||||
-- 创建表 user
|
-- 创建表 user
|
||||||
CREATE TABLE `user` (
|
CREATE TABLE user (
|
||||||
`id` int(10) unsigned NOT NULL COMMENT 'Id',
|
id int(10) unsigned NOT NULL COMMENT 'Id',
|
||||||
`username` varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
username varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
||||||
`password` varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
password varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
||||||
`email` varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱'
|
email varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱'
|
||||||
) COMMENT='用户表';
|
) COMMENT='用户表';
|
||||||
|
|
||||||
-- 创建新表 vip_user 并复制表 user 的内容
|
-- 创建新表 vip_user 并复制表 user 的内容
|
||||||
CREATE TABLE `vip_user` AS
|
CREATE TABLE vip_user AS
|
||||||
SELECT * FROM `user`;
|
SELECT * FROM user;
|
||||||
|
|
||||||
-- 撤销表 user
|
|
||||||
DROP TABLE `user`;
|
|
||||||
|
|
||||||
-- 添加列 age
|
-- 添加列 age
|
||||||
ALTER TABLE `user`
|
ALTER TABLE user
|
||||||
ADD age int(3);
|
ADD age int(3);
|
||||||
|
|
||||||
-- 撤销列 age
|
|
||||||
ALTER TABLE `user`
|
|
||||||
DROP COLUMN age;
|
|
||||||
|
|
||||||
-- 修改列 age 的类型为 tinyint
|
-- 修改列 age 的类型为 tinyint
|
||||||
ALTER TABLE `user`
|
ALTER TABLE user
|
||||||
MODIFY COLUMN age tinyint;
|
MODIFY COLUMN age tinyint;
|
||||||
|
|
||||||
|
-- 撤销列 age
|
||||||
|
ALTER TABLE user
|
||||||
|
DROP COLUMN age;
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# 索引定义
|
# 索引定义
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
-- 创建表 user 的索引 user_index
|
-- 创建表 user 的索引 user_index
|
||||||
CREATE INDEX `user_index`
|
CREATE INDEX user_index
|
||||||
ON `user` (id);
|
ON user (id);
|
||||||
|
|
||||||
-- 撤销表 user 的索引 user_index
|
-- 创建表 user 的唯一索引 user_index2
|
||||||
ALTER TABLE `user`
|
CREATE UNIQUE INDEX user_index2
|
||||||
DROP INDEX `user_index`;
|
ON user (id);
|
||||||
|
|
||||||
-- 创建表 user 的唯一索引 user_index
|
-- 撤销表 user 的索引
|
||||||
CREATE UNIQUE INDEX `user_index`
|
ALTER TABLE user
|
||||||
ON `user` (id);
|
DROP INDEX user_index;
|
||||||
|
ALTER TABLE user
|
||||||
|
DROP INDEX user_index2;
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# 视图定义
|
# 视图定义
|
||||||
|
@ -71,7 +74,7 @@ ON `user` (id);
|
||||||
-- 创建表 user 的视图 top_10_user_view
|
-- 创建表 user 的视图 top_10_user_view
|
||||||
CREATE VIEW top_10_user_view AS
|
CREATE VIEW top_10_user_view AS
|
||||||
SELECT id, username
|
SELECT id, username
|
||||||
FROM `user`
|
FROM user
|
||||||
WHERE id < 10;
|
WHERE id < 10;
|
||||||
|
|
||||||
-- 撤销表 user 的视图 top_10_user_view
|
-- 撤销表 user 的视图 top_10_user_view
|
||||||
|
|
|
@ -3,6 +3,21 @@
|
||||||
* @author Zhang Peng
|
* @author Zhang Peng
|
||||||
* @date 2018/4/28
|
* @date 2018/4/28
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# 运行本例的预置操作
|
||||||
|
#############################################################
|
||||||
|
|
||||||
|
-- 新建数据表 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 'default' COMMENT '用户名',
|
||||||
|
password varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
||||||
|
email varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱',
|
||||||
|
PRIMARY KEY (id)
|
||||||
|
) COMMENT='用户表';
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# 向表中插入新记录
|
# 向表中插入新记录
|
||||||
#############################################################
|
#############################################################
|
||||||
|
@ -15,17 +30,6 @@ VALUES (1, 'root', 'root', 'xxxx@163.com');
|
||||||
INSERT INTO `user`(`username`, `password`, `email`)
|
INSERT INTO `user`(`username`, `password`, `email`)
|
||||||
VALUES ('admin', 'admin', 'xxxx@163.com');
|
VALUES ('admin', 'admin', 'xxxx@163.com');
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# 删除表中的记录
|
|
||||||
#############################################################
|
|
||||||
|
|
||||||
-- 删除符合条件的记录
|
|
||||||
DELETE FROM `user`
|
|
||||||
WHERE `username` = 'robot';
|
|
||||||
|
|
||||||
-- 清空数据表
|
|
||||||
TRUNCATE TABLE `user`;
|
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
# 更新表中的记录
|
# 更新表中的记录
|
||||||
#############################################################
|
#############################################################
|
||||||
|
@ -49,3 +53,14 @@ SELECT * FROM `user`;
|
||||||
-- 查询表中的不重复记录
|
-- 查询表中的不重复记录
|
||||||
SELECT DISTINCT `username`
|
SELECT DISTINCT `username`
|
||||||
FROM `user`;
|
FROM `user`;
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# 删除表中的记录
|
||||||
|
#############################################################
|
||||||
|
|
||||||
|
-- 删除符合条件的记录
|
||||||
|
DELETE FROM `user`
|
||||||
|
WHERE `username` = 'robot';
|
||||||
|
|
||||||
|
-- 清空数据表
|
||||||
|
TRUNCATE TABLE `user`;
|
||||||
|
|
|
@ -9,13 +9,13 @@
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
-- 新建数据表 user
|
-- 新建数据表 user
|
||||||
DROP TABLE IF EXISTS `user`;
|
DROP TABLE IF EXISTS user;
|
||||||
CREATE TABLE `user` (
|
CREATE TABLE user (
|
||||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Id',
|
id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Id',
|
||||||
`username` varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
username varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
||||||
`password` varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
password varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
||||||
`email` varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱',
|
email varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱',
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (id)
|
||||||
) COMMENT='用户表';
|
) COMMENT='用户表';
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
|
@ -26,14 +26,14 @@ CREATE TABLE `user` (
|
||||||
START TRANSACTION;
|
START TRANSACTION;
|
||||||
|
|
||||||
-- 插入操作A
|
-- 插入操作A
|
||||||
INSERT INTO `user`
|
INSERT INTO user
|
||||||
VALUES (1, 'root1', 'root1', 'xxxx@163.com');
|
VALUES (1, 'root1', 'root1', 'xxxx@163.com');
|
||||||
|
|
||||||
-- 创建保留点 updateA
|
-- 创建保留点 updateA
|
||||||
SAVEPOINT updateA;
|
SAVEPOINT updateA;
|
||||||
|
|
||||||
-- 插入操作B
|
-- 插入操作B
|
||||||
INSERT INTO `user`
|
INSERT INTO user
|
||||||
VALUES (2, 'root2', 'root2', 'xxxx@163.com');
|
VALUES (2, 'root2', 'root2', 'xxxx@163.com');
|
||||||
|
|
||||||
-- 回滚到保留点 updateA
|
-- 回滚到保留点 updateA
|
||||||
|
|
|
@ -9,23 +9,23 @@
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
-- 新建数据表 user
|
-- 新建数据表 user
|
||||||
DROP TABLE IF EXISTS `user`;
|
DROP TABLE IF EXISTS user;
|
||||||
CREATE TABLE `user` (
|
CREATE TABLE user (
|
||||||
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Id',
|
id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Id',
|
||||||
`username` varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
username varchar(64) NOT NULL DEFAULT 'default' COMMENT '用户名',
|
||||||
`password` varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
password varchar(64) NOT NULL DEFAULT 'default' COMMENT '密码',
|
||||||
`email` varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱',
|
email varchar(64) NOT NULL DEFAULT 'default' COMMENT '邮箱',
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (id)
|
||||||
) COMMENT='用户表';
|
) COMMENT='用户表';
|
||||||
|
|
||||||
-- 新建数据表 user_history
|
-- 新建数据表 user_history
|
||||||
DROP TABLE IF EXISTS `user_history`;
|
DROP TABLE IF EXISTS user_history;
|
||||||
CREATE TABLE `user_history` (
|
CREATE TABLE user_history (
|
||||||
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
|
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
|
||||||
`user_id` INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
|
user_id INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '用户ID',
|
||||||
`operate_type` VARCHAR(64) NOT NULL DEFAULT 'default' COMMENT '操作类型',
|
operate_type VARCHAR(64) NOT NULL DEFAULT 'default' COMMENT '操作类型',
|
||||||
`operate_time` VARCHAR(64) NOT NULL DEFAULT 'default' COMMENT '操作时间',
|
operate_time VARCHAR(64) NOT NULL DEFAULT 'default' COMMENT '操作时间',
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (id)
|
||||||
) COMMENT='用户记录表';
|
) COMMENT='用户记录表';
|
||||||
|
|
||||||
#############################################################
|
#############################################################
|
||||||
|
@ -33,15 +33,15 @@ CREATE TABLE `user_history` (
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
-- 删除触发器
|
-- 删除触发器
|
||||||
DROP TRIGGER IF EXISTS `trigger_insert_user`;
|
DROP TRIGGER IF EXISTS trigger_insert_user;
|
||||||
|
|
||||||
-- 创建触发器
|
-- 创建触发器
|
||||||
DELIMITER $
|
DELIMITER $
|
||||||
CREATE TRIGGER `trigger_insert_user`
|
CREATE TRIGGER trigger_insert_user
|
||||||
AFTER INSERT ON `user`
|
AFTER INSERT ON user
|
||||||
FOR EACH ROW
|
FOR EACH ROW
|
||||||
BEGIN
|
BEGIN
|
||||||
INSERT INTO `user_history`(user_id, operate_type, operate_time)
|
INSERT INTO user_history(user_id, operate_type, operate_time)
|
||||||
VALUES (NEW.id, 'add a user', now());
|
VALUES (NEW.id, 'add a user', now());
|
||||||
END $
|
END $
|
||||||
DELIMITER ;
|
DELIMITER ;
|
||||||
|
@ -53,6 +53,6 @@ SHOW TRIGGERS;
|
||||||
# 测试
|
# 测试
|
||||||
#############################################################
|
#############################################################
|
||||||
|
|
||||||
INSERT INTO `user`(username, password, email)
|
INSERT INTO user(username, password, email)
|
||||||
VALUES ('admin', 'admin', 'xxxx@163.com');
|
VALUES ('admin', 'admin', 'xxxx@163.com');
|
||||||
SELECT * FROM user_history;
|
SELECT * FROM user_history;
|
||||||
|
|
Loading…
Reference in New Issue