SQL實踐1
借著學校的資料庫實驗,來對之前學習的SQL語言進行實踐和總結,
實驗環境:
- macOS 13.2 (22D49)
- mysql Ver 8.0.32 for macos13.0 on arm64 (Homebrew)
- DataGrip 2022.3.3
一. DataGrip連接本地MySQL
由于我之前都是用vs code的插件連接MySQL的(
可能是我不會用,我感覺不太好使),所以這也是我第一次使用DataGrip,
1. 新建專案

首先先新建一個專案,并給專案命名,
2. 連接MySQL


在此輸入連接的資訊,可以看到,我貌似缺少了驅動,先去裝個驅動,

裝完驅動后測驗連接成功后應用就行了,(測驗前別忘了把資料庫打開)
3. 配置所有架構
剛連接上的時候,我發現資源管理器中并找不到我之前創建的資料庫,后發現需要配置所有架構才可以,

4. 撰寫SQL陳述句
- 先新建一個查詢控制臺

- 然后撰寫SQL陳述句并點那個綠色的小三角就行了

- 以后需要再打開之前撰寫的SQL陳述句在這里打開

一. 創建表
- 表結構

1. 創建學生選課關系資料庫中的STUDENT表
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 <= 120), # 限制年齡為0-120歲
constraint CHECK_SEX
check (SSEX = '男' or SSEX = '女') # 限制性別為男或女
)
comment '學生表';
- 直接用GUI創建會方便很多

2. 創建學生選課關系資料庫中的COURSE表
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 '課程表';
3. 創建學生選課關系資料庫中的SC表
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上說這是事物部分知識點,還沒學到那

三. 修改資料
1. 修改Student表結構,為Student表格添加一個“入學時間”屬性,屬性名為Senrollment,各元組在屬性Senrollment的值是多少?
alter table Student add Senrollment date comment '入學時間';
- 值是null

2. 修改Student表結構,把Ssex列的寬度設定為6個位元組
alter table Student modify SSEX varchar(6);
在此出現了兩個問題:
- 注釋沒掉了

- 之前設定的檢查亂碼了

- 在修改表中欄位時,是覆寫的,之前設定的約束和注釋什么的都要重新設定,
3. 修改Student表結構,洗掉Senrollment列(注:洗掉SYS模式下表的列將被拒絕)
alter table Student drop Senrollment;

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/546592.html
標籤:其他
上一篇:三天吃透MySQL面試八股文
