您的位置:首页 > 数据库

数据库sql语句笔试题--包含创建数据库、表、插入记录、查询等操作

2017-05-14 12:47 1061 查看
数据库笔试题,包含创建数据库、表、插入记录、查询等操作。
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
4000
语句如下:

/*第一步:从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)实习生招聘数据库试题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐