包括的なデータベースの基盤と実践事例の詳細な説明
質問元:
主にデータベースの基本を要約する練習をし、演習に独自の補足的な理解を追加します
「1つの」データベースの基本的な知識:作成して使用する
在数据库中是不严格分大小写的
创建数据库 create database 数据库名
创建数据表 create table 表名
使用/切换数据库 use 数据库名
关闭数据库 close 数据库名
删除数据表 drop 数据表名
删除数据表中元组 delete 元组
显示表结构 desc 表名
-
データベースには4つのテーブルグループがあります
「2つの」データベースの基本的な知識:データの挿入
数据库的数据插入--> insert into 表名(表结构,列1、列2....) values(数据,列1对应的数据、列2对应的数据); 插入多条数据 --> values 第一条数据(数据,列1对应的数据、列2对应的数据),values 第一条数据(数据,列1对应的数据、列2对应的数据)....... 其中的表结构是可以省略的,但是省略后插入数据的顺序必须要按照建表时的表结构顺序 例如--> insert into 表名 values(数据,列1对应的数据、列2对应的数据);
- 学生(学生テーブル)
- コース(コーススケジュール)
- スコア
- 先生
-
テーブルの作成後にデータを挿入します
-
学生データ
insert into Student values('108','曾华','男','1977-09-01','95033'); insert into Student values('105','匡明','男','1975-10-02','95031'); insert into Student values('107','王丽','女','1976-01-23','95033'); insert into Student values('101','李军','男','1976-02-20','95033'); insert into Student values('109','王芳','女','1975-02-10','95031'); insert into Student values('103','陆君','男','1974-06-03','95031');
-
コースデータ
insert into Course values('3-105','计算机导论','825'); insert into Course values('3-245','操作系统','804'); insert into Course values('6-166','数字电路','856'); insert into Course values('9-888','高等数学','831');
- スコアデータ
insert into Score values('103','3-245','86'); insert into Score values('105','3-245','75'); insert into Score values('109','3-245','68'); insert into Score values('103','3-105','92'); insert into Score values('105','3-105','88'); insert into Score values('109','3-105','76'); insert into Score values('101','3-105','64'); insert into Score values('107','3-105','91'); insert into Score values('108','3-105','78'); insert into Score values('101','6-166','85'); insert into Score values('107','6-166','79'); insert into Score values('108','6-166','81');
-
教師データ
insert into Teacher values('804','李诚','男','1958-12-02','副教授','计算机系'); insert into Teacher values('856','张旭','男','1969-03-12','讲师','电子工程系'); insert into Teacher values('825','王萍','女','1972-05-05','助教','计算机系'); insert into Teacher values('831','刘冰','女','1977-08-14','助教','电子工程系');
-
「3つの」データベースの基本的な知識:情報クエリ
1.表内信息查询——> select 查询的表属性 from 表名 where 查询条件
2.其中要查询全部信息 --> select * from 表名;
3.其中当查询的结果不止一项时要用“,”分隔
4.其中如果查询不重复数据 在列前加上 distinct 表示查询的结果无重复项
5.其中当查询有条件限制时,其中and表两个条件都需要满足,or只需满足一个即可
6.其中between...and.....表示的范围和and语句一样
7.在数据库中对字符类型的数据,使用单引号和双引号都是一样的
8.其中在子查询的时候,当子查询的结果只有一个的时候 可以用in 也可以直接用=
-
StudentテーブルのすべてのレコードのSname、Ssex、およびClass列をクエリします。
select Sname,Ssex,Class from Student
-
教師のすべてのユニット、つまり一意のDepart列をクエリします。
select distinct Depart from Teache
-
Studentテーブルのすべてのレコードをクエリします。
select * from Student
-
スコアが60〜80のスコアテーブル内のすべてのレコードをクエリします。
select * from score where degree>60 and degree<80; 还有一种写法对and语句使用 between...and..... select * from Score where Degree between 60 and 80
-
スコアテーブルの85、86、または88のレコードをクエリします。
select *from score where degree=85 or degree=86 or degree=88;
其实在这里推荐使用子查询方式
select * from score where degree in (85,86,88);
表示的意思和上面or一样,表示degree在后面集合中的元素,in在里面的意思。
- Studentテーブルで、クラス「95031」または性別が「女性」であるクラスメートのレコードをクエリします。
select * from Student where class='95031' or Ssex='女'
-
70より大きく90未満のスコアでSno列をクエリします。
select Sno from Score where Degree between 70 and 90
-
85以上のスコアを持つコースCnoがあります。
第一种方法,求出所有成绩大于85的成绩对应的学号 select distinct cno from score where cno in(select cno from score where degree>85); 第二种方法:分组按成绩分组,分出来的一组成绩大于85 select Cno from Score where Degree in (select Degree from Score group by Cno having Degree>85) 第三种方法比较简单: select distinct cno from score where degree>85
「4つの」データベースの基本的な知識:並べ替え
1.数据库的排序有两种升序->由小到大 asc 降序->由大到小 desc
用法:select 列名 from 表名 order by 要排序的列 排序方式
当没有过滤条件是不用加 where
- Studentテーブルのすべてのレコードをクラスの降順でクエリします。
select * from student order by class desc
-
スコアテーブル内のすべてのレコードを、Cnoの昇順およびDegreeの降順でクエリします。
select * from Score order by Cno, Degree desc
「5つの」データベースの基本的な知識:機能
常见的函数有:
1.求某一个项的个数--> count(*) 查询 where过滤后的元组个数有多少个
2.求某一列的最大值--> max(列名)
3.求某一列的最小值--> min(列名)
4.求某一项的平均值--> avg(列名)
5.分组,按某一个规则分组--> group by 规则
比如按照班级分组 select * from 学生表 group by 班级属性
6.Having子句,又称分组过滤子句。需要有Groupby子句支持,换句话说,没有Groupby子句,便不能有Having子句,就是对分组后的每一组进行过滤,在group by后面使用。
6.求最大值max(列名)
7.求最小值min(列名)
8.获取左右两边的某些值,比如获得出生日期的年left(sbirthday,4),表示取出生日期的左边四位
-
クラス「95031」の生徒数を照会する
select count(*) from Student where class='95031'
-
スコアテーブルでスコアが最も高い学生番号とコース番号をクエリします。
select Sno,Cno from Score where Degree=(select max(Degree) from Score)
-
各コースの平均成績を照会する
因为要求每一门课的平均值,这里就要对课程进行按照课程号分组
select avg(Degree) from Score group by Cno;
-
少なくとも5つの選択科目があり、スコアテーブルで3から始まるコースの平均スコアをクエリします。
select avg(Degree) from Score group by Cno having count(*)>=5 and Cno like '3%'
-
Studentテーブルの最大および最小の土曜日の日付値をクエリします
select max(Sbirthday),min(Sbirthday) from Student
-
成績がコースの平均成績よりも低い学生の成績証明書を照会します。
这里要查找的是在每门课程中该课程中的学生成绩小于平均成绩,所以首先要对每门课程进行分组,求出每门课程的平均成绩 select * from Score a where a.Degree<(select avg(Degree) from Score b where a.cno=b.cno group by cno);
「6つの」データベースの基本的な知識:マルチテーブル接続
1.在数据表中要想利用本表的信息查询与之关联的其他表的信息,需要用到外码,外码也就是连个表之间的连接条件,起中间传递作用,如果两个表要想相连,必须有相同项。
外码的作用就是将两个表合并
2.还有一种连接方式 from 表一 join 表二 on 就是将表一加入到表二合成一个新表 不用where 条件直接在on后面即可
- すべての学生のSname、Cno、およびDegree列をクエリします。
select Student.Sname,Score.Cno,Score.Degree from Student,Score where Student.Sno=Score.Sno
select sname,cno,degree from student join score on Student.Sno=Score.Sno
- クラス「95033」の平均スコアを照会する
select avg(Degree) from Score,Student where Student.Sno=Score.Sno and class='95033'
第二种方式子查询方式,思路是一样的先找出95033班的学生,第一种可能容易想到,两种第二种查找效率高点
select avg(Degree) from Score where Sno in (select Sno from Student where class='95033')
-
「張徐」先生の生徒の結果を照会します。
select Degree from Score,Course,Teacher where Teacher.Tname='张旭'and Teacher.Tno=Course.Tno and Course.Cno=Score.Cno 第二种子查询推荐使用 select student.sname,score.degree from student,score where student.sno=score.sno and score.cno in(select course.cno from course,teacher where teacher.tname="张旭" and course.tno=teacher.tno);
-
コースを受講する生徒の数が5人を超える教師の名前を照会します。
select teacher.tname from course,teacher where course.tno=teacher.tno and course.cno in (select cno from score group by cno having count(cno)>5);
「7つの」データベースの基本的な知識:テーブルエイリアス
1.表名 as 别名 -->其中as可以省略 表名 别名
在起别名的时候默认全部信息答应的是两个表的信息
-
選択科目「3-105」の成績が学生番号「109」の成績よりも高いすべての学生の記録を照会します。
select distinct a.Cno,a.Sno,a.Degree from Score a,Score b where a.Cno='3-105' and a.Degree>b.Degree and b.Sno='109' and b.Cno='3-105' 第二种子查询 select * from Score where Cno='3-105' and degree>(select degree from Score where Sno='109' and Cno='3-105 ')
-
最高スコアではないスコアで複数のコースを選択した学生の記録を照会します。
select * from Score where Degree not in (select max(Degree) from Score group by sno having count(*)>1) and
Sno in(count(*)> 1のSnoでスコアグループからSnoを選択)
+ **查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof**。
Tname、Prof from Teacher where Prof not in(Select Prof from Teacher where Depart = '计算机系' and prof in(select Prof from Teacher where Depart = 'electric子測定系'))
+ **求成绩非最高的学生信息**
select * from score wheredegree <some(selectdegree fromscore);
#### 《八》数据库基础知识:日期及年龄
現在の時刻を表示します。curdate()を使用して
、生年月日を取得します。年(誕生日)生年月日で
学生の年齢を確認する方法年月日を取得し
ます。年(誕生日)月(誕生日)日(生年月日)
+ **查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。**
学生からsno、sname、sbirthdayを選択します。year(sbirthday)=(sno =“ 108”の学生からyear(sbirthday)を選択します);
+ **通过出生日期计算年龄**
(YEAR(CURDATE())-YEAR(birth))-(RIGHT(CURDATE()、5)<RIGHT(birth、5))
#### 《九》数据库基础知识:多表显示
複数のテーブルのすべての情報を同時に
表示する方法。unionselectからテーブル1に
表示される列unionselectからテーブル2に表示される列
+ **查询所有教师和同学的name、sex和birthday.**
名前としてTname、性別としてTsex、教師組合から誕生日としてTbirthdayを選択します。StudentからSname、Ssex、Sbirthdayを選択します。
#### 《十》数据库基础知识:模糊匹配
たとえば、一般的に使用されるクエリでは、ファジークエリに使用できるテーブル内のデータの一部のみを記憶しています。
「」のような使用法
1。%->「Zhang%」のような1つ以上に一致する
2_ -> find ZhangSanなどの文字を「张_」
3のように一致させます。\->エスケープ文字エスケープ文字は、一部の特殊文字の特定の意味を削除し、「%」などの通常の文字として扱うために使用されます。文字%に一致させるには、_を使用して文字_に一致させます。
+ **查询Student表中不姓“王”的同学记录。**
Snameが「王%」を好まない学生から*を選択します
#### 《十一》数据库基础知识:信息修改
テーブル内の情報を
変更します
。definemodifyaltertablename [add {colnamedatatype、…}]新しい列を追加します
[drop {整合性制約名}]整合性制約を削除します
[modify {colnamedatatype、…}]列定義
要素の変更変更
更新テーブル名前
セット列名=式
+ **增加新的一列,给学生表添加所属系别属性**
学生
を変更する部門の文字を追加する(10);
+ **将所有的学生的成绩提高10分**
更新スコアセット度=度+10;
#### 《十二》数据库基础知识:NULL 值判断
テーブル内のnull値を照会するには、nullを使用し、nullではありません。
+ **查找没参加考试的学生信息**
次数がnullのスコアから*を選択します。
约束
[modify {colnamedatatype, …}] 修改列定义
元素修改
update 表名
set 列名=表达式
-
新しい列を追加し、学部の属性を学生テーブルに追加します
alter student add dept char(10);
-
すべての生徒の成績を10ポイント上げます
update score set degree=degree+10 ;
「12」データベースの基本知識:NULL値の判断
查询表中的null值时用 is null 和 is not null来查询
-
試験を受けなかった学生に関する情報を探す
select * from score where degree is null;