1. 创建表(CREATE TABLE):
CREATE TABLE students (
student_id SERIAL PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT
);
2. 插入数据(INSERT INTO):
INSERT INTO students (first_name, last_name, age) VALUES
('John', 'Doe', 25),
('Jane', 'Smith', 22),
('Bob', 'Johnson', 28);
3. 查询数据(SELECT):
SELECT * FROM students;
4. 更新数据(UPDATE):
UPDATE students SET age = 26 WHERE first_name = 'John';
5. 删除数据(DELETE):
DELETE FROM students WHERE last_name = 'Smith';
6. 筛选数据(WHERE子句):
SELECT * FROM students WHERE age > 25;
7. 排序数据(ORDER BY):
SELECT * FROM students ORDER BY last_name ASC;
8. 聚合函数(SUM、AVG、COUNT等):
SELECT AVG(age) FROM students;
9. 连接表(JOIN):
SELECT students.first_name, students.last_name, courses.course_name
FROM students
INNER JOIN courses ON students.student_id = courses.student_id;
以上只是一些基础的SQL语法示例。实际上,SQL语法非常丰富,可以执行各种复杂的查询和操作。
转载请注明出处:http://www.zyzy.cn/article/detail/8116/PostgreSQL