SQL基础之增删改查,Union all
本帖最后由 fdfdkkk 于 2014-2-24 14:05 编辑/*增删改查*/
/*插入 */
格式
insert 架构名.表名(列,列,.....) values(值,值,....)
1->基本插入
insert into Mydounaifen.TlStudent(stuName,stuAge,stuSex)
values('豆奶粉',22,'女');
insert into Mydounaifen.TlStudent(stuName,stuAge,stuSex)
values('大鹏',22,'男');
这种写法每次只能插入一条数据
2->为自动编号插入数据(tid为自动编号)
set IDENTIY_INSERT TblTeacher on
insert into TblTeacher(tid,tname,tsalary)
values(100,'bob',50000)
3->当向表中的除自动编号外的所有其他列插入数据的时候,这是可以省略列名。
insert into T_Seats values(LoginId,RealName,Pwd);
4->向一个已经存在的表中插入数据,数据的来源是另外一张表
insert into NewTblTeacher (tname,tage)
select tname,tage from TblTeacher
5->返回刚插入数据的自动增长编号
insert into Exe2.LoginTbl output inserted.id values('zdpdsy123','123');
6->表值函数
/*
格式:insert into 表名(列1,列2,…) values(值1,值2,…), (值1,值2,…),
(值1,值2,…);
*/
insert into StudentTest(stuName,stuSex,StuAge)
values('牛亮亮','m',30),('王成伟', 'm', 28),
('赵晓虎', 'm', 29),('李艳茹', 'f', 19),
('牛亮亮','f',22),('苏坤','m',30),('苏坤','f',27);
/*修改 */
格式:
update 架构名.表名set 字段=值,字段=值.... where 条件
update Mydounaifen.TlStudent set stuAge=20 where stuName='豆奶粉';
/*删除*/
-- delete from 表名where 条件;
-- drop database|table|schema 名字;
-- truncate table 表名
示例
drop table Student;
delete from Mydounaifen.TlStudent where stuName='大鹏';
truncate table TblTeacher
===========================
两者区别
1.delete删除的时候,自动编号没有恢复到默认值,而truncate 可以.
2.truncate 删除数据的时候,只能一次性都清除,不能根据条件来删除 则delete可以.
3.truncate删除数据的速度比delete快的多
4.truncate语句不会触发delete触发器
/*查询 */
/*格式
select distinct |top 数字
字段as常量
,包含字段表达式
,函数 Sum,max
,常量
from
表或结果集
where
条件: 逻辑|空值|多条件|模糊|范围
group by
字段
having
筛选条件
order by
字段 desc | asc
执行顺序
from -> where -> group by -> having -> select -> order by
*/
select
*
from MyStudent --1>先从MyStudent表中拿到数据
where fage>30--2>从MyStudent的数据中筛选出所有年龄大于30的人的信息
group by fgender --3>按照性别分组,分完组以后又得到一个新结果集
having count(*)>500 --4>基于分组以后的结果集,然后再筛选,筛选出人数>500的记录
/*
from 子句
寻找数据源,后面可以跟表,视图,表值函数,结果集等
where子句
对from所得到的临时表做一次筛选,是直接在结果中将筛选到的结果组成一个临时表
null值处理
判断一个字段是否为null使用
字段 is null
null表示的是不知道,凡是与null参与的运算得到的结果都是null和不知道
*/
示例代码
--查看整个表
select * from TblScore;
--考试期中及格
select * from TblScore where ScoreNum>=60;
--期末及格考试
select * from TblScore where ScoreLast>=60;
--按照姓名分组
select stuName from StudentTest
group by stuName;
--取成绩前五名
select top 5 * from MyStudent
order by fag desc
/*模糊查询 */
通配符;
1. % //匹配多个字符
select
*
from Mystudent
where fname like '%敏%'
2. -//匹配一个字符
--查询姓赵的同学,且长度为三个
select * from MyStudent
where fanme like '赵__'
select * from MyStudent
where fanme like '赵%' and len(fname)=3
3. []
--查询出姓名包含'雷'或'伟'的人的姓名
select * from MyStudent
where fname like '%[雷伟]%'
用[]括起来,表示转义字符 如[%]
聚合函数
--聚合函数
--count ,max,min ,avg,sum
--聚合函数默认忽略null值
--基本语法:函数名(字段)
select count(*) from StudentTest;
select stuName,count(*),avg(stuAge) from StudentTest
group by stuName;
select avg(scoreNum) from ScoreAgg;
select count(scoreNum) from ScoreAgg;
别名
-- 别名
-- select列 as 别名 (推荐)
-- select列 别名 (不推荐)
--别名=select列 (推荐)
select
custId as "用户ID"--(推荐用)
,count(*) as [用户订单数]
,
case
when count(*)>1 then '高级用户'
else '第一次访问用户'
end as '用户等级'
,sum(unitprice * quantity * discount) as '用户总金额'
from OrdersTable
group by custId
having sum(unitprice * quantity * discount)>120;
--按英语成绩排序,如果英语一样,则按数学成绩排序
select * from MyStudent
order by FEnligsh desc,FMath desc;
--order by 后面可以跟一个表达式
--order by 永远是在语句最后面执行
/*select into */
/*
select into 语法
select
字段等
into 表名
from
数据源
其他子句
*/
select * into
FormSelect
from StuSetInto;
select * from FormSelect;
/*
select * into 新表名from 数据源where 1>2;
只会把表的结构复制过来,
但是键和约束索引等不会复制过来。
除了自动增长的
*/
select * into NewTable from FormSelect where 1>2;
页:
[1]