SQL server 学习记录(一)

1、创建表
create table(
id int,
name char(10)
)
2、删除表中数据:delete tablename;  直接删除 数据库 数据、结构、日志  drop database;  删除表 drop tablename
3、把表数据插入到另一个表
    3.1:如果要插入目标表不存在: select * into 目标表 from 表 where …
    3.2:如果要插入目标表已经存在: insert into 目的表 select * from 表 where 条件
    3.3:若两表只是有部分(字段)相同,则复制代码 代码如下:insert into b(col1,col2,col3,col4,…) select col1,col2,col3,col4,… from a where…
     如果是跨数据库操作的话: select * into B.btable from A.atable where …
 4、定义变量:declare @name char(10);  设置自变量值:
    4.1 set @name= 'dsaf'
    4.2 select @name= name from [PARTTEST].[dbo].[STUDENT]   where age>1 --从表中选择数据赋值
    4.3 select @name='zhang san'
5、打印数据  print @name
6、全局变量  @@name
    print @@version --当前SQLserver 版本
    print @@total_read  --启动服务器以来,读取总次数
    print @@total_write
    print @@connections --自服务器启动,连接总次数
    print @@pack_sent  --自服务器启动,向网络发送总 数据分组数目
    print @@pack_received
    print @@rowcount  --
    print @@spid --服务器的PID
    print @@trancount --处于活动的事务数量
7、逻辑运算

8、sql server中,数据库名字、表名字、字段名字、关键字(IDUS)不区分大小写。但数据值区分。

9、通配符

select * from student where name like '王%' ---以 王 开头的

select * from student where name like '王_行'  --以王 开头, 以行结尾,并且是3个长度的名字

select * from student where age like '2[2-4]'  --年龄在22,23,24的

select * from sutdent where age like '2[^2-4]' --年领以2 开头,且不包含22,23,24的

10、if 。。。else

declare @x int ,@y int;
set @x=12;
set @y=20;
if @x>@y
    print 'x>y'--如果有多行,要用 begin ...end 包裹
else
    print '! x>y'

11、case用法

select *,add_pro=
 case
 when ID>17028 then 'big'
 else 'small'
 end
 from STUDENT order by id desc

12、while用法,for用法,简单,不写了。

13、waitfor:

waitfor delay '00:00:03' --计时3S,执行
print 'hello delay'

waitfor time '16:45:00' --等到指定的时间,执行
print '16'

14、set 赋值和select赋值:set每次只能赋值一个变量,select可以多个,但set功能更强,且严密,推荐set

declare @x int,@y int;
select @x=1,@y=2;
set @x=1;
set @y=2;

15、视图:向视图中插入数据条件很苛刻,不建议使用。但是可以用查询,对多个表建立视图后,组合得到想要的数据,保存view,后续只用 select *  from  view 就实现了多个表的查询。很好用。

 16、in any ,any

select * from STUDENT where age in (20,23,24,26,28)  --in 在()内的就可以
select * from student where age > all (select age from student where score in (48,96)) --大于 all  后面的所有值
select * from student where age > any (select age from student where score in (48,96))-- 大于 any 后面的任何一个就行

17、having

select age count(age) as 人数 from student where score >80 group by age having count(age) >2

--在成绩大于80分里面,按照年龄分组,选择每组人数 >2人的(年龄-人数 )

18、select 可选 percent

select top 30.6 percent  age as ages,name as names from STUDENT --percent 可选,百分比30.6

19、distinct

--select distinct name,aget from STUDENT --distinct 去除重复的name,age,distinct必须在各个变量的最前面,下面是错误的

select name,distinct age from student

20、多个表的联结

所用数如下:

--select class from STUDENT union  select name from class -- 行增加相加,会删除重复行,结果如下:


--select name from STUDENT union all select name from class  -- 行增加相加,不删除重复行,不排序,速度快,推荐,如下


--select STUDENT.NAME ,class.NAME from STUDENT,CLASS --- 列增加,连接2个表

select * from STUDENT , class --两个表的行数做乘机增加

结果同 交叉联结 cross join, 列相加,行相乘,避免对大表用,计算量太大

select * from STUDENT cross join class


 

21、join联结  join--保留交集,left join 保留左,right join 保留右,full join 补集合

 CLASS.NAME= STUDENT.CLASS或者STUDENT.CLASS=CLASS.NAME 不影响左右,

左右指的  A join B ,A是左,B是右

不论左联结、右联结,A join B的结果都是A在左边,B 在右

22、 select  ...case...

23、

猜你喜欢

转载自blog.csdn.net/u014710355/article/details/81332637