sql03
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;
------------恢复内容结束------------
sql03的更多相关文章
- Teradata基础教程中的数据库试验环境脚本
Teradata基础教程中的数据库表: Customer: 客户信息表 Location: 位置信息表 Employee: 雇员信息表 Job: 工作信息表 Department: 部门表 ...
- SQL Server 2012 读写分离设置
SQL Server 2012 读写分离设置 - AlsoIn 时间 2014-07-21 17:38:00 博客园-所有随笔区 原文 http://www.cnblogs.com/also/p/ ...
- [SQL Server]一次执行资料夹内的.sql 指令码
原文:[SQL Server]一次执行资料夹内的.sql 指令码 初始资料库时,我们Developers们会准备很多.sql指令码来建立资料表.检视甚至初始资料,那麽要怎麽一次执行资料夹内的*.sql ...
- SQL Server高可用——日志传送(4-2)——部署
原文:SQL Server高可用--日志传送(4-2)--部署 前文再续,书接上一回.本章演示一下日志传送的具体过程 准备工作: 由于时间关系,已经装好了3台虚拟机,且同在一个域里面: SQL01:主 ...
- Sql Server 2012 集群配置
基于Windows Server 2008 R2的WSFC实现SQL Server 2012高可用性组(AlwaysOn Group) 2012年5月 微软新一代数据库产品SQL Server 201 ...
- 《2013传智播客视频》-wmv,avi,mp4.目录
\!--14俄罗斯方块\视频\.复习.avi; \!--14俄罗斯方块\视频\ 复习.avi; \!--14俄罗斯方块\视频\ 形状旋转.avi; \!--14俄罗斯方块\视频\ 判断形状能否变形.a ...
- 2013传智播客视频--.ppt,.pptx,.doc,.docx.目录
\!-- JQ03-JQ事件动画\.1版JS+JQ PPT\00JsDom编程01_邹华栋.docx; \!-- JQ03-JQ事件动画\.1版JS+JQ PPT\00JsDom编程01_邹华栋.pp ...
- MariaDB基础详解
数据库结构模型分类 1.层次模型 2.网状模型 3.关系模型 关系模型的组成部分 二维关系 表 row column 索引 index 视图 view (只包含固定字段,不包含其他字段) 关系型数据库 ...
- SQL Server 2012 读写分离设置 - AlsoIn
原文转至:http://www.tuicool.com/articles/a6rmiam/ 引用: http://technet.microsoft.com/zh-cn/library/jj16176 ...
随机推荐
- zabbix server in not running
修改配置文件 vi zabbix.conf.php 修改lochlhost为 自己服务器的IP地址 修改$DB['SERVER'] = '192.168.30.6'; 修改$ZBX_SERVER ...
- TPO4-1 Deer Populations of the Puget Sound
The causes of this population rebound are consequences of other human actions. First, the major pred ...
- TCP、UDP、HTTP与HTTPS
TCP.UDP.HTTP与HTTPS都是通信协议,在这里首先先介绍一下什么是通信协议. 什么是通信协议? 通信协议(communications protocol)是指双方实体完成通信或服务所必须遵循 ...
- asp.net---jquery--ajax 实现滚动条滚动到底部分页显示
前台:aspx页面 var bgtime = $(" #date1 ").val(); var overtime = $(" #date2 ").val(); ...
- Critical-Value|Critical-Value Approach to Hypothesis Testing
9.2 Critical-Value Approach to Hypothesis Testing example: 对于mean 值 275 的假设: 有一个关于sample mean的distri ...
- Method POST, Status (canceled) error message
I have the following code which is giving me a Method POST, Status (canceled) error message: $(docum ...
- django的引入安装
一 django引入 1 web应用介绍 1 什么是web应用 Web应用程序是一种可以通过Web访问的应用程序,程序的最大好处是用户很容易访问应用程序,用户只需要有浏览器即可,不需要再安装其他软件 ...
- emacs 入门第一课:Emacs里的基本概念
Table of Contents 无聊的开场白 buffer(缓冲区) window(窗口)与frame Emacs的mode Emacs Lisp 函数function.命令command.键绑定 ...
- Shell 快速入门(十八):特殊符号的使用
在 Shell 语言中,经常会看到中括号和括号组成的特殊标识,例如:[].[[]].(()).$(()).().这些符号经常使我们非常迷惑,弄清楚它们之间的作用和区别非常必要. 在开始之前,我们先来学 ...
- linux centos的安装及一些相关知识的整理
相关知识点 ***网桥:主机和虚拟机之间使用"桥接"网络组网 VMware 0 ***Net适配器:把本地网中虚拟机的ip地址转换为主机的外部网络地址 ***仅主机适 ...