常用的 SQL 语法和操作:
数据定义语言(DDL)
c
1.创建数据库
CREATE DATABASE database_name;
2.删除数据库
DROP DATABASE database_name;
3.创建表
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
);
4.删除表
DROP TABLE table_name;
5.修改表(添加列)
ALTER TABLE table_name
ADD column_name datatype constraints;
6.修改表(删除列)
ALTER TABLE table_name
DROP COLUMN column_name;
7.修改表(修改列数据类型或约束)
ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype constraints;
数据操作语言(DML)
cpp
8.插入数据
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
9.更新数据
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
10.删除数据
DELETE FROM table_name
WHERE condition;
11.查询数据
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column
LIMIT number;
数据查询和操作
cpp
12.选择所有列
SELECT * FROM table_name;
13.选择特定列
SELECT column1, column2 FROM table_name;
14.条件查询
SELECT * FROM table_name
WHERE column1 = value;
15.排序
SELECT * FROM table_name
ORDER BY column1 ASC, column2 DESC;
16.分组
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
17.聚合函数
SELECT COUNT(*), SUM(column_name), AVG(column_name)
FROM table_name;
18.连接表
19.内连接
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
20.左连接
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
21.右连接
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
22.全外连接
sql
SELECT columns
FROM table1
FULL OUTER JOIN table2 ON table1.column = table2.column;
数据控制语言(DCL)
cpp
23.授予权限
GRANT privilege ON object TO user;
24.撤销权限
REVOKE privilege ON object FROM user;
事务控制
25.开始事务
START TRANSACTION;
26.提交事务
COMMIT;
27.回滚事务
ROLLBACK;
这些语法可以在大多数 SQL 数据库系统(如 MySQL, PostgreSQL, SQL Server, Oracle)中使用,但有些特定的数据库系统可能会有额外的语法或略微不同的实现。