SQL常用语法

SQL(Structured Query Language)是一种用于存储、操作和检索数据库中数据的标准编程语言。以下是一些常用的 SQL 语法:

  1. 数据库操作

    • 创建数据库:CREATE DATABASE database_name;
    • 删除数据库:DROP DATABASE database_name;
    • 选择数据库:USE database_name;
  2. 表操作

    • 创建表:CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
    • 删除表:DROP TABLE table_name;
    • 修改表结构:ALTER TABLE table_name ADD column_name datatype;
    • 删除表中的列:ALTER TABLE table_name DROP COLUMN column_name;
  3. 数据操作

    • 插入数据:INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
    • 更新数据:UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;
    • 删除数据:DELETE FROM table_name WHERE condition;
    • 查询数据:SELECT column1, column2, ... FROM table_name WHERE condition;
  4. 条件查询

    • 等于:SELECT * FROM table_name WHERE column = value;
    • 不等于:SELECT * FROM table_name WHERE column != value;
    • 大于:SELECT * FROM table_name WHERE column > value;
    • 大于等于:SELECT * FROM table_name WHERE column >= value;
    • 小于:SELECT * FROM table_name WHERE column < value;
    • 小于等于:SELECT * FROM table_name WHERE column <= value;
    • BETWEEN:SELECT * FROM table_name WHERE column BETWEEN value1 AND value2;
    • LIKE(模糊查询):SELECT * FROM table_name WHERE column LIKE 'value%';
    • IN:SELECT * FROM table_name WHERE column IN (value1, value2, ...);
    • AND/OR:SELECT * FROM table_name WHERE column1 = value1 AND column2 = value2;
  5. 聚合函数

    • COUNT:SELECT COUNT(*) FROM table_name;
    • SUM:SELECT SUM(column) FROM table_name;
    • AVG:SELECT AVG(column) FROM table_name;
    • MAX:SELECT MAX(column) FROM table_name;
    • MIN:SELECT MIN(column) FROM table_name;
  6. 分组和排序

    • 分组:SELECT column1, COUNT(*) FROM table_name GROUP BY column1;
    • 排序:SELECT * FROM table_name ORDER BY column1 ASC, column2 DESC;
  7. 连接查询

    • 内连接(INNER JOIN):SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;
    • 左连接(LEFT JOIN):SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name = table2.column_name;
    • 右连接(RIGHT JOIN):SELECT * FROM table1 RIGHT JOIN table2 ON table1.column_name = table2.column_name;
    • 全连接(FULL JOIN)(注意:并非所有数据库系统都支持 FULL JOIN):SELECT * FROM table1 FULL JOIN table2 ON table1.column_name = table2.column_name;
  8. 子查询

    • 嵌套查询:SELECT * FROM table_name WHERE column IN (SELECT column FROM other_table WHERE condition);
  9. 事务控制

    • 开启事务:BEGIN TRANSACTION;
    • 提交事务:COMMIT;
    • 回滚事务:ROLLBACK;
  10. 权限控制

    • 授予权限:GRANT SELECT, INSERT ON table_name TO user_name;
    • 撤销权限:REVOKE SELECT, INSERT ON table_name FROM user_name;
相关推荐
Yeats_Liao19 分钟前
Spring 框架:配置缓存管理器、注解参数与过期时间
java·spring·缓存
Yeats_Liao19 分钟前
Spring 定时任务:@Scheduled 注解四大参数解析
android·java·spring
码明19 分钟前
SpringBoot整合ssm——图书管理系统
java·spring boot·spring
某风吾起24 分钟前
Linux 消息队列的使用方法
java·linux·运维
奇舞精选25 分钟前
在 Chrome 浏览器里获取用户真实硬件信息的方法
前端·chrome
xiao-xiang27 分钟前
jenkins-k8s pod方式动态生成slave节点
java·kubernetes·jenkins
网络风云28 分钟前
golang中的包管理-下--详解
开发语言·后端·golang
取址执行38 分钟前
Redis发布订阅
java·redis·bootstrap
小唐C++1 小时前
C++小病毒-1.0勒索
开发语言·c++·vscode·python·算法·c#·编辑器
S-X-S1 小时前
集成Sleuth实现链路追踪
java·开发语言·链路追踪