1. 程式人生 > >數據庫學習

數據庫學習

val 數據 sel insert update pri ble des upd

use shiyan
go
create table Student
(
Sno char(9) not null,
Sname char(20) not null,
Sex char(2) null,
Sage smallint null,
Sdept char(20) null,
primary key (Sno),
unique(Sname),
)


use shiyan
go
create table Course
(
Cno char(4) not null,
Cname char(40) null,
Cpno char(4) null,
Credit smallint null,
primary key(Cno),
foreign key(Cpno) references Course(Cno)
)


use shiyan
go
create table SC
(
Sno char(9) not null,
Cno char(4) not null,
Grade smallint null,
primary key (Sno,Cno),
foreign key (Cno) references Course(Cno),
foreign key (Sno) references Student(Sno),
)
go




insert into Student values(‘200215121‘,‘李勇‘,‘男‘,‘20‘,‘cs‘)
insert into Student values(‘200215122‘,‘劉晨‘,‘女‘,‘19‘,‘cs‘)
insert into Student values(‘200215123‘,‘王敏‘,‘女‘,‘18‘,‘ma‘)
insert into Student values(‘200215125‘,‘張立‘,‘男‘,‘19‘,‘is‘)








insert into Course values(‘1‘,‘數據庫‘,null,‘4‘)
insert into Course values(‘2‘,‘數學‘,null,‘2‘)
insert into Course values(‘3‘,‘信息系統‘,null,‘4‘)
insert into Course values(‘4‘,‘操作系統‘,null,‘3‘)
insert into Course values(‘5‘,‘數據結構‘,null,‘4‘)
insert into Course values(‘6‘,‘數據處理‘,null,‘2‘)
insert into Course values(‘7‘,‘pascal語言‘,null,‘4‘)








insert into SC values(‘200215121‘,‘1‘,‘92‘)
insert into SC values(‘200215121‘,‘2‘,‘85‘)
insert into SC values(‘200215121‘,‘3‘,‘88‘)
insert into SC values(‘200215122‘,‘2‘,‘90‘)
insert into SC values(‘200215122‘,‘3‘,‘80‘)
insert into SC values(‘200215123‘,‘4‘,‘87‘)
insert into SC values(‘200215123‘,‘5‘,‘95‘)


use shiyan
go
alter table Student
add Brithday datetime


use shiyan
go
alter table Student
drop column Brithday


insert into SC values(‘200215121‘,‘5‘,null)


use shiyan
update Student
set Sno=‘200215188‘
where Sno=‘200215127‘


use shiyan
update Student
set Sname=‘李詠‘
where Sname=‘李勇‘ and Sdept=‘cs‘


use shiyan
update SC
set Grade=Grade+5
where Cno=‘1‘


insert into Student values(‘200215199‘,‘王靖靖‘,‘女‘,‘21‘,‘cs‘)


use shiyan
update Student
set Sname=‘王丹丹‘,Sex=‘女‘,Sdept=‘ma‘,Sage=‘20‘
where Sno=‘200215199‘


use shiyan
update Student
set Sdept=null
where Sno=‘200215199‘


use shiyan
go
delete from Student
where Sdept is NULL --此處用“where Sdept = null”錯誤
go


use shiyan
go
select * from Student




use shiyan
go
select * from Course


use shiyan
go
select * from SC


use shiyan
go
select Sdept,count(Sdept) as ‘人數‘ from Student group by Sdept
order by 人數


select Sdept,SUM(Sage)/COUNT(Sdept) as ‘平均年齡‘ from Student group by Sdept
order by 平均年齡 desc


select Sex ,SUM(Sage)/COUNT(Sex)as‘平均年齡‘from Student group by Sex


select Sno,count(Sno)as ‘課程數‘ from SC group by Sno


update SC
set Grade = 60
where Cno =2 and Grade<(select sum(Grade)/COUNT(*) from SC where Cno =2)


use shiyan
go
select Sno,Sname from Student
where Sdept=‘cs‘ and Sage not between 19 and 21




use shiyan
go
create table customer
(
customid Varchar(17) not null,
name Varchar(10) null,
sex Varchar(2) null check (sex=‘男‘ or sex =‘女‘),
age Int null check (age>18 and age<45),
xfg Dec(10,2) null,
address Varchar(50) null,
memo Varchar(100) null,
primary key(customid),
unique(name),
)

數據庫學習