日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

练习-----查询

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 练习-----查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

第一步:建表

 1 create table student   #學生表
 2 (
 3   Sno varchar(20) primary key,  #學號,主鍵
 4   Sname varchar(20) not null,    #學生姓名
 5   Ssex varchar(20) not null,     #學生性別
 6    Sbirthday datetime,       #學生出生日期
 7    Class varchar(20)     #學生所在班級
 8 );
 9 
10 create table Teacher     #教師表
11 (
12    Tno varchar(20) primary key ,    #教職工編號,主鍵
13     Tnam varchar(20) not null,   #教職工姓名
14     Tsex varchar(20) not null,  #教職工性別
15     Tbirthday datetime,    # 教職工出生年月
16      Prof varchar (20),      #職稱
17      Depart varchar(20)not null    #教職工所在部門
18 );
19 
20 create table Course   #課程表
21 (
22    Con varchar(20) primary key,  #課程號,主鍵
23     Cname varchar(20) not null,   #課程名稱
24     Tno varchar(20) not null,   #教職工
25     foreign key(Tno)references Teacher(Tno)    #教工編號,外鍵
26 ); 
27 #           從表的列名                      主表的列名
28 
29 
30 create table Score   #成績表
31 (
32    Sno varchar(20) not null,
33    foreign key(Sno) references Student(Sno) , #學號,外鍵
34    Con varchar(20) not null,  #組合主鍵  
35    Degree Decimal(4,1) ,      #Degree  成績   
36    foreign key(Con) references Course(Con) ,   #課程號,外鍵
37    primary key (Sno,Con)       #成績
38 );
39 #values  值的意思
40 
41 
42 
43 
44 #Student表
45 insert into Student values('108','曾華','','1977-09-01','95033');  
46 insert into Student values('105','匡明','','1975-10-02','95031');
47 insert into Student values('107','王麗','','1976-01-23','95033');
48 insert into Student values('101','李軍','','1976-02-20','95033');
49 insert into Student values('109','王芳','','1975-02-10','95031');
50 insert into Student values('103','陸君','','1974-06-03','95031');
51 
52 
53 
54 
55  #Teacher表
56 insert into Teacher values('804','李誠','','1958-12-02','副教授','計算機系');
57 insert into Teacher values('856','張旭','','1969-03-12','講師','電子工程系');
58 insert into Teacher values('825','王萍','','1972-05-05','助教','計算機系');
59 insert into Teacher values('831','劉冰','','1977-08-14','助教','電子工程系');
60 
61 
62 
63 #Course    課程表
64 insert into Course values('3-105','計算機導論','825');
65 insert into Course values('3-245','操作系統','804');
66 insert into Course values('6-166','數字電路','856');
67 insert into Course values('9-888','高等數學','831');
68  
69 
70 
71 #Score    成績表
72 insert into Score values('103','3-245','86');
73 insert into Score values('105','3-245','75');
74 insert into Score values('109','3-245','68');
75 insert into Score values('103','3-105','92');
76 insert into Score values('105','3-105','88');
77 insert into Score values('109','3-105','76');
78 insert into Score values('101','3-105','64');
79 insert into Score values('107','3-105','91');
80 insert into Score values('108','3-105','78');
81 insert into Score values('101','6-166','85');
82 insert into Score values('107','6-166','79');
83 insert into Score values('108','6-166','81');

?

?查詢題:

1、查詢Student表中的所有記錄的Sname、Ssex和Class列。

? ? ? #答題思路:
? ? ? #運用知識點:select 列名1,列名2,列名3... from 表名

 select Sname,Ssex,Class from Student

?

2、 查詢教師所有的單位即不重復的Depart列。

  ?#答題思路:
? ? ? ?#去重查詢:select distinct 列名 from ?表名 ? ? ? ? ?

? ? ? ?#查出Teacher表中的Depart列 ?加上distinct關鍵字就是去重復

 select distinct Depart from Teacher 

?

3、 查詢Student表的所有記錄。

    #答題思路:
? ? ? ?#select * from 表名 ? ? *整個標的所有列

select * from Student

?

4、 查詢Score表中成績在60到80之間的所有記錄。

? ? ? ?#答題思路:
? ? ? ?# 范圍查詢: select * from 表名 where Price between 范圍值 and 范圍值

select * from Score where Degree between 60 and 80

?

5、 查詢Score表中成績為85,86或88的記錄。

? ? ? ? #答題思路:
? ? ? ? #離散查詢:select * from 表名 where 要查的列名 in (要查的數值1,數值2,數值3)

select * from Score where Degree in (85,86,88)

?

6、 查詢Student表中“95031”班或性別為“女”的同學記錄。

    #答題思路:

    #所有的班級的學生
      select * from student
    #"95031”班的學生
      select * from student where class='95031'
    #找出95031”班的女學生 。用and關鍵字 來查找95031板的女生 。 就一個女學生。
      select * from student where class='95031' and Ssex='女'

 select * from Student where Class='95031' and Ssex='' 

?

?

7、 以Class降序查詢Student表的所有記錄。 ? ?

#desc降序 ? ?排序 order by查找

#select * from 表名 order by 列名 desc

select * from Student order by Class desc 

?

?

8、 以Cno升序、Degree降序查詢Score表的所有記錄。

    #答題思路:

? ? ? ? ? #在Score表,里查

? ? ? ? ?#根據兩個條件來排序。asc升序

? ? ? ? ?#兩個條件 :Cno升序、Degree降序 ? ?先升,后降

 select * from Score order by Cno asc , Degree desc     

?

9、 查詢“95031”班的學生人數。

  #答題思路:

? ? ? ?#count的意思是集合 count(*)的意思是括號里面的字段集合 select count(*) from的意思是 從某某表查詢出該字段的集合也就是有幾條記錄

? ? ? #{延伸知識:語句一:select count(*) from T;

? ? ? ? ? ? ? ? ? ? ? ??作用:查詢T表中記錄的行數。

? ? ? ? ? ? ? ? ? ? ? ??語句二:select * from T;
? ? ? ? ? ? ? ? ? ? ? ? ?作用:查詢T表中所有的記錄。

? ? ? ? ? ? ? ? ? ? ? ? 它倆的區別是:語句一查詢結果為一個數值,就是表中記錄條數;語句二查詢結果是表中所有數據,就是包括字段名字,字段里面的內容的詳細信息。}

?

select count(*) from Student where Class='95031'  

?

?

?

?10、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)

    #答題思路:

? ? ? ? ? ?#根據子查詢來查

    #先查score(成績)表中最高成績?

    #select max(degree) from Score #最大值(最高分)是92

    #再查這個最高分,的學生代號和課程號

    #select sno,cno from score where degree=92

? ? ? ? ? ?#內容延伸:{ ?格式:SELECT [謂語] [表別名] FROM 表名 [AS 表別名] [WHERE 邏輯表達式]它的功能是從一個或多個表中檢索數據簡單的說SELECT后面跟你想檢索的內容,可以的一個字段也可以是多個字段(中間用都好隔開),即可以是字段名也可以用函數(系統自定義的),也可以是一個 * 號,表示輸出表中所有的字段。FROM是檢索內容的來源,就是來自哪個或那些表,跟表的名稱;WHERE的作用是指定查詢條件,只把滿足邏輯表達式的數據作為查詢結果,它是可選項,可有可無.}?

?

 select Sno,Cno from Score where Degree= (select max(Degree) from Score)   

?

?

?11、 查詢每門課的平均成績。 #分組查詢 每門課 每門課的代號 平均成績

?    #擴展知識:group by 有一個原則,就是 select 后面的所有列中,沒有使用聚合函數的列,必須出現在 group by 后面。

?

select avg(Degree),Cno from Score group by Cno    

?

12、查詢Score表中至少有5名學生選修的并以3開頭的課程的平均分數。??

? ? ? #條件: 至少有5名學生選修,以3開頭的課程的 。 ?要在where里?

select avg(Degree) from Score where Cno = (select Cno from Score group by Cno having count(*)>4) and Cno like '3%' group by Cno

?

?

13、查詢分數大于70,小于90的Sno列。

  #BETWEEN 操作符在 WHERE 子句中使用,作用是選取介于兩個值之間的數據范圍。

  #操作符 BETWEEN ... AND 會選取介于兩個值之間的數據范圍。這些值可以是數值、文本或者日期。


  
#第一種方法:

select Sno from Score where Degree between 70 and 90

  #第二種方法:

?

select Sno from Score where Degree>=70 and Degree<=90

?

?

14、查詢所有學生的Sname、Cno和Degree列。

? ? ? ? #join用于根據兩個或多個表中的列之間的關系,從這些表中查詢數據。

? ? ? ?#{有時為了得到完整的結果,我們需要從兩個或更多的表中獲取結果。我們就需要執行 join。數據庫中的表可通過鍵將彼此聯系起來。主鍵(Primary Key)是一個列,在這個列中的每一行的值都是唯一的。在表中,每個主鍵的值都是唯一的。這樣做的目的是在不重復每個表中的所有數據的情況下,把表間的數據交叉捆綁在一起。}

?

  #分析:
  #Sname(學生姓名)列是Student表里的
  #Cno、Degree列是Score表里的
  #通過 Student里的Sno(主鍵)就可知道了 Score里的Sno(外鍵) Sno是有聯系的

?

  #把兩個表連接起來 連接兩張表的關鍵字join
  select * from Student join Score #查詢的結果形成了 形成笛卡爾積
  #我們要進行篩選,篩選出正確的來 join......on....它兩是一對 on的后面加條件
  select * from Student join Score on Student.Sno=Score.Sno

? ? ?#在加上要查的列,就可以了

select Sname,Cno,Degree from Score join Student on Student.Sno = Score.Sno

?

轉載于:https://www.cnblogs.com/yuyu1993/p/5537808.html

總結

以上是生活随笔為你收集整理的练习-----查询的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。