文章目录
数据库中的注释是在前面加上
--
修改语句
插入语句insert
[]表示可以省略
向一个新建的表中插入
语法:
insert [into] 表名 (需要插入的项目)
values(各项目所对应的值)
(使用default表示填入默认值)
insert into Student(studentID, loginpwd, studentname, sex, gradeId, phone, address, email)
values('s004', '45878562', 'JJnoack', '男', 4, '17191011818', '山西省大同市', '5962@qq.com')
从一个现有表向另一个新建好的表中插入数据
语法:
insert [into] 表名(新建表中需要的项目)
select 原表中对应的项目
from 原表名
insert into default_new_student(nname, nsex)
select studentname, sex
from Student
新建一个表把原表中的数据放到新建的表中
(一般用于备份)
语法:
select 需要备份的项目
into 新表名字
from 原表名
select studentID, loginpwd, studentname, sex, gradeId, phone, address, email
into student_temp
from Student
插入多行数据
语法:
insert [into] 表名 (列名)
select 插入列的内容 union
select 插入列的内容 union
...
(使用这种方式插入不能用default)
insert into default_new_student (nname, nsex)
select '罗老','男' union
select '张大奕', '女' union
select '五四', '男'
使用update更新数据行
语法:
update 表名 set 列名=更新值, 列名=列新值,...
[where 更新条件]
update Student set sex = '男'
update Student set address='山西省太原市' where address='山西省大同市'
update Student set gradeId=gradeId+1, where gradeId<3
使用delete删除数据行
delete [from] 表名 [where 删除条件]
--如果没有删除条件就把表全部删除
--删除的时候如果有外键约束,先把子表中的对应数据删除了
delete from result where Id=1
delete from Student where studentID='s002'
使用truncate删除数据行
语法:
truncate table 表名
truncate table Student
--和
delete from Student
--功能类似
注意:
- 表结构、列、约束等不被改动
- 不能用于有外键约束引用的表
- 标识列重新开始编号
- 实际工作中尽量少用,它删除的数据不能恢复
数据查询
语法:
select 列名
from 表名
[where 查询条件表达式]
[order by 排序的列名]
--order by后面加上esc表示升序,desc表示降序,默认是升序
--order by后面有多个规则表示几个规则的先后顺序
select sCode,sName,sAddress
from student
where sSex='男'
order by sCode
小技巧
-- 列别名
select studentID as 编号 ,phone as 电话号码 from Student
select 编号=studentID, 电话号码=phone from Student
--两种方式等价
--用+连接
select name=studentID+sex, studentname from Student
--查询空行
select * from Student where email is not null
--使用常量列
select name=studentname,地址=address,'中国' as 国家 from student
--限制行数
select top 5 * from Student where sex='男' --按照行数限制
select top 20 percent * from Student where sex='男' --按照百分比限制
函数
函数名 | 描述 | 示例 |
---|---|---|
charindex | 寻找一个指定的字符串在另一个字符串中的起始位置 | charindex(‘or’,‘hello world’,1)最后的1表示在第一个位置开始查找 |
len | 返回传递给它的字符串长度 | len(‘helloworld’) |
upper | 把传递给它的字符串转换为大写 | upper(‘sqlsqlsql’) |
ltrim | 清除字符串左边的空格 | ltrim(’ sql’) |
rtrim | 清除字符串右边的空格 | rtrim(sql ') |
right | 从字符串右边返回指定数目的字符 | right(‘helloworld’,3) |
replace | 替换一个字符串中的字符 | replace(‘anappleaday’,‘a’,‘w’)返回:wnwpplewday |
stuff | 在一个字符串中,删除指定长度的字符,并在该位置插入一个新的字符串 | stuff(‘ABCDEFG’, 2, 3, ‘到瑞米发骚拉稀’)返回:A到瑞米发骚拉稀EFG |
日期函数 | ||
getdate | 获取当前系统日期 | getdate() |
dateadd | 将指定的数值添加到指定的日期部分后的日期 | dateadd(month,4,2019-05-01) |
datediff | 两个日期之间的指定日期部分的间隔 | datediff(month,‘01/01/2009’,‘05/01/2009’)返回:4 |
datename | 日期中指定日期部分的字符串形式 | select datename(dw,‘01/01/2000’) |
datepart | 日期中指定日期部分的整数形式 | select datepart(day,‘01/15/2000’)返回:15 |
数学函数 | ||
rand | 返回从0-1之间的随机float值 | rand() |
abs | 取数值表达式的绝对值 | abs(-5) |
ceiling | 取大于或等于指定数值、表达式的最小整数 | ceiling(43.5)返回:44 |
floor | 取小于或等于指定数值、表达式的最大整数 | floor(43.5)返回:43 |
power | 取数值表达式的幂值 | power(5,2)返回:25 |
round | 将数值表达式四舍五入为指定精度 | round(43.543,1)返回:43.500 |
sign | 正数返回+1,负数返回-1,0返回0 | sign(43) |
sqrt | 取浮点表达式的平方根 | sqrt(9)返回:3 |
系统函数 | ||
CONVERT | 用来转变数据类型 | CONVERT (VARCHAR(5),12345)返回:字符串12345 |
current_user | 返回当前用户的名字 | CURRENT_USER返回:你登录的用户名 |
DATALENGTH | 返回用于指定表达式的字节数 | DATALENGTH(‘中国A联盟’)返回:5 |
host_name | 返回当前用户所登录的计算机名字 | SELECT HOST_NAME()返回:你所登录的计算机的名字 |
system_user | 返回当前所登录的用户名称 | SYSTEM_USER返回:你当前所登录的用户名 |
user_name | 从给定的用户ID返回用户名 | USER_NAME(1)返回:从任意数据库中返回“dbo” |
模糊查询
通配符
- 一类字符,代替一个或多个真正的字符
- 与LIKE关键字一起使用
通配符 | 解释 | 示例 | 符合条件的值 |
---|---|---|---|
- | 一个字符 | A LIKE ‘C_’ | CS、Cd等 |
% | 任意长度的字符串 | B LIKE ‘CO%’ | CONST、COKE等 |
[] | 括号中所指定范围内的一个字符 | C LIKE ‘9W0[1-2]’ | 9W01 |
[^] | 不在括号中所指定范围内的一个字符 | D LIKE ‘9W0[^1-2]’ | 9W03 |
LIKE
a=00[^8]%re[acv]_
a可以是004ewreaw
is null
注意:
- 不能是
a=null
而是a is null或者a is not null
- 空字符是
a=''
空字符串是a is null
两者是有区别的
between
查询某一列中的内容在特定范围内的记录
--两者等价
select * from result where studentResult between 60 and 80
select * from result where studentResult>=60 and studentResult<=80
--between 80 and 60是不能用的
in
查询某一列中内容与所列出的内容列表匹配的记录
--两者等价
select studentname as 姓名 from Student where address in ('北京大学', '清华大学')
select studentname as 姓名 from Student where address='北京大学' or address='清华大学'
聚合函数
sum,avg,max,min,count
select sum(ResultSum) as 学号为23的学生总分 from Result where studentId=23
select avg(studentResult) as 平均成绩,max(studentResult) as 最高分 from result where studentResult>=60
--count是统计行数,(不统计空列)
select count(studentId) from student--这里如果studentId有null则会少统计一行
select count(*) from student--最好这样写
连接查询
内连接(INNER JOIN)
语法:
--两种方法等价
select 需要的列的名字
from 表1
inner join 表2
on 条件
select 需要的列的名字
from 表1,表2
where 条件
select s.studentname,r.subjectId,r.studentResult from student as s--这里表示把表student看作s
inner join
result as r
on s.studentId=r.studentId
select s.studentname,r.subjectId,r.studentResult
from Student as s,result as r
where s.studentId=r.studentId
外连接
注意:
- 语法和内连接差不多,不过要用
left [outer] join
和right [outer] join
- 左外连接要把表中没有的也体现出来,比如某学生缺考了用null表示,左外连接也能体现
- 右外连接要把右边的体现
select student.studentname,result,studentResult from
student left join result on student.studentId=result.studentId
分组查询
语法:
select ...... from 表名
where ......
group by ......
order by 排序条件(只能是select后面的)
select count(*) as 人数, GradeId as 年级--这里不能写studentname,因为它不能给每个分组返回一个值的表达式
from Student
group by GradeId
注意:select列表中只能包含
- 被分组的列
- 为每个分组返回一个值的表达式,如聚合函数
多组排序
--多组排序
select gradeid as 年级,sex as 性别,count(*) as 人数 from student
group by gradeid, sex
order by gradeid asc
分组筛选
语法:
select 列1,列2,... from 表名
where 条件
group by 要分组的那几列
having 筛选条件
order by 排序条件
select count(*) as 人数, gradeId as 年级
from student
group by gradeId
having count(*)>15
--筛选出总人数大于15的年级
重点:
- where:用来筛选from子句中指定的操作所产生的行
- group by:用来分组where子句的输出
- having:用来从分组的结果中筛选行
- 从上到下执行