Sql Server 学习笔记回顾

来源:互联网 发布:女神联盟女神进阶数据 编辑:程序博客网 时间:2024/06/06 07:48

 

create table tCity(cId int,cName varchar(100),cZip nvarchar(1000),zFax char(100))

alter table tCity drop column  [cZip]

alter table tCity add cZip nvarchar(100),cFax char(100) 

insert into tCity values(1,'中国aa','中国aa','中国aa')

select datalength(cName),datalength(cZip),datalength(cFax) from tCity

68100

----------------------------

text, nText / 2G ,1G  2的32次

 

----------------------------

binary, varbinary,image 

----------------------------

cursor

timestamp 邮戳

 

variant  --- Object

 

rowVersion 本版戳

 

uniqueidentifier  -- uGuid

C#: Guid.NewGuid();

Sql:  newid()

 

int --

Guid -- 全球唯一,占16个字节。 

 

select newId()

 

XML 类型, 可把XML流存入到这种类型,有点可以用 xpath

Table 类型 用于变量的数据类型

 

DECLARE @myTable TABLE

 

创建表,表的唯一键

关系/索引  不完全依赖

代码中还是要写。 主外键的关系

 

1。 没有设立主从表,速度更加快

2。 所以可以直接在代码里,写明

 

==================================

 

select *

from 

where---- 针对字段

having..  ---  针对分组后 Group by

 

distinct

top

order by 

 

update 不可用 Group

delete 不可用 having

 

视图: 安全的数据表

 

=====================

 

数据字典是指所有表的集合

 

inner join

left join

right join

 

select * from myTable as m INNER JOIN herTable as h on m.eId = f.eId

where ...

 

语义,按照语义的去书写

 

======================

 

SELECT @1 FROM tEmployee 

GROUP BY @2

 

select * from employees

select titleOfCourtesy,count(*) as numb  from employees 

group by titleOfCourtesy

having count(*) >2

 

=====================

处里脏数据

 

 

select 

case genderId

when 1 then '男'

when 2 then '女'

end as GenerName

from 

 

select 

 

case 

when max(..) is null Then

( )

Else

( )

end

 

----------

存储过程没有返回值,只要 True 和 False

函数在国外经常用

与SQL语句可以自如的融合一起。.

 

 

 getUdcDate() 获得国际事件

 

select getdate()

 

流水号

 

insert into tOrder(orderName,price)

select 

orderNumber = 

(

select case

when Max()

),

price = 12.3

 

------------- 

 

select convert(varchar(8),getDate(),112) + '_001'

 

select unicode('猪')

 

 

select pName, pJob

case pJob

when '科长' then 4,

when '处长' then 3,

when '局长' then 2,

when '部长' then 1

Else then 0

end as orderNumber

from tPerson

order by orderNumber

 

------------------------------------------

select case pJob

when '科长' then 4,

when '处长' then 3,

when '局长' then 2,

when '部长' then 1

Else 0

end as '等级'

count(*) as '个数'

from tPerson

group by 

case pJob

when '科长' then 4,

when '处长' then 3,

when '局长' then 2,

when '部长' then 1

Else 0

end

 

========================================

做高级的菜,简单只要原料好,就可以了。 有时可不是这样。 

 

----------------------------------------

有时要去脏数据,这是就要好的SQL脚本功夫

----------------------------------------

 

Delete tSudent

where Id Not in

(

select Min(*) from tStudent 

group by 课程,学生

having count(*) = 1

)

 

SQL中强大的就是子查询

not in 

group by 

order by 

--

case .. when .. then..

-- 

   case when .. then..

   else ...

 

 

Having 总是为 Group by  做优先服务

 

日期函数

字符串函数

数字函数

 

insert into tOrder(column1,column2) select 'aa','bb')