T-SQL组成 DCL(数据控制语言) DDL(数据定义语言) DML(数据操作语言)
数据的插入:insert into 表名[列名] values [值列表]
insert into 表名[列名] select [值列表] union
select [值列表] union
。。。。。。。。
select [值列表]
insert 表名(新表) into 列名 from 源表名
insert into 表名[列名] select 列名 from 源表名
列的插入: alter table 表名 add 列名 数据类型
数据的修改:update 表名 set 列名=列值 where 条件
数据的删除:delete from 表名 [where 条件]
数据库及表的删除:drop table/database 表名/库名
数据的查找:select [列名] from 表名
模糊查询:like,between,in
select 列名 from 表名 where 列名 like
select 列名 from 表名 where 列名 between 。。and。。
select 列名 from 表名 where 列名 in[。。 ,。。 ]
分组查询:
select 列名 from 表名 group by 列名 having 对列的要求
排序:
select 列名 from 表名 order by 列名asc(升序)/desc(降序)
多表查询:
selecct 列名 from 表1,表2,。。where 主外键连接 and 主外键连接
主连接
select 列名 from 主表名 inner join 字表1 inner join 字表2
where 条件
左外连接
select 在查询结果中出现的列名 from 主表
left outer join 表1
where 查询条件
右外:
select 在查询结果中出现的列名 from 主表
right outer join 表1
where 查询条件
随即查处三条数据:
select top 3 * from 表名 order by newid()
函数:聚合函数:sum()用于对int数据进行求和 ,
max()用于求出该数据中最大的一组 ,
min()用于求出改组数据中最小的一组,
count()用于对数据的多少进行求和 ,
avg()用于对int类数据进行平均
日期函数:datetime,smalldatetime
时间函数:
货币函数:money , smallmoney
字符串函数:select chairindex('字符串','字符串中的字符') 用于查询字符在字符串中的位置
len('字符串') 返回字符串的长度
rtrim/ltrim('字符串') 用于去掉字符串左/又边的空格
lower('字符串') 用于将字符串中的大写字母转换为小写
upper('字符串') 用于将字符串中的小写字母转换为大写
replace('字符串','要替换的字符','替换后的字符')
convert(转换后的数据类型,'字符串')
约束:defaule , check() , nuique 唯一约束, 主键primary key ,
外键 foreign key references 主外键的数据类型必须相同
三范式:1NF 原子性 2NF 去除无关依赖性 3NF 去除传递依赖性
数据库的备份:sp_addunpdevice 'disk', '设备名称','设备路径'
backup database 数据库名称 to 设备名称
或者 backup database 数据库名 to disk='设备路径' with fomat (仅适用于SQL Sever 2005)
删除设备: sp_dropdevice 设备名
数据库的还原:
数据库的分离:sp_attach_dp 数据库名称
数据库的附加:sp_attach_dp @filenaem1='要附加的文件路径'
@filenaem2='要附加的文件路径'
。。。。。。。。。。。。。。
数据类型:数字型:精确:int ,bigint,smallint,tinyint,bit,money,smallmoney,近似的:float real
数据类型 范围 存储
bigint -2^63 (-9,223,372,036,854,775,808) 到 2^63-1 (9,223,372,036,854,775,807) 8 字节
int -2^31 (-2,147,483,648) 到 2^31-1 (2,147,483,647) 4 字节
smallint -2^15 (-32,768) 到 2^15-1 (32,767) 2 字节
tinyint 0 到 255 1 字节
日期和时间: datetime,smalldatetime
数据类型 范围 精确度
datetime 1753 年 1 月 1 日到 9999 年 12 月 31 日 3.33 毫秒
smalldatetime 1900 年 1 月 1 日到 2079 年 6 月 6 日 1 分钟
字符串:char(范围) ,varchar(范围),nchar(),nvarchar(),text,ntext (以n开头的为unicode编码的)
创建登陆:exec sp_addlogin '用户名','密码'
用户授权: 授权为数据库用户 sp_grantdbaccess '用户名','数据库名'
授权:exec 权限 on 表名 to '用户名'