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 提取的数据 ...
随机推荐
- 阿里云CentOS中vsftp安装、配置、卸载
1--卸载 查看当前服务器中的vsftpdrpm -qa|grep vsftpd 例如结果为:vsftpd-2.2.2-13.el6_6.1.x86_64执行卸载rpm -e vsftpd-2.2.2 ...
- while循环中出现ssh导致读取文件错误
while read line do ...... ssh ... done < $file 使用上面包含ssh命令的while循环,ssh命令将$file内容全部吞噬,导致只处理完一行即退出: ...
- 20155208徐子涵 2016-2017-2 《Java程序设计》第5周学习总结
20155208徐子涵 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 异常处理 8.1 语法与继承结构 Java中所有错误都会被打包为对象,运用tr ...
- 通信协议演进与JCE协议详解
一.通信协议概述通信协议是两个节点之间为了协同工作.实现信息交换,而协商的规则和约定,例如规定字节序,各个字段类型,使用什么压缩算法或加密算法等. 1.原始数据假设A和B通信,获取或设置用户基本资料, ...
- heptio scanner kubernetes 集群诊断工具部署说明
heptio scanner 是一款k8s 集群状态的诊断工具,还是很方便的,但是有一点就是需要使用google 的镜像 参考地址 https://scanner.heptio.com/ 部署 kub ...
- C# to IL 3 Selection and Repetition(选择和重复)
In IL, a label is a name followed by the colon sign i.e ":". It gives us the ability to ju ...
- oracle数据字典-权限-角色-参数
每个数据库都提供了各自的数据字典的方案,虽然形式不同,但是目的和作用是一样的,比如在mysql里数据字典是在information_schema 里表现的,sqlserver则是在sys这个系统sch ...
- [转]一致性hash算法 - consistent hashing
consistent hashing 算法早在 1997 年就在论文 Consistent hashing and random trees 中被提出,目前在 cache 系统中应用越来越广泛: 1 ...
- pip报错解决:EnvironmentError: mysql_config not found
centos7下使用python类库MySQL-python操作mysql.pip安装类库:pip install MySQL-python报错提示:mariadb EnvironmentError: ...
- bzoj 4709 [Jsoi2011]柠檬——单调栈二分处理决策单调性
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4709 题解:https://blog.csdn.net/neither_nor/articl ...