資料庫基本語句大全

2023-01-15 00:15:05 字數 5168 閱讀 3360

-建立資料庫

create database mlgb

go--建立學生表

use mlgb

gocreate table s (

sno char (10) not null,

sname varchar (15) not null,

s*** char (2),

sage int ,

sdept varchar (20)

constraint stuno primary key(sno),

constraint stu*** check (s*** in ('男','女')))go

--建立課程表

create table c(

cno char (10) not null,

cname varchar (20) not null,

tname varchar (10) not null,

csemi int)go

--建立選課表

create table sc(

sno char (10) not null,

cno char (10) not null,

grade int

)--插入學生資訊

insert into s values ('200213808','李楓',

'女',22,'軟體工程');

insert into s values ('200225645','張強',

'男',29,'軟體工程');

insert into s values ('200275613','李平',

'男',30,'英語');

insert into s values ('200213108','王依萍',

'女',18,'網路工程');

insert into s values ('200224119','胡東',

'男',24,'網路工程');

insert into s values ('200213828','李小玲',

'男',24,'網路工程');

--插入課程資訊

insert into c values ('720','資料結構',

'朱虹', 5);

insert into c values ('730','離散數學',

'趙強',4);

insert into c values ('825','高等數學','朱虹',2);

insert into c values ('621','英語','李蘭',1);

insert into c values ('623','英語寫作','李蘭',2);

--插入選課資訊

insert into sc values('200213808','720',85);

insert into sc values('200225645','730',68);

insert into sc values('200213808','730',70);

insert into sc values('200275613','621',90);

insert into sc values('200213828','621',78);

insert into sc values('200225645','825',86);

insert into sc values('200213808','825',69);

insert into sc values('200275613','730',84);

insert into sc values('200213828','730',89);

--修改表

alter table s

add splace char (20) null

drop table s

--建立索引

create unique index studno on s (sno)

create unique index cno on c (cno asc)

--刪除索引

drop index courseno on c

drop index studno on s

--建立檢視

create view student1

as select sno,sname,sage

from s

where sdept='網路工程'

go--學生成績表檢視

create view stugrade (sno,sname,cno,grade)

as select

from s,c,sc

where and and '網路工程'

go--學生成績統計檢視

create view stucj (sno, sgrade, agrade)

as select sno,sum(grade),**g(grade)

from sc

group by sno

go--查詢資料

select sno,sname

from s

where sdept='軟體工程'and s***='女'

select sname,sage,sdept

from s

--原表上建新表

select as newgrade

from s,sc

where sdept='軟體工程'

--級聯刪除

delete

from sc

where sno='200213808'

delete

from s

where sdept='軟體工程'

--檢索未選修任何課程的學生學號

select sno,sname

from s

where sno not in(select distinct

from sc

--檢索趙老師所授的課程號,課程名

select cno,cname

from c

where tname like '趙%'

--檢索所有姓李的同學的基本資訊

select *

from s

where sname like '李%'

--檢索選修'離散數學'課程的學生的學號和姓名

select

from sc,c,s

where and cname='離散數學'and

--檢索年齡介於李平同學年齡和歲之間的學生的基本資訊

select *

from s

where sage between 20 and (select sage

from s

where sname='李平')

--檢索年齡介於和歲之間的學生的基本資訊

select *

from s

where sage between 15 and 30

--檢索至少選修了一門趙強所講授課程的學生姓名,學號

select

from s,sc,c

where and and tname='趙強'

--將學生表中查詢的資料儲存到一張臨時表newtable中

select *

into newtable

from s

--查詢新錶

select* from newtable

--統計所有學生選課的門數

select count(

from sc

group by

--統計教師李蘭所授每門課程學生的平均成績

select **g(grade) as 平均成績

from sc,c

where tname='朱虹'and

/*統計所有選修人數多於人的課程號和選課人數,

並按人數降序排列,若人數相等則按課程號公升序排列*/

select cno,count(sno) as 選課人數

from sc

group by cno h**ing count(*)>=3

order by 選課人數 asc

--檢索「高等數學」成績高於該課平均成績的同學學號和姓名;

select

from s,sc,c

where and cname='高等數學'and and grade >(select **g(grade )

from sc,c where and cname='高等數學')

--檢索所有成績不及格的同學的學號,姓名和課程號

select

from s,sc,c

where and grade <80 and

--將「軟體工程」專業選修課程號為「」的成績全部提高分

select grade,grade+5 as newgrade

from sc,c

where

/*建立乙個新錶,表名為new,用來存放成績大於分得學生資訊,此表包括:

學號,姓名,課程名和成績。然後從利用實驗已經建立並儲存過的學生表、課程表、選課表中查詢相關資料資訊裝入此表中*/

create table made (

sno char (15) not null,

sname char (20) not null,

cname char (20),

grade int)

select

into made

from s,sc,c

where and grade>80 and

--查詢新錶

select * from made

--檢索所有學生的學號、姓名、課程號和成績(注:沒選某門課的學生成績相應為null)

select ,sname ,

from s

left outer join sc

on left outer join c

on --實驗

--利用實驗已經建立並儲存過的學生表、課程表,學生表按學號建立惟一索引,課程表按課程名建立惟一索引

create unique index sno on s (sno);

create unique index cno on c (cno);

--利用實驗已經建立並儲存過的學生表和選課表建立乙個「網路工程」專業學生成績檢視js**(sno,cno,grade)

資料庫應用基礎 SELECT語句練習

資料庫應用基礎 access2010 select語句練習 一 班級姓名 在資料庫 school 中建立 學生 成績 表,學生表包含的字段有學號 姓名 性別 年齡 系別,成績表包含的字段有學號 課程號 成績。1.查詢年齡在19至21歲之間的女生的學號 姓名 年齡,按年齡從大到小排序。2.查詢姓名中第...

資料庫種類與資料庫結構

2 網狀結構模型 某醫院醫生 病房和病人之間的聯絡。即每個醫生負責 三個病人,每個病房可住一到四個病人。如果將醫生看成是乙個資料集合,病人和病房分別是另外兩個資料集合,那麼醫生 病人和病房的比例關係就是m n p 即m個醫生,n個病人,p間病房 這種資料結構就是網狀資料結構,它的一般結構模型,記錄r...

資料控制資料庫

資料庫實驗報告 實驗四實驗題目 資料控制 指導老師 李萍 專業班級 電腦科學與技術系1001班 姓名 劉萌 2010100155 2012年 11月10日 實驗型別 驗證實驗室 軟體實驗室一 一 實驗題目 資料控制 安全性和完整性 二 實驗目的和要求 理解sql server的使用者與許可權管理機制...