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