-
注:数据库对大小写不敏感
创建表:
CREATE
TABLE
T_Person3
(
Id
INT
NOT
NULL,
Name
NV
ARCHAR
(
50
p>
),
Age
INT
NULL)
增加字段
:
alter table
t_person1 add class nvarchar(50)
删除表(放弃表)
:
drop
table
t_person3
在不表中插入数据(
id
自动增长)
:
insert
into
T_person3
(<
/p>
Id
,
Name
,
Age
)
values
(
1
,
< br>'
张三
'
,
23
)
错,
因为
< br>Id
是自动增长的
,
故在此不给
Id
设定值
.
故为:
insert
into
T_person3
(<
/p>
Name
,
Age
)
values
(
'
张三
'
,
< br>23
)
或者
insert
into
t_person3
values
(
'
张<
/p>
三
'
,
23
p>
)
(一般不建议这么用)
如动态插入
guid
:
ins
ert
into
T_person4
(
Id
,
Name
,
Age
)
values
(
newid
(),
'
张二
'
,
27
)
可以设置表中
guid
的默认值为
newid
()
,
让
guid
动态添加,
但是顺序为后加数据在表
上面。
SQL
< br>Server
中常用的
主键数据类型
:
int
(或
bigint
)
+
标识列(又称自动增长字段)
;
uniqueidentifier
(又称
p>
Guid
,
UUID).
在
SQL Server
中产生不重复
的
guid
值:
select
newid()
在
c#
代码中产生<
/p>
guid
:
Guid
id =
Guid
.NewGuid();
更新数据
(
把表中的所有
Age
都设为
30
)
:
update
T_person4
set
age
=
30
(
p>
把
表
中
的
所
有
Age
都
设
为
30,Name
设
为
tom
)
u
pdate
T_person4
set
age
=
30
,
name
< br>=
'tom'
(
把表中
的所有
Age
都加
1
< br>)
update
T_person4
set
age
=
age
+
1
(
p>
把年龄大于等于
20
的人的昵称改为
青年人
update
T_Person1
set
NickName
=
N
'
青
年人
'
where
Age
>=
20-------(N
是为了防
止中文乱码
)
(
p>
把年龄等于
20
的人的昵称改为
二十岁
update
p>
T_Person1
set
NickName
=
N
'
二十
岁
'
where
Age
=
20
(
把
年
p>
龄
不
等
于
20
的
人
的
昵
称
改
为
不
是
二
十
岁
update
< br>
T_Person1
set
NickName
=
N
'
不是二十岁
'
where
Age
<>
20
(
把年龄等于
20
或者
30
的人的昵称改为
壮年
update<
/p>
T_Person1
set
NickName
=
N
'
壮年
'
where
Age
=
20
or
age
=
30
(
把年龄大于
20
小于
30
或者等于
80
的人的昵称改为
壮青年
update
T_Person1
set
NickName
=
N
'
青壮年
'
where
Age
>
20
and
age
<
40 or age=80
删除表中全部数据:
delect form T_Perso
n
,
Delect
只是删除数据表还在
和
drop table T_Person
不同,
drop
删除表
删除年
龄大于
50
的数据
delete
from
T_Person1
where
age
>
50
检索数据:
select
*
from
T_person1
where
age
>
50
and
nickname
=
'
不惑之年
'
只检索
name
,
age
字段:
select
name
,
age
from
T_person1
where
age
>
50
and
nickname
=
'
不惑之
年
'
给字段起个别名
:
select
name
as
姓名
,
age
as
年龄
,
class
as
班级
,
lick
as
爱好
,
nickname
as
昵
称
from
T_person1
where
age
>
30
and
nickname
=
'
壮年
'
selec
t
的一些用法不和任何表有关系
:
Se
lect
newid()
获得
gui
d,
select
getdate()
获得系统当前时
间
;
select<
/p>
@@version
查看
sql
server
的版本
数据的聚合
查看所有数据
:
select
count
(*)
from
t_person1
<
/p>
查看在某些条件下的数据
:
select
count
(*)
from
t_person1
where
age
>
50
and
nickname
=
'
足球
'
查寻最高<
/p>
(
低
)
年龄
p>
:
select
max
(
age
)
from
t_person1;
select
min
(
age
)
from
t_person1
所有年龄的和
:
select
p>
sum
(
age
)
from
t_person1 <
/p>
平均年龄
:
select
avg
(
age
)
from
t_person1
数据的排序
数据的排序
(
按年龄升序
):
s
elect
*
from
t_person1
order
by
age
ASC
数据的排序
< br>(
按年龄降序
):
select
*
from
t_person1
order
by
age
DESC
如果年龄相同则按
number
降序排列:
select
*
from
t_person1
order
by
age
ASC
,
number
DESC
通配符过滤
单字符通配符:
半角下划线,匹配单个出现的字符,以任意字符开头,剩余部分为“
_<
/p>
班”
:
select
*
from
t_person1
where
class
like
'_
班
'
;
多字符匹配的通配符
:
半角百分号
“
%
”
,
它匹配任意次数
(零个或者多个)
出现的任意字符。
例如“
k%
”匹配以“
k
”开头、任意长度字符串。检索姓名中包含“
伟”的人员信息:
select
*
from
t_person1
where
name
like
'%
伟
'
;
select
*
from
t_person1
where
class
like
'%
级三
%'
;(
匹配中间相同的字符<
/p>
)
空值处理
关于
null
:
null
不表示没有
,
表示不知道<
/p>
,
有
null
参
与的运算都是不知道
查询表中的字段为
null
的数据:
select
*
from
T_person1
where
class
is
null
(不是
class=null
)
查询表中的字段不为
null
的数据:
select
*
from
T_person1
where
class
is
not
null
(不是
class=null
)
多值匹配
从某几个固定的值选
:
select
*
from
T_person1
where
age
=
21
or
age
=
24
or
age
=
40
或者
select
*
from
t_person1
where
age
in (
21
,
24
,
40
)
从某个范围选
:
select
*
from
t_person1
where
age
>
20
and
age
<
30
或者
select
*
from
t_person1
where
age
between
20
and
30
数据分组
根据年龄进行分组
:
select
age
,
count
(*)
from
t_person1
group
by
age
Having
语句
(
对分组后信息的过滤
,
p>
能用的列和
select
中能用的列一样<
/p>
)
注
:
聚合函数
不应出现在
where
语句中
区别
:where
是对原始数据进行过滤
p>
,having
是对分组后的数据进行过滤的
.
选取人数大于一个的组
select
age
,
count
(*)
from
t_person1
group
by
age
having
count
(*)>
1
限制结果集范围
如果我们想按年龄降
序排列:
select
*
from
t_person1
order
by
age
desc
如果我们想取得年龄最大的前三名降序排列:
select
top
3
*
from
t_person1
order
by
age
desc
除去年龄最大的前五
,
剩余的年龄最大前三个
:
select
top
3
*
from
t_person1
where
age
not
in
(
select
top
5 age
from
t_person1
order
by
age
desc
)
order
by
age
desc
选出前五个
,
然后排除掉
,
就
是
从
第
六
个
开
始
了
.desc
为降序排列
消除重复数据
先选择需要选择的数据
:
select
lick
from
t_person1(
从表里面选择喜欢爱好字段图左
)
过滤掉重复的
select
distinct
lick
from
t_person1
<
/p>
如需要限制两个字段条件
,
过滤掉两个字
段都重复的行则有
select
distinct
class
,
lick
from
t_person1
联合结果集
union
把两个查询结果合成一个表:
Uni
on
会把完全重复的结果去掉,如果不想去掉可以把
selec
t
name
,
age
from
t_person1
union
改为
union
all
,如没有特殊理由请不要丢到
all
Union
select
name
,
age
from
t_person3
上下
两个查询的表必须字段个数相同,如字段个数不同,需要补充相关字段的值。如下:
select
name
,
age
,
class
from
t_person1
union
select
name
,
age
,
'
毕业了,没班级
'
from
t_person3
<
/p>
写报表一般把不同的查询结果写到一张表上
,
例如
select
't_person1
最高年龄
'
as
'
< br>类别
'
,
max
(
age
)
as
age
from
t_person1
union
all
select
't_person1
最低年龄
'
,
min
(
age
)
from
t_person1
union
all
select
't_person1
平均年龄
'
,
avg
(
age
)
from
t_person1
union
all
select
't_person3
最高年龄
'
,
max
(
age
)
from
t_person3
union
all
select
't_person3
最低年龄
'
,
min
(
age
)
from
t_person3
union
all
select
't_person3
平均年龄
'
,
avg
(
age
)
from
t_person3
会生成如下表
:
查出
T_person1
的姓名
,
年龄表
,
以及年龄合计表然后合成一张表
:
select
name
,
age
from
t_person1
union
all
select
'
年龄合计
'
,
sum
(
age
)
from
t_person1