SQL操作复习题及答案
更新时间:2023-11-12 11:54:01 阅读量: 教育文库 文档下载
- Sql投影操作推荐度:
- 相关推荐
- 1 -
1 数据库操作
1.创建数据库:
操作1.1:创建一个test数据库,其主数据文件逻辑名test_data,物理文件名test_data.mdf,初始大小10MB,最大尺寸为无限大,增长速度1MB;数据库日志文件逻辑名称为test_log,物理文件名为test_log.ldf,初始大小为1MB,最大尺寸为5MB,增长速度为10%。
参考答案:
create database Test ON primary ( ) LOG ON ( ) GO
name = test_log,
filename = 'd:\\test\\test_log.ldf', size = 1MB, maxsize = 5MB, filegrowth = 10% name = test_data,
filename = 'd:\\test\\test_data.mdf', size = 5MB,
maxsize = unlimited, filegrowth = 1MB
2.查看数据库属性:
操作1.2:使用T-SQL语句查看数据库test属性 参考答案:
EXEC sp_helpdb test
3.删除数据库:
操作1.3:使用T-SQL语句删除数据库test 参考答案:
drop database Test
2 表操作
1.创建表:
操作2.1:创建学生表:
表名:student 长度 说明:学生基本信息表 空值 列约束 说明 属性列 数据类型 - 2 -
st_id st_nm st_sex st_birth st_score st_date st_from st_dpid st_mnt nVarChar nVarChar nVarChar datetime int datetime nChar nVarChar tinyint 9 8 2 20 2 Not Null Not Null Null Null Null Null Null Null Null PK 学生学号 学生姓名 学生性别 出生日期 入学成绩 入学日期 学生来源 所在系编号 学生职务 参考答案:
USE test GO
CREATE TABLE student (
st_id nVarChar(9) primary key NOT NULL , st_nm nVarChar(8) NOT NULL , st_sex nVarChar(2) NULL , st_birth datetime NULL , st_score int NULL , st_date datetime NULL , st_ from nVarChar(20) NULL , st_dpid nVarChar(2) NULL , st_ mnt tinyint NULL ) GO 操作2.2:创建课程信息表:
表名:couse 长度 4 20 说明:课程信息表 空值 Not Null Not Null Null Null 列约束 PK 说明 课程编号 课程名称 课程学时 课程学分 属性列 cs_id cs_nm cs_tm cs_sc 数据类型 nVarChar nVarChar int int 参考答案: USE test GO
CREATE TABLE couse (
cs_id nVarChar(4) primary key NOT NULL , cs_nm nVarChar(20) NOT NULL , cs_tm int NULL , cs_sc int NULL ) GO 操作2.3:创建选课表:
- 3 -
表名:slt_couse 属性列 cs_id st_id score sltdate 数据类型 nVarChar nVarChar int datetime 长度 4 9 说明:选课表 空值 Not Null Not Null Null Null 列约束 FK FK 说明 课程编号 学生编号 课程成绩 选课日期 参考答案: USE test GO
CREATE TABLE couse (
cs_id nVarChar(4) NOT NULL , st_id nVarChar(9) NOT NULL , score int NULL ,
sltdate datetime ) GO
操作2.4:创建院系信息表:
表名:dept NULL
长度 2 20 8 12 说明:院系信息表 空值 Not Null Not Null Null Null 列约束 说明 系编号 院系名称 院系主任 联系电话 属性列 dp_id dp_nm dp_drt dt_tel 数据类型 nVarChar nVarChar nVarChar nVarChar 参考答案: USE test GO
CREATE TABLE dept (
dp_id nVarChar(2) NOT NULL , dp_nm nVarChar(20) NOT NULL , dp_drt nVarChar(8) NULL , dp_tel nVarChar(12) NULL ) GO
2.修改表结构:
(1)向表中添加列:
操作2.5:为“dept”表添加“dp_count”列(数据类型为nvarchar,长度为3,允许为空) 参考答案:
ALTER TABLE dept ADD dp_count nvarchar(3) NULL (2)修改列数据类型:
操作2.6:修改“dept”表的“dp_count”列数据类型为int 参考答案:
- 4 -
ALTER TABLE dept ALTER COLUMN dp_count int NULL (3)删除表中指定列:
操作2.7:删除“dept”表的“dp_count”列 参考答案:
ALTER TABLE dept DROP COLUMN dp_count
3.删除表
操作2.8:删除“dept”表 参考答案:
DROP TABLE student
4.向表中输入数据记录
操作2.9:分别向“student”表、“couse”表、“slt_couse”表、“dept”表中输入数据记录
3 数据完整性
1.空值约束( NULL )
操作3.1:将student表中的st_sex列属性更改为NOT NULL 参考答案:
ALTER TABLE student ALTER COLUME st_nm nVarChar(8) NOT NULL
2.默认值约束( DEFAULT )
操作3.2:将student表中的st_from列默认值设置为“陕西省” 参考答案:
ALTER TABLE student ADD DEFAULT '陕西省' FOR st_from
3.默认值对象
操作3.3:创建默认值对象df_today为当前日期,并将其绑定到slt_couse表中的sltdate列,然后取消绑定,最后删除默认值对象df_today。
参考答案:
CREATE DEFAULT df_today AS Getdate( ) GO
EXEC sp_bindefault df_today, 'slt_couse.sltdate' GO
EXEC sp_unbindefault 'slt_couse.sltdate' GO
DROP DEFAULT df_today GO
4.检查约束( CHECK )
操作3.4:将slt_couse表中的score列的检查约束设置为>=0且<=100 参考答案:
ALTER TABLE slt_couse ADD CHECK (score>=0 AND score<=100)
- 5 -
5.规则约束对象
操作3.5:创建规则约束对象rl_sex,用于检查性别的取值仅限于“男”和“女”,并将其绑定到student表中的st_sex列,然后取消绑定,最后删除规则约束对象rl_sex。
参考答案:
CREATE RULE rl_sex AS @chksex ’男’ OR @chksex=’女’ 或
CREATE RULE rl_sex AS @chksex IN (’男’, ’女’) GO
EXEC sp_bindrule rl_sex, 'student.st_sex' GO
EXEC sp_unbindrule 'student.st_sex' GO
DROP RULE rl_sex GO
6.主键
操作3.6:将dept表中的dp_id列设置为主键 参考答案:
ALTER TABLE dept ADD PRIMARY KEY (dp_id)
7.唯一性约束( UNIQUE )
操作3.7:将dept表中的dp_nm列设置为唯一性约束 参考答案:
ALTER TABLE dept ADD UNIQUE (dp_nm)
8.标识列
操作3.8:向slt_couse表中添加标识列id,第1行默认值为1,相邻两个标识列间的增量为1 参考答案:
ALTER TABLE slt_couse ADD id INT IDENTITY(1,1) NOT NULL
9.外键( FOREIGN KEY )
操作3.9:被参照表为dept,参照表为student 参考答案:
ALTER TABLE student
ADD FOREIGN KEY (st_dpid) REFERENCES dept(dp_id)
4 数据更新
1.表中插入数据
操作4.1:向dept表插入一条记录,系号11,系名自动控制系,系主任为李其余,电话81234567
INSERT INTO dept VALUES('11', '自动控制系', '李其余', '81234567')
操作4.2:向student表插入一条记录,学号070201001,姓名为王小五,性别为男,出生日期为1990年9月9日,系号为11,其余字段为NULL或默认值
INSERT INTO student(st_id, st_nm, st_sex, st_birth, st_dpid)
正在阅读:
SQL操作复习题及答案11-12
广州版新版小学三年级英语上册同步练习MODULE1-207-29
夜校学习心得体会04-03
外研版(三年级起)小学英语单词汇总默写表(汉译英)10-18
就业困境下的大学生心理需求及应对01-21
跑腿公司大学生创业计划书03-11
嘉兴市城市规划管理技术规定 - 图文03-20
汽车行业典型的78个品质不良的案例分析05-21
cass7.0土方计算详解 - 图文05-21
投资促进局行政服务中心20xx年工作总结02-26
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 复习题
- 答案
- 操作
- SQL
- (部分)国家安全知识试题 及答案(1)
- 关于加强个体税收征管的思考
- 浙大远程生产运营管理离线作业答案
- 如何正确理解经济危机
- 历史教师个人专业发展规划
- 大学迎新晚会简报
- 第七章植物生长物质单元自测题
- 学习足球的心得体会
- 故宫介绍
- 安捷伦6890N气相色谱
- Z5140A、Z5150A使用说明书 - 图文
- 02开题报告2015版 - 图文
- 王安石变法 导学案 - 图文
- 部编版语文一年级下册句子专项训练题
- 高鸿宾《有机化学》(第四版)复习提纲-48课时2013
- 20160321务川县镇南完小教育脱贫攻坚专题会简报 - 图文
- 高级摄影师考试题库10套标注答案版
- 工作座椅一般人类工效学要求
- 华南理工大学硕士学位论文开题报告(适用于全日制硕士专业学位研究生-2014年10月版)
- 名著整理