-- 删除数据表DROPTABLE users;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
2
6. 索引
索引可以加速查询操作。MySQL 支持多种类型的索引。
6.1 创建索引
-- 创建单列索引CREATEINDEX idx_username ON users(username);-- 创建复合索引CREATEINDEX idx_username_password ON users(username, password);
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
2
3
4
5
6.2 查看索引
SHOWINDEXFROM users;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
6.3 删除索引
DROPINDEX idx_username ON users;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
7. 视图
视图是一个虚拟表,可以简化复杂查询。
7.1 创建视图
CREATEVIEW user_view ASSELECT username, created_at FROM users;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
2
7.2 查询视图
SELECT*FROM user_view;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
7.3 删除视图
DROPVIEW user_view;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
8. 存储过程
存储过程是一组 SQL 语句的集合,可以重复执行。
8.1 创建存储过程
DELIMITER//CREATEPROCEDURE GetAllUsers()BEGINSELECT*FROM users;END//DELIMITER;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
2
3
4
5
6
7
8
8.2 调用存储过程
CALL GetAllUsers();
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
8.3 删除存储过程
DROPPROCEDURE GetAllUsers;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
9. 触发器
触发器是在特定事件发生时自动执行的 SQL 语句。
9.1 创建触发器
DELIMITER//CREATETRIGGER before_insert_users
BEFORE INSERTON users
FOR EACH ROWBEGINSET NEW.created_at =NOW();END//DELIMITER;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
1
2
3
4
5
6
7
8
9
10
9.2 删除触发器
DROPTRIGGER before_insert_users;
class="hljs-button signin active add_def" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
评论记录:
回复评论: