SQL Server Exercise 1 (Basic SQL Server Database Operations)

  This blog is used to record the questions and answers of the sql server exercises, as well as some problems and summaries encountered by the editor.

topic

1. Database Creation
Name : stuManage
Main Data File:
Logical File Name: stuManage_M_data
Physical File Name: stuManage__data.mdf
Initial Size: 3M
Increment: 10%
Limit File Growth: 500M
Times Data File:
Logical File Name: stuManage_N_data
Physical File Name: stuManage__data2 .ndf
Initial Size: 3M
Increment: 1M
Limit File Growth: Unlimited
Log File:
Logical File Name: stuManage_log
Physical File Name: stuManage__log.ldf
Other Information: Default Please set the path of the
physical file to the myData folder of the D drive.

2. Table creation Create the following table
in the stuManag database
这里写图片描述
这里写图片描述

3. SQL editing of data in the
table (1) Add a new department to the table, and add 2 professional information and 2 teacher information for the department. The specific data is prepared by yourself.
(2) Change the title of all teachers whose birth date is over 41 years old and whose title is lecturer to "associate professor".
(3) Add the word "Internet" in front of all the professional names of the "d01006" department.
(4) Delete the “d01006” department and all related information.

4、思考题
(1)请思考如何使用SQL语句快速删除表中的全部数据?
(2)如果要实现一个巨型表并行读取,以提高读取效率,应当如何实现?

解答

1.建数据库

-- 建立数据库
create database stuManage
ON (
name = stuManage_M_data,
fileName = 'D:\myData\stuManage_data.mdf',
size = 5, -- 由于sql server允许的最小值为5m,只能从5m开始,不能从3m开始,否则报错主文件必须至少是 5 MB 才能容纳 model 数据库的副本
maxsize = 500,
filegrowth = 10%)
LOG ON(
name = stuManage_N_data,
fileName = 'D:\myData\stuManage__log.ldf',
size = 3,
filegrowth = 1);

2.建数据库表

-- 创建表
-- 创建院系信息表
use stuManage;
create table department(
    dNo char(6)  primary key, -- 院系号
    dName varchar(20) not null, -- 院系名称
    dTel varchar(11) -- 院系电话
);

-- 创建专业信息表
create table major(
    mNo char(6) primary key, -- 专业号
    mName varchar(40) not null, -- 专业名称
    mFirSubject varchar(40) not null, -- 一级学科
    mSecSubject varchar(40),-- 二级学科
    dNo char(6) constraint dNo_fore foreign key references department(dNo)  -- 院系编号
);

-- 创建教师信息表
create table teacher(
    tNo char(6) primary key, -- 教师号 
    tName varchar(20) not null,-- 姓名
    tSex char(4), -- 性别
    tBirth datetime, -- 出生年月
    tRank varchar(20) not null, -- 职称
    dNo char(6) constraint dNo_teacher_fore foreign key references department(dNo)   -- 院系编号
);

3.表中数据的SQL方式编辑
(1)

-- 添加纪录
-- 添加系记录
insert into department(dNo,dName,dTel) values('161','金融数学与统计学院','1234567');
-- 添加专业记录
insert into major(mNo,mName,mFirSubject,mSecSubject,dNo)
values(151612,'信息与计算科学','理科','数理科','161')
,(151611,'金融数学','理科','数理科','161');
-- 添加教师记录
insert into teacher(tNo,tName,tSex,tBirth,tRank,dNo)
values('1','张三三','男','1990-10-10 12:10:00','教授',161),
('2','李四四','女','1950-10-10 12:10:00','教授',161);

(2)将所有出生年月在年龄超过41岁职称为讲师的教师的职称改为“副教授”。

-- 法一
update teacher set tRank = '副教授' where  datediff(yy,tBirth,getDate())>41 and tRank='讲师';
-- 法二:自连接删除,顺便复习了一下自连接
update tem1 set tRank = '副教授' 
from teacher as tem1
inner join teacher as tem2 on  datediff(yy,tem1.tBirth,getDate())>41 and tem1.tRank='讲师';

(3)将“d01006”号院系所有的专业名称前面加上“互联网”三个字。

update major set mName='互联网'+mName where dNo= 'd01006';

(4)删除“d01006”号院系及其的所有相关信息。

-- 法一,逐个删除,先删除子表再删夫表
delete major where dNo='d01006';
delete teacher where dNo='d01006';
delete department where dNo='d01006';
-- 法二,建立了级联操作,可以直接删除父表
alter table major drop constraint dNo_fore; 
alter table major add constraint dNo_fore foreign key(dNo) references department(dNo) ON UPDATE CASCADE ON DELETE CASCADE;
alter table teacher drop constraint dNo_teacher_fore;
alter table teacher add constraint dNo_teacher_fore foreign key(dNo) references department(dNo) ON UPDATE CASCADE ON DELETE CASCADE;
delete department where dNo='161';

4.思考题
1.
法一:delete from 删除的数据可以回滚(事务)
法二:truncate table: 可以全表删除
2.
暂时还不知怎么解答,因为对于并行没有太多了解。

总结

  这次练习主要做了一些sql语句建库,建表的操作,都是比较简单的,不过需要注意的一些地方是在建库,增长量的时候是可以有两个度量单位选择的,filegrowth =10%时,是以10%来增长,如果是filegrowth =10则默认单位是m,以10m来增长。本次练习对于我来说是比较简单,因为之前有学过mysql数据库的相关课程,对于外键,查询,插入之类的操作已经是比较熟悉了的。就我个人体会来看,初次接触sql server的时候,感觉sql server操作与mysql操作并没有什么不同。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685677&siteId=291194637
Recommended