学生表的DDL和DML

DDL

sql 复制代码
-- 创建学生表
CREATE TABLE students (
    student_id INT PRIMARY KEY AUTO_INCREMENT,
    studentname VARCHAR(50),
    age INT,
    gender VARCHAR(10)
);

-- 创建课程表
CREATE TABLE courses (
    course_id INT PRIMARY KEY AUTO_INCREMENT,
    course_name VARCHAR(50)
);

-- 创建教师表
CREATE TABLE teachers (
    teacher_id INT PRIMARY KEY AUTO_INCREMENT,
    teachername VARCHAR(50),
    department VARCHAR(50)
);

-- 创建成绩表
CREATE TABLE grades (
    grade_id INT PRIMARY KEY AUTO_INCREMENT,
    student_id INT,
    course_id INT,
    grade DECIMAL(5, 2),
    FOREIGN KEY (student_id) REFERENCES students(student_id),
    FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

-- 创建授课表
CREATE TABLE teachings (
    teaching_id INT PRIMARY KEY AUTO_INCREMENT,
    teacher_id INT,
    course_id INT,
    FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id),
    FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

DML

sql 复制代码
-- 插入学生数据
INSERT INTO students (name, age, gender)
VALUES ('张三', 18, '男'),
       ('李四', 19, '女'),
       ('王五', 20, '男');

-- 插入课程数据
INSERT INTO courses (course_name)
VALUES ('数学'),
       ('英语'),
       ('物理');

-- 插入教师数据
INSERT INTO teachers (name, department)
VALUES ('赵老师', '数学系'),
       ('钱老师', '英语系'),
       ('孙老师', '物理系');

-- 插入成绩数据
INSERT INTO grades (student_id, course_id, grade)
VALUES (1, 1, 85.5),
       (1, 2, 78.0),
       (2, 1, 90.0),
       (2, 2, 88.5),
       (3, 1, 75.0),
       (3, 3, 80.0);

-- 插入授课数据
INSERT INTO teachings (teacher_id, course_id)
VALUES (1, 1),
       (2, 2),
       (3, 3);
相关推荐
AOwhisky5 小时前
MySQL 学习笔记(第四期):SQL 语言之多表查询
linux·运维·网络·数据库·笔记·学习·mysql
小红卒5 小时前
mysql之udf提权
数据库·mysql·网络安全
Trouvaille ~5 小时前
【Redis篇】Redis 哨兵(Sentinel):高可用自动故障转移
数据库·redis·缓存·中间件·sentinel·高可用·哨兵
qfljg5 小时前
oracle 迁移到postgres
数据库·oracle
rockey6276 小时前
基于AScript的SQL脚本语言发布啦!
sql·c#·.net·script·expression·动态脚本
giaz14n9X6 小时前
Redis 分布式锁进阶第五十七篇
数据库·redis·分布式
剑神一笑6 小时前
Linux ls 命令深度解析:从目录遍历到颜色输出的实现原理
linux·服务器·数据库
Maynor9967 小时前
Codex API 网关迁移与流量优化实战
数据库·oracle
WyCAGy8ij7 小时前
Redis 分布式锁进阶第二篇讲解
数据库·redis·分布式
南极企鹅7 小时前
MySQL的两大支柱:undo Log&redo log
数据库·mysql·oracle