1.  
  2. --选择  
  3. select * from student where  name='insertName20' 
  4. --插入  
  5. insert into student (name,sex,class) values('insertname',1,'insertclass')  
  6. insert into student values('insertname2',1,'insertclass2',GETDATE(),GETDATE(),GETDATE())  
  7. --PS:id 为自增长列,不能手动插入  
  8. --更新   
  9. update student set name='insertname3' where name='insertname' 
  10. --删除  
  11. delete student where name='insertname3' 
  12. --查找  
  13. select *  from student where name like '%insert%'  
  14. --排序  
  15. select * from student order by name desc,id --没必要必须是数字时间等等 (先按name 降序,相同的话在按id升序)  
  16. --总数 表中总行数  
  17. select COUNT(*) as totalcount from student   
  18. select COUNT(1) as totalcount from student  
  19. select COUNT(id)as totalcount from student   
  20. select COUNT(name) as totalcount from student --如何一列都可以  
  21. --求和  
  22. select SUM(id) as sumvalue from student --必须要是数字类型  
  23. --平均  
  24. select AVG(id) as avgvalue from student  
  25. --最大  
  26. select MAX(id) as maxvalue from student  
  27. --最小  
  28. select MIN(id) as minvaule from student  
  29.  
  30. --向表中添加字段   
  31. alter table student add grade int   
  32. --添加check约束  
  33. alter table student add  
  34. constraint ck_student_check check (grade>0 and grade<=150)   
  35. --添加主键约束  
  36. alter table student add   
  37. constraint pk_student_primary Primary key(id)  
  38. --增加外键约束  
  39. alter table student add classId int  
  40. alter table student add   
  41. constraint fk_student_foreign foreign key(classId) references student2(id)--id 为student2的主键  
  42. --增加唯一键约束  
  43. alter table student add   
  44. constraint U_student Unique(id)  
  45. --增加默认值  
  46. alter table student add  
  47. constraint df_class default '1班' for class   
  48.  
  49. --删除约束  
  50. alter table student drop constraint fk_student_foreign  
  51. --删除列  
  52. alter table student drop column classId --列若有约束,需先删除约束  
  53.  
  54. --修改字段类型  
  55. alter table student   
  56. alter column sex bit  
  57.  
  58. --用存储过程重命名对象名称  
  59. --表名  
  60. exec sp_rename 'student','studentNew'  
  61. select * from studentNew  
  62. exec sp_rename 'studentNew','student'  
  63. --列名  
  64. exec sp_rename 'student.inserttime','createtime','column'  
  65.  
  66.  
  67. --查看某表中的某字段是否存在  
  68. if exists(select * from syscolumns where id=OBJECT_ID('student') and name='name')  
  69. print '列name 存在'  
  70. else  
  71. print '列name不存在'  
  72.  
  73. --判断表的存在  
  74. select COUNT(*)from sysobjects where type='U' and name='student' 
  75. --判断字段是否存在  
  76. select * from syscolumns where id =  
  77. (select id from sysobjects where type='U' and name='student')  
  78. and name='name' 
  79.  
  80. --创建索引  
  81. create index idxname on student(name)  
  82. drop index student.idxname -- 必须要加上表名  
  83.  
  84. --union ,except,intersect   
  85. --A: UNION 运算符   
  86. -- UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。  
  87.  
  88. -- B: EXCEPT 运算符   
  89. -- EXCEPT 运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当 ALL 随 EXCEPT 一起使用时 (EXCEPT ALL),不消除重复行。   
  90.  
  91. -- C: INTERSECT 运算符  
  92. -- INTERSECT 运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当 ALL 随 INTERSECT 一起使用时 (INTERSECT ALL),不消除重复行。  
  93. -- 注:使用运算词的几个查询结果行必须是一致的  
  94.  
  95. --判断要添加列的表中是否有主键  
  96. if exists(select 1 from sysobjects where parent_obj=OBJECT_ID ('student') and xtype='PK' )  
  97. begin   
  98. print '表中已有主键'  
  99. alter table student add classid int default 0  
  100. end  
  101. else  
  102. begin   
  103. print '表中无主键,添加主键列'  
  104. alter table student add classid int primary key default 0  
  105. end  

 --创建备份数据的device
exec sp_addumpdevice disk,testBackUp,'D:\juan\MyNwind_1.dat'
--开始备份
backup database MyTest to testBackUp --在目标位置已经存在的MyTest的备份库