T-SQL目录汇总1
DDL alter create drop
DML select update delete insert
DCL grant revoke deny
===================================================================================
创建表:
create table 表名(
col_1 数据类型 约束,
col_2 数据类型 约束,
...
id int identity(1,1) primary key
)
数据类型:
varchar char nvarchar int tinyint smallint bitint float numeric(p,n) datetime...
insert into 表名 (字段1,字段2) values (context1,context2)
alter table 表名 add col_1 数据类型 约束
alter table emp add address varchar(25) default "上海"
alter table emp drop column address
alter table emp drop constraint 约束名
drop table 表名
===================================================================================
1.主键
create table A_1(
aId int primary key, ---1
aName varchar(20),
constraint PK_A_1 primary key (aId) ---2
)
*alter table A_1 alter column aId int not null
alter table A_1 add constraint PK_A_1 primary key (aId) ---3
2.外键 1(父表):n(子表)关系,子表里的外键是父表里的主键
create table emp(
empno int primary key,
ename varchar(10),
depno int foreign key references dep (depno),
constraint FK_emp foreign key (..) references dep (..)
)
create table dep(
depno int primary key,
dname varchar(10)
)
alter table emp add constraint foreign key (..) references dep (..)
3.检查约束 check约束
create table people(
pName varchar(20),
pSex varchar(2) constraint ck_sex check (pSex = '男' or pSex = '女'),
pId smallint not null constraint ck_pId check (pId between 1 and 100000)
)
4.default
create table people(
pSex varchar(2) default '男',
constraint ck_Sex check (pId between 1 and 100000)
)
primary key
foreign key references
default check
===================================================================================
1.insert update delete
insert into 表(col_1,col_2,...) values (value1,value2,...)
update 表 set col_1=val_2,col_2,val_2,... [where 条件]
delete from 表 where 条件
2.select
select [col_1,col_2]|* from tab1,tab2,...
[where 条件]
[group by 分组的列名]
[having 聚合函数比较操作]
[order by 排序的列名]
别名:select id,name as 姓名, '年龄'=age from student
select distinct id from tab1 distinct:过滤重复值
*where 条件1[or|and][条件2]......
1.比较操作 > < >= <= <> =
2.逻辑操作 and or not
select * from sc where score not between 70 and 80
--and
--or
--not
--between .. and
3.like % _ select * from tab where sname like '%$_%' escape '$' 张_中
4.in ... where sid in (2,3,4)
5.聚合函数 max min count avg sum
6.group by select sex,count(sex) from student group by sex
select col1[,col2...] from 表 group by col1[,col2...]
7.having ... having count(*) > 2 对聚合函数修饰
8.等值连接操作(内连接)
select tname,cname from teacher t,course c where t.tno = c.tno
select tname,cname from teacher t inner join course c on t.tno = c.tno
9.外连接
left outer join ... on ...
right outer join ... on ...
full outer join ... on ...
===================================================================================
1.where子查询
2.from子查询
3.in子查询
4.any >any 大于最小的 <any小于最大的 =any 相当于in
select * from 表 where col >any (select * from ...)
5.all >all 小于最大的 <all小于最小的 =all没有数据
===================================================================================
一。函数操作:
1.getdate():获取当前日期
在当前系统增加2天:getdate()+2
2.dateadd(datepart, numer, date) dateadd(dd,2,getdate())
3.datediff(datepart,start,end) datediff(dd, getdate(), getdate()+3) ==3
4.datepart(datepart,date) 返回日期某一部分
5.convert(datatype, data, style) 返日期转换成新数据类型
select convert(varchar(10), getdate(), 103) dd/mm/yyyy
yyyy-mm-dd hh:mi:ss(24h):120
参考网站:w3school.com.cn
二。字符函数的操作
1.len:字符串长度
select len(sname) from tab ...... where len(sname) =3
2.replace:字符串替换函数 replate(expression,str2,str3)
select replace('abcdefg', 'abc', '$$$') -> $$$defg
3.substring(expression, start, length)
4.upper(s)
5.lower(s)
6.rtrim(s)
7.ltrim(s)
8.charindex(expression1, expresseion2 [,start_location])
charindex('a', 'abc') char('a','abcabc',2)
三。数字函数
abs:绝对值 select abs(-2.5)
sqrt:平方根 select sqrt(4)
rand:随机数 select rand()*10 从0-9之间的数
select convert(int, rand()*10)
...
四。其他重要
select scope_identity():插入成功的最后一个
===================================================================================
视图 create view t_view as select * from 表
1.创建视图 create view <视图名> [with encryption] as <子查询> [with check option]
2.修改数据 一张表 Or with check option 不行:聚合函数 group by 多张表
3.修改视图结构 alter view <视图名> as <子查询>
4.查看视图 sp_helptxt <视图名>
===================================================================================
存储过程:(主要是变量)
一、定义变量,更新数据
declare @num int;
set @num = 10;
update product set price = price + @num
-------->>>
create procedure ModifyPrice
(
@num money;
)
as
update produce set price = price + @num [where ...]
执行:exec ModifyPrice @num=10
Or execute ModifyPrice 10
-------->>>
参数:传入参数(普通参数),付出参数(output),return参数(返加状态,默认0,成功。)
create procedure GetCustomerCount
(
@count int output;
)
as
declare @num int;
select @num = count(*) from customers
set @count = @num;
使用-->
declare @mycount int;
exec GetCustomerCount @count = @mycount output;
Or execute GetCustomerCount @mycount output
print @mycount;
-------->>>
create procedure CreateUser
(
@username nvarchar(100);
)
as
declare @namelen int;
set @namelen = len(username);
if @namelen >=2
return 0;
else
return 1;
使用-->
declare @returnValue int;
exec @returnValue = CreateUser @username = '林鑫';
print @returnValue;
===================================================================================
用户自定义函数
create function GetMinute
(
@datevalue datetime;
)
return int
as
begin
declare @min int;
set @min = datepart(minute, @datevalue);
return @min;
end;
:-->> select GetMinute(getdate())
===================================================================================
触发器 after触发器 instead of触发器 DDL触发器 DML触发器
create trigger tg_ship
on Shippers after insert,delete,update
as
insert into shipper_log (onuser, op, shipname, shipphone)
select user_name(), 'Insert', companyname, phone
form inserted;
go
inserted insert update受影响的新数据镜像
updated delete update受影响的旧数据镜像
===================================================================================
事务和并发 Transaction and rollback/commit
隐式事务:每执行一条DML操作,就直接提交到数据库保存
delete from table where id = ?
显式事务,明确指出事务的边界
begin transaction
delete from table where id = ?1
delete from table where id = ?2
delete from table where id = ?3
--结束事务(回滚\提交)
rollback;
commit;
原子性(atomicity)、一致性(consistency)、隔离性(Isolation)、持久性(Durability)
T-SQL目录汇总1的更多相关文章
- ASP.NET MVC4入门到精通系列目录汇总
序言 最近公司在招.NET程序员,我发现好多来公司面试的.NET程序员居然都没有 ASP.NET MVC项目经验,其中包括一些工作4.5年了,甚至8年10年的,许多人给我的感觉是:工作了4.5年,We ...
- ASP.NET MVC4入门到精通系列目录汇总(转)
序言 最近公司在招.NET程序员,我发现好多来公司面试的.NET程序员居然都没有 ASP.NET MVC项目经验,其中包括一些工作4.5年了,甚至8年10年的,许多人给我的感觉是:工作了4.5年,We ...
- SQL小汇总
SQL小汇总 1.对每个时段的数据进行统计2.查询时间条件(to_date)3.插入序列号和系统时间4.查询当天.7天内.30天内5.查询前后x小时.分钟.天.月.6.保留小数点后4位7.查询字段A中 ...
- 史上最全面的SignalR系列教程-目录汇总
1.引言 最遗憾的不是把理想丢在路上,而是理想从未上路. 每一个将想法变成现实的人,都值得称赞和学习. 致正在奔跑的您! 2.SignalR介绍 SignalR实现服务器与客户端的实时通信 ,她是一个 ...
- Spring Cloud Eureka(一): 开篇说明及目录汇总
开篇简述 基于Spring Boot 和 Spring Cloud 的微服务应用,本人在工作中已经使用两年有余了,伴随着个人学习计划的实施,希望借助博文的方式,将工作中使用到的技术点体系化的总结出来, ...
- SQL语句汇总(一)——数据库与表的操作以及创建约束
首先,非常感谢大家对上篇博文的支持,真是让本菜受宠若惊,同时对拖了这么久才出了此篇表示抱歉. 前言:此文旨在汇总从建立数据库到联接查询等绝大部分SQL语句.SQL语句虽不能说很多,但稍有时间不写就容易 ...
- SQL语句汇总(一)——数据库与表的操作以及创建约束
首先,非常感谢大家对上篇博文的支持,真是让本菜受宠若惊,同时对拖了这么久才出了此篇表示抱歉. 前言:此文旨在汇总从建立数据库到联接查询等绝大部分SQL语句.SQL语句虽不能说很多,但稍有时间不写就容易 ...
- SQL注入汇总(手注,盲注,报错注入,宽字节,二次编码,http头部){10.22、23 第二十四 二十五天}
首先什么是SQL注入: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. SQL注入有什么危害? 危害:数据泄露.脱库 ...
- SQL语句汇总(终篇)—— 表联接与联接查询
上一篇博文说到相关子查询效率低下,那我们怎么能将不同表的信息一起查询出来呢?这就需要用到表联接. 和之前的UNION组合查询不同,UNION是将不同的表组合起来,也就是纵向联接,说白了就是竖着拼起来. ...
- 初级SQL开发汇总指南
汇总部分内容来自网络(作者 :zhtbs),比较基础的东西,能够了解比较基础的一些东西. Select语句概要 数据库中数据的提取(查询)使用select 语法,主要有以下几点作用 l 提取的数据 ...
随机推荐
- .npy,.mat,.txt转换
.npy--->.txt: http://blog.csdn.net/wgf5845201314/article/details/73825785 .npy<->.mat: i ...
- Unity 3D观察者设计模式-C#委托和事件的运用
C#观察者设计模式 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...
- 1.Tensorflow的基本概念:
1.Tensorflow的基本概念: 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Va ...
- Project Euler 54
#include<bits/stdc++.h> using namespace std; ]; ]; ; map<char,int> mp; //map<char,cha ...
- hdu3488 Tour 拆点+二分图最佳匹配
In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000) one-way r ...
- hdu1907 John 博弈
Little John is playing very funny game with his younger brother. There is one big box filled with M& ...
- linux----CenterOS7中在线安装jdk
summary: 一直以来,都在windows上玩java,今天是一个具有里程碑的一天,感觉正式踏入进入了linux大门. 原来一直以为在linux上安装jdk,需要去官网下载适合linux的jdk, ...
- Java中动态获取项目根目录的绝对路径
https://www.cnblogs.com/zhouqing/archive/2012/11/10/2757774.html 序言 在开发过程中经常会用到读写文件,其中就必然涉及路径问题.使用固定 ...
- 【BZOJ2820】ygy的gcd
不知道为什么不想写总结,只是(因为是用别人的权限号交的所以)屯一个代码 #include<iostream> #include<cstdio> #include<algo ...
- mongodb副本集加分片集群安全认证使用账号密码登录
mongodb副本集加分片集群搭建网上资料有很多.粘贴一个写的比较好的.副本集加分片搭建 对于搭建好的mongodb副本集加分片集群,为了安全,启动安全认证,使用账号密码登录. 默认的mongodb是 ...