--创建数据库
create database
--删除数据库
drop database
--创建表
use sunjob  --选中数据库
create table
(
int identity(1,1) not null, --identity设置标识列
char(20) null,
bit null,
varchar(50),
)
 
--删除表
drop  table master ..Student
drop table
 
--增加一列 alter table 表名 add 字段 数据类型 是否为空
alter table Student add sGrade int null
alter table Student add sAge int null
--删除一列:  alert table 表名 drop 字段
alter table Student drop  sAge ;
--查看表结构
select * from
--插入记录
/*有标识列的时候,不能手动添加
  保持数据完整性,如果允许为空的时候,可以省略不写,但要同时
  有检查约束时候要按要求插入内容
default插入数据
*/
insert into Student(sName,sAddress,sGrade,sAge) values('猪八戒',default,2,12)
 
--删除表内容 而不是删除表delete from 表
delete from
--插入多行内容
--添加记录的表要存在,可以重复执行   通过查询存在的表的字段执行插入指定的表中字段数据
insert into score (names,grade)
select sName,sGrade
from
 
--表 (字段Aa,字段Ab) select 字段Ba,字段Bb from B表
create table
 
(
id int identity(1,2) not null,
char(20),
int,
 
)
select * from
 
 
--第二中方法
--表可以不存在自动创建,不可以重复执行
select sName,sGrade,identity(int,1,1) as
into
from
 
select *from
drop table
delete from
--第三种方法
--列值和列名要对应
--into 有或没有是没影响,要求插入不同的内容才可,重复的内容会覆盖
insert into lesson (sGrade,sName)
select 12,'孙悟空' UNION
select 11,'孙悟空' UNION
select 12,'孙悟空' UNION
select 12,'孙悟空'
 
--更新内容
update lesson set sName='猪无能' where lessonid=18
update lesson set sName='猴子'where sName like '孙%'
update lesson set sName='猪' where lessonid>=19
update lesson set sName='好人一个'
--删除表记录
--delete 是一行一行的删除
--truncate 是全部一次性删除,而且是不能约束条件
delete from lesson where lessonid=19
truncate table
--条件查询
select * from score where id=3
 
 
 
………………………………………………………………………………………………………
--变量
/*
  局部变量(短暂存储中间数据)
       定义方式:declare  @名称数据类型
       赋值(初始化)
              set @ 
              select   多个赋值
*/
--定义
declare @name varchar(10)
declare @age int
declare @seat int
--赋值
set @name='飞龙'--使用set 不能同时给多个变量赋值
print @name   --不能多个输出
select @name=stuName,@age=stuAge,@seat=stuSeat from stuInfo where stuNo='s001'
select @name='龙岗',@age=1,@seat=2
--输出
print '*'
print '姓名是:'+@name 
print '年龄是:'+convert(varchar(10),@age)   --convert 的使用
print '座位号:'+convert(varchar(10),@seat)
 
--案例找出小花的左右座位的同学
declare @seat int
select @seat =stuSeat from stuInfo where stuName='小花'
print @seat   --消息窗口显示
select *from stuInfo where stuSeat in(@seat+1,@seat-1)  --结果
select * from
--全局变量(sql server 系统自动赋值)
print @@version  --你的安装的版本
print @@trancount   --事务统计
print @@servername --服务器名称
print @@rowcount   --数据操作所影响的行数,最后一条语句影响
print @@error     --打印错误号无错得到 有错是一个大于零的数
--逻辑控制语句
--if- else
/*统计并显示本班笔试平均分,如果平均分在以上,
显示“成绩优秀“,并显示前三名学员的考试信息;如果在以下,
显示“本班成绩较差“,并显示后三名学员的考试信息。*/
 
declare @avg  float
select @avg=avg(writtenExam)from
print
if(@avg>70)
begin
print'成绩优秀'
select top 3 * from scroe order by writtenExam desc
end
else
begin
print'成绩较差'
select top 3 * from scroe order by
end
 
--while()循环
/*问题:ÿuc2本¾次Î考¼试Ô成É绩¨较Ï差î,¬假Ù定¨要ª提á分Ö,¬确·保£每¿人Ë笔Ê试Ô都¼通¨过ý。£
提分规则很简单,先每人都加分,看是否都通过,如果没有全部通过,
每人再加分,再看是否都通过,如此反复提分,
直到所有人都通过为止。
*/
select count(*) from scroe where writtenExam<60
while(1=1)
begin
declare @count int
select @count=count(*) from scroe where writtenExam<60
if(@count>0)
update scroe set writtenExam=writtenExam+2
else
break
end
--修正
update scroe set writtenExam=100 where writtenExam>100
select * from
 
--方法二
while(1=1)  --注意下== 要用逻辑表达式
begin
if not exists(select * from scroe where writtenExam>60)
update scroe set writtenExam=writtenExam+2
else
break
end
--修正
update scroe set writtenExam=100 where writtenExam>100
 
 
--case --end
 
declare @labexam int
select @labexam=labExam  from scroe  where stuNo='s003'
print
declare @str varchar(20)  --定义一个变量
set @str=case
when @labexam<60 then '加加油'
when @labexam between 60 and 70 then '一般啦'
when @labexam between 70 and 90 then '还可以啦'
else '还行,'
end
print
 
--案例
--