--主键表 SELECT TOP 50 * FROM sys.key_constraints --没有主键的表 SELECT TOP 50 * FROM sys.tables a WHERE a.object_id NOT IN( SELECT parent_object_id FROM sys.key_constraints ) SELECT TOP 50 * FROM sys.parameters --查询计算列 SELECT TOP 50 * FROM sys.computed_columns
月度归档: 2013年11月
查询有自增长列的表及目前自增长值
Select Object_Name(id.object_id) As [table_name] , id.name As [column_name] , t.name As [data_type] ,seed_value ,increment_value , Cast(id.last_value As bigint) As [last_value] , Case When t.name = 'tinyint' Then 255 When t.name = 'smallint' Then 32767 When t.name = 'int' Then 2147483647 When t.name = 'bigint' Then 9223372036854775807 End As [max_value] From sys.identity_columns As id Join sys.types As t On id.system_type_id = t.system_type_id Where id.last_value Is Not NULL ORDER BY last_value DESC
sqlserver安全设置
隐藏 SQL Server 数据库引擎的实例
-
在“SQL Server 配置管理器”中,展开“SQL Server 网络配置”,右键单击“<server instance> 的协议”,然后选择“属性”。
-
在“标志”选项卡的“隐藏实例”框中,选择“是”,然后单击“确定”关闭对话框。 对于新连接,更改会立即生效。
[Except]How to compare the rows of two tables and fetch the differential data.
In this blog I would like to demonstrate a scenario where users want to move the changes between the tables in two different databases. Let’s say we would like to compare and move the changes between the databases for some tables using T-SQL The below example talks about moving the changes between the two databases in the same instance However the same can be extended across instances if you use linked server or SSIS packages. Also we can write queries to move the DML changes from source to destination and vice versa. Let’s look at the below example --creating a source database create database source --create source table use source create table Product( Pid int primary key , Pname varchar (10), Pcost float, source int , location varchar(10)) --create destination database create database Destination --create destination table use Destination create table Product( Pid int primary key , Pname varchar (10), Pcost float, source int, location varchar(10) ) --Insert data into source table use source insert into product values ( 1,'rdbms',100,200,'ind') insert into product values ( 2,'dbm',20,100,'US') insert into product values ( 3,'arp',30,250,'UK') insert into product values ( 4,'mqr',40,100,'ind') insert into product values ( 5,'ttp',50,200,'us') -- EXCEPT returns any distinct values from the left query that are not also found on the right query. --The below query gives us difference between sourec and destination -- we can use except ket word to look at selected columns or entire table select * from source.dbo.product except select * from [Destination].dbo.product --updating destination table with the changes from source insert into [Destination].dbo.product select * from source.dbo.product except select * from [Destination].dbo.product -- We see that the destination is populated with all the rows from source select * from [Destination].dbo.product --Now lets update the row in the source and see how it works update source.dbo.product set pname='sql' where pid =1 --run the below query select * from source.dbo.product except select * from [Destination].dbo.product -- the result gives us the only row which was changed in source -- loading the deiffrences to a temp table select * into #temp from source.dbo.product except select * from [Destination].dbo.product --updating the destination with changes update [Destination].dbo.product set [Destination].dbo.product.pname= #temp.pname from #temp where #temp.pid= [Destination].dbo.product.pid --lets run the statement to see the difference between these tables select * from source.dbo.product except select * from [Destination].dbo.product --lets see how the delete works delete from source.dbo.product where pid= 2 -- to see the rows which were deleted at source or inserted at destination only select * from [Destination].dbo.product except select * from source.dbo.product --based on the application logic either we will insert it back in the source or delete from dest --lets say we want to delete from dest as well , select * into #temp from [Destination].dbo.product except select * from source.dbo.product delete from [Destination].dbo.product where pid in ( select pid from #temp) -- Now lets see that difference between the tables select * from [Destination].dbo.product except select * from source.dbo.product
来自:How to compare the rows of two tables and fetch the differential data.
sqlserver内存设置
该表已为了复制而被发布,所以无法重命名。
场景:从发布库上将一数据库移到另一服务器,在对表改名时提示“该表已为了复制而被发布,所以无法重命名。”
原因:移的数据库原来参与了复制分发,需要在新服务器上去掉。
方法:sp_removedbreplication 'mydb'
sp_removedbreplication
只有当其他删除复制对象的方法都失败后,才应当使用此过程。
按月统计数据
--设置周一为第一天,默认为周日为第一天 SET DATEFIRST 1 --获取本周是今年的第几个星期 SELECT DATEPART(week,GETDATE()) --今天是第几个季度 SELECT DATEPART(quarter,GETDATE()) --按周统计数据,要求显示每周开头与结尾时间 declare @num int,@year varchar(4),@date datetime select @num=45 select @year='2008-01-01' select @date=dateadd(wk,@num-1,@year) select dateadd(dd,1-datepart(dw,@date),@date),dateadd(dd,7-datepart(dw,@date),@date)
即时设置数据库状态
在设置数据库状态时
with子句忽略,则当数据库中存在任何锁时,ALTER DATABASE 语句将无限期等待。
with NO_WAIT ,指定若有事务还没有提交,则立即失败,不再进行下面的操作
with ROLLBACK AFTER integer [SECONDS] | ROLLBACK IMMEDIATE:指定是在指定秒数之后回滚还是立即回滚。
注意:并非所有数据库选项都使用 WITH <termination> 子句,也不是所有数据库选项都能结合其他选项指定。
ALTER DATABASE AdventureWorks2012 SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO ALTER DATABASE AdventureWorks2012 SET READ_ONLY GO ALTER DATABASE AdventureWorks2012 SET MULTI_USER; GO
IOMeter测试磁盘性能
(注意:如果操作完成的非常快,磁盘实际看到的队列深度可能更少,默认值是1)举个例子:假设选中了一个Manager,选中8个Disk,指定# of Outstanding I/O of =16,磁盘被分布到咯咯worker(每个worker分到2个disk),每个worker对其下的每一个disk生成最大16个未处理I/O,那么整个系统中该Manager每次将生成最多128个未处理I/O(4 worker * 2disk/worker * 16未处理I/O/disk)。
4)average i/o response time:平均延迟时间
随机读8K测试结果 | |
机器 | IOPS每秒 |
我的电脑 | 100 |
183硬盘 | 1000 |
192硬盘阵列 | 2000 |
183fushion卡 | 100000 |
redis:windows连虚拟机
redis不支持windows,而现在的windows版本的redis还不是很成熟。故选择虚拟机下安装centeros,跑redis,在windows下访问。
一、windows连接虚拟机的redis
相关:
利用fiddler进行回响
在autoresponse设置匹配的url即可,当对应url进来时转到指定的文件
regex:(?insx)^http://172.16.88.55:9528/Resources/Scripts/Call.js\?.*
redis使用
redis入门指南笔记
一、安装 wget http://download.redis.io/redis-stable.tar.gz tar xzf redis-stable.tar.gz cd redis-stable make 启动 src/redis-server 停止 redis-cli shutdown 客户端: src/redis-cli cp redis-benchmark redis-cli redis-server /usr/bin/ #这个倒是很有用,这样就不用再执行时加上./了,而且可以在任何地方执行 在线测试 http://try.redis.io/ 二、各种类型 键命名:对象类型:对象ID:对象属性 设置 set myname asong 获取 get myname 判断存在 exists myname 删除 del myname mset,mget设置获取多个键 mset myname asong age 28 查询所有键:keys * 查询以w开头的键 keys w* type key 获取类型 是原子的,避免冲突 整数自增 incr i decr i incrby i 10 decrby i 10 加小数 incrbyfloat i 1.5 向尾部添加值,若不存在,则新值 append myname hello 获取长度 strlen myname 类型: 字符串类型,整数类型 散列类型:用来存储对象 hset user name asong hget user name 设置多个值 hmset user name asong age 28 hgetall user --显示所有 hexists user name 判断字段是否存在 hincry user age hdel user age hkeys user hlen user --获取字段数量 列表类型:存储一个有序的字符串列表,在列表两端添加元素,或者获得列表的某一个片段,一双向链表,顺序即插入的次序。 若是一个对象,可以序列化成字符串存储 应用:网站上的最新文章top 10 直接从列表类型中取出即可,因为每次都将最新的插入到头部 lpush users asong fang bcc rpush users dd ff 从列表删除元素 lpop users rpop users 获取片段 lrange users 1 10 删除列表指定的值 lrem users -1 asong lrem key count value count>0 从列表左边开始删除前count个值为value的元素。 <0,从右边 =0 删除所有值为value的元素 获取指定位置的值 lindex users 1 向列表指定位置插入元素 linsert users before 7 niuniu 集合类型: 集合:无序的每个元素唯一的合集 sadd users asong fangfang 获取: smembers users 删除:sren users asong 判断集合是否存在 sismember users asong 集合运算 sdiff:多个集合差集 属于A但不属于B的集合 sinter:交集 sunion:并集 有序集合类型:插入数据时指定权重,自动处理排序 zadd users asong 100 fang 200 niu 1000 获得元素的权重 zscore users asong 获取权重在某个范围的元素列表 zrange users 1 10
数据库文件损坏修复
【场景】:
对象浏览器中只有数据库名,数据库展不开,查看日志【由于数据库没有完全关闭,无法重新生成日志。】,推测原因是服务器异常关停,造成数据库没有正常写完数据造成。
【处理方法】:修复数据,舍弃异常数据。
DBCC CHECKDB (mydb, REPAIR_ALLOW_DATA_LOSS),前提是要在单用户下
【完整的处理方法】:
ALTER DATABASE [mydb] SET EMERGENCY --EMERGENCY状态下 数据库标记为 READ_ONLY,并禁用日志记录,并且仅限 sysadmin 固定服务器角色的成员进行访问。 EMERGENCY 主要用于故障排除。 ALTER DATABASE [mydb] SET SINGLE_USER DBCC CHECKDB (mydb, REPAIR_ALLOW_DATA_LOSS) ALTER DATABASE [mydb] SET MULTI_USER
【注】
若以上执行不成功,按以下方式处理:
1,数据库mydb脱机
2,将mdf文件copy到新位置
3,删除原数据库mydb
4,建立新库mydb
5, 脱机mydb
6, 将备份的mdf文件覆盖新库
7,联机新库,此时是置疑状态
8,执行【完整的处理方法】