当前位置: 首页 > article >正文

SQL命令大全---超细【保姆级】

前言:以下涵盖了 MySQL 中的常见操作和查询类型。对于更高级的用法和特性,建议查阅 MySQL 的 官方文档。

详细数据库设计教程可也参考:https://blog.csdn.net/weixin_44892179/article/details/141821867

基本连接与退出

  • 连接到 MySQL 数据库
mysql -u username -p

这会提示你输入密码。连接到默认的 localhost 和 3306 端口。可以通过 -h 和 -P 参数指定主机和端口。

  • 退出 MySQL
EXIT;

查询操作

  1. 基本查询
  • 选择所有列
SELECT * FROM table_name;
  • 选择特定列
SELECT column1, column2 FROM table_name;
  1. 条件查询
  • 使用 WHERE 子句
SELECT * FROM table_name WHERE condition;

例:

SELECT * FROM employees WHERE department = 'Sales';
  • 使用 AND 和 OR
SELECT * FROM table_name WHERE condition1 AND condition2;
SELECT * FROM table_name WHERE condition1 OR condition2;

例:

SELECT * FROM employees WHERE department = 'Sales' AND hire_date > '2024-01-01';
  • 使用 IN 和 NOT IN
SELECT * FROM table_name WHERE column_name IN (value1, value2, ...);
SELECT * FROM table_name WHERE column_name NOT IN (value1, value2, ...);

例:

SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
  • 使用 LIKE 和 NOT LIKE
SELECT * FROM table_name WHERE column_name LIKE 'pattern';
SELECT * FROM table_name WHERE column_name NOT LIKE 'pattern';

例:

SELECT * FROM employees WHERE name LIKE 'J%'; -- 以 J 开头的所有名字
  • 使用 BETWEEN 和 NOT BETWEEN
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2;
SELECT * FROM table_name WHERE column_name NOT BETWEEN value1 AND value2;

例:

SELECT * FROM employees WHERE hire_date BETWEEN '2024-01-01' AND '2024-12-31';
  1. 排序与限制
  • 排序结果
SELECT * FROM table_name ORDER BY column_name [ASC|DESC];

例:

SELECT * FROM employees ORDER BY hire_date DESC;

  • 限制返回行数
SELECT * FROM table_name LIMIT number;

例:

SELECT * FROM employees LIMIT 5;
  • 限制返回行数并指定偏移量
SELECT * FROM table_name LIMIT offset, number;

例:

SELECT * FROM employees LIMIT 5, 10; -- 从第6行开始,返回10行
  1. 聚合函数
  • 计算总数
SELECT COUNT(column_name) FROM table_name;
  • 计算总和
SELECT SUM(column_name) FROM table_name;
  • 计算平均值
SELECT AVG(column_name) FROM table_name;
  • 计算最大值
SELECT MAX(column_name) FROM table_name;
  • 计算最小值
SELECT MIN(column_name) FROM table_name;
  • 分组
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
  • 分组并筛选
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING COUNT(*) > value;

例:

SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5;
  1. 连接查询
  • 内连接(INNER JOIN)
SELECT columns
FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column;

例:

SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
  • 左连接(LEFT JOIN)
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column;

例:

SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
  • 右连接(RIGHT JOIN)
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;

例:

SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
  • 全连接(FULL JOIN) MySQL 不直接支持 FULL JOIN,但可以通过 UNION 实现类似效果:
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column
UNION
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;
  1. 子查询
  • 作为 SELECT 的一部分
SELECT column_name
FROM table_name
WHERE column_name = (SELECT column_name FROM other_table WHERE condition);

例:

SELECT name
FROM employees
WHERE department_id = (SELECT id FROM departments WHERE department_name = 'Sales');
  • 作为 FROM 的一部分
SELECT *
FROM (SELECT column_name FROM table_name WHERE condition) AS alias;

例:

SELECT *
FROM (SELECT name FROM employees WHERE hire_date > '2024-01-01') AS recent_employees;

数据操作

  1. 插入数据
  • 插入单行数据
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
  • 插入多行数据
INSERT INTO table_name (column1, column2, ...)
VALUES (value1a, value2a, ...), (value1b, value2b, ...), ...;
  • 从另一个表插入数据
INSERT INTO table_name (column1, column2, ...)
SELECT column1, column2, ...
FROM other_table
WHERE condition;
  1. 更新数据
  • 更新单个字段
UPDATE table_name
SET column_name = new_value
WHERE condition;
  • 更新多个字段
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
  1. 删除数据
  • 删除满足条件的行
DELETE FROM table_name
WHERE condition;
  • 删除所有行
DELETE FROM table_name;

事务管理

  • 开始事务
START TRANSACTION;
  • 提交事务
COMMIT;
  • 回滚事务
ROLLBACK;

索引操作

  • 创建索引
CREATE INDEX index_name
ON table_name (column_name);
  • 删除索引
DROP INDEX index_name
ON table_name;

用户管理

  • 创建用户
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
  • 授予权限
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
  • 撤销权限
REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'host';
  • 删除用户
DROP USER 'username'@'host';

备份和恢复

  • 备份数据库
mysqldump -u username -p database_name > backup.sql
  • 恢复数据库
mysql -u username -p database_name < backup.sql

http://www.kler.cn/a/301808.html

相关文章:

  • go 集成swagger 在线接口文档
  • .netcore + postgis 保存地图围栏数据
  • 「Mac玩转仓颉内测版5」入门篇5 - Cangjie控制结构(上)
  • Spring Boot3 实战案例合集上线了
  • Shell基础2
  • 【C语言】实现二维数组按列排序
  • QT界面中的区域以及图像大小,想随着QT界面的放大缩小变化,如何实现?
  • 故障处理系列】elasticsearch 索引未正常分片导致集群状态异常如何分析处理
  • 【超详细】Plaxis软件简介、 Plaxis Python API环境搭建、自动化建模、Python全自动实现、典型岩土工程案例实践应用
  • YOLOV3实现越界检测——智能安防
  • 使用 Visual Studio Code 配置 C/C++ 开发环境
  • 无人机之遥控器工作频率篇
  • Java | Leetcode Java题解之第392题判断子序列
  • FreeRTOS学习笔记(十一)内存管理
  • 计算机的错误计算(九十一)
  • Python中的内存池机制
  • php实现Socket 编程
  • vue如何做到计算属性传参?
  • 研1日记9
  • 【C++多线程编程】 线程安全与对象生命周期管理
  • Docker Compose与私有仓库部署
  • 10分钟在企业微信中集成一个 AI 助手
  • 汽车EDI:montaplast EDI对接
  • XWiki中添加 html 二次编辑失效
  • C# WinForm:禁用Panel容器滚动条自动移动位置的功能
  • leetcode 刷题周报(9.3-9.10)