====檢視所有表、表結構、庫等資料====
exec sp_help //查詢所有表
exec sp_help t_student //檢視具體表結構
select name from sysobjects where type = 'U' //查詢使用者表
Create database student_info; --建立庫
use student_info; --使用庫
sp_helpdb student_info --檢視庫
alter table t_student modify ssex char(3) //修改表字段類型
exec sp_rename 't_score.sorce','score' //修改表列名
exec sp_help t_student //檢視表結構
drop table t_teaher //删除表
=======================================
資料源
Create database student_info; --建立庫
use student_info; --使用庫
1.建表語句
--學生表
create table t_student(
sid varchar(10),
sname varchar(10),
sage date,
ssex char(3)
)
--教師表
create table t_teacher(
tid varchar(10),
tname varchar(10)
)
--課程表
create table t_course(
cid varchar(10),
cname varchar(10),
tid varchar(10)
)
--分數表
create table t_score(
sid varchar(10),
cid varchar(10),
score float
)
exec sp_help t_teacher --檢視表資料
2.測試源資料及例題
================工具Interactive SQL==============================
--學生表
insert into t_student values('01' , '趙雷' , '1990-01-01' , '男')go
insert into t_student values('02' , '錢電' , '1990-12-21' , '男')go
insert into t_student values('03' , '孫風' , '1990-12-20' , '男')go
insert into t_student values('04' , '李雲' , '1990-12-06' , '男')go
insert into t_student values('05' , '周梅' , '1991-12-01' , '女')go
insert into t_student values('06' , '吳蘭' , '1992-01-01' , '女')go
insert into t_student values('07' , '鄭竹' , '1989-01-01' , '女')go
insert into t_student values('09' , '張三' , '2017-12-20' , '女')go
insert into t_student values('10' , '李四' , '2017-12-25' , '女')go
insert into t_student values('11' , '李四' , '2012-06-06' , '女')go
insert into t_student values('12' , '趙六' , '2013-06-13' , '女')go
insert into t_student values('13' , '孫七' , '2014-06-01' , '女')go
select * from t_student go
-- 教師表
insert into t_teacher values('01' , '張三') go
insert into t_teacher values('02' , '李四') go
insert into t_teacher values('03' , '王五') go
select * from t_teacher go
-- 課程表
insert into t_course values('01' , '國文' , '02') go
insert into t_course values('02' , '數學' , '01') go
insert into t_course values('03' , '英語' , '03') go
select * from t_course go
-- 成績表
insert into t_score values('01' , '01' , 80) go
insert into t_score values('01' , '02' , 90) go
insert into t_score values('01' , '03' , 99) go
insert into t_score values('02' , '01' , 70) go
insert into t_score values('02' , '02' , 60) go
insert into t_score values('02' , '03' , 80) go
insert into t_score values('03' , '01' , 80) go
insert into t_score values('03' , '02' , 80) go
insert into t_score values('03' , '03' , 80) go
insert into t_score values('04' , '01' , 50) go
insert into t_score values('04' , '02' , 30) go
insert into t_score values('04' , '03' , 20) go
insert into t_score values('05' , '01' , 76) go
insert into t_score values('05' , '02' , 87) go
insert into t_score values('06' , '01' , 31) go
insert into t_score values('06' , '03' , 34) go
insert into t_score values('07' , '02' , 89) go
insert into t_score values('07' , '03' , 98) go
select * from t_score go
==========================================
3.例題
01)查詢" 01 "課程比" 02 "成績高的學生的資訊及課程分數(即查詢某人國文成績比數學成績高)
02)查詢同時存在" 01 "課程和" 02 "課程的情況(即選課同時存在國文和數學)
03)查詢存在" 01 "課程但可能不存在" 02 "課程的情況,02不存在時顯示為 null (即查詢存在國文成績但可能不存在數學成績的資訊)
04)查詢不存在" 01 "課程但存在" 02 "課程的情況(不存在國文但存在數學課程)
05)查詢平均成績大于等于 60 分的同學的學生編号和學生姓名和平均成績
06)查詢在t_score表存在成績的學生資訊,沒有成績的不顯示/顯示 null
======================解析(附有多種解法)==========================
01)查詢" 01 "課程比" 02 "成績高的學生的資訊及課程分數(即查詢某人國文成績比數學成績高)
select stu.*,s.score 國文成績,s2.score 數學成績
from t_student stu
inner join t_score s
on s.sid=stu.sid
inner join t_score s2
on s2.sid=stu.sid
where s.cid='01' and s2.cid='02' and s.score>s2.score
go
02)查詢同時存在" 01 "課程和" 02 "課程的情況(即選課同時存在國文和數學)
select stu.*,s.score 國文,s2.score 數學
from t_student stu inner join t_score s
on s.sid=stu.sid
inner join t_score s2
on s2.sid=stu.sid
where s.cid='01' and s2.cid='02'
go
select s1.sid,s1.cid,s2.cid ,s1.score 國文分數,s2.score 數學分數
from t_score s1
inner join t_score s2
on s1.sid=s2.sid
where s1.cid='01' and s2.cid='02'
go
select s1.sid,s1.score 國文分數,s2.score 數學分數,s1.cid,s2.cid
from t_score s1,t_score s2
where s1.sid=s2.sid and s1.cid='01' and s2.cid='02'
go
03)查詢存在" 01 "課程但可能不存在" 02 "課程的情況,02不存在時顯示為 null (即查詢存在國文成績但可能不存在數學成績的資訊)
select s1.sid,s1.cid 課程ID1,s1.score 國文成績,s2.cid 課程ID2,s2.score 數學成績
from t_score s1
left join t_score s2
on s2.sid =s1.sid and s2.cid='02'
where s1.cid='01'
go
04)查詢不存在" 01 "課程但存在" 02 "課程的情況(不存在國文但存在數學課程)
--反向查詢:先查詢存在01課程的學生id,再排除存在01課程的學生id(not in)
select *
from t_score
where sid not in
(select sid from t_score where cid='01')
and cid='02'
go
select *
from t_score s1
where s1.cid='02'
and sid not in
(select sid from t_score where cid in ('01'))
go
05)查詢平均成績大于等于 60 分的同學的學生編号和學生姓名和平均成績
select stu.sid,stu.sname,avg(s.score) avg_score
from t_student stu
inner join t_score s
on stu.sid=s.sid
group by stu.sid
having avg(s.score)>=60
order by avg_score desc
go
select stu.sid,stu.sname,s.avg_score
from t_student stu,
(select avg(score) avg_score,sid
from t_score
group by sid
having avg(score) >=60)s
where s.sid=stu.sid
order by avg_score desc
go
06)查詢在t_score表存在成績的學生資訊,沒有成績的不顯示/顯示 null
--不顯示沒有成績的學生(不包含沒有成績的)
select stu.*,s.sum_score 總成績,s.avg_score 平均成績,ct 課程數 from t_student stu
inner join
(
select sid,sum(score) sum_score,avg(score) avg_score,count(cid) ct
from t_score
where score >=0 --此條件可省略
group by sid
)s
on s.sid=stu.sid
go
--顯示所有學生的成績(包含沒有成績的)
select stu.*,s.sum_score 總成績,s.avg_score 平均成績,ct 課程數 from t_student stu
left join
(
select sid,sum(score) sum_score,avg(score) avg_score,count(cid) ct
from t_score
group by sid
)s
on s.sid=stu.sid
go
================篇幅過長,待續=====================
================附:常見問題處理====================
/*
1.sybase資料庫解決中文亂碼問題(中文顯示問号問題-?)
解決辦法:
修改locales.dat
找到[NT]
最後一行的字尾編碼修改為 utf8,然後重新開機sybase資料庫
2.表字段無法修改
解決辦法:
use master go
sp_dboption 'student_info','select into/bulkcopy/pllsort',true go
*/
=================================================