sql 基本操做

 

  
  
  
  
  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的備份庫
 ide

相關文章
相關標籤/搜索