1、约束

约束详解
->约束的目的:保证数据的完整性. not null ->默认值约束、可空约束、主键约束、外键约束、唯一键约束、检查约束

1) 用sql语句为表添加新的字段

2) 为字段添加默认值

alter table 表名 add constraint 约束名称(DF_表名_字段名) 约束方法 for 子段名;

alter table [dbo].[user] add constraint DF_user_age default(18) for age;

这里生成的约束可在“约束”内删除

3)删除某一字段

如果字段存在约束,需要先删除约束

alter table [dbo].[user] drop column birthdate;

4) 修改某一字段

alter table [dbo].[user] alter column [name] nvarchar(16);

5)主键约束

alter table 表名 add constraint 约束名称(PK_表名_字段名)  primary key(字段名)

6)唯一键约束

alter table [user] add constraint UQ_user_name unique (name);

7)外键约束

一对一,一对多,多对多

外键表关联主键表的主键

添加外键列:

alter table [user] add ClassId int null;
--添加外键关系
alter table [user] add constraint FK_user_Class foreign key(ClassId) references Class(ClassId);

alter table 表名 add constraint 约束名 foreign key(关联字段)  references 主键表名(主键表主键);

2、select 查询

1)其他用法:

添加自定义常数列:

找表中最短的列进行统计

select count(*) from [user];

查找表前5条数据

select top 5 * from [user];

*处可以用各个字段进行代替

3、聚合函数

1)平均值:avg

select avg(字段名1),,avg(字段名2)... from 表名;

select avg(age) from [user];

2)计数:count

select count(age) from [user];

3)求和与最值

select max(age) as 最大值,min(age) as 最小值,sum(age)as 和 from [user];

4、top

一般跟排序order连用

select top 5 * from [user] order by age asc,id desc;

asc升序,desc降序

5、去重distinct

select distinct [age] from [user] order by age asc;

distinct 只能跟在select后

如果distinct后跟多个字段,则系统会综合这几个字段进行去重

6、where 过滤

not 用<>进行表示

select * from [user] where age<>21;

7、区间过滤

可以用and解决

也可用between

select * from [user] where age between 20 and 50;

8、模糊查询

like 关键字

%:匹配任何多个字符(0~多个)

_:仅匹配1个字符

1)例1:查找名字以J开头的数据:

select * from [user] where [name] like 'J%';

2)例2:查找名字中包含a的数据‘’

select * from [user] where [name] like '%a%';

3)例3:查找名字中第4个字符为a的数据‘’

select * from [user] where [name] like '___a%';

4)例4,:查询内容中包含',两个单引号表示1个

select * from [user] where [name] like '%''%';

5) 查找包含数字的1到2

select * from [user] where [age] like '[1-2]%';

6)匹配一个左中括号

select * from [user] where [name] like '[[]%';

或者声明转义

select * from [user] where [name] like '\[%' escape '\';

7)查询空数据

select * from [user] where [age] is null;

可在is后加not表示非空

9、分组group

分组使用时在select后只能跟分组相关信息与聚合函数

select ClassId,count(*),sum(age) from [user] group by ClassId;

------------恢复内容结束------------

最新文章

  1. ThreadLocal内部机制及使用方法
  2. 不可或缺 Windows Native (25) - C++: windows app native, android app native, ios app native
  3. javascript栈的建立样码
  4. linux:指令与档案的搜索
  5. 不加班的实践(1)——这真的该用try-catch吗?
  6. Android ListFragment实例Demo(自己定义适配器)
  7. stat~~~访问文件状态的利器
  8. C语言实现界面(不通过MFC\避免遗忘)
  9. iphone手机上的click和touch
  10. ZUFE OJ 2145 05机关图
  11. nodejs服务实现反向代理,解决本地开发接口请求跨域问题
  12. [SCOI2007]降雨量
  13. 系列博文-Three.js入门指南(张雯莉)-照相机
  14. Python爬虫入门教程 35-100 知乎网全站用户爬虫 scrapy
  15. H5 notification浏览器桌面通知
  16. Bullet3的一些理解
  17. Pyhton2.x 和Python3.x
  18. SQL记录-PLSQL函数
  19. 转: wireshark的使用说明
  20. smali-2.2.4.jar &amp; baksmali-2.2.4.jar

热门文章

  1. mediawiki问题
  2. Pay Back(模拟)
  3. ubuntu 14.04 搜狗拼音安装
  4. PAT甲级——1036 Boys vs Girls
  5. [LC] 345. Reverse Vowels of a String
  6. Java虚拟机内存划分
  7. Derby 命令
  8. python-django-linux上mysql的安装和配置_20191124
  9. VSTO自动安装、卸载工具
  10. [LC] 318. Maximum Product of Word Lengths