黑马程序员_学习日记46_614数据库开发及ADO.Net(约束、数据检索)

一、约束

(一)通过设计器创建约束

索引/

CHECK约束(检查约束)

默认约束

主外键关系

(二)通过SQL语句创建约束

--增加主键约束

alter table Employees

add constraint PK_Employees_EmpId primary key(EmpId)

--增加非空约束

alter table Employees

alter column EmpName varchar(50) not null

--增加唯一约束

alter table Employees add constraint

UQ_Employees_EmpName unique(EmpName)

--增加默认约束

alter table Employees add constraint

DF_Employees_EmpGender default(‘’) for EmpGender

--检查约束

alter table Employees add constraint

CK_Employees_EmpAge check(empage>=0 and empage<=120)

--增加外键约束

alter table Employees add constraint

FK_Employees_Department foreign key(DeptId)

references Department(DepId) on delete cascade  --级联删除

--删除外键

alter table Employees drop constraint

FK_Employees_Department

--一条语句删除多个约束,约束名用“,”隔开

alter table Employees drop constraint

FK_Employees_Departmant,

CK_Employees_EmpAge,

UQ_Employees_EmpName

--用一条语句增加多个约束

alter table Employees add

constraint UQ_Employees_EmpName unique(EmpName),

constraint CK_Employees_EmpAge check(EmpAge>=0 and EmpAge<=120)

 

二、数据检索

(一)给列起别名

Fid as 学号,

fname as 姓名,

fage 年龄,

性别=fgender,

数学成绩=fmath,

英语成绩=fenglish

(二)关键字

1select

--获取系统当前时间

select getdate()

2top

--top数字后加percent表示百分比,如果有小数则直接进位。

select top 30 percent * from MyStudent order by fage desc

3distinct去除重复数据,针对查询出的数据

select distinct * from Test2

select distinct * from Test2

(三)聚合函数

--聚合函数把多条汇总成一条,不能和fname匹配

--错误:fname没有出现在聚合函数或group by语句中

select fname,max(fMath) as 数学成绩最高分 from MyStudent

--计算平均分对空值不处理

--count统计列也不考虑空值

(四)带条件的查询

1select筛选列,where筛选行

--in替换多个or

--1,2,3中选择任意一个班级都返回true

select * from TblStudent

where tsclassid in(1,2,3)

2、模糊查询

--查询所有姓赵的同学,通配符%表示任意多个任意字符

select * from Mystuent

where fname like ‘%’

--查询姓赵且姓名总字符个数为3,通配符_表示任意单个字符

select * from Mystudent

where fname like ‘__’

--通配符[],表示[]中的任意字符只选一个匹配

--查询出姓名中包含‘磊’或‘伟’的

select * from Mystudent

where fname like ‘%[磊伟]%’

--表示xy之间只要不是‘磊’或‘伟’的任意单个字符都可以

select * from MyStudent

where fname like ‘x[^磊伟]y’

--查询包含百分号的字符串,用[]括起来表示转义

select * from Mystudent

where fname like ‘%[%]%’

3、空值处理

--通过sql语句将表中的数据修改为null

update Mystudent set fMath=null

where fname like ‘%’

--null在数据库中表示unknown,判断一个值是否为null,也就不能用=<>判断

--查询所有fmathnull的值。null与任何数据运算还是null

select * from Mystudent where fmath is null

--将成绩null换为“缺考”,这里的“缺考”只存在于查询结果中,表中数据不变

select

         fname,

         fage,

         数学成绩=isnull(cast(fmath as varchar(50)),’缺考’)

from Mystudent

4、排序(order by在最后执行)

select * from (select * from Mystudent where fage>20) as tbl

select * from (select top * from Mystudent where fage>20 order by fage desc) as tbl

--先按英语成绩排序,再按数学成绩排序

select * from Mystudent

order by FEnglish desc,FMath desc

--不写排序方式,默认是asc

5、数据分组

--统计男同学个数和女同学个数

--一般分组语句都和聚合函数配合使用

select

         fgender as 性别 --fgender出现在group语句中时,可以和聚合函数一起使用

count(*) as 人数

from Mystudent

group by fgender

6having语句:对组的筛选,哪些组显示,哪些组不显示

         wherehaving的区别:where在分组前筛选,having在分组后筛选

--having语句后能跟什么列,主要看分组后的结果集中包含什么列

select

         tsclassId as 班级Id,

         count(*) as 班级人数

from TblStudent

where tsgender=’

group by tsclassId

having 班级人数>2 --这里不能使用别名“班级人数”,因为当程序执行到having时,select语句还没有被执行,所以这里还没有创建别名。

7Sql语句执行顺序

5>  Select 5-1>选择列,5-2>distinct,5-3>top

1>     From

2>     Where 条件

3>     Group by

4>     Having 筛选条件

6>  Order by

例:

(五)数据转换(castconvert

select cast(100 as varchar(10))+’hello’

select convert(varchar(10),100)+’hello’

(六)联合查询

注意:1、多个额结果集中的的数据类型必须一一对应

           2、列的个数必须一样

union 去除重复数据

union all 不去除重复数据

 

(七)复制表结构(不要数据)

select * into NewStuents from MyStudent where 1<>1

select top 0 * into NewStudents from MyStudent

(八)字符串函数

len(‘哈哈hello’)  --返回字符个数

datalength(‘哈哈hello’)  --返回字节数

left(‘hello welcome to China.’,10)  --从左数,截取10个字符

right(‘hello welcome to China.’,10)  --从右数,截取10个字符

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值