MySQL實驗,數據庫原理實驗,插入數據隨機生成器

文章目錄

MySQL實驗

實驗一:利用SQL語言完成數據庫定義

建立數據庫結構,完成數據庫中的基本表的建立,同時將表中數據進行添加。

實驗題目1:建立數據庫結構,完成數據庫中的基本表的建立

實驗代碼及註釋:

建立Student表
在這裏插入圖片描述
Course表之前已建立

建立Sc表
在這裏插入圖片描述

實驗題目2:修改表結構

實驗代碼及註釋:

Student表增加新的屬性s_entrance

Sc表grade屬性的類型改爲int
在這裏插入圖片描述
刪除student表中s_entrance屬性
在這裏插入圖片描述

實驗題目3:插入數據

實驗代碼及註釋:

Student表插入數據(中間錯誤是因爲忘更改學號了)
在這裏插入圖片描述
Course表插入數據
在這裏插入圖片描述
Sc表插入數據
在這裏插入圖片描述

mysql> insert sc(sno,cno,grade) values('201215121','7',50);

mysql> insert sc(sno,cno,grade) values('201215121','4',45);

mysql> insert sc(sno,cno,grade) values('201215121','6',55);

實驗二:利用SQL語言完成數據庫維護

對於已完成數據庫中數據進行修改、刪除操作。

實驗題目1:修改“CS”的學生成績,不及格學生的成績增加5分。

實驗代碼及註釋:

mysql> update sc

  	-> set grade=grade+5

  	-> where 'cs'=

  	-> (select sdept

  	-> from student

  	-> where student.sno=sc.sno and sc.grade<60 );

實驗題目2:刪除“CS”系學生的成績記錄。(不小心寫成CS的了。。。)

實驗代碼及註釋:

mysql> delete

  	-> from sc

  	-> where 'CS'=

  	-> (select sdept

  	-> from student

  	-> where student.sno=sc.sno);

自造題目3:刪除所有2號課程的成績

實驗代碼及註釋:

mysql> delete

    -> from sc

    -> where cno=2;

自造題目4:選修cno=6的課程的成績中高於90分的都扣5分。

實驗代碼及註釋:

mysql> update sc

    -> set grade=grade-10

    -> where grade>90 and cno=6;

實驗三:利用SQL語句完成數據庫查詢

採用查詢語句對於數據庫中數據進行查詢或統計功能的操作。

實驗題目1:查詢平均成績大於80的學生姓名。

實驗代碼及註釋:
在這裏插入圖片描述

實驗題目2:查詢課程成績大於課程平均成績的選課信息,顯示學生姓名、課程名稱和成績。

實驗代碼及註釋:

mysql> select sname,cname,grade

    -> from student,sc,course

    -> where student.sno=sc.sno

    -> and course.cno=sc.cno

    -> and grade>

    -> (select avg(grade)

    -> from sc

    -> where course.cno=cno);

實驗題目3:查詢至少選修了C1和C2課程的學生名單。

實驗代碼及註釋:

mysql> select sname

    -> from student

    -> where exists

    -> (select *

    -> from sc

    -> where sno=student.sno and cno=1)

    -> and exists

    -> (select *

    -> from sc

    -> where sno=student.sno and cno=2);

實驗題目4:查詢選修了C1課程而沒有選修C2課程的學生名單。

實驗代碼及註釋:

mysql> select sname

    -> from student

    -> where exists

    -> (select * from sc where student.sno=sno and cno=1)

    -> and not exists

    -> (select * from sc where student.sno=sno and cno=2);

實驗題目5:統計每門課程成績大於80分的學生數。

實驗代碼及註釋:

mysql> select cno,count(sno)

    -> from sc

    -> where grade>80

    -> group by cno;

實驗題目6:統計計算機系CS學生的平均分

實驗代碼及註釋:

mysql> select avg(grade)

    -> from sc

    -> where sno in

    -> (select sno

    -> from student

    -> where sdept='CS');

實驗題目7:統計至少選修了兩門課程的學生數

實驗代碼及註釋:

mysql> select count(sname)

    -> from student

    -> where exists

    -> (select *

    -> from sc

    -> where student.sno=sno

    -> group by sno

    -> having count(cno)>2);

實驗題目8:查詢至少選修了兩門課程的學生名單

實驗代碼及註釋:

mysql> select sname

    -> from student

    -> where exists

    -> (select *

    -> from sc

    -> where student.sno=sno

    -> group by sno

    -> having count(cno)>=2);

實驗題目9:查詢沒有被選修的課程信息

實驗代碼及註釋

mysql> select cno,cname,cpno,ccredit

    -> from course

    -> where not exists

    -> (select *

    -> from sc

    -> where course.cno=cno);

實驗題目10:查詢沒有選修C1課程的學生信息

實驗代碼及註釋:

mysql> select *

    -> from student

    -> where not exists

    -> (select *

    -> from sc

    -> where cno=1 and sno=student.sno);

實驗題目11:統計沒有選修C1課程的學生人數

實驗代碼及註釋:

mysql> select count(sno)

    -> from student

    -> where not exists

    -> (select *

    -> from sc

    -> where cno=1 and sno=student.sno);

實驗題目12:查詢平均分最高的課程信息

實驗代碼及註釋:

mysql> select *

    -> from course

    -> where cno=
  
    -> (select cno

    -> from sc

    -> group by cno

    -> having avg(grade)>= all

    -> (select avg(grade)

    -> from sc

    -> group by cno));

實驗題目13:查詢平均分最高的課程的選課信息(學號,姓名,課程名程,成績)

實驗代碼及註釋:

mysql> select sc.sno,sname,cname,grade

    -> from sc,student,course

    -> where sc.sno=student.sno

    -> and course.cno=sc.cno

    -> and sc.cno=7;

這個課程號是我上一問求出來的,大家根據自己的數據課程號可能不同。我只是懶得上面再重複一遍了。

實驗題目14:查詢平均分最高的學生所在院系。

實驗代碼及註釋:

mysql> select sdept

    -> from student

    -> where sno=

    -> (select sno

    -> from sc

    -> group by sno

    -> having avg(grade)>= all

    -> (select avg(grade)

    -> from sc

    -> group by sno));

實驗題目15:統計學生平均選修課程數。

實驗代碼及註釋:

mysql> select count(*)/count(distinct sno) from sc;

實驗題目16:統計各院系學生平均選課數。

實驗代碼及註釋:

mysql> select sdept,count(*)/count(distinct student.sno)

    -> from student,sc

    -> where student.sno=sc.sno

    -> group by sdept;

實驗題目17:統計每門課程的選課人數,最高分,平均分和最低分。

實驗代碼及註釋:

mysql> select cno,count(*),max(grade),avg(grade),min(grade)

    -> from sc

    -> group by cno;

實驗題目18:查詢平均分75以上,並且沒有不及格成績的學生信息。

實驗代碼及註釋:

mysql> select *

    -> from student

    -> where sno in
 
    -> (select sno

    -> from sc

    -> group by sno

    -> having avg(grade)>75

    -> and min(grade)>60);

實驗四:利用SQL語句完成視圖設計

對於已有數據庫進行建立相應視圖,同時在視圖上進行相應的查詢、修改、刪除等操作

實驗題目1:查詢沒有選修C1課程的學生信息

實驗代碼及註釋:

創建視圖:
mysql> create view not_c1
    -> as
    -> select *
    -> from student
    -> where not exists
    -> (select *
    -> from sc
    -> where cno=1 and sno=student.sno);
查詢視圖:
mysql> select sname
    -> from not_c1
    -> where sage=18;
修改視圖:
mysql> update not_c1
    -> set sage=sage+1
    -> where sdept='MA';
刪除視圖:
mysql> drop view not_c1;

實驗題目2:查詢平均分75以上,並且沒有不及格成績的學生信息。

實驗代碼及註釋:

創建視圖
mysql> create view good_stu
    -> as
    -> select *
    -> from student
    -> where sno in
    -> (select sno
    -> from sc
    -> group by sno
    -> having avg(grade)>75
    -> and min(grade)>60);
插入新的數據
mysql> insert sc(sno,cno,grade) values('201215127','7',100);
視圖自動更新
mysql> select * from good_stu;
查詢視圖:
mysql> select *
    -> from good_stu
    -> where sage<20;
刪除視圖:
mysql> drop view good_stu;

實驗題目3:統計每門課程的選課人數,最高分,平均分和最低分。

實驗代碼及註釋:

創建視圖:
mysql> create view final(cno,countt,maxx,avgg,minn)
    -> as
    -> select cno,count(*),max(grade),avg(grade),min(grade)
    -> from sc
-> group by cno;
該視圖不可更改!
查詢視圖:
mysql> select *
    -> from final
    -> where countt>=4;

實驗題目4:查詢選修了C1課程而沒有選修C2課程的學生名單。

實驗代碼及註釋:

創建視圖:
mysql> create view c2_yes
    -> as
    -> select *
    -> from student
    -> where exists
    -> (select * from sc where student.sno=sno and cno=1)
    -> and not exists
-> (select * from sc where student.sno=sno and cno=2);
更新視圖:
刪除所有選修2的記錄。
mysql> delete
    -> from sc
    -> where cno=2;

插入數據隨機生成器

生成sc表插入數據,輸出格式是可以直接用MySQL運行的。

生成一個在本cpp同目錄下的sctxt.out文件,用記事本打開即可。

生成數據隨機,可能不符合範式,可能不均勻,自行更改。

#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
typedef long long ll;
int main()
{
    freopen("sctxt.out","w",stdout);
    srand((int)time(0));
    for(int i=1;i<=20;i++) 
    {
        int sno=rand()%6,cno=rand()%8,grade=rand()%100;//學號末尾1~6,課程號1~7,成績百分制,可自行更改
        if(sno==0) sno=1;
        if(cno==0) cno=1; 
        cout<<"insert sc(sno,cno,grade) values(\'20121512"<<sno<<"\',"<<cno<<","<<grade<<");"<<endl;
        
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章