SQL Server 练习

Posted ʚVVcatɞ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server 练习相关的知识,希望对你有一定的参考价值。

练习:《数据库原理》(第四版)

创建数据库 GradeManager(成绩管理)

create database GradeManager;

创建Student、Course、Class、Grade表

切换数据库

use GradeManager;

创建 Studnet 表

CREATE TABLE Student(
Sno CHAR(7)NOT NULL UNIQUE,  /*学号唯一*/
Sname VarChar(20) NOT NULL,  /*学生姓名*/
Ssex CHAR(2) NOT NULL,       /*性别*/
Sage Smallint NULL,          /*年龄*/
Clno CHAR(5) NOT NULL        /*学生所在班级*/
);

创建 Course 表

CREATE TABLE Course(
Cno CHAR(1) NOT NULL UNIQUE,  /*课程号唯一*/
Cname VarChar(20) NOT NULL,  /*课程名称*/
Credit Smallint NULL,        /*学分*/
);

创建 Class 表

CREATE TABLE Class(
Clno CHAR(5) NOT NULL UNIQUE,     /*班级号唯一*/
Speciality VarChar(20) NOT NULL,  /*班级所在专业*/
Inyear Char(4) NOT NULL,          /*入校年份*/
Number Integer NULL,              /*班级人数*/
Monitor Char(7) NULL,             /*班长学号*/
);

创建 Grade 表

CREATE TABLE Grade(
Sno CHAR(7) NOT NULL,          /*学号*/
Cno Char(1) NOT NULL,          /*课程号*/
Gmark Numeric(4, 1) NULL,      /*成绩*/
);

对 Studnet 表添加数据

INSERT INTO Student
VALUES('2000101', '李勇', '男', 20, '00311'),
('2000102', '刘诗晨', '女', 19, '00311'),
('2000103', '王一鸣', '男', 20, '00312'),
('2000104', '张婷婷', '女', 21, '00312'),
('2001101', '李勇敏', '女', 19, '01311'),
('2001102', '贾向东', '男', 22, '01311'),
('2001103', '陈宝玉', '男', 20, '01311'),
('2001104', '张逸凡', '男', 21, '00311');
SnoSnameSsexSageClno
12000101李勇2000311
22000102刘诗晨1900311
32000103王一鸣2000312
42000104张婷婷2100312
52001101李勇敏1901311
62001102贾向东2201311
72001103陈宝玉2001311
82001104张逸凡2100311

对 Course 表添加数据

INSERT INTO Course
VALUES('1', '数据库', 4),
('2', '离散数学', 3),
('3', '管理信息系统', 2),
('4', '操作系统', 4),
('5', '数据结构', 4),
('6', '数据处理', 2),
('7', 'C语言', 4);

对 Class 表添加数据

INSERT INTO Class
VALUES('00311', '计算机软件', '2000', 120, '2000101'),
('00312', '计算机应用', '2000', 140, '2000103'),
('01311', '计算机软件', '2001', 220, '2001103');

对 Grade 表添加数据

INSERT INTO Grade
VALUES('2000101', '1', 92),
('2000101', '3', NULL),
('2000101', '5', 86),
('2000102', '1', 78),
('2000102', '6', 55),
('2000103', '3', 65),
('2000103', '6', 78),
('2000103', '5', 66),
('2000104', '1', 54),
('2000104', '6', 83),
('2001101', '2', 70),
('2001101', '4', 65),
('2001102', '2', 80),
('2001102', '4', NULL),
('2000103', '1', 83),
('2000103', '2', 76),
('2000103', '4', 56),
('2000103', '7', 88);
SnoCnoGmark
2000101192.0
20001013NULL
2000101586.0
2000102178.0
2000102655.0
2000103365.0
2000103678.0
2000103566.0
2000104154.0
2000104683.0
2001101270.0
2001101465.0
2001102280.0
20011024NULL
2000103183.0
2000103276.0
2000103456.0
2000103788.0

(1)给 Studnet 表增加属性 Nation,数据类型为 Varchar(20);

ALTER TABLE Student
ADD Nation Varchar(20) NULL;
SnoSnameSsexSageClnoNation
12000101李勇2000311NULL
22000102刘诗晨1900311NULL
32000103王一鸣2000312NULL
42000104张婷婷2100312NULL
52001101李勇敏1901311NULL
62001102贾向东2201311NULL
72001103陈宝玉2001311NULL
82001104张逸凡2100311NULL

(2)删除 Studnet 表中新增的属性Nation;

ALTER TABLE Student
DROP COLUMN Nation;
SnoSnameSsexSageClno
12000101李勇2000311
22000102刘诗晨1900311
32000103王一鸣2000312
42000104张婷婷2100312
52001101李勇敏1901311
62001102贾向东2201311
72001103陈宝玉2001311
82001104张逸凡2100311

(3)向成绩表中插入记录(“2001110”, “3”, 80);

INSERT INTO Grade
VALUES('2001110', '3', 80);
SnoCnoGmark
2000101192.0
20001013NULL
2000101586.0
2000102178.0
2000102655.0
2000103365.0
2000103678.0
2000103566.0
2000104154.0
2000104683.0
2001101270.0
2001101465.0
2001102280.0
20011024NULL
2000103183.0
2000103276.0
2000103456.0
2000103788.0
2001110380.0

以上是关于SQL Server 练习的主要内容,如果未能解决你的问题,请参考以下文章

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

SQL Server - select语句练习

SQL server语句练习

SQL Server 练习

自已的sql server练习小记

SQL server练习