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 begin
    set @studentCount=80
    -- ===年级信息===
    if @classCount=1 begin
        set @className='一年级'
    end
    else if @classCount=2 begin
        set @className='二年级'
    end
    else begin
        set @className='三年级'
    end
    set @classCount+=1
    -- ===/年级信息===

    -- ===遍历创建学生记录===
    while @studentCount>0 begin
        -- 生成名字
        declare @name nvarchar(50)
        declare @nameLength int
        set @nameLength=1+round(rand()*1,0)
        set @name=(select name from @surnameTable where id=round(rand()*20+1,0))
        while(@nameLength>0) begin
            set @name+=(select name from @nameTable where id=round(rand()*20+1,0))
            set @nameLength-=1
        end

        -- ===年级课程===
        -- 语文
        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-=1
    end

end

新建一个查询,把代码粘贴进去执行,执行完之后如图。

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的数据

猜你喜欢

转载自blog.csdn.net/qq_51528941/article/details/130018173