联结+变量+索引+表数据转存

本文深入探讨SQL查询语句的使用,包括选择类型、聚合函数、连接操作、交叉连接、内连接、优化器选择、外连接(左外、右外、全外)、自连接、视图创建、索引原理及应用,以及异常处理机制。同时,通过实例展示了如何在SQL中进行数据插入、更新、删除操作,并介绍了SQL查询的高级特性如UNION、UNION ALL、连接查询和链接查询。此外,还涉及了变量、常量、条件判断、循环等基本概念的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

select type,sum(price) from titles
where type in('business','mod_cook')
group by type
having type='business'
create database stu

use stu

drop table stu
create table stu
(
sid int identity(1,1) primary key,
sname nvarchar(20)
)

insert into stu values('c')
create table scos
(
ssid int identity(1,1) primary key,
sid int references stu(sid),
sco int,
km nvarchar(20)
)
insert into scos values (2,60,'c')
insert into scos values (NULL,90,'c')

select * from stu
select * from scos
--
--交叉.定义:场景:
select * from stu,scos

--内联接:定义:两个表相同的部分,场景:
select a.sname,b.sid,b.sco,b.km,b.sco
from stu a,scos b
where a.sid=b.sid

select a.sname,b.sid,b.sco,b.km,b.sco
from stu a inner join scos b
on a.sid=b.sid

select a.sname,b.sid,b.sco,b.km,b.sco
from stu a join scos b
on a.sid=b.sid

--采用优化器 hash,merge,romote,
select a.sname,b.sid,b.sco,b.km,b.sco
from stu a inner merge join scos b
on a.sid=b.sid


--外联结
--左外:内联接+左边异同
select a.sname,b.sid,b.sco,b.km,b.sco
from stu a left outer join scos b
on a.sid=b.sid
--右外
select a.sname,b.sid,b.sco,b.km,b.sco
from stu a right outer join scos b
on a.sid=b.sid

--全外
select a.sname,b.sid,b.sco,b.km,b.sco
from stu a full outer join scos b
on a.sid=b.sid

--自联结,定义
select a.city,a.au_lname,b.au_lname from
authors a
inner join authors b
on (a.city=b.city and a.au_lname !=b.au_lname)
order by a.city
--连接,a b串联
--union,minus
select 1 a,2 b
union all
select 1 a,2 b

select fname,hire_date from employee
union
select au_lname,getdate() from authors
--链接查询,也叫即席查询
select fname,lname from employee
union
select firstname,lastname from northwind..employees

select * from OPENROWSET('MSDASQL',
'DRIVER={SQL Server};SERVER=localhost;UID=sa;PWD=sa',
pubs.dbo.authors)

--any some all
-->any/some 大于最小值
-->all大于最大值
--标准sql end
--T-SQL
--变量常量
declare @i int
--给变量赋值
--m1:
set @i=1
--m2:
select @i =2
--输出
--m1:
print @i
--m2:
select @i
--if
declare @j int
set @j = 2
if @j>1
print '...'
else
print 'sss'
--代码段
declare @j int
set @j = 2
if @j>1
begin
print '...'
print '...ddd'
end
else
print 'sss'
--case when
declare @k int
select @k=1
select
case @k
when 1 then 'dsf'
when 2 then 'dsf2'
else 'dsf3'
end

--select into
select * into stu2 from stu
--while
select * from stu2

declare @i int
set @i=1
while @i<1000
begin
insert into stu2 values('test')
set @i=@i+1
end

--table类型、
declare @t table (a int ,b int)
insert into @t values(1,2)
select * from @t

--临时表 session
select * into #tmp
from stu


select * from #tmp

--插入:
-- insert
--select into
--insert into
insert into #tmp
selecty * from biao

--异常处理
--抛出异常,raiseroor
--捕获异常:@@error

declare @i int
set @i=0

begin
if @i=0
raiserror('除数不能为0',16,1)
end
if @@error>0
select '除数不能为0'

declare @i int
set @i=0
print (1/@i)
select @@error

--视图:sql语句,他的数据来源于基表
--可以通过视图插入数据吗?
create view V_STU
with encryption
as
select * from stu

--索引
create table test
(
a int ,
b int
)
insert into test values(4,1)
select * from test

/*
定义:提升查询效率。比方:select * from stu where sname='abc'
索引的分类:
聚集索引:有且仅有一个,一般都给主键了。
要影响物理存储的,并将数据排序
非聚集索引:有很多>=-0个
使用逻辑算法将数据排序,不会影响物理存储。
程序员建立的索引默认都是非聚集
create index IND_TEST on test(a)
场景
*/
create clustered index IND_TEST on test(a)
create index IND_TEST on test(a)
create nonclustered index IND_TEST on test(a)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值