當前位置:首頁 » 數據倉庫 » sql資料庫經典例題豆丁網
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql資料庫經典例題豆丁網

發布時間: 2022-09-26 15:28:44

A. sql資料庫

第一個
select*fromswheresnoin(
selectsnofromscwherecnoin(
selectcnofromcwherecteacher='李明'
)
)

第二個
selects.sname,sc,sum(sgrade)fromsleftjoinscons.sno=sc.sno
wheresc.sgrade>=60
groupbys.sname
第三個
createindexc_cno_indexONc(cno)

B. SQL資料庫題目

1、創建資料庫
create database 學生成績資料庫
on primary
(name='學生成績資料庫_mdf',
filename='e:\學生成績資料庫.mdb',
size=1,
maxsize=10,
filegrowth =10%)
log on
(name='學生成績資料庫_ldf',
filename='e:\學生成績資料庫.ldf',
size=1,
maxsize=10,
filegrowth =10%)
2、創建課程表
create table 課程表
(課程號 char(6) primary key,
課程名稱 char(20) not null,
任課教師 char(8))
3、 創建學生表
create table 學生表
(學號 char(6) primary key,
姓名 char(8) not null,
性別 char(2) constraint ck_性別 check( 性別 in ('男','女')),
民族 char(20) not null default '漢')
4、創建成績表
create table 成績表
(學號 char(6) not null foreign key(學號) references 學生表(學號),
課程號 char(6) not null foreign key(課程號) references 課程表(課程號),
分數 int constraint ck_分數 check(分數 between 0 and 150))
5、添加信息
insert 課程表(課程號,課程名)
values ('100001', '大學語文')
insert 課程表(課程號,課程名)
values ('100002', '大學英語')
6. 寫出創建成績表視圖(學號,姓名,課程號,課程名稱,成績)的代碼
create view 成績表視圖
as
select 學生表.學號,姓名,課程表.課程號,課程名稱,成績
from 學生表,課程表,成績表
where 學生表.學號=成績表.學號 and 成績表.課程號=課程表.課程號
7. 寫出計算大學語文課程成績最高分、最低分、平均分的代碼
select max(分數) '最高分數',min(分數) '最低分數',avg(分數) '平均分數'
from 成績表
where 學號 in (select 學號
from 課程表
where 課程名稱='大學語文')
8、 檢索姓李的女同學的信息:姓名、性別、民族
select 姓名,性別,民族
from 學生表
where 姓名 like '李%' and 性別='女'

C. SQL資料庫的例題請教一下大家

看了你的題,首先幾個表需要說明一下Out_Goods和In_Goods在定義表時少了Sh_address列,列的屬性參考Provider表中的Address

問題解答:
問題1、自定義一個函數,計算供貨商編號為PD001所提供產品的平均價格,如果平均價格〉70,則輸出「價格適中」;如果平均價格<=70,則輸出價格較低。
CREATE FUNCTION [dbo].[fn_calc_avg_price]
(
@PID char(10) = 'PD001'-----------貨商編號,此處默認值為PD001
)

RETURNS varchar(50) AS

BEGIN
DECLARE @vchPriceDesc varchar(50)
DECLARE @mnyPriceAvg money

SELECT @mnyPriceAvg = AVG(PPrice)
FROM Proct WHERE PID = @PID

IF (@mnyPriceAvg > 70) SET @vchPriceDesc = '價格適中'
IF (@mnyPriceAvg <= 70) SET @vchPriceDesc = '價格較低'

RETURN @vchPriceDesc
END

問題2、創建一個內聯表值函數,返回一個時間段內的出貨信息,要求包括出貨商品名稱、供貨商名稱、出貨價格、出貨數量、出貨日期。
CREATE FUNCTION [dbo].[fn_get_out_goods_info]
(
@dtBeginTime datetime, --------------------------時間段的起始時間
@dtEndTime datetime --------------------------時間段的終止時間
)

RETURNS @TempTable table
(
out_proct_name varchar(20),
out_provider_name varchar(20),
out_proct_price money,
out_proct_num int,
out_date datetime
)
AS

BEGIN
INSERT INTO @TempTable
SELECT
Proct.PName,
Provider.ProviderName,
Out_Goods.OutPrice,
Out_Goods.OutNum,
Out_Goods.OutDate
FROM
Proct, Provider, Out_Goods
WHERE
Provider.ProviderId = Out_Goods.ProviderId
AND Proct.PId = Out_Goods.PId
AND Out_Goods.OutDate <= @dtEndTime
AND Out_Goods.OutDate >= @dtBeginTime
RETURN
END

問題3、創建一個after觸發器,當Provider 供貨商信息表中的ProviderId發生更改時,同時更改In_Goods進貨信息表和Out_Goods出貨信息表ProviderId欄位的值。
CREATE TRIGGER [trig_update_proID]
ON [dbo].[Provider]
AFTER UPDATE

AS

IF UPDATE(ProviderID)
BEGIN
UPDATE In_Goods SET ProviderID = (SELECT ProviderID FROM INSERTED) WHERE ProviderID = (SELECT ProviderID FROM DELETED)
UPDATE Out_Goods SET ProviderID = (SELECT ProviderID FROM INSERTED) WHERE ProviderID = (SELECT ProviderID FROM DELETED)
END

問題4、定義一個存儲過程,要求使用游標,計算某段時間內售出產品的平均價格。

坦白的說,其實沒必要使用游標,既然用了,那就順便用下WHILE循環好了
CREATE PROCEDURE [dbo].[sp_calc_part_sales_price]
(
@dtBeginTime datetime,
@dtEndTime datetime
)
AS

DECLARE @mnyTotalPrice money
DECLARE @mnyCurPrice money
DECLARE @nCount int SELECT @nCount = 0

DECLARE cursPrice CURSOR LOCAL
FOR SELECT OutPrice FROM Out_Goods WHERE Out_Goods.OutDate <= @dtEndTime AND Out_Goods.OutDate >= @dtBeginTime

OPEN cursPrice
FETCH NEXT FROM cursPrice INTO @mnyCurPrice

WHILE @@FETCH_STATUS=0
BEGIN
SET @nCount = @nCount + 1
SET @mnyTotalPrice = @mnyTotalPrice + @mnyCurPrice
FETCH NEXT FROM cursPrice INTO @mnyCurPrice
END

IF @nCount = 0 SELECT 0 AS '平均價格'
IF @nCount > 0 SELECT @mnyTotalPrice/@nCount AS '平均價格'

OK,就這樣吧,如果有不明白或者我寫錯的地方,再聯系,Good Luck!

D. SQL資料庫練習題

1. SQL Server 2000是典型的關系型資料庫產品。 ( 1 )
2. 在一台計算機上可以同時運行多個版本的SQL Server。 ( 1 )
3. 在SQL Server中日誌文件是維護資料庫完整性的重要工具。 ( 0 )
4. 在定義數據表時,定義某列為標識列的關鍵字是Identity。 ( 1 )
5. 浮點數據類型的優點是能夠存儲范圍非常大的數字,但容易發生誤差。 ( 0 )
6. 資料庫完整性的目的是為了防止錯誤信息輸入和輸出。 ( 0 )
7. 在Update語句中,一次可以更新多個表。 ( 0)
8. 盡量使用Select * ,可以加快查詢速度。 ( 0 )
9. 在SQL Server 2000中表示注釋可以用類似c語言的/*...*/和//。 ( 0 )
10. 在SQL Server中,RTRIM函數刪除字元串右邊的空白字元。 ( 1 )
11. 一個表只能有一個聚集索引(簇索引)。 ( 1 )
12. SQL查詢語言中,如果沒有指定排序方式,則默認是升序方式。 ( 1 )
13. 在SQL Server 2000中ntext類型的欄位不能進行排序操作。 ( 0 )
14. 在SQL Server 2000中bit類型的欄位不能建立索引。 ( 1 )
15. 在被定義為唯一索引的列上的數據不能有重復的值。 ( 1 )
16. 在被定義為唯一索引的列上的數據不允許空。 ( 0可以的但是只能有一個null值 )
17. 在SQL Server中,每張表都應該建立一個索引,以提高查詢速度。 ( 0 )
18. 視圖在SQL Server中是一張虛擬表。 ( 1 )
19. 當一個視圖由2個以上基本表構成時,不能進行刪除視圖中的數據。 ( 0 )
20. 在SQL Server中,觸發器是一種特殊的存儲過程。 ( 1 )
21. 由於存儲過程是解釋執行,所以每次執行時都要檢查是否有語法錯誤。 ( 0 )
22. 可以在用戶正在使用的資料庫上執行資料庫恢復操作。 ( 0 )
1表示正確

E. SQL資料庫幾個題目,100分懸賞!高手請進!

1、 select eno, ename,age from emp where job_title=『高級』 and gender=1

2、
∏eno, ename,age (φjob_title=『高級』 and gender=1 (emp))
注:其中的∏表示投影,φ表示選擇.

4、create index 索引名 on emp (eno)

5、select dept.dname ,count(emp .eno) from dept ,emp
where emp.dno=dept.dno
group by dename

6、select dept.dno ,dept.dname, avg(salary) from dept ,emp
where emp.dno=dept.dno and salary>3000
group by dename ,dno

F. SQL資料庫題目!!!急急急!!!

1)Students表:包含SNo,SName,SSex,SClass,SAge 其中SNo為主鍵
2)Courses表:包含CNo,CName,CGrade(開設學期),CScore(學分)、IsProfession(是否是專業課) 其中CNo為主鍵
3)Scores表:包含SNo,CNo,Score 其中SNo和CNo分別與Students中的SNo和Courses中的CNo有外鍵關系。
1)查詢全班年齡在20歲至24歲之間的所有學生的信息。
select * from Students where SAge>=20 and SAge<=24 (是否包含20和24,修改相關=號)
2)查詢開設學期為2的所有的專業課程號、課程名稱、和學分。
select CNo,CName,CScore from Courses where CGrade=2 and IsProfession=1(假設專業課是此值為1)
3)查詢學號為000004學生的所有課程的成績。
select Scores.SNo,Scores.CNo,Courses.CName,Scores.Score from Scores INNER JOIN Courses ON Scores.CNo=Courses.CNo where Scores.SNo='000004'
4)查詢姓名為「王明」的學生的所有及格課程的課程名稱和成績。
select Scores.SNo,Scores.CNo,Courses.CName,Scores.Score from Scores INNER Join
Courses ON Scores.CNo=Courses.CNo where Scores.SNo in (select SNo from Students where SName='王明') and Scores.Score>=60
5)查詢班級為「0401」的所有學生的課程名為「SQL資料庫管理」的成績,按成績降序的方式排列,如果成績相同,則按照學號進行排列。
select Scores.SNo,Scores.CNo,Courses.CName,Scores.Score from Scores INNER Join
Courses ON Scores.CNo=Courses.CNo where Scores.SNo in (select SNo from Students
where SClass='0401' order by Scores.Score

G. SQL資料庫試題求解

------------------------------------------------------
create table students(st_id varchar(20),st_name varchar(50),sex varchar(10))

insert into students(st_id,st_name,sex)
select 'st001','張傑', '男' union all
select 'st002', '公孫燕飛' ,'男' union all
select 'st003', '王楠', '女' union all
select 'st004', '王偉', '男' union all
select 'st005','李燕紋', '女' union all
select 'st006', '孫武' ,'男'
select *
from students

create table teachers(t_id varchar(20),t_name varchar(50),t_lesson varchar(50))

insert into teachers

select 't001', '張老師' ,'數學' union all
select 't002', '李老師', '英語'

delete from results
create table results(r_id varchar(20),r_fenshu int,r_stid varchar(50),r_tid varchar(50))

insert into results
select 'r001','90', 'st001', 't002' union all
select 'r002', '68', 'st005', 't001' union all
select 'r003', '92', 'st003' ,'t001' union all
select 'r004', '82', 'st006', 't002' union all
select 'r005', '70', 'st002', 't002' union all
select 'r006', '86', 'st002', 't001' union all
select 'r007', '57', 'st003', 't002' union all
select 'r008', '76', 'st006', 't001' union all
select 'r009', '55', 'st001', 't001' union all
select 'r010', '77', 'st004', 't002' union all
select 'r011', '58', 'st005', 't002'
----------------------------------------------------------
1.
select st_id
from students
where st_name = '王偉'

2.select st_id,st_name
from students
where st_name like '__燕%'

3 select st_name,len(st_name) as 名字長度
from students
where sex ='男'

4 select min(r_fenshu) as 最低分數
from teachers t inner join results r on t.t_id =r.r_tid
where t_lesson ='數學' --這個是不考慮成績中有null值的
5 select s.st_id as 學生編號,r_fenshu as分數,r_tid as 課目號
from students s inner join results r on s.st_id =r.r_stid
where s.sex='女'
--如果還要課目的名稱的話請用下面的
select s.st_id as 學生編號,r.r_fenshu as 分數,r.r_tid as 課目號,t.t_lesson as 課目名稱
from students s inner join results r on s.st_id =r.r_stid
inner join teachers t on r.r_tid = t.t_id
where s.sex='女'

6 select avg(r.r_fenshu)
from results r inner join teachers t on r.r_tid = t.t_id
where t.t_lesson='英語'

7.select *
from students s inner join results r on s.st_id =r.r_stid
inner join teachers t on r.r_tid = t.t_id
where s.st_id in (select top 2 st_id from students order by st_id desc)
order by s.st_id desc

8 select sum(r.r_fenshu) as 總分
from results r inner join students s on r.r_stid =s.st_id
where s.st_name = '王楠'
9.select distinct s.st_id,s.st_name
from students s inner join results r on s.st_id = r.r_stid
where st_id not in (select r_stid from results where r_fenshu<60) and st_id not in (select r_stid from results where r_fenshu >=90)

10 update results
set r_fenshu = r_fenshu + 10
--如果分數不可能大於100請用這句 set r_fenshu = case when r_fenshu + 10 <=100 then r_fenshu + 10 else 100 end
where r_stid in (select st_id from students where sex='女')

1 進階題
select t.t_name,count(*)
from students s,teachers t,results r
where r.r_tid = t.t_id
and s.st_id =r.r_stid
and r.r_fenshu >= 60
and t.t_id in (select t_id from teachers where t_lesson='數學' )
--and t_lesson='數學'
group by t.t_name

2

select top 1 sum(r_fenshu) as 總分,t.t_lesson,t_id,t_name
from results r,teachers t
where r.r_tid = t.t_id
group by t.t_lesson,t_id,t_name
order by 總分 desc

3. delete from results where r_stid in (select r_stid from results group by r_stid having count(r_tid) = 1)

1 選做題
select d.name from sysobjects d where d.xtype='U'
2.select top 5 * from students order by newid()

H. sql資料庫一道試題幫忙做做

---1)
創建一張學生表,包含以下信息,學號,姓名,年齡,性別,家庭住址,聯系電話
CREATE
TABLE
student
(

[id]
[int]
IDENTITY(1,1)
NOT
NULL,

[student_id]
[nvarchar](50)
NULL,

[studen_name]
[nvarchar](50)
NULL,

[age]
[int]
NULL
,

[sex]
[nvarchar](5)
NULL,

[address]
[nvarchar](200)
NULL,

[tel]
[nvarchar](20)
NULL
)
--2)
修改學生表的結構,添加一列信息,學歷
ecation
alter
table
student
add
ecation
nvarchar(10)
NULL
--3)
修改學生表的結構,刪除一列信息,家庭住址
alter
table
student
drop
column
address
--5)
修改學生表的數據,將電話號碼以11開頭的學員的學歷改為「大專」
update
student
set
ecation='大專'
where
tel
like
'11%'
--6)
刪除學生表的數據,姓名以C開頭,性別為『男』的記錄刪除
delete
student
where
studen_name
like
'C%'
and
sex='男'
--7)
查詢學生表的數據,將所有年齡小於22歲的,學歷為「大專」的,學生的姓名和學號示出來
select
studen_name,student_id
from
student
where
age<12
and
ecation='大專'
--8)
查詢學生表的數據,查詢所有信息,列出前25%的記錄
select
TOP
25
PERCENT
*
from
student
--9)
查詢出所有學生的姓名,性別,年齡降序排列
select
studen_name,sex,age
from
studen
order
by
age
desc
--10)
按照性別分組查詢所有的平均年齡
select
avg(age)
as
age
from
studen
group
by
sex

I. 求諸位大俠幫我看看這個SQL資料庫的題目

一、單項選擇題(2分*15)
1. 一個班級有多個學生,每個學生只能屬於一個班級,班級與學生之間是(B )
A. 一對一的聯系 B.一對多的聯系 C.多對一的聯系 D.多對多的聯系
2. 一個關系中的主屬性( A )
A.至多一個 B.可多個 C.必須多個 D.可以零個
3. 顧客可到多個商場購物,商場有很多顧客購物,商場與顧客之間的聯系方式是( C )
A.1:1 B.1:n C. m:n D.m:1
4. 實體間的聯系方式有( D )
A.1種 B.2種 C.3種 D.4種
5. 按給定條件從一個關系中挑選出指定的屬性組成一個新關系運算是( A )
A.選擇 B.投影 C.連接 D.自然連接
6. DB,DBS,DBMS三者之間的關系是( C )
A.DB包括DBS和DBMS B.DBS包括DB和DBMS
C.DBMS包括DBS和DB D.DBS與DB和DBMS無關
7. 表示資料庫的概念模型一般使用( C )
A.用戶活動圖 B.數據流圖
C.E-R圖 D.流程圖
8.DB是指( D )
A.資料庫應用軟體B.資料庫管理軟體C.數據的集合 D.資料庫系統
9.資料庫系統達到了數據獨立性,是因為採用了( D )
A.層次模型 B.網狀模型 C.關系模型 D.三級模式結構
10.數據獨立性是指( B )之間相互獨立,彼此不受影響。
A.應用程序和用戶 B.應用程序和數據 C.資料庫和用戶 D.資料庫和程序員
11.在SQL語言中,創建基本表應使用( B )語句
A.CREATE SCHEMA B.CREATE TABLE
C.CREATE VIEW D.CREATE DATABASE
12.與WHERE AGE BETWEEN 18 AND 20 完全等價的是( D )
A.WHERE AGE > 18 AND AGE < 20
B.WHERE AGE >= 18 AND AGE < 20
C.WHERE AGE > 18 AND AGE <= 20
D.WHERE AGE >= 18 AND AGE <= 20
13.在WHERE子句的條件表達式中,可以用( B )通配符與所在位置的零個或多個字元相匹配。
A. * B.% C. ? D. __
14.在WHERE子句的條件表達式中,可以用( D )通配符與所在位置的單個字元相匹配 A. * B.% C. ? D. __
15.SQL屬於( C )資料庫語言
A. 層次型 B. 網狀型 C.關系型 D.面向對象型

二、填空題(1分*10)
1. 資料庫的三級模式指的是 外模式、模式和內模式 。
2.一個表上只能創建 ( 1 ) 個主鍵約束。
3.資料庫的一個表中的一個欄位被定義為char數據類型,長度為15,在這個欄位中輸入「123」,則此欄位佔用 ( 15 ) 個存儲空間。
4.使用T-SQL語句刪除表需要使用( DROP TABLE ) 語句。
5.資料庫的一個表中的一個欄位被定義為varchar數據類型,長度為15,在這個欄位中輸入「123」,則此欄位佔用 ( 3 ) 個存儲空間。
6. 資料庫系統的數據獨立性包括 ( 邏輯 ) 獨立性和 ( 物理 ) 獨立性。
7.創建資料庫的T-SQL語句是 ( CREATE DATABASE ) 。

J. 請回答,下面的SQL資料庫題目,不勝感激!

這題都不好好做,以後怎麼工作噢。。。

4://考察in語法
select * from kc where 課程號 in ('101','102','302');

6.//考察對group by的理解
select 課程號,count(*) from xs_kc group by 課程號;

8.//考察對where語句中的and的理解。
select count(*) from xs_kc where 課程號=(select 課程號from kc where 課程名='c語言') and 成績>60;