A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Dej@vu 中级黑马   /  2013-8-6 18:15  /  1747 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文


--1.数据类型
文本数据类型:char(电话号码);字符 varchar(姓名,地址,性别,ID,密码);nvarchar(邮箱);
数字数据:    int(年龄,余额);
货币:        Money(货币);
datetime:     时间 日期;
Bit:          Bit(性别);

--2.check 约束表达式
成绩在0~100之间:   0<Score AND Score<100
邮箱地址是否有效: SEmail LIKE '%@%'
--3.运算符 通配符
运算符: <>    不等于;   !非
通配符:'_'    一个字符;         A Like'C_'
          %     任意长度字符串;   B Like 'CO_%'
         []   范围内的一个字符; C like 'ST0[1-9]'
         [^]    不在范围内的~~~; D like 'ST0[^1-6]'
--4.SQL语句应用
1.插入数据行:
   insert into表名(列名) values (值列表)
2.插入多行数据
   insert 表名 (列名)     
   select 值,... union
   select 值(default 默认)
3.数据添加到新表:
   select 表名(列名) as 列名
   into 新表名
   from 原始表名
4.更新数据:
   update (表名)
   set (列名 = 更新值) [where 更新条件]
5.删除数据:
   delete from 表名 [删除条件]
6.使用truncate table删除   truncate table 表名
7.复制 新标存在
insert into 新表名 列名
select 旧列名
from 旧表名
新标不存在
select 列名
into 新表名
from 旧表名
8.添加自动增长列
select identity (int,1,1) as 列名
9.添加自动增长列
select identity (int,1,1) as 列名
10.查询列是否空值的记录
select * from 表名 where 表名 is null
select * from 表名 where 表名 is not null
11.查询排序
select 列名 as '别名',(列名*0.9+5) as '别名'
from 表名
where 条件
order by 列名 desc降序,asc升序
12.查询前几行
select top 值 [percent百分比] 列名
from 表名
where 条件
13.字符串函数
select charindex('要查字符','被查找的字符',起始位置) 查找指定字符在另一个字符的起始位置
select len('字符串')                                 查询字符串长度
select upper(lower) ('字符串')                     将字符转为大写(小写)
select ltrim(rtrim) (' 字符串 ')                   清除字符串左(右)的空格
select left(right)('字符串',长度)                  从字符串左(右)提取字符
select replace ('字符串','要替换的字符','新字符')    替换指定字符串中的字符
select stuff ('字符串',起始值,长度,'新字符')         删除字符串中指定的字符并替换
14.日期函数
select getdate ()                                    取得当前系统日期
select dateadd (时间单位[mm月],值,'日期时间')        将值添加到指定日期中
select datediff(时间单位,'日期时间','日期时间(大)')比较日期指定单位的差
select datename(时间单位[dw星期],'日期时间')         指定日期名字
select datepart(时间单位[day天],'日期时间')          日期中指定日期部分的整数形式
15.数学函数
16.系统函数
Convert select convert(varchar(5),123415)            用来转换数据类型
--5.SQL查询语句
1.模糊查询
select * from 表名
where 列名 like '通配符'
2.between 范围查询
select * from Score
where Score (not) between 60 and 80
3.使用in在列表值内查询
select sname as '姓名'
from student
where saddress in ('北京','青岛')
order by saddress
--6.SQL中的聚合函数
1.Sum 求和
select sum(score) as '总成绩'
from Score
where 列名 = '列名值'
2.Avg 平均值
select avg(score) as '平均成绩'
from Score
where Score >=60
3.Max和Min 最大最小值
select avg(Score) as '平均成绩', max(Score) as '最高分', min(Score) as '最低分'
from Score
where Score >=60
4.Count 返回提供表达式中非空值的计数
select count(*) as '及格人数'
from Score
where Score>=60
--使用Group by 分组查询
1.Group by 进行分组
select 列名,avg(Score) as '课程平均成绩'
from Score
group by 列名
2.select 列名1 as 编号,列名2 as '内部测试',avg(Score) as '内测平均成绩'
from Score
group by 列名1,列名2
3.使用Having子句进行分组筛选
select StudentID as '学员编号',StudentID as '内部测试',avg(Score) as '内测平均成绩'
from Score
group by StudentID,StudentID
having count(Score)>1

查询"有多个员工工资不低于2000的部门编号"
select 部门编号, count (*)
from 员工信息表
where 工资 >=2000
group by 部门编号
having count(*) > 1
--多表连接查询
内连接查询
1.select 表名1.列名,表名2.列名
from 表名1,表名2
where 表名1.关系列 = 表名2.关系列
2.select 表名1.列名,表名2.列名
from 表名1
inner join 表名2
on (表名1.关系列 = 表名2.关系列)
外连接查询
1.左外连接查询
select S.Sname,C.CourseID,C.Score
from Student as S
left outer join Score as C on S.SCond = C.StudentID
2.右外连接查询
select Titles.Title_id,Titles.Title,Publishers.Pub_name
from titles
right outer join Publishers on Titles.Pub_id = Publishers.Pub_id

查询'一张表中的奇数行和偶数行'
select A,IDENTITY(int,1,1) as ID
into TEMPTABLE
from TBL

select sum(A) as 奇数列汇表
from TEMPTABLE
where ID%2 <> 0

select sum(A) as 偶数列汇表
from TEMPTABLE
where ID%2 = 0

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1

查看全部评分

2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
顶一个!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马