[1]--============手动增加约束==========
--手动删除一列(删除EmpAddress列)
alter table Employees drop column EmpAddress
--手动增加一列(增加一列EmpAddr varchar(1000))
alter table Employees add EmpAddr11 varchar(500),AA int ,bb varchar(10)
--手动修改一下EmpEmail的数据类型(varchar(200))
alter table Employees alter column EmpAddr varchar(1000)
--为EmpId增加一个主键约束
alter table Employees add constraint PK_EmpId primary key(EmpId)
--非空约束,为EmpName增加一个非空约束
alter table Employees alter column EmpName varchar(50) not null
--为EmpName增加一个唯一约束
alter table Employees add constraint UQ_EmpName unique(EmpName)
--为性别增加一个默认约束,默认为‘男‘
alter table Employees add constraint DF_EmpGender default(‘男‘) for EmpGender
--为年龄增加一个检查约束:年龄必须在-120岁之间,含岁与岁。
alter table Employees add constraint CK_EmpAge check(EmpAge >=0 and EmpAge<=120)
--创建一个部门表,然后为Employee表增加一个DepId列。
alter table Employees add DeptId int
--增加外键约束
alter table Employees add constraint FK_DeptId_DepId foreign key(DeptId) references Department(DepId)
[2]数据检索
-
执行备注中的代码创建测试数据表。
-
简单的数据检索 :SELECT * FROM Student
-
只检索需要的列 :SELECT sName FROM Student 、SELECT sName,sAge FROM Student
-
列别名:SELECT sName AS 姓名,sAge AS 年龄,sBirthday AS 出生日期 FROM Student
-
使用where检索符合条件的数据:SELECT sName FROM Student WHERE sSex=‘女‘。
-
还可以检索不与任何表关联的数据:select 1+1;select select getdate();
[3]
查询 前10条数据select top 10 * from Customers
查询前百分之十select top 10 percent * from Customers
表达式要加括号select top (10*2) percent * from Customers
select top 5 * from Products order by UnitPrice desc--默认是升序,这样表示降序
select top 5 * from Products order by UnitPrice asc--默认升序(可不写)
============================================================
select top 5 * from TblStudent order by tSAge—年龄最小的学生
select top 10 percent * from TblStudent order by tSAge desc—获得年纪最大的10%学生---有小数就进1
===========================================================================
select distinct * from T1--关键字作用是已经查询后的结果再去除重复
Select distinct sname,sage,sgender from T1
=======================================================
Select top 5 sName,sAge from student
Select top 30 percent sName,sAge from student 非四舍五入,返回最大整数 2.1 返回3
select tSId,tSName+tSGender+tSAddress as信息 from TblStudent----查询信息合集
[4]
类型转换:
CAST ( expression AS data_type [ (length ) ])
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
[5]
--查询没有及格的学生的学号
select tScoreId from TblScore where tMath<60
--查询年龄在20-30岁之间的男学生
select * from TblStudent where tSAge>20 and tSAge<30 and tSGender=‘男‘
select * from TblStudent where tSAge between 20 and 30—这个简单
=============================
select * from Customers
select * from Customers where Country=‘UK‘ or Country=‘USA‘
select * from Customers where Country in(‘UK‘,‘USA‘)
==========================================查询国家的方式
条件多的时候按规定来,则不高效
select studentId from score where english < 60
select sName,sAge,sSex from student where sAge >=20 and sAge <=30 and sSex =‘男‘
select sName,sAge,sSex from student where sAge between 20 and 30 and sSex =‘男‘
[6]
其中关于条件,SQL提供了四种匹配模式:
1,% :表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
比如 SELECT * FROM [user] WHERE u_name LIKE ‘%三%‘
将会把u_name为"张三","张猫三"、"三脚猫","唐三藏"等等有"三"的记录全找出来。
另外,如果需要找出u_name中既有"三"又有"猫"的记录,请使用and条件
SELECT * FROM [user] WHERE u_name LIKE ‘%三%‘ AND u_name LIKE ‘%猫%‘
若使用 SELECT * FROM [user] WHERE u_name LIKE ‘%三%猫%‘
虽然能搜索出"三脚猫",但不能搜索出符合条件的"张猫三"。
2,_ : 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:
比如 SELECT * FROM [user] WHERE u_name LIKE ‘_三_‘
只找出"唐三藏"这样u_name为三个字且中间一个字是"三"的;
再比如 SELECT * FROM [user] WHERE u_name LIKE ‘三__‘;
只找出"三脚猫"这样name为三个字且第一个字是"三"的;
3,[ ] :表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
比如 SELECT * FROM [user] WHERE u_name LIKE ‘[张李王]三‘
将找出"张三"、"李三"、"王三"(而不是"张李王三");
如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为"0-4"、"a-e"
SELECT * FROM [user] WHERE u_name LIKE ‘老[1-9]‘
将找出"老1"、"老2"、……、"老9";
4,[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
比如 SELECT * FROM [user] WHERE u_name LIKE ‘[^张李王]三‘
将找出不姓"张"、"李"、"王"的"赵三"、"孙三"等;
SELECT * FROM [user] WHERE u_name LIKE ‘老[^1-4]‘;
将排除"老1"到"老4",寻找"老5"、"老6"、……
5,查询内容包含通配符时
由于通配符的缘故,导致我们查询特殊字符"%"、"_"、"["的语句无法正常实现,而把特殊字符用"[ ]"括起便可正常查询。据此我们写出以下函数:
function sqlencode(str)
str=replace(str,"[","[[]") ‘此句一定要在最前
str=replace(str,"_","[_]")
str=replace(str,"%","[%]")
sqlencode=str
end function
在查询前将待查字符串先经该函数处理即可,并且在网页上连接数据库用到这类的查询语句时侯要注意:
如Select * FROM user Where name LIKE ‘老[^1-4]‘;上面 《‘》老[^1-4]《‘》是要有单引号的,别忘了!
[*]
--模糊查询
select * from tblstudent
--找张什么什么的
--%表示任意字符多个字
select * from TblStudent where tSName like ‘张%‘
--(-)表示任意字符一次
select * from TblStudent where tSName like ‘张__‘
select * from TblStudent where tSName like ‘张%‘ and LEN(tSName)=3
select * from TblStudent where tSName like ‘张[a-z]_‘
select * from TblStudent where tSName like ‘张[0-9]_‘
select * from TblStudent where tSName like ‘张[^0-9]_‘
select * from TblStudent where tSName like ‘张%[%]%‘
select * from TblStudent where tSName like ‘张%[_]%‘
[7]
select * from TblScore where tEnglish is null 查null
select * from TblScore where tEnglish is not null 查不是null
Null和任何东西加后还是null
[8]
备注1:
select
tSClassId as 班级Id,
count(*) as 班级人数
from TblStudent
group by TSClassId
备注2:
select
tSClassId as 班级Id,
count(*) as 班级人数
from TblStudent
where tSGender=‘男‘
group by TSClassId
可以使用having子句限制返回的结果集。group by 子句可以将查询结果分组,并返回行的汇总信息. 按照group by 子句中指定的表达式的值分组查询结果。
--当在一个查询语句中使用group by语句的时候,
--在select 语句的选择列里面,只能包含group by 语句中出现的列,或者是聚合函数,
--不能包括其他列。
[9]
备注1:
select
tSClassId as 班级Id,
count(*) as 班级人数
from TblStudent
group by TSClassId
having count(*)>3
========================================
select sClassId,count(sName) from student where count(sName)>3 group by sClassId
聚合函数不应出现在WHERE 子句中
select sClassId,count(sName) from student group by sClassId having count(sName)>3
Group by 前可以有where,是对筛选过后的数据进行分组
select sClassId,count(sName) from student where sSex=‘男‘ group by sClassId
select sClassId,count(sName) from student group by sClassId having sAge>30
//错,having是对分组后信息的过滤,能用的列和select中能用的列是一样。
//having无法代替where。
[10]
SELECT 语句的处理顺序
以下步骤显示 SELECT 语句的处理顺序。
1.FROM
2.ON
3.JOIN
4.WHERE
5.GROUP BY
6.WITH CUBE 或 WITH ROLLUP
7.HAVING
8.SELECT
9.DISTINCT
10.ORDER BY
11.TOP
[11]
group by
1.简单分组
2.分组后筛选
3.分组后排序
4.根据多列分组
[12]
Sql中
+:①字符串,连接;
②数字,加法
=======================================
select ‘日期为‘+99
select ‘考试成绩为‘+ cast(99 as varchar(10));
select ‘考试成绩为‘+CONVERT(varchar(10),99);
select * from T2
update T2 set uname=‘凤姐‘
select * from T2 order by convert(int, uId) desc
======================================
select GETDATE()
select CONVERT(nvarchar(10),GETDATE(),120)
select
isnull(convert(varchar(10),tEnglish),‘缺考‘)
from TblScore
select ‘平均成绩是‘ + cast(30 as varchar(3))
select cast(9.85 as int) 舍去小数
ROUND() 4舍5入
在SQL语句中,两个连续的 单引号 ,表示 一个单引号 。(单引号的转义符。)
[13]
=========================================================
扩展知识—自己学习
SELECT FIdNumber,
CAST(RIGHT(sNo,3) AS INTEGER) as 后三位的整数形式,
CAST(RIGHT(sNo,3) AS INTEGER)+1 as 后三位加1,
CONVERT(INTEGER,RIGHT(sNo,3))/2 as 后三位除以2
FROM student
对编号排序,但编号是字符串类型。1、2、11、3、21、36…
=============================================================