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;
相关推荐
hackeroink1 小时前
【2024版】最新推荐好用的XSS漏洞扫描利用工具_xss扫描工具
前端·xss
2401_857439692 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
迷雾漫步者2 小时前
Flutter组件————FloatingActionButton
前端·flutter·dart
SoraLuna3 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
向前看-3 小时前
验证码机制
前端·后端
xlsw_3 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
神仙别闹4 小时前
基于java的改良版超级玛丽小游戏
java
燃先生._.4 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
Dream_Snowar4 小时前
速通Python 第三节
开发语言·python