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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值