MySQL数据库操作school

  • 插入数据
use school;
-- 插入数据
insert into tb_college values(default, '计算机学院', '学习计算机科学与技术,助力国家信息化建设');

-- 给指定的列赋值 (没有被赋值的列要么允许为空,要么有默认值) 
insert into tb_college (col_name, col_intro) values
('外国语学院', '学习英语、德语、法语');

-- 批量插入
insert into tb_college (col_name, col_intro) values
('经济管理学院', 'XXX'),
('物理学院', 'YYY'),
('化学学院', 'ZZZ');

-- 插入学生数据
insert into tb_student values(1, '张三', default, '2020-1-1', '四川', 1);

-- 批量插入
insert into tb_student(stu_id, stu_name, stu_sex, stu_birth, col_id)values
(2, '李四', 1, '1995-1-1', 2),
(3, '王五', 1, '1998-2-3', 1),
(4, '赵六', 1, '1999-2-4', 3),
(5, '小花', 0, '2005-1-5', 3);
  • 删除数据
use school;

-- insert / delete / update
-- 删除一定要带条件where
delete from tb_college where col_id=5;
-- 删除多行
delete from tb_college where col_id in (3, 4)
-- 删除有外键约束的,会报错
  • 更新数据
use school;
-- 更新数据
update tb_student set stu_addr='四川成都' where stu_id in (1, 5);

-- 更改多个字段,用逗号隔开就可以
update tb_student set stu_birth='1990-1-1', stu_addr='四川绵阳' where stu_id between 2 and 4;
  • 创建数据库和表
-- 如果存在名为school的数据库就删除它
drop database if exists `school`;

-- 创建名为school的数据库并设置默认的字符集和排序方式
create database `school` default charset utf8mb4;

-- 切换到school数据库上下文环境
use `school`;

-- 创建学院表
create table `tb_college`
(
`col_id` int unsigned auto_increment comment '编号',
`col_name` varchar(50) not null comment '名称',
`col_intro` varchar(5000) default '' comment '介绍',
primary key (`col_id`)
) engine=innodb comment '学院表';

-- 创建学生表
create table `tb_student`
(
`stu_id` int unsigned not null comment '学号',
`stu_name` varchar(20) not null comment '姓名',
`stu_sex` boolean default 1 comment '性别',
`stu_birth` date not null comment '出生日期',
`stu_addr` varchar(255) default '' comment '籍贯',
`col_id` int unsigned not null comment '所属学院',
primary key (`stu_id`),
foreign key (`col_id`) references `tb_college` (`col_id`)
) engine=innodb comment '学生表';

-- 创建教师表
create table `tb_teacher`
(
`tea_id` int unsigned not null comment '工号',
`tea_name` varchar(20) not null comment '姓名',
`tea_title` varchar(10) default '助教' comment '职称',
`col_id` int unsigned not null comment '所属学院',
primary key (`tea_id`),
foreign key (`col_id`) references `tb_college` (`col_id`)
) engine=innodb comment '老师表';

-- 创建课程表
create table `tb_course`
(
`cou_id` int unsigned not null comment '编号',
`cou_name` varchar(50) not null comment '名称',
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值