Mysql学生表常用操作(3)

本文主要介绍了如何在MySQL中创建学生数据库,包括数据库的创建、数据的添加以及一系列操作,适合初学者参考。
摘要由CSDN通过智能技术生成

1.题目

学生选课系统
1. 创建学生选课系统
2. 切换数据库
3. 创建学生表 TbStudent
    主键stuid ,姓名stuname,
    性别stusex,生日stubirth,电话stutel,住址stuaddr
    照片stuphoto(以二进制存)
4. 创建课程表TbCourse
主键cosid, 班级名称cosname,学分coscredit,课程描述cosintro
5. 学生选课记录表TbSC
主键scid,学生外键sid ,班级外键cid,创建日期scdate, 分数score

2.创建学生数据库

create database student;

use student;

-- 创建学生表
create table tbstudent 
( 
stuid integer not null,
stuname varchar(20) not null,
stusex bit default 1,
stubirth datetime not null,
stutel varchar(11),
stuaddr varchar(255),
stuphoto  longblob,
primary key (stuid)
);

-- 如果TBCourse已经存在就删除它
drop table if exists TbCourse;

-- 创建课程表
create table TbCourse
(
cosid integer not null,
cosname varchar(50) not null,
coscredit tinyint not null,
cosintro varchar(255)
);

-- 给课程表指定主键
alter table TbCourse add constraint pk_course primary key (cosid);

-- 创建学生选课记录表
create table tbSC 
(
scid integer primary key auto_increment,
sid integer not null,
cid integer,
scdate datetime not null,
score float
);

-- 给表TBSC添加外键约束
alter table TbSC add constraint fk_sid foreign key (sid) references TbStudent (
stuid) on delete cascade on update cascade;
alter table TbSC add constraint fk_cid foreign key (cid) references TBCourse (
cosid) on delete set null on update cascade;
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值