码迷,mamicode.com
首页 > 数据库 > 详细

20151020sql2

时间:2015-10-31 15:39:26      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

SQL对数据增删改

1、增加

添加所有的字段 ID自动生成

insert into StuInfor Values(‘ruanmou1‘,‘20‘,‘中国‘,‘男‘,‘一班‘,‘1999-1-1‘,‘222222‘)

按照特定的字段 ,其它没有的字段以NULL代替

insert into StuInfor (StuName,Age,Sex,QQ) Values(‘ruanmou2‘,‘20‘,‘男‘,‘33333‘)

2、修改

update StuInfor set StuName=‘小张‘,Age=28 where StuId=12

3、删除

delete StuInfor where StuId=12

做删除修改的时候要先备份

SQL分页

分页语句

查记录数行号 不是通过ID

获取行号

select StuId,row_number() over(order by StuId) as row from StuInfor

分页

select * from (select StuId,row_number() over(order by StuId) as row from StuInfor) where row between 1 and 5

SQL修改表字段

alter table Category

add

Age int null

3、删除表

drop table Users

SQL年月日处理

获取日期中的年份

select year(Birthday) from StuInfor

月份

select month(Birthday) from StuInfor

日

select daty(Birthday) from StuInfor

获取当前年份 可以参与计算

year(getdate())

当前月份

moth(getdate())

当前日期

day(getdate())

日期计算

dateadd加

select dateadd(yy,100,getdate());

select dateadd(mm,100,getdate());

select dateadd(dd,100,getdate());

datadiff间隔

select datediff(yy,getdate(),‘2108/12/31‘)

select datediff(mm,getdate(),‘2016/01/01‘)

select datediff(dd,getdate(),‘2016/01/01‘)

SQL isnull

判断为NULL是给定一个默认值

select StuName,Age,Sex,ISNULL(country,‘‘) from StuInfor

 SQL case

判断

select StuName, case

when(EnglishScore < 50) then ‘差‘

when(EnglishScore between 50 and 70) then ‘普通‘

when(EnglishScore between 70 and 100) then ‘优秀‘

end as A

from Score

表-字段约束

CHCK约束(保持数据的完整性)

年龄不能大于200岁

字段-约束-添加-表达式:Age>0 and Age<=100

表-字段索引

聚集索引:拼音查字法  比如有顺序的 ID 年龄 时间

只有一个,默认的聚集索引 是主键 不需要重新创建

非聚集索引:偏旁查字法 国家 (查询量不是特别大的时候不需要) 

 查询的比较多字段 例如产品的名称、分类

创建:字段-索引/键-添加-列-StuName

在表-可以查看 索引

索引会产生碎片文件,要定期的整理碎片-重新组织-重新生成

访问量大的时候,一个星期需要定期整理。

表-视图

作用:联合查询

表-视图-选择三个表数据-找到其中的ID

当作表来用。

select * from View_1

缺点:表里的数据如果做了修改,会造成数据不同步,还需要进行创建一次

表-存储过程

作用:1、安全性高,sql注入

select * from StuInfor where StuName=‘‘ and Country=‘‘

如果传值 张三‘--delete StuInfor 会把表删除了 SQL语句--表示注释

select * from StuInfor where StuName=‘张三‘--delete StuInfor ‘ and Country=‘‘

通过@传参的方式

 

创建存储过程

create proc procStuInfor

(

@StuName nvarchar(50),

@Country nvarchar(50)

)

as

select * from StuInfor where StuName=@StuName and Country=@Country

可编程性 -存储过程

执行存储过程

exec procStuInfor ‘张三’,‘中国‘

不能在SQL界面传sql注入的参数否则也会删除

2、C#写sql语句是需要编译,写在存储过程是不需要编译,效率高一些。

查询比较多,逻辑比较复杂的SQL语句存储在存储过程中。

表--储发器

在执行sql语句的时候,同时也能执行另外一条语句

删除的时候,查询删除的语句

创建

Create trigger trigStuInforDelete

ON StuInfor

after delete /*有三种insert delete update*/

AS

begin

  select * from deleted/*查询删除的语句*/

end

数据库备份

1、任务-备份-选项-覆盖(一个) 追加(两个)-确定 bak文件

2、mdf文件 log文件 删除文件就没有了 所以应该-任务-分离

数据库还原

1、bak文件还原 创建同名的数据库-任务-还原-设备-文件-覆盖-确定

2、mdf文件还原 数据库-附加-mdf文件-确定

 

sqlserver高版本属性-选项-兼容级别-sqlserver2005

 数据导入/导出

MYDB-导出到TESTDB

导出-任务-导出- MYDB-TestDB-表-完成

StuInfor表的主键会消失,所以要重新添加

数据库自动备份 -作业

自动备份

 

新建作业-backupDB-确定

步骤-新建step1 -MYDB-命令

backup database mydb

to disk=‘c:\test.bak‘

确定-计划-添加计划-每周一 时间0:00:00

确定

 

自动删除

删除某个条件

 

SQLServer Profiler 检控网站卡的时候sql语句的执行情况

MSSQL -SQLSERVER

20151020sql2

标签:

原文地址:http://www.cnblogs.com/16lily521/p/4925548.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!