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

[经验分享] sql server 学习笔记

[复制链接]

尚未签到

发表于 2016-10-31 02:50:21 | 显示全部楼层 |阅读模式
  1.将数据库的字符集修改为:
alter database dbname collate Chinese_PRC_CI_AS

2、如果是表中的字段:
ALTER TABLE Userinfo ALTER COLUMN name VARCHAR(4) COLLATE Chinese_PRC_CI_AI

如果修改整个服务器的默认排序规则,用Rebuildm.exe重建master库 

1、create database school 创建数据库school

2、drop database school 删除数据库school

3、use school 连接到school数据库,使其成为当前数据库

4、create table class(classID int primary key identity not null)
创建一个名为class的表,其有一个int型数据classID字段,该字段设置了主键约束
并自动编号列且不能为空


7、select * into class2 from class
   将class表中的所有数据复制到class2表中

8、select * into class2 from class where 1=0 只复制表结构

9、insert into class2(className) values('Juhn')或
   insert into class2(className,tel) values('Bile','0731-2255664')   在class2表中插入一条记录
Insert into RPMIItbSales ([ControlProcessed],[ControlTimeStamp])[]可有可无
  
10、 delete from Student where StudentID between  13 and 15
    删除Student表中StudentID在13至15之间的数据(包括13和15)

11、alter table class2 add tel varchar(15) default('没有电话')
    修改表class2,为它添加一个tel列并将其默认值设为'没有电话'

12、alter table class2 drop column tel 删除列

13、alter table student add constraint telDefault  default('没有电话') for tel
    修改tel列的默认值

14、create table class3(classID int ,constraint id_key primary key(classID))
    创建一个名为class3的表并为它设置了名为id_key的主键约束

16、alter table class2 add age int check (age between 0 and 120)
    为class2添一个age列,并为其设置检查约束,使其的取值在0到120之间

17、alter table class2 add age int ,constraint ageCheck check
    (age between 0 and 120)
    其设置检查约束方法二,为约束取名为ageCheck

18、alter table class2 add tel varchar(15) ,check
(tel like '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
其设置检查约束方法三,此为模糊约束

19、create table class3(ID int primary key identity,classID int ,
    name varchar(15) ,constraint classID foreign key(classID) references
    class2(classID))
    设置外键约束

20、alter table class3 drop classID
删除class3的classID外键约束

21、create index class2Name on class2(className)
    在class2表的calssName字段上创建一个class2Name的索引

22、create unique index class2Name on class2(className)
    创建唯一索引

23、select classID,className from class2 where className='body'
    select classID,className from class2 where className like '%s'
    创建索引后查询的的速度将更快,但会降低insert、update、delete的执行速度

24、drop index class2.class2Name 删除表class2上的class2Name索引

25、update class2 set className='Lida',tel='13787277732' where classID=3
    将class2表中className和tel列、classID=3
    行的单元格的值改为'Lida'和'13787277732',注意忽略where语句将改变表中所有的行

26、create default sexDefault as '男'; 创建一个名为sexDefault的默认值
    sp_bindefault sexDefault,'student.sex';
    将创建的sexDefault默认值绑定到student表的sex字段上

27、insert into class2(name,names) select name,names from class1
    将class1中的数据全部复制到class2中

28、truncate table class 删除class表中所有的行

29、select Name 国家,Population 人口 from BBC where Name
    in('France','Cermany','Italy United')
    查询BBC表中'France','Cermany','Italy United'三个地区的所在的国家和人口数

30、select Name 国家 from BBC where Name like '%United%'
    查询BBC表中的Name字段中包含United字符的国家,通配符"_"表示匹配任意单个字符

30、select Name 国家, Population 人口 from BBC where Population>100000000 order by Population desc
    查询BBC表中的Population字段大于100000000的国家和人口,并按降序排序,默认为升序asc

31、select Name,round(Population/1000000,0) as '人口(百万)' from BBC where Region='South Asia'
    查询BBC表中的Region='South Asia'国家和百万人口数(round是四舍五入)

32、select distinct Region from BBC
    查询BBC表中的Region字段中的非重复数据,distinct排除重复数据
    如有多列则作用在列的组合上,而不再作用在单列上

33、select top 50 percent * from BBC
    查询BBC表中的所有字段,但只返回总行数的50%,percent表百分数、可选
  select top 3*from BBC

34、select * from BBC where Area>100 and not GDP<10000000
    查询BBC表中的所有Area小于100并且GDP不小于10000000的数据,会返回所有的列,不只是Area列

33、select * from BBC where Area not between 20000 and 30000
    查询BBC表中的所有Area不在20000和30000之间的数据,会返回所有的列,不只是Area列

34、select distinct Name+str(Age) 学生 from Student
    查询Student表中Name和Age字段都不重复的数据
    str(Age)返回Age的字符串表达形式,"学生"是别名
  
35、select * from Student where Nealth is null
    查询Student表中Nealth字段为null的数据
  
36、exec sp_helpconstraint 'Teacher'
    查看'Teacher'表中的所有约束
  
37、select * from Student where StudentID=1
for xml raw
返回XML语句
  
39、create procedure insert_Procedure
@Name varchar(10),
@Sex varchar(2),
@Age int,
@Tel varchar(20),
@Address varchar(50)
as
insert into student(Name,Sex,Age,Tel,Address) values(@Name,@Sex,@Age,@Tel,@Address)
创建一个插入数据的存储过程
40、select datediff(day,'20090403',getdate())
用指定时间减去当前时间,返回的是天数,还可以用month返回月数

运维网声明 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-293378-1-1.html 上篇帖子: Sql Server 触发器(二) 下篇帖子: SQL Server循环语句
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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