1. 程式人生 > >資料庫sql語句筆試題--包含建立資料庫、表、插入記錄、查詢等操作

資料庫sql語句筆試題--包含建立資料庫、表、插入記錄、查詢等操作

資料庫筆試題,包含建立資料庫、表、插入記錄、查詢等操作。
1.建立資料庫。要求用SQL語句建立滿足如下要求的資料庫:
(1)資料庫名稱為School;
(2)主資料檔案:邏輯名為SchoolData,檔名為“D:\School\SchoolData.mdf”,檔案初始大小為50MB,檔案的最大大小不受限制,檔案的增長率為20%。

(3)日誌檔案:邏輯名為SchoolLog,檔名為“D:\School\SchoolLog.ldf,”檔案初始大小為10MB,檔案的最大大小為50MB,檔案的增長率為1MB;

create database School  
on primary  
(name='SchoolData',  
filename='D:\School\SchoolData.mdf',  
size=50MB,  
maxsize=unlimited ,  
filegrowth=20%)  
log on(  
name='SchoolLog',  
filename='D:\School\SchoolLog.ldf',  
size=10MB,  
maxsize=50MB,  
filegrowth=1MB  
)  
2.建立資料型別和表,增加約束。

(1)表tblstudent(學生表):



SQL語句如下:

create table tblstudent(  
stuID bigint primary key,  
stuName nvarchar(10) not null,  
stuSex nchar(1) not null,  
stuBirth datetime,  
stuNum nvarchar(18) unique,  
Departed int,  
City nvarchar(10) default '杭州' ,//設定預設值  
constraint ck_tblstudent_stusex check(stuSex IN('男','女') ),//建立約束  
constraint ck_tblstudent_stuBirth check(stuBirth<getdate())  
)   
(2)表tblscore(成績表):

sql語句如下:

create table tblscope(  
stuID bigint ,  
Math int,  
English int,  
Computer int,  
Summary int,  
/*建立外來鍵約束*/  
constraint stuID_FK foreign key (stuID) references tblstudent(stuID),  
constraint ck_tblscope_Math CHECK(Math between 0 and 100 or Math is null),  
constraint ck_tblscope_English CHECK(English between 0 and 100 or English is null),  
constraint ck_tblscope_Computer CHECK(Computer between 0 and 100 or Computer is null)  
)  

3.使用SQL語句插入下表中的資料:

/*批量插入,當新增一條新的記錄時,可以省略欄位名,但每個欄位都必須有值)*/  
insert into tblscope values(1001,70,80,90,null),(1002,45,55,60,null);  

4.找出總成績最高的學生的學號和姓名

 SQL語句如下:

/*第一步:從tblscope表中找出最高成績*/  
select  max(Math+English+Computer) from tblscope   
  
/*第二步:從tblscope表中找出總成績=最高分的那位學生ID*/  
select stuID from tblscope group by stuID having   
sum(Math+English+Computer)=(select  max(Math+English+Computer) from tblscope )  
  
/*第三步:根據最高分查出來的學生stuID再來查詢學生資訊*/  
select stuID,stuName from tblstudent where stuID=  
(select stuID from tblscope group by stuID having   
sum(Math+English+Computer)=(select  max(Math+English+Computer) from tblscope )) 

5.統計男生女生的人數

SQL語句如下:

select stuSex as '性別',count(*) as '人數 'from tblstudent group by stuSex  
轉載自部落格:http://blog.csdn.net/beauxie/article/details/52946590#

恆生電子2016(2018)實習生招聘資料庫試題