SQL Server 数据的简单操作个人笔记(一)

--创建数据库

create database sunjob

--删除数据库

drop database sunjob

--创建表

use sunjob  --选中数据库

create table Student

(

  ID int identity(1,1) not null, --identity设置标识列

  sName char(20) null,

  sSex  bit null,

  sAddress varchar(50),

)

 

--删除表

drop  table master ..Student

drop table Student

 

--增加一列 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 Student

--插入记录

/*有标识列的时候,不能手动添加

  保持数据完整性,如果允许为空的时候,可以省略不写,但要同时

  有检查约束时候要按要求插入内容

 有默认值的时候 可以使用default插入数据

*/

insert into Student(sName,sAddress,sGrade,sAge) values('猪八戒',default,2,12)

 

--删除表内容 而不是删除表delete from

  delete from Student

--插入多行内容

--添加记录的表要存在,可以重复执行   通过查询存在的表的字段执行插入指定的表中字段数据

insert into score (names,grade)

select sName,sGrade

from Student

 

-- insert into A (字段Aa,字段Ab) select 字段Ba,字段Bb from B 

create table score

 

(

id int identity(1,2) not null,

 names char(20),

 grade int,

 

)

select * from score

 

 

--第二中方法

--表可以不存在自动创建,不可以重复执行

select sName,sGrade,identity(int,1,1) as lessonid

into lesson

from Student

 

select *from lesson

drop table lesson

delete from lesson

--第三种方法

--列值和列名要对应

--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 lesson

--条件查询

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 stuInfo

--全局变量(sql server 系统自动赋值)

 print @@version   --你的安装的版本

 print @@trancount   --事务统计

 print @@servername --服务器名称

 print @@rowcount   --数据操作所影响的行数,最后一条语句影响

 print @@error     --打印错误号无错得到 有错是一个大于零的数

--逻辑控制语句

--if- else

/*统计并显示本班笔试平均分,如果平均分在以上,

显示成绩优秀,并显示前三名学员的考试信息;如果在以下,

显示本班成绩较差,并显示后三名学员的考试信息。*/

 

declare @avg  float

select @avg=avg(writtenExam)from scroe

print @avg

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 writtenExam

  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 scroe

 

--方法二

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 @labexam

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 @str

 

--案例

--

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值