当涉及到具体的数据库操作时,我会给出实际的示例,以更清楚地说明每个命令的用法。
创建数据库:
sql
CREATE DATABASE students;
列出数据库:
sql
SHOW DATABASES;
使用数据库:
sql
USE students;
创建表:
sql
CREATE TABLE student_info (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT,
gender ENUM('男', '女', '未知'),
course VARCHAR(50)
);
列出表:
sql
SHOW TABLES;
列出表的属性:
sql
SHOW COLUMNS FROM student_info;
列出数据表的详细索引信息:
sql
SHOW INDEX FROM student_info;
显示数据库test中所有表的信息:
sql
SHOW TABLE STATUS FROM test;
显示表名包含test的:
sql
SHOW TABLE STATUS FROM test LIKE '%test%';
插入数据:
sql
INSERT INTO student_info (id, name, age, gender, course) VALUES
(1, '张三男', 21, '男', '数学'),
(2, '张三女', 22, '女', '语文'),
(3, '李四未知', 20, '未知', '英语');
查询数据:
sql
SELECT * FROM student_info;
更新数据:
sql
UPDATE student_info SET course = '语文' WHERE id = 1;
删除数据:
sql
DELETE FROM student_info WHERE id = 3;
条件查询:
sql
SELECT * FROM student_info WHERE age > 20 AND gender = '男';
排序:
sql
SELECT * FROM student_info ORDER BY age DESC;
聚合函数:
sql
SELECT COUNT(*) AS total_students FROM student_info;
SELECT AVG(age) AS avg_age FROM student_info;
SELECT SUM(age) AS total_age FROM student_info;
连接查询:
sql
SELECT student_info.name, student_courses.course_name
FROM student_info
JOIN student_courses ON student_info.course = student_courses.course_name;
假设我们有一个名为"student_courses"的表,它保存课程信息,结构如下:
sql
CREATE TABLE student_courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
并且表中的数据如下:
sql
INSERT INTO student_courses (course_id, course_name) VALUES
(1, '数学'),
(2, '语文'),
(3, '英语');