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 提取的数据 ...
随机推荐
- 大一下第2次作业(markdown改)
一.作业 6-7 删除字符串中数字字符 1.设计思路 (1)主要描述题目算法 第一步:用for循环和if语句,一个一个字符判断,找到数字字符就跳过去判断下一个,否则使指针指向不是(已判断过的)数字字符 ...
- ARM Linux中断发生时内核堆栈切换
转载注明出处:http://www.wowotech.net/forum/viewtopic.php?id=54 对ARM Linux中断非常简洁.精确的描述. 发生了中断,最重要的是保存现场,在中断 ...
- aliyun服务器对象存储oss
aliyun OSS 使用简单.方便. 官方网址 aliyun.com 首先通过aliyun管理控制台申请OSS服务.通过AccessKeys分配Access Key ID和Access Key Se ...
- numpy unable to find vcvarsall.bat
出现这种情况,是需要引用vc的编译器,可以安装vs来解决,并且安装numpy前, 设置如下宏 SET VS100COMNTOOLS=%VS110COMNTOOLS% SET VS90COMNTOOLS ...
- pc端复制方法
dom结构如下: <div id="btn">复制</div> <input id="content" type="te ...
- git 应用
git - 简易指南 助你开始使用 git 的简易指南,木有高深内容,;). 安装 下载 git OSX 版 下载 git Windows 版 下载 git Linux 版 创建新仓库 创建新文件夹, ...
- 走进 AQS 瞧一瞧看一看
并发中有一块很重要的东西就是AQS.接下来一周的目标就是它. 看复杂源码时,一眼望过去,这是什么?不要慌,像剥洋葱一样,一层层剥开(哥,喜欢"扒开"这个词). 参考资源: http ...
- LG3187 [HNOI2007]最小矩形覆盖
题意 题目描述 给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形,输出所求矩形的面积和四个顶点坐标 输入输出格式 输入格式: 第一行为一个整数n(3<=n<=50000),从第2至第 ...
- Cassandra基础2
========================================================= gossip协议1.点对点(peer to perr)的网络通信协议,节点间地位相同 ...
- PowerDesigner ---- 数据库设计(物理模型CDM和概念模型PDM)
前言 上一篇介绍了个PowerDesigner工具的安装和汉化,现在我就说一下怎么用这个PowerDesigner建数据库吧. 内容 第一种方法:概念模型转物理模型 1.首先新建模型--选择概念模 ...