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

约束

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

存储过程

相关推荐
合尘猫5 分钟前
AI客服国标落地一个月:从工程角度看,真正要改的是三处数据结构
数据库·人工智能·架构
ljt272496066111 分钟前
Vue笔记(十五)--自定义引用
笔记·1024程序员节
xieyan081120 分钟前
笔记不只是仓库:让 AI 帮旧想法重新参与思考
人工智能·笔记
Maynor9961 小时前
免费 Cloudflare 只有 10 个 D1?满了之后的「穷鬼打法」:表前缀复用同一个库
数据库·后端·云计算·cloudflare
江屿风1 小时前
【Linux-System】【System V共享内存:从原理到实战的完整解析】流食般投喂
linux·运维·服务器·笔记·系统架构·1024程序员节
東隅已逝,桑榆非晚1 小时前
搜索二叉树
c++·笔记·学习·1024程序员节
冰暮流星1 小时前
mysql之索引设计的原则
数据库·mysql·1024程序员节
昵称画9 小时前
交接班记录电子化:三班之间的信息不漏
开发语言·数据库·人工智能·低代码·php
缓慢更新10 小时前
审批的加签与会签:多人意见怎么合
android·java·数据库·低代码·rxjava
跨境联盟11 小时前
大健康实体如何搭建产品差异化:对比普通营养品与合规跨境膳食补充剂服务模式
数据库·ai·健康医疗·健康管理·精准营养