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

约束

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

存储过程

相关推荐
问商十三载6 分钟前
AI引擎生成式优化服务商怎么选?技术核验清单(附脚本)
数据库·人工智能·机器学习
TDengine (老段)7 分钟前
集群从“能用“到“高可用“:副本、选主与故障切换到底怎么配
大数据·网络·数据库·物联网·oracle·时序数据库·tdengine
小小、码农7 分钟前
Ubuntu / Linux 软件安装完整笔记
linux·笔记·ubuntu
ly76897 分钟前
ISR 收缩与 HW 推进:副本同步的边界条件与 UnderReplicated 排障
数据库·kafka·c#·linq·isr·hw·副本同步
Evand J17 分钟前
【MATLAB例程】三维A*路径规划与TOA-AOA-TDOA融合定位算法。附下载链接
数据库·算法·matlab
xzal1222 分钟前
Python 列表操作笔记:append / extend / + / pop
笔记·python
Dxy123931021622 分钟前
MySQL 条件注释语法
数据库·mysql
小小龙学IT25 分钟前
Go 语言 database/sql 标准库深度解析:从连接池到驱动抽象
数据库·sql·golang
维克兜率天30 分钟前
【维克】价差交易实战:配对交易的核心参数设置
大数据·笔记·算法·量化
Seraphina3634 分钟前
SQL注入(一)
数据库·经验分享·笔记·sql·网络安全