<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    想飛就別怕摔

    大爺的并TM罵人

    SQL語句整理[第二章](學習筆記)

     廢話少說用例子來說話。
    1.create database sqltest    //創建數據庫;

    2.create table students (sno numeric(6,0)not null,sname varchar(8)not null,age numeric(3,0),
    sex char(2),bplace varchar(20),primary key(sno))  
     //創建表students;

    3.create table courses (cno char(4)not null,cname varchar(10)not null,credit int)
    //創建表courses;

    4.alter table courses add primary key (cno)  
    //為courses表的cno字段添加主鍵約束;

    5.create table enrools(sno numeric(6,0)not null,cno char(4)not null,grade int,primary key(sno,cno),
    foreign key(sno)references students(sno),foreign key(cno) references courses(cno))   
    //創建表enrools,并添加主外鍵約束;

    6.select sno,sname,age into girl from students where sex='女'   
    //根據Student表創建gril表,字段為3個;

    7.create view faculty as select sno,age,sname from students   
    //根據Student表創建視圖;

    8.create view grade_table as select sname,cname,grade from students,enrools,courses where students.sno=enrools.sno and courses.cno=enrools.cno   
    //從Student、enrools、courses 表中產生一視圖,包括sname、cname、grade;

    9.drop view grade_table    
    //刪除視圖;

    10.select cno,cname from courses where credit=3  
    //在表courses中找出3個學分的cno,cname;

    11.select * from students where age >22    
    //在Student表中找出age大于22的學生狀況;

    12.select sname,age from students where sex='男' and bplace='北京'  
    //在Student表中找出北京籍男生的sname,age;

    13.select sno,sname,age from students age between 20 and 23 order by age   
     //找出年齡在20~23之間的學生的sno,sname,age;

    14.select sname,sex from students where age<23 and bplace in('北京','上海')   
     //找出年齡小與23北京上海的學生的sname,sex;

    15.select * from students where sname like '張%'   
     //找出姓張同學的狀況;

    16.select sname from students where  sno=(select sno from enrools where  grade=95)   
     //找出學分為95分的同學姓名,只限查找一條記錄(子查詢);

    17.select sname from students where sno=any(select sno from enrools where grade>90)
    或者select sname from students where sno in(select sno from enrools where grade>90)    
    //找出學分為90分的同學姓名,查找多條記錄(子查詢);

    18.select sname,cno,grade from students,enrools where students.sno=enrools.sno   
    //查找全部學生的姓名,課程號,成績;(連接查詢);


    19.select sname,bplace,grade from student,enrools where bplace in ('北京','上海') and grade>90 and students.sno=enrools.sno
    //找出籍貫是北京或上海,成績在90分以上的學生sname,bpalce,grade;

    20.select min(age) from students    
    //找出年齡最小的學生;

    21.select count(*) from students where age<=22   
     //找出年齡小于等于22的學生人數;

    22.select avg(grade),courses=count(*) from enrools group by son   
     //找出學生的平均成績和課程門數;

    23.select sname from students where age>(select avg(age) from students)    
    //找出年齡大于平均年齡的學生名字

    24.select con, avg(grade), students=count(*)  from enrools group by con ,having count(*)>=3    
    //找出個課程的平均成績,按課程號分組,且只選擇課程號超過3個的成績;(GROUP BY子句把一個表按某一指定列(或一些列)上的值相等的原則分組,然后再對每組。GROUP BY 子句總是跟在 Where 子句后面,當 Where 子句缺省時,它跟在 FROM 子句后面HAVING 子句常用于在計算出聚集之后對行的查詢進行控制。

    25.select sname,sno from students where not exists (select * from enrools where students.sno=enrools.sno)    
    //查詢沒有任何課程學生的學號和姓名(
    當一個子查詢涉及到一個來自外部查詢的列時,稱為相關子查詢correlated Subquery 。相關子查詢要用到存在測試謂詞EXISTS 和 NOT EXISTS 及 ALL 、 ANY ( SOME )等。);

    26.select * from students where bplace='北京' natural join (select * from enrools where grade>=80) 
    //查詢籍貫是北京,成績在80分以上的學生信息;

    27.inster into students values (009901,'張三',23,'男','北京')    
    //插入學生信息;

    28.inster into teachers (tno,tname) select distinct sno,sname from students,enrools where students.sno=enrools .sno and grade>90   
     // 把成績大于90的同學加入到教師的表中,留校。

    29.update students set age=age+1  
    //所有學生年齡加1;

    30.update enrolls set grade=0 where con='c1' and '張三'=(select sname from students where students.sno=enrools.sno)   
     //把張三的成績改為0;

    31.delete from students where age>30   
     //刪除年齡大于30的學生資料;







     

    posted on 2008-09-25 15:31 生命的綻放 閱讀(404) 評論(0)  編輯  收藏 所屬分類: 數據庫

    <2008年9月>
    31123456
    78910111213
    14151617181920
    21222324252627
    2829301234
    567891011

    導航

    統計

    常用鏈接

    留言簿(5)

    隨筆分類(94)

    隨筆檔案(93)

    文章分類(5)

    文章檔案(5)

    相冊

    JAVA之橋

    SQL之音

    兄弟之窗

    常用工具下載

    積分與排名

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 亚洲av永久综合在线观看尤物| 亚洲精品第一国产综合亚AV| 亚洲高清专区日韩精品| 亚洲欧洲自拍拍偷综合| 青草青草视频2免费观看| 亚洲一区免费观看| 亚洲成A∨人片天堂网无码| 亚洲欧洲国产精品你懂的| 国产精品亚洲专一区二区三区| 无码av免费网站| 亚洲AV无码久久精品蜜桃| 亚洲AV性色在线观看| 91精品视频在线免费观看| 亚洲午夜无码AV毛片久久| 亚洲中文字幕久久精品无码VA | 亚洲综合色区在线观看| 免费人成又黄又爽的视频在线电影| 91精品成人免费国产片| 国产亚洲婷婷香蕉久久精品| 朝桐光亚洲专区在线中文字幕| 国产免费不卡视频| 午夜影视日本亚洲欧洲精品一区| 青青草97国产精品免费观看| 中文字幕专区在线亚洲| 亚洲av综合日韩| 国内精品久久久久久久亚洲| 色屁屁www影院免费观看视频| 亚洲偷自拍拍综合网| 久久国产免费观看精品| 亚洲精品乱码久久久久久久久久久久 | 国产精品免费视频一区| 亚洲国产成人九九综合| 亚洲一区二区在线免费观看| 亚洲免费电影网站| 国产真人无遮挡作爱免费视频| caoporn国产精品免费| 亚洲成年看片在线观看| 成人影片一区免费观看| 亚洲av一综合av一区| 手机在线看永久av片免费| 亚洲第一成人在线|