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
write picture description here
write picture description here

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. Questions
(1) Please think about how to quickly delete all the data in the table using SQL statements?
(2) If you want to implement parallel reading of a huge table to improve reading efficiency, how should it be implemented?

answer

1. Build a database

-- 建立数据库
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. Build database tables

-- 创建表
-- 创建院系信息表
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 editing of the data in the table
(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) Change the title of all teachers whose birth date is over 41 years old and whose title is lecturer to "associate professor".

-- 法一
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) Add the word "Internet" to the front of all the professional names of the "d01006" department.

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

(4) Delete the “d01006” department and all related information.

-- 法一,逐个删除,先删除子表再删夫表
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. Thinking questions
1.
Method 1: delete from deleted data can be rolled back (transaction)
Method 2: truncate table: can delete the entire table
2.
I don't know how to answer it for the time being, because I don't know much about parallelism.

Summarize

  In this exercise, I mainly do some SQL statements to build a database. The operation of building a table is relatively simple. However, some points need to be paid attention to is that the database is being built. When increasing the amount, there are two units of measurement to choose from, filegrowth = When it is 10%, it will grow by 10%. If it is filegrowth = 10, the default unit is m, and the growth will be 10m. This exercise is relatively simple for me, because I have studied mysql database related courses before, and I am familiar with operations such as foreign keys, queries, and insertions. From my personal experience, when I first came into contact with sql server, I felt that sql server operation was no different from mysql operation.

Guess you like

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