借着学校的数据库实验,来对之前学习的SQL语言进行实践和总结。
实验环境:
- macOS 13.2 (22D49)
- mysql Ver 8.0.32 for macos13.0 on arm64 (Homebrew)
- DataGrip 2022.3.3
由于我之前都是用vs code的插件连接MySQL的(
可能是我不会用,我感觉不太好使),所以这也是我第一次使用DataGrip。

首先先新建一个项目,并给项目命名。


在此输入连接的信息。可以看到,我貌似缺少了驱动,先去装个驱动。

装完驱动后测试连接成功后应用就行了。(测试前别忘了把数据库打开)
刚连接上的时候,我发现资源管理器中并找不到我之前创建的数据库,后发现需要配置所有架构才可以。





create table Student
(
SNO varchar(9) not null comment '学号',
SNAME varchar(5) null comment '姓名',
SSEX varchar(1) null comment '性别',
SAGE int null comment '年龄',
SDEPT varchar(10) null comment '专业',
constraint Student_NO
primary key (SNO), # 设置学号为主键
constraint CHECK_AGE
check (SAGE >= 0 and SAGE

create table COURSE
(
CNO varchar(3) not null comment '编号',
CNAME varchar(10) not null comment '课程名',
CPNO int null comment '前置课程号',
CCREDIT int null comment '学分',
constraint COURSE_NO
primary key (CNO) # 设置编号为主键
)
comment '课程表';
create table SC
(
SNO varchar(9) not null comment '选修学生学号',
CNO varchar(3) not null comment '选修课程号',
GRADE int null comment '成绩',
constraint SC___CNO
foreign key (CNO) references COURSE (CNO), # 链接到课程表的外键
constraint SC___SNO
foreign key (SNO) references Student (SNO) # 链接到学生表的外键
)
comment '选修表';
insert into Student(sname,ssex,sno, sage, sdept) values('李勇','男','200215121',20,'CS');
insert into Student(sname,ssex,sno, sage, sdept) values('刘晨','女','200215122',19,'CS');
insert into Student(sname,ssex,sno, sage, sdept) values('王敏','女','200215123',18,'MA');
insert into Student(sname,ssex,sno, sage, sdept) values('张立','男','200215125',19,'IS');

insert into course(cno,cname,cpno,ccredit) values('6','数据处理',null,2);
insert into course(cno,cname,cpno,ccredit) values('2','数学',null,2);
insert into course(cno,cname,cpno,ccredit) values('7','PASCAL语言','6',4);
insert into course(cno,cname,cpno,ccredit) values('5','数据结构','7',4);
insert into course(cno,cname,cpno,ccredit) values('1','数据库','5',4);
insert into course(cno,cname,cpno,ccredit) values('3','信息系统','1',4);
insert into course(cno,cname,cpno,ccredit) values('4','操作系统','6',3);

insert into sc(sno,cno,grade) values('200215121','1',92);
insert into sc(sno,cno,grade) values('200215121','2',85);
insert into sc(sno,cno,grade) values('200215121','3',88);
insert into sc(sno,cno,grade) values('200215122','2',90);
insert into sc(sno,cno,grade) values('200215122','3',80);
commit; # Google上说这是事物部分知识点,还没学到那

alter table Student add Senrollment date comment '入学时间';

alter table Student modify SSEX varchar(6);
在此出现了两个问题:


alter table Student drop Senrollment;

登录查看全部
参与评论
手机查看
返回顶部