事务 事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点 例: begin tran insert into student values('111','王五','男','1999-09-09','95033') if @@ERROR>0 goto tranrollback--直接到tranrollback insert into course values('3-102','语文','804') if @@ERROR>0 goto tranrollback insert i…
触发器(方便备份) 本质上还是一个存储过程,只不过不是通过exec来调用执行,而是通过增删改数据库的操作来执行(可以操作视图) 全部禁用触发器 alter table teacher disable trigger all 全部开启触发器 alter table teacher enable trigger all create trigger TR_student_delete--默认触发器名 on student --在哪个表上操作 instead of delete --为了对表做什么而建…
视图 即虚拟表 系统-右键-新建视图 编辑前200行 select *from studentscore 代码创建法: create view studentscore as select student.sno,sname,ssex,sbirthday,class,cno,degree from student join score on student.Sno=score.sno 删除视图: drop view studentscore 修改视图: alter view cts as sel…
exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen.name = '销售部' )and age>35 (运行方法为逐条查询) select name,sex,age,(select name from bumen where bumen.code = haha.bumen)as 部门 from haha select name,sex,age,(s…
子查询 (用来进行两个或以上表之间的查询) 1.首先新建一个bumen表和一个haha表,填充数据 2.利用两表进行子查询: --部门人数大于5的部门中最大年龄的人的信息--- select MAX(age) from haha where bumen = '销售部' ---子查询 select *from haha where age in ( select MAX(age) from haha where bumen = '销售部' )and bumen in ( ) -------练习1:…
分组 group by select class from xuesheng group by class select class,AVG(chinese)from xuesheng group by class select math from xuesheng where math >80 group by math select math from xuesheng where math between 80 and 90 group by math select math,count(…
聚合函数 --求平均 select AVG(age) as 年龄 from xuesheng select AVG(chinese) as 语文 from xuesheng where class = 1 *只能对数字类型的进行操作 --求个数 select COUNT(*) from xuesheng/*查询表中有多少条数据*/ select COUNT(*) from xuesheng where name like '王%' select COUNT(distinct class) fr…
分离.附加.备份.还原 --去重 select distinct 列名from表名 --更新 update fenshu set name = ‘李四’where code = 9 --更改表名 sp_rename 表名 drop database 数据库名(删除数据库)drop table 表名 Delete from table where 列 表达式 值 select *from xinxi where fenshu between 80 and 100(两数必需前小后大) select…
事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点 具体的实现代码如下: begin tran insert into student values(') goto tranrollback--直接到tranrollback insert into course values(') goto tranrollback insert into score values() begin tranrollback:---回滚事务 rollback tran end else comm…
在程序中,数据库操作是必不可少的部分,所以我们要备足数据库相关知识才能去应付程序中出现的种种问题.基于此,我特地在国外网站.博客上整理了一些问题,并附带了答案和解释.参考.为了保证“原汁原味”,我就保留了英文.大家也来看看你答对了多少? 1.SQL Server 2008 Backup 题目:Is it possible to restore a SQL Server 2008 Enterprise Edition compressed backup to a SQL Server 2008…