2014年12月15日

摘要: 事务事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点例:begin traninsert into student values('111','王五','男','1999-09-09','95033')if @@ERROR>0goto tranrollback--直接到tranr... 阅读全文
posted @ 2014-12-15 16:25 leaves0529 阅读(217) 评论(0) 推荐(2) 编辑
 
摘要: 触发器(方便备份)本质上还是一个存储过程,只不过不是通过exec来调用执行,而是通过增删改数据库的操作来执行(可以操作视图)全部禁用触发器alter table teacher disable trigger all全部开启触发器alter table teacher enable trigger ... 阅读全文
posted @ 2014-12-15 16:23 leaves0529 阅读(1105) 评论(0) 推荐(2) 编辑
 
摘要: 视图即虚拟表系统-右键-新建视图编辑前200行select *from studentscore代码创建法:create view studentscoreasselect student.sno,sname,ssex,sbirthday,class,cno,degree from studentj... 阅读全文
posted @ 2014-12-15 16:17 leaves0529 阅读(198) 评论(0) 推荐(2) 编辑
 
摘要: 存储过程procedure(proc)数据库—可编程性—存储过程新建存储过程:create proc firstprocasselect *from fenshugo执行存储过程:存储过程—右键—执行存储过程declare @fanhuizhi intexec @fanhuizhi = firstp... 阅读全文
posted @ 2014-12-15 16:13 leaves0529 阅读(209) 评论(0) 推荐(2) 编辑
 
摘要: 流程控制begin ……end将一个语句块包含起来,中间可以写任何语句格式:begin--开始 select *from studentend--结束ifdeclare @bianliang intset @bianliang = 10if @bianliang>10 print '你好'else ... 阅读全文
posted @ 2014-12-15 16:04 leaves0529 阅读(668) 评论(0) 推荐(2) 编辑
 
摘要: 运算符+ - * / %(取余),赋值运算符 =declare @jia intset @jia = 1+1print @jiadeclare @jia intset @jia = 10%3print @jia比较运算符> ,=, (不等于),!=, ! ?逻辑运算符and, or, all(条件全... 阅读全文
posted @ 2014-12-15 16:01 leaves0529 阅读(479) 评论(0) 推荐(2) 编辑
 
摘要: 变量定义变量:declare @hello varchar(20)赋值:set @hello = ‘你好’select(结果框显示)/print(消息框显示) @hello*三行必须同时运行declare @hello varchar(20)set @hello = '销售部'select *fro... 阅读全文
posted @ 2014-12-15 15:59 leaves0529 阅读(171) 评论(0) 推荐(2) 编辑
 
摘要: 约束 除主键约束、外键约束外 唯一约束(主键列、索引列的候选索引) 设计---右键---索引/键---需要修改的列----是唯一的----忽略重复键 代码方式: cid varchar (20) unique 联合主键 所谓主键就是可以唯一确定该行数据,由此可以知道,当一个字段不能决定该行的值时,就 阅读全文
posted @ 2014-12-15 15:46 leaves0529 阅读(252) 评论(0) 推荐(2) 编辑
 
摘要: exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen.name = '销售部' )and age>35 (运行方法为逐条查询) select name, 阅读全文
posted @ 2014-12-15 15:42 leaves0529 阅读(408) 评论(0) 推荐(2) 编辑
 
摘要: 约束主键约束防止在新增数据时出错,有约束性,起唯一标志的作用,在新增条目的时候防止不慎添加重复内容(不允许有null值)1、 右键—设计—设置主键2、在创建表格时设置code int primary key,3、可以设置自增长的功能 code int primary key identity(1,1... 阅读全文
posted @ 2014-12-15 15:40 leaves0529 阅读(162) 评论(0) 推荐(2) 编辑
 
摘要: 子查询(用来进行两个或以上表之间的查询)1、首先新建一个bumen表和一个haha表,填充数据2、利用两表进行子查询:--部门人数大于5的部门中最大年龄的人的信息---select bumen as 部门,COUNT(*)as 人数 from haha group by bumen having C... 阅读全文
posted @ 2014-12-15 15:39 leaves0529 阅读(416) 评论(4) 推荐(2) 编辑
 
摘要: 日期时间数据类型*系统常量:@@DATEFIRST(返回当前时间)DATEADD增加时间语法:DATEADD (datepart , number , date )select DATEADD(YEAR,2,'2013-11-2')DATEDIFF两个日期之间的距离select DATEDIFF(Y... 阅读全文
posted @ 2014-12-15 15:37 leaves0529 阅读(435) 评论(0) 推荐(3) 编辑
 
摘要: 字符串函数ASCII返回字符串首字母的ascii编码select ASCII('name')select ASCII(name) from xueshengselect *from xuesheng where ASCII(name)>=200CHAR--将ascii代码转换成对应的字符select... 阅读全文
posted @ 2014-12-15 15:34 leaves0529 阅读(169) 评论(0) 推荐(2) 编辑
 
摘要: 规范函数:绝对值select abs(-5)print abs(-5)表中取绝对值的方法:select code,name,abs(chinese)as yuwen from xueshengselect *from xuesheng where ABS(chinese)>90天花板、地板selec... 阅读全文
posted @ 2014-12-15 15:32 leaves0529 阅读(196) 评论(0) 推荐(2) 编辑
 
摘要: 分组group byselect class from xuesheng group by classselect class,AVG(chinese)from xuesheng group by classselect math from xuesheng where math >80 group... 阅读全文
posted @ 2014-12-15 15:31 leaves0529 阅读(217) 评论(0) 推荐(2) 编辑
 
摘要: 聚合函数--求平均select AVG(age) as 年龄 from xueshengselect AVG(chinese) as 语文 from xuesheng where class = 1*只能对数字类型的进行操作--求个数select COUNT(*) from xuesheng/*查询... 阅读全文
posted @ 2014-12-15 15:27 leaves0529 阅读(251) 评论(0) 推荐(2) 编辑
 
摘要: 分离、附加、备份、还原--去重select distinct 列名from表名--更新update fenshu set name = ‘李四’where code = 9--更改表名sp_rename 表名drop database 数据库名(删除数据库)drop table 表名Delete f... 阅读全文
posted @ 2014-12-15 15:20 leaves0529 阅读(198) 评论(0) 推荐(2) 编辑