- --选择
- 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的备份库