1. 程式人生 > 資料庫 >SQL基礎語句

SQL基礎語句

不區分大小寫,但建議關鍵字大寫;語句最好用分號結尾;根據需要進行縮排換行註釋:
* 單行註釋 #或者-- 註釋
* 多行註釋 /* 註釋 */

1、檢視當前所有的資料庫show databases;2、開啟指定的庫use 庫名3、檢視當前庫的所有表show tables;4、檢視其它庫的所有表show tables from 庫名;5、建立一個表create table 表名( 列名 型別 id int, name varchar(20));6、查看錶結構desc 表名;7、檢視資料庫版本select version();---------(表操作)建表:重點:
* create table 表名(

            列名  型別,                列名  型別            );
* 新增主鍵:primary key (列名),
* 新增外來鍵:foreign key (列名) references 表名(列名),
* 新增外來鍵等操作時,使用到的列名要加括號!

刪表:重點:
* drop table 表名;

改表:重點:
* 在表內新增欄位: alter table 表名 add 列名(屬性);
* 在表內刪除欄位: alter table 表名 drop 列名(屬性);

查表:重點:
* select * from 表名;

---------(表內容操作)*****重點!!!!新增:重點:

* insert into 表名 (屬性,屬性) value (內容,內容);------------------屬性如不新增,則預設要按順序新增所有的內容
* insert into major(mno,mname) value(2,'軟體工程');

刪除:重點:
* delete from 表名 where 屬性=值;------------------如不新增限制條件,則預設刪除所有
* delete from stu where sno=1;

修改:重點:
* updata 表名 set 屬性=值 where 屬性=值'
* update stu set sname="張錦波" where sex="男";

查詢:基礎:
* select

單表查詢: 基礎查詢方法 distinct去重 like模糊搜尋 in(條件1,條件2)在條件範圍內
* select * from sc;
* -- 1、查詢全部學生的全部資訊
* -- 2、查詢全部學生的學號sno和姓名sname
* -- 3、查詢全部學生的姓名和出生年份
* -- 4、查詢有選修課程的學生學號,需要去重
* -- 5、查詢姓名是‘小十’的學生的全部資訊
* -- 6、查詢課號是20201且學生成績高於80的學生學號
* -- 7、查詢年齡在18-19之間的學生姓名
* -- 8、查詢專業號為01,02,04的學生資訊
* -- 9、查詢專業號不是01,02,04的學生資訊
* -- 10、查詢所有姓彭的學生資訊
* -- 11、查詢名字帶有"小"字的學生資訊
* -- 12、查詢名字中第二字為"小"的學生資訊
* -- 13、查詢有選課記錄但沒有考試成績的選課資訊

答案:
1. select * from stu;
2. select sno,sname from stu;
3. select sname,2020-age as birth_date from stu;
4. select distinct sno from sc;
5. select * from stu where sname like "小十";
6. select sno from sc where cno=20201 && grade>80;
7. select sname,age from stu where age between 18 and 19;select sname,age from stu where age>=18 && age<=19;
8. select * from stu where mno in(01,02,04);
9. select * from stu where mno not in(01,02,04);
10. select * from stu where sname like "彭%";
11. select * from stu where sname like "%小%";
12. select * from stu where sname like "_小%";
13. select * from sc where grade is null;

        order by----聚集函式-----group by                                       從低到高 :order by根據條件                                     從高到低: order by 根據條件 desc                                   查詢表列數:count(*)                                  查詢某記錄數:count(條件)                                                                      count統計的是條件中不為空的值的數量                                where是分組前過濾資料,不可包含聚集函式                                having是分組後過濾資料,經常包含聚集函式        order by排序
* -- 1、查詢學生成績,由低到高
* -- 2、查詢學生成績,由高到低

    count()統計
* -- 3、查詢學生總人數
* -- 4、查詢選修了課程的學生人數

    AVG()平均    MAX()最高    MIN()最低
* -- 5、查詢選修20201課程的學生平均成績
* -- 6、查詢選修20201課程的學生最高成績

    group by根據欄位進行分組----------不需要特別加入distinct去重
* -- 7、求各個課程號以及相應的選修人數
* -- 8、查詢平均成績大於等於90的學生學號和平均成績

答案:
1. select * from sc order by grade;
2. select * from sc order by grade desc;
3. select count(*) from stu;
4. select count(distinct sno) from stu;
5. select AVG(grade) from sc where cno=20201;
6. select MAX(grade) from sc where cno=20201;
7. select cno,count(sno) from sc group by cno;
8. select sno,AVG(grade) from sc group by sno having AVG(grade)>=90;

多表查詢: left join(左聯接) 返回包括左表中的所有記錄和右表中聯結欄位相等的記錄   right join(右聯接) 返回包括右表中的所有記錄和左表中聯結欄位相等的記錄  inner join(等值連線) 只返回兩個表中聯結欄位相等的行 普通多表 from多表,在where之後新增表關聯
* -- 1、查詢選修了20201課程的學生名字
* -- 2、查詢每個學生的資訊和選修課程的資訊與學時

        左外連結        from 主表 left outer join 從表 on 表關聯
* -- 3、查詢所有學生的資訊和選課資訊,沒有選修的學生也要顯示出來
* -- 4、查詢每個專業的學生人數,假設每個專業都有人(不需要使用連結,使用group by進行分組即可)
* -- 5、查詢每個專業的學生人數,有的專業可能沒有人(在保證所有專業都在列表的前提下,就需要用到左外連線)

答案:
1. select sname from stu,sc where stu.sno=sc.sno and sc.cno=20201;
2. select stu.,sc.,ctime from stu,sc,cou where stu.sno=sc.sno and sc.cno=cou.cno;
3. select stu.,sc. from stu left outer join sc on stu.sno=sc.sno;
4. select mno,count(sno) from stu group by mno having mno between 1 and 4;
5. select major.mno,count(stu.sno) from major left outer join stu on major.mno=stu.mno group by major.mno;

巢狀查詢: 不相關巢狀查詢 子查詢不依賴父查詢 型別(兩個不同表):父查詢輸出資訊,子查詢進行判斷,兩者各司其職
* -- 1、查詢選修了20201課程的學生名字

        相關巢狀查詢        子查詢依賴父查詢        型別(兩個不同表):父查詢規範限制條件,子查詢輸出對應集合進行匹配,子查詢需要與父查詢建立聯絡        型別(單表雙查詢):通過別名來區分同一個表,查詢邏輯與上面類似        型別(派生表查詢):通過派生表來進行基本的多表查詢操作        型別(exists查詢):通過exists字句對條件進行判斷,在內部需要建立聯絡,會根據條件是否吻合對應所建立的聯絡輸出true或false
* -- 2、查詢選修了20201課程的學生名字
* -- 3、查詢選擇了C語言課程的學生學號
* -- 4、查詢每個學生超過他平均分的課程號
* -- 5、查詢每個學生超過他平均分的課程號(派生表實現)
* -- 6、查詢選修了20201課程的學生名字,帶有exists

答案:
1. select sname from stu where sno in (select sno from sc where cno=20201);
2. select sname from stu where 20201 in (select cno from sc where sc.sno=stu.sno);
3. select sno from sc where "C語言" in (select cname from cou where cou.cno=sc.cno);
4. select sno,cno from sc x where grade>(select AVG(grade) from sc y group by sno having x.sno=y.sno);
5. select sno,cno from sc,(select sno,AVG(grade) from sc group by sno) as avg_sc(avg_sno,avg_grade) where sc.sno=avg_sc.avg_sno and sc.grade>avg_sc.avg_grade;
6. select sname from stu where exists(select * from sc where cno=20201 and stu.sno=sc.sno);

                    集合操作:union並操作、intersect交操作、except差操作    是SQL server 的語法 
* -- 1、查詢年齡是18且mno=1的學生學號        union並操作
* -- 2、查詢年齡是18且mno=1的學生學號        except差操作
* -- 3、查詢選修‘20201’號課程且‘20203’的學生學號        intersect交操作

答案:
1. select sno from stu where age=18 union select sno from stu where mno=1;
2. select sno from stu where age=18 except select sno from stu where mno=1;
3. select sno from sc where cno=20201 intersect select sno from sc where cno=20203;

-- major-- mno mnamecreate table major ( mno int, mname varchar(20), primary key (mno));-- stu-- sno sname age sex mnocreate table stu ( sno int, sname varchar(20), age smallint, sex varchar(2), mno int, primary key(sno), foreign key (mno) references major(mno));-- cou-- cno cname ctime ccreditcreate table cou ( cno int, cname varchar(20), ctime smallint, ccredit decimal(4,2),-- 四位,兩個小數點 primary key (cno));-- sc-- sno cno gradecreate table sc( sno int, cno int, grade decimal(5,2), primary key (sno,cno), foreign key (sno) references stu(sno));-- 外來鍵補充alter table sc add constraint fk_sc foreign key(cno) references cou(cno);