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

约束

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

存储过程

相关推荐
liuxiaowei38 分钟前
【绝版教程】新版MySQL DBA高级实战进阶班 MySQL8.0 姜承尧-腾讯数据库总监
数据库·mysql·dba
破碎的南瓜29 分钟前
sqli--sql注入过关笔记(1,2,3,4,5,9)
数据库·笔记·sql
你想知道什么?1 小时前
线性回归-学习笔记
笔记·学习·线性回归
谙弆悕博士1 小时前
系统集成项目管理工程师教程(第3版)笔记——第17章:法律法规和标准规范
笔记·职场和发展·学习方法·业界资讯·软考·法律·法规
吠品2 小时前
Zabbix Web界面误报Server未运行的排查与解决
java·服务器·数据库
SelectDB2 小时前
Apache Doris Variant 动态 JSON 实战教程:从建表到跑通 100M Agent 日志查询
数据库
sg_knight3 小时前
MySQL 存储过程详解:从入门到实战
android·数据库·mysql·database·dba·关系型数据库·db
谙弆悕博士3 小时前
系统集成项目管理工程师教程(第3版)笔记——第4章:信息系统架构
笔记·系统架构·项目管理·创业创新·学习方法·业界资讯·物理
星空露珠3 小时前
28种颜色对应名称,
开发语言·数据库·算法·游戏·lua
踏月的造梦星球4 小时前
DM8 DSC 单机双实例部署
运维·开发语言·数据库