第一篇:簡(jiǎn)單SQL語(yǔ)句小結(jié)
簡(jiǎn)單SQL語(yǔ)句小結(jié)
注釋:本文假定已經(jīng)建立了一個(gè)學(xué)生成績(jī)管理數(shù)據(jù)庫(kù),全文均以學(xué)生成績(jī)的管理為例來描述。1.在查詢結(jié)果中顯示列名:
a.用as關(guān)鍵字:select name as '姓名' from students order by age b.直接表示:select name '姓名' from students order by age 2.精確查找: a.用in限定范圍:select *
from students
where native in('湖南', '四川')b.between...and:select * from students where age between 20 and 30 c.“=”:select * from students where name = '李山' c.like: select * from students where name like '李%'(注意查詢條件中有“%”,則說明是部分匹配,而且還有先后信息在里面,即查找以“李”開頭的匹配項(xiàng)。所以若查詢有“李”的所有對(duì)象,應(yīng)該命令:'%李%';若是第二個(gè)字為李,則應(yīng)為'_李%'或'_李'或'_李_'。)e.[] 匹配檢查符:select * from courses where cno like '[AC]%'(表示或的關(guān)系,與“in(...)”類似,而且“[]”可以表示范圍,如:select * from courses where cno like '[A-C]%')3.對(duì)于時(shí)間類型變量的處理
a.smalldatetime:直接按照字符串處理的方式進(jìn)行處理,例如:
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'
4.集函數(shù)
a.count()求和,如:select count(*)
from students(求學(xué)生總?cè)藬?shù))b.avg(列)求平均,如:select avg(mark)
from grades
where cno=’B2’
c.max(列)和min(列),求最大與最小 5.分組group:常用于統(tǒng)計(jì)時(shí),如分組查總數(shù):
select gender,count(sno)from students group by gender(查看男女學(xué)生各有多少)
注意:從哪種角度分組就從哪列“group by”
對(duì)于多重分組,只需將分組規(guī)則羅列。比如查詢各屆各專業(yè)的男女同學(xué)人數(shù),那么分組規(guī)則有:屆別(grade)、專業(yè)(mno)和性別(gender),所以有“group by grade, mno, gender” select grade, mno, gender, count(*)from students group by grade, mno, gender
通常group還和having聯(lián)用,比如查詢1門課以上不及格的學(xué)生,則按學(xué)號(hào)(sno)分類有:
select sno,count(*)from grades where mark<60 group by sno having count(*)>1
6.UNION聯(lián)合
合并查詢結(jié)果,如:
SELECT * FROM students WHERE name like ‘張%’
UNION [ALL] SELECT * FROM students WHERE name like ‘李%’
7.多表查詢
a.內(nèi)連接
select g.sno,s.name,c.coursename from grades g JOIN students s ON g.sno=s.sno JOIN courses c ON g.cno=c.cno(注意可以引用別名)
b.外連接
b1.左連接
select courses.cno,max(coursename),count(sno)from courses LEFT JOIN grades ON courses.cno=grades.cno group by courses.cno
左連接特點(diǎn):顯示全部左邊表中的所有項(xiàng)目,即使其中有些項(xiàng)中的數(shù)據(jù)未填寫完全。
左外連接返回那些存在于左表而右表中卻沒有的行,再加上內(nèi)連接的行。
b2.右連接
與左連接類似
b3.全連接
select sno,name,major from students FULL JOIN majors ON students.mno=majors.mno
兩邊表中的內(nèi)容全部顯示
c.自身連接
select c1.cno,c1.coursename,c1.pno,c2.coursename from courses c1,courses c2 where c1.pno=c2.cno
采用別名解決問題。
d.交叉連接
select lastname firstname from lastname CROSS JOIN firstanme
相當(dāng)于做笛卡兒積
8.嵌套查詢
a.用關(guān)鍵字IN,如查詢李山的同鄉(xiāng):
select * from students where native in(select native from students where name=’ 李山’)
b.使用關(guān)鍵字EXIST,比如,下面兩句是等價(jià)的:
select * from students where sno in(select sno from grades where cno=’B2’)
select * from students where exists(select * from grades where grades.sno=students.sno AND cno=’B2’)
9.關(guān)于排序order
a.對(duì)于排序order,有兩種方法:asc升序和desc降序
b.對(duì)于排序order,可以按照查詢條件中的某項(xiàng)排列,而且這項(xiàng)可用數(shù)字表示,如:
select sno,count(*),avg(mark)from grades group by sno having avg(mark)>85 order by 3 10.其他
a.對(duì)于有空格的識(shí)別名稱,應(yīng)該用“[]”括住。
b.對(duì)于某列中沒有數(shù)據(jù)的特定查詢可以用null判斷,如: select sno, courseno from grades where mark IS NULL
c.注意區(qū)分在嵌套查詢中使用的any與all的區(qū)別,any相當(dāng)于邏輯運(yùn)算“||”而all則相當(dāng)于邏輯運(yùn)算“&&”
d.注意在做否定意義的查詢是小心進(jìn)入陷阱:
如,沒有選修‘B2’課程的學(xué)生 :
select students.* from students, grades where students.sno=grades.sno AND grades.cno <> ’B2’
上面的查詢方式是錯(cuò)誤的,正確方式見下方:
select * from students where not exists(select * from grades where grades.sno=students.sno AND cno='B2')
11.關(guān)于有難度多重嵌套查詢的解決思想:
如,選修了全部課程的學(xué)生:
select * from students where not exists(select * from courses where NOT EXISTS(select * from grades where sno=students.sno AND cno=courses.cno))
最外一重:從學(xué)生表中選,排除那些有課沒選的。用not exist。由于討論對(duì)象是課程,所以第二重查詢從course表中找,排除那些選了課的即可。
第二篇:sql常用語(yǔ)句
//創(chuàng)建臨時(shí)表空間
create temporary tablespace test_temp
tempfile 'E:oracleproduct10.2.0oradatatestservertest_temp01.dbf'size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
//創(chuàng)建數(shù)據(jù)表空間
create tablespace test_data
logging
datafile 'E:oracleproduct10.2.0oradatatestservertest_data01.dbf'size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
//創(chuàng)建用戶并指定表空間
create user username identified by password
default tablespace test_data
temporary tablespace test_temp;
//給用戶授予權(quán)限
//一般用戶
grant connect,resource to username;
//系統(tǒng)權(quán)限
grant connect,dba,resource to username
//創(chuàng)建用戶
create user user01 identified by u01
//建表
create table test7272(id number(10),name varchar2(20),age number(4),joindate date default sysdate,primary key(id));
//存儲(chǔ)過程
//數(shù)據(jù)庫(kù)連接池
數(shù)據(jù)庫(kù)連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫(kù)連接
//
//創(chuàng)建表空間
create tablespace thirdspace
datafile 'C:/Program Files/Oracle/thirdspace.dbf' size 10mautoextend on;
//創(chuàng)建用戶
create user binbin
identified by binbin
default tablespace firstspace
temporary tablespace temp;
//賦予權(quán)限
GRANT CONNECT, SYSDBA, RESOURCE to binbin
//null與""的區(qū)別
簡(jiǎn)單點(diǎn)說null表示還沒new出對(duì)象,就是還沒開辟空間
個(gè)對(duì)象裝的是空字符串。
//建視圖
create view viewname
as
sql
//建索引
create index indexname on tablename(columnname)
//在表中增加一列
alter table tablename add columnname columntype
//刪除一列
alter table tablename drop columnname
//刪除表格內(nèi)容,表格結(jié)構(gòu)不變
truncate table tableneme
//新增數(shù)據(jù)
insert into tablename()values()
//直接新增多條數(shù)據(jù)
insert into tablename()
selecte a,b,c
from tableabc
//更新數(shù)據(jù) new除了對(duì)象,但是這“”表示
update tablename set columnname=? where
//刪除數(shù)據(jù)
delete from tablename
where
//union語(yǔ)句
sql
union
sql
//case
case
when then
else
end
第三篇:SQL語(yǔ)句
SQL練習(xí)
一、設(shè)有如下的關(guān)系模式,試用SQL語(yǔ)句完成以下操作:
學(xué)生(學(xué)號(hào),姓名,性別,年齡,所在系)
課程(課程號(hào),課程名,學(xué)分,學(xué)期,學(xué)時(shí))
選課(學(xué)號(hào),課程號(hào),成績(jī))
1. 求選修了課程號(hào)為“C2”課的學(xué)生學(xué)號(hào)
2. 求選修了課程號(hào)為“C2”課的學(xué)生學(xué)號(hào)和姓名
3. 求沒有選修課程號(hào)為“C2”課的學(xué)生學(xué)號(hào)
4. 求選修了課程號(hào)為“C2”,又選修了課程號(hào)為“C3”課的學(xué)生學(xué)號(hào)
5.求選修了課程號(hào)為“C2”或“C3”課的學(xué)生學(xué)號(hào)
6.求選修了全部課程的學(xué)生學(xué)號(hào)
7.求至少選修了學(xué)號(hào)為“98002”的學(xué)生所學(xué)過的所有課程的學(xué)生的學(xué)號(hào)和姓名。
8.查詢學(xué)生選課表中的全部數(shù)據(jù)
9.查詢計(jì)算機(jī)系學(xué)生的姓名、年齡
10.查詢成績(jī)?cè)?0—80分之間的學(xué)生的學(xué)號(hào)、課程號(hào)和成績(jī)
11.查詢計(jì)算機(jī)系年齡在18—20之間且性別為“男”的學(xué)生的姓名和年齡
12.查詢成績(jī)?cè)?0分以上的學(xué)生的姓名、課程號(hào)和成績(jī),并按成績(jī)的降序排列結(jié)果。
13.查詢哪些課程沒有人選修,要求列出課程號(hào)和課程名。
14.查詢數(shù)學(xué)系成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào),姓名
15.查詢課程號(hào)為“C02”的課程的最高分?jǐn)?shù)。
16.查詢計(jì)算機(jī)系學(xué)生的最大年齡和最小年齡。
17.統(tǒng)計(jì)每個(gè)系的學(xué)生人數(shù)。
18.統(tǒng)計(jì)每門課程的選課人數(shù)和考試最高分。
19.統(tǒng)計(jì)每個(gè)學(xué)生的選課門數(shù)和考試總成績(jī),并按選課門數(shù)的升序顯示結(jié)果。
20.查詢總成績(jī)超過200分的學(xué)生,要求列出學(xué)號(hào)、總成績(jī)。
21.用子查詢實(shí)現(xiàn)如下查詢:
(1)查詢選修了課程號(hào)“C01”的學(xué)生的姓名和所在系。
(2)查詢數(shù)學(xué)系成績(jī)?cè)?0分以上的學(xué)生的學(xué)號(hào)和姓名。
(3)查詢計(jì)算機(jī)系考試成績(jī)最高的學(xué)生的姓名。
22.刪除選課成績(jī)小于60分的學(xué)生的選課記錄。
23.將所有選修了課程“C01”的學(xué)生的成績(jī)加10分。
24.將計(jì)算機(jī)系所有選修了課程“計(jì)算機(jī)文化基礎(chǔ)”課程的學(xué)生的成績(jī)加10分。
25.創(chuàng)建查詢學(xué)生的學(xué)號(hào)、姓名、所在系、課程號(hào)、課程名、課程學(xué)分的視圖。
26.創(chuàng)建查詢每個(gè)學(xué)生的平均成績(jī)的視圖,要求列出學(xué)生學(xué)號(hào)和平均成績(jī)。
27.創(chuàng)建查詢每個(gè)學(xué)生的選課學(xué)分的視圖,要求列出學(xué)生學(xué)號(hào)及總學(xué)分。
第四篇:SQL語(yǔ)句
SQL語(yǔ)句,用友的SQL2000,通過查詢管理器寫的語(yǔ)句
1、查詢
2、修改
3、刪除
4、插入
表名:users 包含字段:id,sname,sage
查詢 select * from users查詢users表中所有數(shù)據(jù)
select id,sname,sage from users查詢users表中id,sname,sage字段所有數(shù)據(jù)
可以限定查詢條件比如:
select sname from users where sage>20查詢年齡大于20歲的所有人的名字
修改 update users set sname='李四',sage=22將users表中數(shù)據(jù)都改為姓名李四,年齡22
update users set sname='李四',sage=22 where id=1限定id為1的人的信息修改為
姓名李四,年齡22
可以加where條件。
刪除 delete from users where id=2刪除users表中id為2的一行數(shù)據(jù)delete from users 代表刪除users中所有數(shù)據(jù)
插入 insert into users(id,sname,sage)values(5,'劉三',21)插入一條數(shù)據(jù)
SQL四條最基本的數(shù)據(jù)操作語(yǔ)句:Insert,Select,Update和Delete。
例如:SELECT columns FROM tables;其中columns為列的名稱,tables為表格名稱
1、查詢:select 字段名 from 表名 where 條件
查找在表(如A)中符合條件的字段
2、修改:update 表名 set 字段名=‘所要修改的內(nèi)容’
修改在表(如A)中的字段的值為:所要修改的內(nèi)容
3、刪除: delete from 表名 where 條件
刪除符合條件的表(如A)中的信息
4、插入: insert into 表名(字段名)(‘插入內(nèi)容’)
在表(如A)中插入字段為:插入內(nèi)容 的一條信息
第五篇:常用SQL語(yǔ)句
一、創(chuàng)建數(shù)據(jù)庫(kù)
create database 數(shù)據(jù)庫(kù)名
on(name='數(shù)據(jù)庫(kù)名_data',size='數(shù)據(jù)庫(kù)文件大小',maxsize='數(shù)據(jù)庫(kù)文件最大值',filegrowth=5%,//數(shù)據(jù)庫(kù)文件的增長(zhǎng)率
filename='數(shù)據(jù)庫(kù)存放的位置')
log on
(name='數(shù)據(jù)庫(kù)名_log',size='數(shù)據(jù)庫(kù)文件大小',maxsize='數(shù)據(jù)庫(kù)文件最大值',filegrowth=5%,//數(shù)據(jù)庫(kù)文件的增長(zhǎng)率
filename='數(shù)據(jù)庫(kù)存放的位置')
二、創(chuàng)建數(shù)據(jù)表
create table 表名
(字段名 字段類型 是否為空 primary key//是否為主鍵)
三、select語(yǔ)句
1、去除重復(fù)查詢
select distinct 字段名 from 表名
2、按條件查詢
select * from 表名 where 條件
3、排序desc升序/asc降序
select 字段名 from 表名 order by desc/asc
四、刪除語(yǔ)句
delect from 表名 where 條件
五、修改語(yǔ)句
update from 表名 set 字段名1=‘字段值’,字段名2=‘字段值’ where 條件
六、插入語(yǔ)句
insert into 表名(字段名)values(字段值)//字段名與字段值一一對(duì)應(yīng)
七、創(chuàng)建存儲(chǔ)過程
use 數(shù)據(jù)庫(kù)名
go
create proc 存儲(chǔ)過程名
as
要存儲(chǔ)在里面的SQL語(yǔ)句
八、sql分頁(yè)
select top 查詢的條數(shù) *
from表名 where 主鍵 not in(select top 第幾條開始查詢 id from 表名 order by 主鍵)order by 主鍵 desc