Sql server 2005 的基本语句

 1.在输入命令时,不区分大小写,都可以执行所写的命令。

 2.备份数据库的命令:BACKUP DATABASE student to disk='F:studentbackup11.bak'

   USE master
   EXEC sp_addumpdevice 'disk', 'studentbackup', 'c:mssql7backupMyNwind_1.dat'

  sp_addumpdevice

 3.恢复数据库的命令:RESTORE DATABASE "mydb" FROM DISK='C:11.bak';

 4.在数据库的表中添加一列属性:alter table StudentInfo add address varchar(40) null;

 5.修改表中数据列的属性名(重命名列的名称):exec sp_rename 'StudentInfo.user','account','column';

 6.重命名表的名称:exec sp_rename 'StudentInfo','student';

 7.修改表中一条或几条数据:update studentinfo set address='北京',age=20 where id='2013003'

 8.求某一列的和:select sum(income) as sumvalue from teacher

 9.求某一列的平均值:select avg(income) as avgvalue from teacher

 10.求某一列的最大值:select max(income) as maxvalue from teacher

 11 .求某一列的最小值:select min(income) as minvalue from teacher

 12.当两表数据字段有一部分是一样时,插入数据可以使用:insert into teacher(id,name,income) select id,name,income from studentinfo

 13.having的基本用法:select income from teacher  group by income having  income<5000

  当同时含有where子句、group by 子句 、having子句及聚集函数时,执行顺序如下:

  执行where子句查找符合条件的数据;

  使用group by 子句对数据进行分组;对group by 子句形成的组运行聚集函数计算每一组的值;

  最后用having 子句去掉不符合条件的组。

 14.对数据库进行操作:
  
分离数据库: sp_detach_db; 附加数据库:sp_attach_db 后接表明,附加需要完整的路径名

 15.如何修改数据库的名称:
    sp_renamedb 'old_name', 'new_name'

 

  16.说明:复制表(只复制结构,源表名:a 新表名:b) :select * into admin from studentinfo where 1<>1

  17. 把一个表中的数据导入到另一个表上,字段一样:

update a set a.dwdm = b.dwdm from jf12 a,test00_jwl b where a.dwmc = b.dwmc

  18.子查询:select a,b,c from a where a IN (select d from b )

 19.说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
       select * from table1 where time between time1 and time2
       select a,b,c, from table1 where a not between 
数值1 and 数值2

 20. 说明:两张关联表,删除主表中已经在副表中没有的信息 
       delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

 21.说明:随机取出10条数据
       select top 10 * from tablename order by newid()

 22. 触发器:

declare @i int
set @i=1
while @i<30
begin
    insert into admin (id) values(@i)
    set @i=@i+1
end

23.Sql中,返回当前时间语句:update admin set time=getdate() where id=2013005

24.复制表结构:select * into admin1 from admin where 1 <>1

25.拷贝表中数据到另外一个表中:insert into admin1 select * from admin

 insert into b(a, b, c) select d,e,f from a;

26.修改更新后的表和备份的表:update admin1 set name=(select name from admin where id=admin1.id),只能修改一列的数据,并且需要有相关联的数据。

27.按姓氏笔画查询:Select * From studentinfo Order By name Collate  Chinese_PRC_Stroke_ci_as

28.查询N—M的数据:Select Top 3-1 * From admin Where ID in (Select Top 1 ID From admin) Order by ID Desc

29.查询表中视图,函数,存储过程:select a.* from sysobjects a,syscomments b where a.id = b.id and b.text like '%admin'

30.查询表中有多少条数据:select conut(*) from admin

31.数据库加密: select encrypt('原始密码')
select pwdencrypt('
原始密码
')
select pwdcompare('
原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码
')
select pwdencrypt('
原始密码
')
select pwdcompare('
原始密码','加密后密码') = 1--相同;否则不相同

32.判断两个表是否相等:

if (select checksum_agg(binary_checksum(*)) from admin)
     =
    (select checksum_agg(binary_checksum(*)) from admin1)
print '相等'
else
print '不相等'

33.数据库中所有的用户表:select Name from sysobjects where xtype='u' and status>=0

34.--查询示例

 select * from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

 --生成本地表

 select * into  from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

 --把本地表导入远程表

 insert openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)

 select *from 本地表

 --更新本地表

 update b set b.A=a.A

  from openrowset( 'SQLOLEDB ', 'sql服务器名 '; '用户名 '; '密码 ',数据库名.dbo.表名)as a inner join 本地表 b

 on a.column1=b.column1

 --openquery用法需要创建一个连接

 --首先创建一个连接创建链接服务器

 exec sp_addlinkedserver   'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 '

 --查询

 select * FROM openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

 --把本地表导入远程表

 insert openquery(ITSV,  'SELECT *  FROM 数据库.dbo.表名 ')

 select * from 本地表

 --更新本地表

 update b

 set b.B=a.B

 FROM openquery(ITSV,  'SELECT * FROM 数据库.dbo.表名 ') as a 

 inner join 本地表 b on a.A=b.A

 SELECT   * FROM   opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ' ).test.dbo.roy_ta

 --把本地表导入远程表

 insert opendatasource( 'SQLOLEDB ',  'Data Source=ip/ServerName;User ID=登陆名;Password=密码 ').数据库.dbo.表名

 select * from 本地表 

35.插入新增数据:

insert srv2.库名.dbo.author(id,name,telphone) 

 select id,name,telphone from author i

 where not exists(

 select * from srv2.库名.dbo.author where id=i.id)