设为首页 收藏本站
查看: 499|回复: 0

[经验分享] SQL Server 数据的简单操作个人笔记(一)

[复制链接]

尚未签到

发表于 2016-11-5 08:58:44 | 显示全部楼层 |阅读模式
--创建数据库
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
 
--案例

--

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-295990-1-1.html 上篇帖子: SQL Server 数据库崩溃后的恢复方法 下篇帖子: 实例说明 sql server 多表级联删除的两种方式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表