create table 學生信息表
(
學號 varchar(20) not null primary key,
姓名 varchar(50),
性別 varchar(4),
出生日期 datetime,
班級 int,
)
create table 課程表
(
課程號 varchar(20) not null primary key,
課程名 varchar(20),
老師號 varchar(20)
)
create table 成績表
(
學號 varchar(20) not null foreign key references 學生信息表(學號),
課程號 varchar(20) not null foreign key references 課程表(課程號),
成績 int操作系統
)code
insert 學生信息表table
values('101','李軍','男','1976-02-20','95033')
insert 學生信息表date
values('103','陸君','男','1974-06-03','95031')
insert 學生信息表select
values('105','匡明','男','1975-10-02','95031')
insert 學生信息表im
values('107','王麗','女','1976-01-23','95033')
insert 學生信息表統計
values('108','曾華','男','1976-09-01','95033')
insert 學生信息表數據
values('109','王芳','女','1976-02-10','95031')
select *
from 學生信息表datetime
insert 課程表查詢
values('3-105','計算機導論','825')
insert 課程表
values('3-245','操做系統','804')
insert 課程表
values('6-166','數字電路','856')
select *
from 課程表
insert 成績表
values('101','3-105',64)
select *
from 成績表
insert 成績表
values('101','6-166',85)
insert 成績表
values('103','3-105',92)
insert 成績表
values('103','3-245',86)
insert 成績表
values('105','3-105',88)
insert 成績表
values('105','3-245',75)
insert 成績表
values('107','3-105',91)
insert 成績表
values('107','6-166',79)
insert 成績表
values('108','3-105',78)
insert 成績表
values('108','6-166',81)
insert 成績表
values('109','3-105',76)
insert 成績表
values('109','3-245',68)
select *
from 成績表
create table 學生信息表
(
學號 varchar(20) not null primary key,
姓名 varchar(50),
性別 varchar(4),
出生日期 datetime,
班級 int,
)
create table 課程表
(
課程號 varchar(20) not null primary key,
課程名 varchar(20),
老師號 varchar(20)
)
create table 成績表
(
學號 varchar(20) not null foreign key references 學生信息表(學號),
課程號 varchar(20) not null foreign key references 課程表(課程號),
成績 int
)
insert 學生信息表
values('101','李軍','男','1976-02-20','95033')
insert 學生信息表
values('103','陸君','男','1974-06-03','95031')
insert 學生信息表
values('105','匡明','男','1975-10-02','95031')
insert 學生信息表
values('107','王麗','女','1976-01-23','95033')
insert 學生信息表
values('108','曾華','男','1976-09-01','95033')
insert 學生信息表
values('109','王芳','女','1976-02-10','95031')
select *
from 學生信息表
insert 課程表
values('3-105','計算機導論','825')
insert 課程表
values('3-245','操做系統','804')
insert 課程表
values('6-166','數字電路','856')
select *
from 課程表
insert 成績表
values('101','3-105',64)
select *
from 成績表
insert 成績表
values('101','6-166',85)
insert 成績表
values('103','3-105',92)
insert 成績表
values('103','3-245',86)
insert 成績表
values('105','3-105',88)
insert 成績表
values('105','3-245',75)
insert 成績表
values('107','3-105',91)
insert 成績表
values('107','6-166',79)
insert 成績表
values('108','3-105',78)
insert 成績表
values('108','6-166',81)
insert 成績表
values('109','3-105',76)
insert 成績表
values('109','3-245',68)
select *
from 成績表
select 學號,姓名
from 學生信息表
select
學號 as'xuehao', 姓名 as'xingming', 性別 as'xingbie', 出生日期 as'chushengri', 班級 as'banji' from 學生信息表
select *
from 學生信息表
select 學生號,成績
from 成績表
select 學生號,成績
into newtable
from 成績表
select 姓名,'年齡',2015-datepart(yy,出生日期)
from 學生信息表]
select distinct 學生號
from 成績表
select top 4 *
from 成績表
select 學生號,成績
from 成績表
where 課程號='3-105' order by 成績 desc
select *
from 學生信息表
order by 班級,出生日期
select 學號,姓名
from 學生信息表
where 出生日期>'1975-01-01'
select 學號,姓
from 學生信息表
where not 出生日期<='1975-01-01'
select *
from 成績表
where 成績between 60 and 80
select *
from 成績表
where 成績not between 60 and 80
-
select *
from 成績表
where 成績in (85,90,95)
select *
from 學生信息表
where 學號like '101'
select 姓名,學號, 性別
from 學生信息表
where 姓名like '王%'
select 學號, 課程號
from 成績表
where 成績is not null
select *
from 成績表
where 成績>=60 and 成績<=80
select *
from 學生信息表
where 性別='女' and 班級='95033'
select count(*)
from 學生信息表
select count(學號)
from 學生信息表
select count(distinct 班級)
from 學生信息表
select avg(成績)
from 成績表
where 課程號='3-105'
select 課程號,count(*) as '選修課人數'
from 成績表
group by 課程號
select 學號, count(*)
from 成績表
where 成績> 85 group by 學號 having count(*)>= 2
select *
from 學生信息表
where 班級in
( select 班級 from 學生信息表 where 姓名='王芳' )
select 學號,姓名
from 學生信息表
where 學號 in ( select 學號 from 成績表 where 課程號='3-105' )
use 學生庫
select *
from 學生信息表
where 班級= ( select 班級 from 學生信息表 where 姓名= '王芳' )
select 姓名出生日期
from 學生信息表
where 出生日期> any (
select 出生日期 from 學生信息表 where 班級= '95031' )
select 姓名出生日期
from 學生信息表
where 出生日期> (
select max(出生日期) from 學生信息表 where 班級= '95031' )
select 姓名
from 學生信息表
where exists
( select * from 成績表 where 學號= 學生信息表.學號and 課程號= '3-425' )
由相關表查詢數據,需將表進行鏈接 外鏈接(左外鏈接,有外鏈接,全外鏈接);內鏈接(天然鏈接) 即在兩張表上查詢數據,同時調用
兩張表以上非嵌套查詢(相關子查詢;子查詢)
查詢學生的 學生信息表和成績表的所有數據
select 學生信息表. , 成績表.
from 學生信息表, 成績表
where 學生信息表.學號=成績表.學號
選修課3-105的成績高於學號109的全部學生記錄
select x. ,y.
from 成績表x , 成績表y
where x.課程號= '3-105' and y.課程號='3-105' and y.學號='109' and x.成績> y.成績
order by x.成績 desc
select x., y.
from 成績表x inner join 成績表y
on x.課程號= '3-105' and y.課程號='3-105' and y.學號='109' and x.成績> y.成績
select 成績表. ,成績表.
from 學生信息表inner join 成績表
on 學生信息表.學號=成績表.學號 and 成績表.課程號='3-105' and 成績表.成績>90
select 學生信息表.學號, 姓名, 課程名, 成績
from 學生信息表, 課程表, 成績表
where 學生信息表.學號= 成績表.學號 and 課程表.課程號= 成績表.課程號
select 學生信息表.學號, 姓名, 課程名, 成績
from 學生信息表inner join 成績表 on 學生信息表.學號=成績表.學號 inner join 課程表
on 課程表.課程號=成績表.課程號
update 學生信息表
set 姓名='李軍改'
where 學號='101'
update 學生信息表
set 姓名='李軍'
where 學號='101'
update 成績表
set 成績=成績+20
update 成績表
set 成績=成績-20
delete
from 成績表
where 學號='1001'
delete 成績表
where 學號='1001'
delete 成績表from 成績表where 學號='1001'