SQLServer數據庫.後臺.增刪改查數據表

建立表操做

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

在表中插入信息.01

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 課程表

在表中插入信息.02

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 成績表

查看該表的全部列,顯示前4排數據

select top 4 *

from 成績表

查看該表的指定列,用關鍵詞過濾,結果按成績倒序排列

select 學生號,成績

from 成績表

where 課程號='3-105'
 order by 成績 desc

查詢該表的全部列,結果用班級、出生日期順序排列

select *

from 學生信息表

order by 班級,出生日期

查詢該表的指定列,用關鍵、>過濾.01

select 學號,姓名

from 學生信息表

where 出生日期>'1975-01-01'

查詢該表的指定列,用關鍵字、<=過濾.02

select 學號,姓

from 學生信息表

where not 出生日期<='1975-01-01'

查詢該表的全部列,用關鍵字、between and過濾.01

select *

from 成績表

where 成績between 60 and 80

查詢該表的全部列,用關鍵字、between and過濾.02

select *

from 成績表

where 成績not between 60 and 80

-

查詢該表的全部列,用關鍵字、in過濾

select *

from 成績表

where 成績in (85,90,95)

查詢該表的全部列,用關鍵字、like過濾

select *

from 學生信息表

where 學號like '101'

查詢該表的全部列,用關鍵字、like、%過濾

select 姓名,學號, 性別

from 學生信息表

where 姓名like '王%'

查詢該表的指定列,用關鍵字、is not null過濾

select 學號, 課程號
from 成績表
where 成績is not null

查詢該表的指定列,用關鍵字、>=、<=過濾

select *

from 成績表

where 成績>=60 and 成績<=80

查詢該表的全部列,用關鍵字、and、=過濾

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 課程號

查詢.高難度.01

select 學號, count(*)
from 成績表

where 成績> 85
      group by 學號
            having count(*)>= 2

查詢.高難度.02

select *
from 學生信息表
where 班級in

(
     select 班級
     from  學生信息表
     where 姓名='王芳'
    )

查詢.高難度.03

select 學號,姓名
from 學生信息表

where 學號 in
     (
        select 學號
        from  成績表
        where  課程號='3-105'
      )

查詢.高難度.04

use 學生庫
select *
from 學生信息表

where 班級= (
            select 班級
            from 學生信息表
            where   姓名= '王芳'
           )

查詢.高難度.05

select 姓名出生日期
from 學生信息表
where 出生日期> any (

select 出生日期
                   from  學生信息表
                   where 班級= '95031'
                  )

查詢.高難度.06

select 姓名出生日期
from 學生信息表
where 出生日期> (

select max(出生日期)
                   from  學生信息表
                   where 班級= '95031'
                  )

查詢.高難度.07

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 課程表.課程號=成績表.課程號

將學號是101的同窗的姓名改成 李軍

update 學生信息表
set 姓名='李軍改'
where 學號='101'

update 學生信息表
set 姓名='李軍'
where 學號='101'

更改爲績表,設置成績爲加上20分.01

update 成績表
set 成績=成績+20

更改爲績表,設置成績爲減去20分.02

update 成績表
set 成績=成績-20

刪除學號是1001的同窗的全部數據.01

delete
from 成績表
where 學號='1001'

刪除表中的一行數據.02

delete 成績表
where 學號='1001'

刪除表中 學號屬性值是1001 的那一行.03

delete 成績表from 成績表where 學號='1001'

相關文章
相關標籤/搜索