当前位置:网站首页>[database] SQL Server quickly creates tables to simulate departments, courses, teachers, students and scores
[database] SQL Server quickly creates tables to simulate departments, courses, teachers, students and scores
2022-06-22 21:55:00 【Full stack small 5】
I'm in 「 Nuggets · Sailing plan 」
In this article , It is mainly about creating tables and how to quickly simulate table data , Convenient and fast for query testing and verification
Compared with addition, deletion and modification , Queries for any system , Are the main functions , And some operations can only be carried out after there is a certain amount of data , such as : Statistics of average scores of students, etc
- List of main knowledge points
| Number | Language or plug-in | Knowledge point | explain |
|---|---|---|---|
| 1 | sql server | create table | key word , Create table |
| 2 | sql server | indentity | Self growth function |
| 3 | sql server | primary key | Set the primary key id |
| 4 | sql server | len() | Statistical length |
| 5 | sql server | rand() | Random function ,0~1 Between the value of the |
| 6 | sql server | substring() | Intercepting string ,substring('',1,1) |
| 7 | sql server | convert() | Data type conversion ,convert(int,'') |
| 8 | sql server | left join | Make connections , Mainly the left table , There will be 3 * 5=15 The situation of this record , It needs to be used properly on Conditions |
| 9 | sql server | avg() | averaging |
【 Query exercise requirements 】
1) List presence 86 Name and course number of students with above grades
2) List “C Language ” Students whose grades are lower than average
【 Table design and analysis 】
1) Pass the above requirements , The following can be designed simply 5 A watch , Omit the class table for the time being
2) Department table
Department number 、 Department name
- Create table
create table system_table
(
system_id int identity(1,1) primary key,
system_name nvarchar(50)
)
- Analog data
-- Suppose the school has the following department data : department of business 、 Management Department 、 Department of Computer Science 、 Architecture Department 、 Art Department ,5 Personal records
insert into system_table(system_name)
values(' department of business '),(' Management Department '),(' Department of Computer Science '),(' Architecture Department '),(' Art Department ')
3) The curriculum Department number 、 Course number 、 Course name
create table course_table
(
course_id int identity(1,1) primary key,
course_name nvarchar(50),
system_id int
)
- Analog data
-- At this time, the data is different , There will be associated tables , Different departments have different courses
-- Here we only simulate the computer system 3 Course ,C Language 、 Software Engineering 、 database
insert into course_table(course_name,system_id)
values('C Language ',3),(' Software Engineering ',3),(' database ',3)
3) Teachers list
Department number 、 Course number 、 Teacher number 、 Teacher's name
create table teacher_table
(
teacher_id int identity(1,1) primary key,
teacher_name nvarchar(50),
course_id int,
system_id int
)
- Analog data
-- It is also related , Simulate here 3 Bar record , Zhang San 、 Li Si 、 Wang Wu
insert into teacher_table(teacher_name,course_id,system_id)
values(' Zhang San ',1,3),(' Li Si ',2,3),(' Wang Wu ',3,3)
4) Student list
Department number 、 Course number 、 Teacher number 、 Student number 、 The student's name 、 Gender
create table student_table
(
student_id int identity(1,1) primary key,
student_name nvarchar(50),
sex int, --1= schoolboy ,0= girl student
teacher_id int,
course_id int,
system_id int
)
- The data simulation
declare @myName nvarchar(50)
declare @surname_arr nvarchar(1000)
declare @name_arr nvarchar(1000)
set @surname_arr=' Wang Li Zhang Liu Chen Yang Huang Zhao Wu Zhou Xu sun Ma Zhu Hu Guo '
set @name_arr=' Love dream box Yu Yu Si Pei poem purple string sharp moon summer phantom shadow dark according to dancing Ling drop Yu Yingying '
declare @sn_length int
declare @rand_index int
-- Analog data : Department of Computer Science 、C Language courses 、 Mr. Zhang San 30 Famous student
declare @student_count int
set @student_count=30
declare @index int
set @index=0
declare @sex int
set @sex=1
while @index<@student_count begin
set @myName='' -- Initial value of Fu , Otherwise it won't work + plus
-- Get the last name value
if len(@surname_arr)>0 begin
set @sn_length=len(@surname_arr)
set @rand_index=convert(int,(rand()*@sn_length))-1
set @myName+=substring(@surname_arr,@rand_index,1)
end
-- Get the name
if len(@name_arr)>0 begin
set @sn_length=len(@name_arr)
set @rand_index=convert(int,(rand()*@sn_length))-1
set @myName+=substring(@name_arr,@rand_index,2)
end
-- Gender
set @sex=convert(int,(rand()*2))
insert into student_table(student_name,sex,teacher_id,course_id,system_id)
values(@myName,@sex,1,1,3)
set @index+=1
end
- Inquire about
select a.*,b.teacher_name,c.course_name,d.system_name
from student_table a
left join teacher_table b on b.teacher_id=a.teacher_id
left join course_table c on c.course_id=a.course_id
left join system_table d on d.system_id=a.system_id
5) Student transcript
Student number 、 Department number 、 Course number 、 Teacher number 、 fraction
create table student_score_table
(
score_id int identity(1,1) primary key,
score int,
student_id int,
teacher_id int,
course_id int,
system_id int
)
- Analog data
declare @index int
set @index=1
declare @score int
set @score=0
while @index<=30 begin
set @score=convert(int,(rand()*50))+50
insert into student_score_table(score,student_id,teacher_id,course_id,system_id)
values(@score,@index,1,1,3)
set @index+=1
end
- Inquire about
select a.*,a2.student_name,b.teacher_name,c.course_name,d.system_name
from student_score_table a
inner join student_table a2 on a2.student_id=a.student_id
left join teacher_table b on b.teacher_id=a.teacher_id
left join course_table c on c.course_id=a.course_id
left join system_table d on d.system_id=a.system_id
【 Formal inquiry of the requirements mentioned above 】
1) List presence 86 Name and course number of students with above grade
select a.score,c.student_name,a.course_id,b.course_name
from student_score_table a
left join course_table b on b.course_id=a.course_id
left join student_table c on c.student_id=a.student_id
where a.score>86
2) List “C Language ” Students whose grades are lower than average
It's used here , The average score of the inline sub query is
select a.score,b.avg_score,a.student_id,c.student_name
from student_score_table a
left join(
--C Average score of language courses
select course_id,avg(score) as avg_score
from student_score_table
where course_id=1
group by course_id
) as b on b.course_id=a.course_id
left join student_table c on c.student_id=a.student_id
where a.score<b.avg_score
边栏推荐
- Kali2021 installing the rtl8188gu wireless network card [tl-wn726n] driver
- RealNetworks vs. Microsoft: the battle in the early streaming media industry
- An example of 89 Oracle SQL writing and optimizer defects
- Lesson 030: file system: introduce a big thing | after class test questions and answers
- The problem that Jericho can't play the prompt tone in the music mode using the interrupt mode [chapter]
- [book delivery at the end of the article] AI has spread all over the Internet to color old photos. Here is a detailed tutorial!
- Five uses of 87 with as
- 基于AI驱动大分子药物发现,「华深智药」获近5亿元A轮融资
- 第033讲:异常处理:你不可能总是对的2 | 课后测试题及答案
- TC397 Flash
猜你喜欢

Lesson 026: Dictionary: when the index is not easy to use 2 | after class test questions and answers

微软 Edge 浏览器将支持网络测速,内置计算器和单位转换工具

勒索病毒横行下设备该如何进行加密防护

第018讲:函数:灵活即强大 | 课后测试题及答案
![Jerry's problem of opening the near end of four channel call [chapter]](/img/54/d74a90e37deb2d3929f019d695f9ee.png)
Jerry's problem of opening the near end of four channel call [chapter]
![Jerry's dynamic switching EQ document [chapter]](/img/2d/9a0245b87fb05ea61dbfc7922ea766.png)
Jerry's dynamic switching EQ document [chapter]

300. longest increasing subsequence ●●

第029讲:文件:一个任务 | 课后测试题及答案

什么是数据资产?数据资产管理应该如何落地?

杰理之开启四声道通话近端卡顿问题【篇】
随机推荐
Optimization solver | gurobi's Mvar class: a sharp tool for matrix modeling and an alternative solution to dual problems (with detailed cases and codes attached)
The second harmonyos application development training
杰理之列免晶振一拖八烧录升级【篇】
When the AUX1 or aux2 channel is used in Jerry's aux mode, the program will reset the problem [chapter]
es 按条件查询数据总条数
什么是数据资产?数据资产管理应该如何落地?
第026讲:字典:当索引不好用时2 | 课后测试题及答案
Can the characteristics of different network structures be compared? Ant & meituan & NTU & Ali proposed a cross architecture self supervised video representation learning method CaCl, performance SOTA
Niuke's height in the 52nd monthly race B (thinking problem simulation problem)
90- review of several recently optimized Oracle databases
84- I am on Internet & lt; 52 SQL statement performance optimization strategies & gt; Some views of
Lesson 016: sequence | after class test questions and answers
Lesson 019: function: my site listen to my after-school test questions and answers
RuntimeError: CUDA error: CUBLAS_STATUS_EXECUTION_FAILED when calling `cublasSgemmStridedBatched( ha
HarmonyOS应用开发培训第二次
牛客 52次月赛 B牛牛的身高 (思维题 模拟题)
PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
Research hotspot - Official publicity! The release time of JCR zoning and impact factors will be determined in 2022!
Lesson 022: function: recursion is god horse after class test questions and answers
RealNetworks vs. Microsoft: the battle in the early streaming media industry