当前位置: 首页 > news >正文

sql server笔记

创建数据库

use master
go

create database stuuuuu

//删除数据库

if db_id ('$$$') is not null

Drop database [$$$]
go


//新建表

USE [studyTest]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Table_1](
	[id] [int] NULL,
	[name] [varchar](10) NULL
) ON [PRIMARY]
GO



//删除表
USE [studyTest]
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Table_1]') AND type in (N'U'))
DROP TABLE [dbo].[Table_1]
GO

插入  insert into aaa ( name ) values ('asd')

/* 更新*/
update aaa set name='我是' where id=1


/* 删除*/
delete aaa where id =1
go

/*条件查询*/
select id as '标识',
        name as '姓名',
        score as 分数
        from aaa where score >20 and score <90 
        /* score >20 and score <90 可以替换成between 50 and 90
            不为空条件  where score is not null*/

/*百分比查询数据*/
select top(50) percent id as '标识',
        name as '姓名',
        score as 分数
        from aaa where score >20 and score <90         

caseWhen判断

select name,score ,case when score >20 and    score<50 then '不及格' 
                        when   score>60 then '好' 
                        else '零'
                        end as '等级'
                    from aaa order by score asc


// in
select name,score from aaa where id in (2)
// like
select name,score from aaa where name like 'a%'

// with
with tt as (select name,score from aaa where name like 'a%')
select * from tt
//distinct
select distinct id from aaa
//order by 升序 asc 降序 desc
select *from aaa order by score asc

//聚合group by
select  name,sum(score) scoresum from aaa group by name

//左连接   left inner right  full join
select *from Company c left join SysUser s on c.Id=s.CompanyId and .....

// union unionall 把结构相同的表合并,union可以去重, unionall 不会去重
 

//递归查询

with  con(id,meanname,parentid,le) as 

 (

               select id,meanname,parentid,le from meaninfo  where id=1

                union all

                select a.id .... , le = le+1 from meaninfo a join con a.parent = con.id

)

 select  id,meanname,parentid,le  from con

数据类型 

date 年月日3byte  datetime 年月日时分秒8byte time 时分秒5byte

约束 

主键约束,外键约束 ,非空约束,唯一约束,检查约束

存储过程

相关文章:

  • 15KM无线数字图传,双路全高清视频无人机遥控器技术详解
  • C++之string类的模拟实现(超详细)
  • 目标检测tricks
  • Python 3.11 69 个内置函数(完整版)
  • Centos服务器GCC安装
  • 解决后端跨域问题
  • 形式化数学编程在AI医疗中的探索路径分析
  • CaffeineCache自定义缓存时间
  • 数字IC低功耗后端设计实现之power gating和isolation技术
  • “深入解析 SQL Server 子查询:从基础到应用”
  • 江协科技/江科大-51单片机入门教程——P[1-1] 课程简介P[1-2] 开发工具介绍及软件安装
  • 《人工智能之高维数据降维算法:PCA与LDA深度剖析》
  • HBuilder X中,uni-app、js的延时操作及定时器
  • AOP基础-01.快速入门
  • FMEA软件系统在制造业应用的必要性解析
  • 【Linux】初始操作系统和进程(一)
  • deepseek sse流式输出
  • 音乐游戏Dance Dance Revolution(DDR)模拟器
  • 【LLM】本地部署LLM大语言模型+可视化交互聊天,附常见本地部署硬件要求(以Ollama+OpenWebUI部署DeepSeekR1为例)
  • RAGS评测后的数据 如何利用influxdb和grafan 进行数据汇总查看
  • 网站建设 论文/大数据营销系统多少钱
  • 中央农村工作会议时间/九幺seo优化神器
  • 排名前50名免费的网站/app推广怎么做
  • 网站建设交易/西安专业网络推广平台
  • 请解释网站开发的主要流程/龙岗seo优化
  • 免费发布推广信息软件/英文seo外链发布工具