增删改查
  Ol6Fq6uc0Glb 2024年03月24日 39 0

  1. 增加单条数据

    insert into Department(DepartmentName,DepartmentDesc) values('研发部','这是研发部')--插入单条数据
    
  2. 多条

    /*insert into [Rank](RankName,RankDesc)
    select 'A','A级'union
    select 'B','B级'union
    select 'C','C级' --插入多条数据
    */
    

  • 语法

    delete from 表名 where 条件
    
    • 删除员工表所有记录

      delete from Employee
      
    • 删除研发部中工资大于10000的员工

      update Employee set EmployeeSalary = 10000 where DepartmentId =1 and EmployeeSalary<=10000
      

--关于drop delete truncate
--drop 删除表
--delete 可选删,加入删除自动编号为1,2,3数据,删除数据后,编号将被永远删除,之后再添加数据编号从4,5,6开始
--truncate 必须全部删除表中数据,清空数据后再添加编号依然从1,2,3开始

  • 语法
update 表名 set 字段1=值1,字段2=值2 where 条件
  • 工资调整,每个人工资增加1000

    update Employee set EmployeeSalary +=1000
    
  • 将员工编号为6的工资加100

    update Employee set EmployeeSalary +=100 where EmployeeId = 6
    
  • 将研发部工资低于10000的涨到10000

    update Employee set EmployeeSalary = 10000 where DepartmentId =1 and EmployeeSalary<=10000
    

  • 查询所有列

    select * from Department
    
  • 查询指定列

    select EmployeeName,EmployeeSalary from Employee
    
  • 指定查询后的中文名

    select EmployeeName 员工名,EmployeeSalary*1.2 加薪资后工资 from Employee
    

    image-20240320182227465

条件查询

select * from Employee where EmployeeSalary>=10000 and EmployeeSalary<=20000
select * from Employee where EmployeeSalary between 10000 and 20000
  1. 排序
select * from Employee order by EmployeeSalary asc --默认为asc可不写
select * from Employee order by EmployeeSalary desc --逆序
select top 5 * from Employee order by EmployeeSalary desc --工资最高的5个人
select top 10 percent * from Employee order by EmployeeSalary desc --工资最高的10%个人
  1. null 查询地址没有填写或填写了地址的员工信息

    select * from Employee where EmployeeAddress is null
    select * from Employee where EmployeeAddress is not null
    select * from Employee where EmployeeAddress=''--空字符串
    
  2. 查询工资比大乔高的人的信息

    select * from Employee where EmployeeSalary>(select EmployeeSalary from Employee where EmployeeName = '大乔')
    

模糊查询

like

与通配符搭配

通配符 含义

  • % 包含零个或更多字符的任意字符串。
    _(下划线) 任何单个字符。
    [ ] 指定范围(例如 [a-f])或集合(例如 [abcdef])内的任何单个字符。
    [^] 不在指定范围(例如 [^a - f])或集合(例如 [^abcdef])内的任何单个字符。

eg

  • LIKE '赵%' 将搜索姓赵的人名或者说以汉字‘赵’ 开头的字符串(如 赵刚、赵小刚等)。
    LIKE '%刚' 将搜索以汉字‘刚’结尾的所有字符串(如 刘刚、李小刚等)。
    LIKE '%小%' 将搜索在任何位置包含汉字‘小’的所有字符串(如赵小刚、李小刚、山本小郎等)。
    LIKE '_小刚' 将搜索以汉字“小刚”结尾的所有三个汉字的名称(如 李小刚、赵小刚)。
select * from Employee where EmployeePhone like '183[0-5]%[^2,3]' --前三位为183,第四位为0-5,最后一位不是2,3 

SUBSTRING

select * from Employee where SUBSTRING(EmployeeName,3,1) = '香' --从第三个位置截取一个

聚合函数

  1. AVG

    在 SQL Server 中, AVG() 函数是用于计算指定列中所有数值的平均值的聚合函数。

  2. COUNT

    在 SQL Server 中, COUNT() 函数是用于计算指定列或表达式中的行数的聚合函数。

  3. MAX

    在 SQL Server 中, MAX() 函数是用于计算指定列或表达式中最大值的聚合函数。

  4. MIN

    在 SQL Server 中,MIN() 函数是用于计算指定列或表达式中最小值的聚合函数。

  5. SUM

    在 SQL Server 中,SUM() 函数是一个聚合函数,用于计算指定列的数值之和。

综合案例

select count(*) 员工总数,max(EmployeeSalary) 最高工资,min(EmployeeSalary) 最低工资,SUM(EmployeeSalary) 工资总和,ROUND(AVG(EmployeeSalary),3) 平均工资三位小数 from Employee
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月24日 0

暂无评论

推荐阅读
  az2L92p17wYQ   2024年04月12日   41   0   0 SQL Server
  Ol6Fq6uc0Glb   2024年03月24日   139   0   0 SQL Server
  Ol6Fq6uc0Glb   2024年03月24日   39   0   0 SQL Server
Ol6Fq6uc0Glb
作者其他文章 更多

2024-03-29

2024-03-24

2024-03-24