[SQL] SQLを書けない?一般的なSQLを入手するための記事

0、前に書かれています

次の内容は、主に作成者の宿題の演習からのものであり、SQL構文のほとんどが含まれているはずです。この記事のデータベースもっとよく食べましょう。

1.準備

作成:学生テーブル学生、コーステーブルコース、選択テーブルコース。データを挿入します。

create table if not exists student(
    sid char(6) not null primary key,
    name varchar(25) not null,
    sex int(1) not null,
    age int(3) not null,
    academy varchar(10) not null,
    major varchar(10) not null
    );
create table if not exists course(
    cid char(3) not null primary key,
    name varchar(10) not null,
    hour int(2) not null,
    property varchar(5) not null
);
create table if not exists take(
    sid char(6) not null ,
    cid char(3) not null ,
    constraint fk_s foreign key (sid) references student(sid),
    constraint fk_c foreign key (cid) references course(cid),
    grade int(3) not null
);

2.要件:

1.「データベース」コースを受講した学生の名前を照会します。

select student.name from student,take,course 
	where course.name = '数据库' and course.cid = take.cid and take.sid = student.sid;
#优化,使用自然连接相当于做了个过滤,省去了一个and条件
select s.name from student s where s.sid in (
	select t.sid from course c natural join take t where c.name = '数据库';
)
#使用in,可见in可用于连接多个语句
select student.name from student where student.sid in(
	select take.sid from take where take.cid in(
    	select take.cid from course where course.name = '数据库'
    ) 
);

2.「データベース」コースの成績を照会し、降順で並べ替えます

select grade from take,course 
	where course.name = '数据库' and course.cid = take.cid 
	order by grade desc;
#优化,asc/desc 升、降序
select grade from take t natural join course c
	where c.name = '数据库' order by grade desc;

3.コースを受講していない学生の学生数を照会します

select student.sid from student 
	where sid not in (select sid from take);

4.もちろんc01の最高スコアを照会します

 select max(take.grade) from take,course where course.cid ='c01'; #0.032ms
 #优化 0.011ms(4条数据情况下测试的)
 select max(take.grade) from take natural join course where course.cid ='c01'; 

5.コンピュータサイエンス学部の学生の選択記録をクリアします

delete from take where take.sid = any 
	(select student.sid from student where student.academy='计算机学院');

6.必要なすべてのコースの単位時間を16増やします

update course set course.hour = course.hour + 16 
	where course.property = '必修';

7.各コースの平均成績を照会するビューを作成します

create view avg_view as
    select course.name,take.cid,avg(grade) as 平均成绩 from course,take
    where take.cid = course.cid
    group by take.cid;
#视图直接进行查询
select * from ava_view;

8.学生番号が入力されたときに選択科目の総数を返すストアドプロシージャを作成します

create procedure pro_select_total(in id char(6))
begin
    select count(take.cid) from take  where take.sid = id;
end;

#调用存储过程
call pro_select_total(202055);

9.トリガーを作成します。学生が削除されると、そのコース選択情報がすべて同時に削除されます。

create trigger trigger_del after delete
    on student for each row
    begin
        delete from take where sid not in
        (select sid from student);
    end;

実際の実行プロセスではset foreign_key_checks = 0;、外部キー制約チェックをオフにする必要があります。オフにしないと、エラーが報告されます。

次に、delete from student where student.sid = '202033';ステートメントテストを実行します。

2番目の仕事:

2.要件

1.少なくとも10個のクエリを設計します(集計、結合など、単一テーブルクエリ≤3を含む)。

1. 100〜110の単位を取得した学生の名前を確認します(単一テーブル、関数)

select name from student where tot_cred in (100,110);

2.給与が最も高い10人の教師を見つけ、給与の降順で並べます。(**単一のテーブル、降順)**

select name,salary from instructor  order by salary desc limit 10;

3.2番目の文字cである名前の学生の数を照会します。 (単一テーブル、ファジークエリ)

select count(student.name) from student where student.name like '_c%';

4.生徒と教師の名前が同じかどうかを確認し、名前情報を表示します。(複数のテーブル)

select instructor.name,student.name from student,instructor where student.name = instructor.name;

5.画像処理の成績がAを超えている人の数を問い合わせます。(マルチテーブル、自然結合)

select count(takes.grade) from takes 
	natural join course where course.title = 'image Processing'
	and (takes.grade = 'A+' or takes.grade = 'A');

6.Lembrという名前の教師が教えているコースについて問い合わせます。(複数のテーブル、最初は自然接続、次にデカルト積)

select name, title from teaches 
	natural join instructor,course 
	where teaches.course_id = course.course_id and instructor.name = 'Lembr';

次のように書くこともできます:(dept_namesも等しい場合に、部門にないコースが欠落しないように、自然な接続としてcourse_idのみを使用するように2回目を制限します)

select name, title from teaches 
	natural join instructor join course useing (course_id)
	where instructor.name = 'Lembr';

7. 2009年秋と2010年春に提供されるコースを確認してください。(複数のテーブル、交差操作、mysqlはユニオン並列操作のみをサポートし、交差操作は並列操作で重複データを取得することで実現できます)

select course_id,title from course where course_id = any (
 (select course_id from section where semester = 'Fall' and year = '2009') 		union all
	(select course_id from section where semester = 'Spring' and year = '2010')
	)group by course_id having count(*) > 1;

8.コンピューター教師の平均給与を計算します。(複数のテーブル、集計関数、平均)

select avg(salary) as 平均工资 from instructor where dept_name = 'Comp. Sci.';

9.クラスルーム808で開催されているコースを確認します。(マルチテーブル、2次フィルター、または自然結合)

select title from course where course_id = 
	any(select course_id from section where room_number = '808');
select title from course natural join section where room_number = '808';

10.ワールドヒストリーコースが教えられている建物、教室番号、教師、大学、特定の時間、学期などについて問い合わせます。(複数のテーブル)

select * from course natural join section 
	join time_slot using (time_slot_id) where title = 'World History' ;

2.少なくとも1つのビューを作成し、ビューを介してデータを表示します

1.実行ステートメントが次のビューを作成します。IDが10076の学生が選択したすべてのコースを照会します。

create view view_1 as
 	select student.name,title from takes natural join 
     	student join course using (course_id) where ID = '10076' ;

3.少なくとも1つのインデックスを作成し、インデックス作成の前後のデータクエリの効率を分析します

クエリの効率を最適化するためのインデックスを作成します。

create index index_grade on takes(grade);

実験環境:リモートデータベースに接続する場合、テストサンプルレコードは約30,000です。

単一テーブルのクエリステートメントを実行します。select takes.ID from takes where grade = 'A';

作成前は平均59ミリ秒、作成後は平均56ミリ秒かかります。

マルチテーブルクエリステートメントを実行します。select name from student,takes where grade = 'A';

作成前は平均59ミリ秒、作成後は平均56ミリ秒かかります。

結論:インデックスを確立すると、クエリの効率をある程度向上させることができますが、テストやサンプルが少なすぎるため、効果が明確でない場合がありますが、インデックスを多用しないでください。また、インデックスを実行する必要があります。それ自体にリソースのオーバーヘッドがあります。

4.少なくとも1つのストアドプロシージャを作成し、を呼び出してデータを表示します

IDで学生名をクエリするストアドプロシージャを作成します

create procedure pro_select(in id vachar(5))
begin
	select name from student where ID = id;
end;

移行:call pro_select(10705);

5.少なくとも1つのトリガーを作成し、トリガーアプリケーションの結果を観察します

学生を削除した後、対応するコース選択情報を削除するインデクサーを作成します

create trigger trigger_delete after delete
on student for each row
begin
  delete from takes where takes.ID not in
  (select ID from student);
end;

実行delete from student where ID = '1000';トリガーが有効になります。

6. ERダイアグラムを使用して、大学のデータベーススキーマに対応する概念モデルを提供します

データベースの説明:データベース全体に11個のテーブルがあり、そのうちtime_slotは時間情報を記録し、他のテーブルとは関係ありません。学生テーブルとインストラクターはそれぞれ学生と教師の情報を記録し、コースはコース情報を記録します。これは、コース選択テーブルのテイクと教育テーブルの教えを通じて、それぞれ学生と教師のテーブルに関連付けられます。セクションはレコードセグメント化されたレッスンテーブルであり、教室、コース、部門のテーブルに関連付けられます。dapartmentテーブルには、学生、コース、および教師のテーブルに関連する情報も含まれています。さらに、前提条件テーブルprereqは、コースの前提条件コース情報を記録します。アドバイザーは、教師と学生の間のガイダンス関係を記録します。

まず、データベースツールを使用して関係図を生成します。

次に、それに応じてER図を描きます。

ER図を描く別の方法:

画像-20201015112405970

おすすめ

転載: blog.csdn.net/qq_40589204/article/details/118659412