- --選擇
- select * from student where name='insertName20'
- --插入
- insert into student (name,sex,class) values('insertname',1,'insertclass')
- insert into student values('insertname2',1,'insertclass2',GETDATE(),GETDATE(),GETDATE())
- --PS:id 爲自增加列,不能手動插入
- --更新
- update student set name='insertname3' where name='insertname'
- --刪除
- delete student where name='insertname3'
- --查找
- select * from student where name like '%insert%'
- --排序
- select * from student order by name desc,id --不必必須是數字時間等等 (先按name 降序,相同的話在按id升序)
- --總數 表中總行數
- select COUNT(*) as totalcount from student
- select COUNT(1) as totalcount from student
- select COUNT(id)as totalcount from student
- select COUNT(name) as totalcount from student --如何一列均可以
- --求和
- select SUM(id) as sumvalue from student --必需要是數字類型
- --平均
- select AVG(id) as avgvalue from student
- --最大
- select MAX(id) as maxvalue from student
- --最小
- select MIN(id) as minvaule from student
- --向表中添加字段
- alter table student add grade int
- --添加check約束
- alter table student add
- constraint ck_student_check check (grade>0 and grade<=150)
- --添加主鍵約束
- alter table student add
- constraint pk_student_primary Primary key(id)
- --增長外鍵約束
- alter table student add classId int
- alter table student add
- constraint fk_student_foreign foreign key(classId) references student2(id)--id 爲student2的主鍵
- --增長惟一鍵約束
- alter table student add
- constraint U_student Unique(id)
- --增長默認值
- alter table student add
- constraint df_class default '1班' for class
- --刪除約束
- alter table student drop constraint fk_student_foreign
- --刪除列
- alter table student drop column classId --列如有約束,需先刪除約束
- --修改字段類型
- alter table student
- alter column sex bit
- --用存儲過程重命名對象名稱
- --表名
- exec sp_rename 'student','studentNew'
- select * from studentNew
- exec sp_rename 'studentNew','student'
- --列名
- exec sp_rename 'student.inserttime','createtime','column'
- --查看某表中的某字段是否存在
- if exists(select * from syscolumns where id=OBJECT_ID('student') and name='name')
- print '列name 存在'
- else
- print '列name不存在'
- --判斷表的存在
- select COUNT(*)from sysobjects where type='U' and name='student'
- --判斷字段是否存在
- select * from syscolumns where id =
- (select id from sysobjects where type='U' and name='student')
- and name='name'
- --建立索引
- create index idxname on student(name)
- drop index student.idxname -- 必需要加上表名
- --union ,except,intersect
- --A: UNION 運算符
- -- UNION 運算符經過組合其餘兩個結果表(例如 TABLE1 和 TABLE2)並消去表中任何重複行而派生出一個結果表。當 ALL 隨 UNION 一塊兒使用時(即 UNION ALL),不消除重複行。兩種狀況下,派生表的每一行不是來自 TABLE1 就是來自 TABLE2。
- -- B: EXCEPT 運算符
- -- EXCEPT 運算符經過包括全部在 TABLE1 中但不在 TABLE2 中的行並消除全部重複行而派生出一個結果表。當 ALL 隨 EXCEPT 一塊兒使用時 (EXCEPT ALL),不消除重複行。
- -- C: INTERSECT 運算符
- -- INTERSECT 運算符經過只包括 TABLE1 和 TABLE2 中都有的行並消除全部重複行而派生出一個結果表。當 ALL 隨 INTERSECT 一塊兒使用時 (INTERSECT ALL),不消除重複行。
- -- 注:使用運算詞的幾個查詢結果行必須是一致的
- --判斷要添加列的表中是否有主鍵
- if exists(select 1 from sysobjects where parent_obj=OBJECT_ID ('student') and xtype='PK' )
- begin
- print '表中已有主鍵'
- alter table student add classid int default 0
- end
- else
- begin
- print '表中無主鍵,添加主鍵列'
- alter table student add classid int primary key default 0
- end
--建立備份數據的device
exec sp_addumpdevice disk,testBackUp,'D:\juan\MyNwind_1.dat'
--開始備份
backup database MyTest to testBackUp --在目標位置已經存在的MyTest的備份庫
ide