SQL增加
insert into 表名 values(值1,- - -,值n)
SQL修改
update 表名
set 列名1=值1,- - -,列名n=值n
where 更新条件表达式
SQL删除
delete from 表名 where 删除条件表达式
查询结果排序
order by
升序:ASC;降序:DESC
通过制定ALL/Top关键词选择查询结果中的行
例:查询学号最小的3个学生学号
select Top 3 sno from Student Order by Sno
消除取值重复的行
distinct
指定distinct关键词,去掉表中重复的行
select distinct Sno from SC
使用列别名改变查询结果的列标题
as
字符串函数
①datalength(char_expr)
返回字符串包含字符数,但不包含后面的空格
char_expr:可以是字段/变量/表达式
datalength('abc')返回3
②right(char_expr,int_expr)返回字符串右边int_expr个字符
③left(char_expr,int_expr)返回字符串左边int_expr个字符
left('张三丰',1)返回张
算术函数
power(numeric_expr,power)返回power次方
power(3,2)返回9
round(numeric_expr,power)返回numeric_expr精确到power位
round(2.56789,2)返回2.57
sqrt(float_expr)查询float_expr的平方根
sqrt(2)返回1.4142135623731
日期函数
getdate()返回当前日期
Year(date_expr)获取data_expr的年份
Month(data_expr)获取data_expr的月份
day(date_expr)获取date_expr的日期
聚集函数
count(*)统计记录个数(数据行数)
count(distinct 列名)统计一列中值的个数
select count(distinct Sno) from SC 查询选修了课程的学生人数
sum(distinct 列名)计算一列值的总和
查询所有学生所有课程的总分数 select sum(Grade) from SC
avg(distinct 列名)计算一列值的平均值
计算学生呢过所有课程的平均成绩 select avg(Grade) from SC
max(distinct 列名)
min(distinct 列名)
查询学生最高分数 select max(Grade) from SC
group by
按指定的一列或多列值分组,值相等的为一组
select Cno,min(Grade) from sc group by cno