SQL server 模拟数据进行select基本查询
目录
1 命名规范
2 模拟生成数据
1 新建数据表
2 在数据表中,模拟生成80条数据
2 select查询
count函数
sum函数
avg函数
max函数
min函数
Select+case…when…then语句
top语句
1 命名规范
命名规范:大模块+功能模块+具体功能
1)驼峰式
单词首字母大写
比如:StudentScore
2)半驼峰
首个单词全小写,其他单词首字母大写
比如:studentScore
3)下划线【本次使用】
一般是用于全小写+下划线区分单词
比如:student_score
2 模拟生成数据
1 新建数据表
选中刚刚创建好的数据库,比如:test,然后再点击【新建查询】,最后复制下面的代码即可创建表,右键数据库点击刷新即可查看新建的数据表。
create table student_score
(id int identity(1,1) primary key,studentName nvarchar(50),className nvarchar(50),courseName nvarchar(50),scoreValue int,createTime datetime
)
使用identity(1,1),增量和种子
primary key ,定义id为主键
2 在数据表中,模拟生成80条数据
declare @classCount int
set @classCount=1
declare @className nvarchar(50)
declare @courseName nvarchar(50)
declare @scoreValue int
declare @studentCount int
set @studentCount=80-- 定义一个姓氏表变量,表添加两个字段,自增编号和名字
declare @surnameTable table(id int identity(1,1) primary key, name nvarchar(10)
)-- 定义一个名字表变量,表添加两个字段,自增编号和名字
declare @nameTable table(id int identity(1,1) primary key, name nvarchar(10)
)-- 姓和名字
-- 姓氏
insert @surnameTable values
('王'),('李'),('张'),('刘'),('陈'),('杨'),('黄'),('赵'),('周'),('吴'),
('徐'),('孙'),('马'),('胡'),('朱'),('郭'),('何'),('罗'),('高'),('林')
-- 名称
insert @nameTable values
('芸'),('荷'),('星'),('秋'),('嘉'),('娜'),('珊'),('菲'),('素'),('嫣'),
('慧'),('慕'),('歆'),('巧'),('绮'),('羽'),('静'),('柔'),('采'),('沐'),
('苑'),('姣'),('芳'),('宁'),('沛'),('玥'),('文'),('如'),('悦'),('若'),
('德'),('蕾'),('颜'),('依'),('盼'),('菀'),('秀'),('草'),('莺'),('倩'),
('柳'),('娴'),('彨'),('舒'),('雅'),('淑'),('冉'),('云'),('凝'),('棋')-- 循环遍历班级
while @classCount<=3 beginset @studentCount=80-- ===年级信息===if @classCount=1 beginset @className='一年级'endelse if @classCount=2 beginset @className='二年级'endelse beginset @className='三年级'endset @classCount+=1-- ===/年级信息===-- ===遍历创建学生记录===while @studentCount>0 begin-- 生成名字declare @name nvarchar(50)declare @nameLength intset @nameLength=1+round(rand()*1,0)set @name=(select name from @surnameTable where id=round(rand()*20+1,0))while(@nameLength>0) beginset @name+=(select name from @nameTable where id=round(rand()*20+1,0))set @nameLength-=1end-- ===年级课程===-- 语文set @courseName='语文'set @scoreValue=round(rand()*50+50,0)insert into student_score(studentName,className,courseName,scoreValue,createTime)values(@name,@className,@courseName,@scoreValue,getdate())-- 数学set @courseName='数学'set @scoreValue=round(rand()*50+50,0)insert into student_score(studentName,className,courseName,scoreValue,createTime)values(@name,@className,@courseName,@scoreValue,getdate())-- 英语set @courseName='英语'set @scoreValue=round(rand()*50+50,0)insert into student_score(studentName,className,courseName,scoreValue,createTime)values(@name,@className,@courseName,@scoreValue,getdate())-- ===/年级课程===set @studentCount-=1endend
新建一个查询,把代码粘贴进去执行,执行完之后如图。
2 select查询
子句 | 说明 |
---|---|
SELECT | 要返回的列或表达式 |
FROM | 从中检索的数据表 |
WHERE | 行级过滤 |
GROUD BY | 分组说明 |
HAVING |
组级过滤 |
ORDER BY | 输出排序顺序 |
查询全部
select * from student_score
count函数
count(*)表示计算总行数,括号中写星与列名,结果相同
select count(*) from student_score --* 统计表中的数量
sum函数
sum(列)求此列之和(注:sum运算符与数字类型连用)
select SUM(scoreValue) from student_score --* 求此列之和
avg函数
avg(列) 表示求此列的平均值(注:avg运算符与数字类型连用)
select avg(scoreValue) from student_score --* 求平均值
max函数
max(列)求此列的最大值
min函数
min(列)求此列的最小值
select max(scoreValue) from student_score --* 求最大值
select min(scoreValue) from student_score --* 求最小值
Select+case…when…then语句
case…when…then语句,相当于编程语言中if判断
select a.courseName,(case a.studentName when '0' then '语文' when '1' then '数学' else '未上传' end) as 科目情况
from student_score as a
top语句
top:取表中前多少的数据
select top 10 * from student_score--* 取出前十条数据
select top 50 percent * from student_score--* 取出百分之50的数据
SQL server 模拟数据进行select基本查询相关推荐
- SQL server模拟数据实现稍复杂查询语句
目录 1 DISTINCT(不允许重复)的用法 2 BETWEEN(在某个范围内)的用法 3 IN(属于若干个孤立的值)的用法 4 NULL(空值)的用法 5 ORDER BY(排序)的用法 6 模糊 ...
- 05.SQL Server大数据群集小试牛刀--HDFS查询
05.SQL Server大数据群集小试牛刀--HDFS查询 SQL Server大数据群集查询HDFS ,利用之前创建好的大数据群集,使用官方提供的测试数据进行测试.脚本是官方的脚本,要知道干了什么 ...
- sql server中数据约束相关的查询
根据表名查找数据约束 SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'CMS_EventLog'; SEL ...
- SQL Server T-SQL数据查询
SQL Server T-SQL数据查询 SELECT查询语句 SELECT语句的语法格式 SELECT [ALL|DISTINCT] 列表达式 [INTO 新表名] FROM 表名列表 [WHERE ...
- SQL Server 调优系列进阶篇 - 查询语句运行几个指标值监测
前言 上一篇我们分析了查询优化器的工作方式,其中包括:查询优化器的详细运行步骤.筛选条件分析.索引项优化等信息. 本篇我们分析在我们运行的过程中几个关键指标值的检测. 通过这些指标值来分析语句的运行问 ...
- SQL Server【二】单表查询
查询 计算列 select * from emp; -- *通配符,表示所有的字段 -- from emp 从emp表查询select empno, ename from emp; select en ...
- mysql桦仔_Microsoft SQL Server 2005技术内幕:T-SQL查询笔记
Microsoft SQL Server 2005技术内幕:T-SQL查询笔记 目录 f f f f f f f f 第二章 物理查询处理 分析,代数化,查询优化 f f f f f. 分析--> ...
- 使用SSIS包导入SQL Server FILESTREAM数据
初始配置 (Initial configuration) We have been exploring the SQL Server FILESTREAM feature in this ongoin ...
- 通过DMVS采集并存储SQL Server性能计数器数据
通过DMVS采集并存储SQL Server性能计数器数据 问题描述 我想定期去采集并存储一些SQL Server性能计数器数据,因为服务器太多,又不想用Windows系统自带的性能监视器在每一台服务器 ...
最新文章
- 第一章 基础设施,1.3 阿里视频云ApsaraVideo是怎样让4000万人同时狂欢的(作者:蔡华)...
- 自学Java汇报(3)
- 矩阵运算——平移,旋转,缩放
- php tire树,Immutable.js源码之List 类型的详细解析(附示例)
- 【定位问题】基于matlab GUI RSSI无线定位【含Matlab源码 1054期】
- 陈式新架一路八十三式口诀
- 20.数据集成、数据整合、数据融合
- flutter实现搜索框
- 中规院交通院《上海全球城市综合交通体系承载能力与开发研究》
- oracle weituxinxi,Oracle 语句记录
- 从网易博客转入CSDN
- 【Error】初始化ant design pro项目时遇到“pro 不是内部或外部命令”
- Adobe Acrobat Professional 7.0
- 短视频制作小技巧,配音字幕都要跟上,做好细节才能成功
- 三值的排序 Sorting a Three-Valued Sequence(洛谷 P1459)
- PMCAFF微课17期 - 教你如何用数据玩转移动互联网APP运营(笔记下载)
- 游戏安全反汇编分析开启取消自动攻击call
- Matlab中抽象类和类成员
- Oracle常用问题1000问
- 【虚拟语气练习题】对过去的虚拟