SQL SERVER 上机测试小编亲自整理哦

1. 简答题  ( 10.0 分 )

创建数据库test(使用SQL语句)test,主文件的逻辑名称为test,物理名称为test.mdf,初始大小为6MB,最大尺寸为20MB,增长速度为15%;数据库的日志文件逻辑名称为test_log,物理文件的名称为test.ldf,初始大小为3MB,最大尺寸为30MB,增长速度为2MB。路径D盘test文件夹

   CREATE DATABASE test ON PRIMARY 

   ( NAME = 'test',

FILENAME = 'D:\test\test.mdf',

SIZE=6MB,

MAXSIZE=20MB,

FILEGROUP=15%

)

LOG ON

( NAME = 'test_log',

FILENAME = 'D:\test\test.ldf',

SIZE=3MB,

MAXSIZE=30MB,

FILEGROUP=2MB

)

2. 简答题  ( 10.0 分 )

    在test数据库中创建表student(studentno nchar(10),studentname nchar(10),age int),其中主键为studentno。

CREATE TABLE student 

(

studentno nchar (10) PRIMARY KEY ,

studentname nchar (10) NOT NULL ,

age int NOT NULL 

)

3. 简答题  ( 10.0 分 )

    利用SQL语句修改表student,插入列phone nchar(11)

INSERT INTO student VALUES ('phone nchar(11)')

4. 简答题  ( 10.0 分 )

    利用SQL语句为student表中的年级列创建规则“age between 18 and 25”

CREATE RULE nianji 

AS @age BETWEEN 18 AND 25

5. 简答题  ( 10.0 分 )

    在student 表中插入('1202023456',‘李磊’,19,‘18890876543’)(‘1202023457’,‘张宇’,20,‘17789403289’)修改张宇的手机号为‘19909876521’

INSERT INTO student VALUES ('1202023456','李磊',19,'18890876543')('1202023457','张宇',20,'17789403289')

UPDATE student SET phone = '19909876521'

6. 简答题  ( 10.0 分 )

    为student表中的列phone创建唯一性约束

ALTER TABLE student ADD CONSTRAINT UN_phone UNIQUE(phone)

7. 简答题  ( 10.0 分 )

    查询student表中年龄大于平均年龄的学生信息

select * from student

Where DATEDIFF(year,birthdate,getdate()) > (

select avg(DATEDIFF(year,birthdate,getdate()))

from student)

go

8. 简答题  ( 10.0 分 )

    声明一个长度为10的nchar变量tname,并为它赋值‘SQL的测试’。

DECLARE @tname nchar(10)

SET @tname = 'SQL的测试'

SELECT @tname

9. 简答题  ( 10.0 分 )

    为查询语句select * from student 声明一个游标cStudent。

DECLARE cStudent CURSOR STATIC FOR select * from student

10. 简答题  ( 10.0 分 )

    使用FETCH命令访问题9中的游标中的每一条记录。

DECLARE cStudent CURSOR STATIC FOR select * from student

OPEN cStudent

DECLARE @studentno nchar (10) , @sname nchar (5) , @sex nchar (2) , @count nchar (2) , @message nvarchar(37) , @message1 nvarchar(37)

FETCH FROM cStudent INTO @studentno , @sname , @sex  

WHILE @@FETCH_STATUS = 0

BEGIN

fetch next from student_n into @studentno,@sname,@sex

PRINT ''

SELECT @message = ' 学号: ' + @studentno + ' 姓名: ' + @sname + ' 性别: ' + @sex

PRINT @message

END

PRINT ''

CLOSE cStudent

DEALLOCATE cStudent

猜你喜欢

转载自blog.csdn.net/weixin_57398221/article/details/123781087