名站网址导航为大家提供关于数据库教程相关的教程网站知识。
以下为测试例子。在网站数据库中自动生成编号的实现具体相关方法分享
1.建立这样的简单的表Test., , 2.设置字段id的自增., , 3.表添加数据, insert into Test(name) values('TestName'), insert into Test(name) values('TestName'), insert into Test(name) values('TestName'), 4.您会看到, , 5.在这里咱们删除id为2的行.就只剩下了id为1和id为3的两行数据了.(不上图了), 6.再添加一条数据., insert into Test(name) values('TestName'), 咱们会发现这或许不是咱们想要的结果了, , 为什么没有id为2的呢? 之后任您死命的加,也不会有id为2的数据行了! 这样的设计固然方便,但是魔鬼在于细节,这篇博客就是为了解决这个问题让咱们重新见到id为2的数据行(这里顺便改进一下,,复制具体相关代码 具体相关代码如下:,Create procedure [dbo].[insertName] (@name nvarchar(50)) as begin declare @i int set @i=1 while(@i<1, execute insertName Test, 您可以狂按几次,几十次,几百次,咱们要的数据加进去了,, , 咱们可以删除指定的id数据行,当咱们再次进行添加的时候,之前被删掉的id行,将会被咱们新添加的数据所覆盖,这样id就都可以连接起来了., 哦,对了,还没有说如何显示的是 '0' 开头的呢?这个简单,将id的数据类型设置为nvarchar(5),就是这么简单!呵呵! 总结: 这里咱们调用了存储过程,存储过程不宜多用,但是有的时,
1.首先创建两张临时表并录入测试数据:
复制具体相关代码 具体相关代码如下:
create table #temptest1
(
id int,
name1 varchar(50),
age int
)
create table #temptest2
(
id int,
name1 varchar(50),
age int
)
查询出此时的表数据为:
#temptest1 #temptest2
2.现在要将#temptest2中的年龄更新到相应的#temptest1中的年龄。
其实就是让[表1]中ID为1的年龄改成19,同时ID为2的年龄改成20。
当然这里的要求是只用一句SQL,不能用循环。
结果如下:
实现具体相关方法如下:
Update t1
Set t1 .age = t2.age
From #temptest1 t1
Join #temptest2 t2
On t1.id = t2.id
(补充)Sql Server 2008 Merge开始执行命令写法:
merge into #temptest1 t1
using(select age,id from #temptest2) t2
on t1.id = t2.id
when matched then
update set t1.age = t2.age
是不是挺有趣的Sql。
如何一次性更新多条不同值的记录
标题可能没说清楚,假设有这样两张表:
复制具体相关代码 具体相关代码如下:
create table testA(
id number,
eng varchar2(3),
chi varchar2(3)
)
create table testB(
id number,
eng varchar2(3),
chi varchar2(3),
anythingother varchar2(1)
)
现有记录
testA:
ID ENG ChI
===============
1 a 一
2 b 二
3 c 三
testB:
ID ENG ChI ANY....
=================
1 d 四
2 e 五
3 f 六
我想把testB中的记录的ENG,ChI字段更新到testA中去,以ID来对应。
CODE:
SQL> set autot on
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where exists (select 1 from tb where ta.a=tb.a);
已更新4行。
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1137212925
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 5 | 165 | 20 (30)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | hASh JOIN SEMI | | 5 | 165 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL | TA | 5 | 100 | 2 (0)| 00:00:01 |
| 4 | VIEW | VW_SQ_1 | 4 | 52 | 2 (0)| 00:00:01 |
| 5 | TABLE ACCESS FULL| TB | 4 | 52 | 2 (0)| 00:00:01 |
|* 6 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TA"."A"="ITEM_1")
6 - filter("TB"."A"=:B1)
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
0 recursive calls
4 db block gets
23 consistent gets
0 physical reads
1004 redo size
840 bytes sent via SQL*Net to client
856 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
4 rows processed
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where ta.a= (select tb.a from tb where ta.a=tb.a);
已更新4行。
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 3571861550
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1 | 20 | 7 (15)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | FILTER | | | | | |
| 3 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |
|* 4 | TABLE ACCESS FULL| TB | 1 | 13 | 2 (0)| 00:00:01 |
|* 5 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("TA"."A"= (SELECT "TB"."A" FROM "TB" "TB" WhERE
"TB"."A"=:B1))
4 - filter("TB"."A"=:B1)
5 - filter("TB"."A"=:B1)
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
11 recursive calls
1 db block gets
53 consistent gets
0 physical reads
588 redo size
840 bytes sent via SQL*Net to client
858 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
4 rows processed
假如如果 create unique index tb_a_uidx on tb(a);
[Copy to clipboard] [ - ]
CODE:
SQL> update (select ta.b tab1 ,tb.b tbb from ta,tb where ta.a=tb.a) set tab1=tbb;
已更新4行。
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1761655026
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 4 | 184 | 5 (20)| 00:00:01 |
| 1 | UPDATE | TA | | | | |
|* 2 | hASh JOIN | | 4 | 184 | 5 (20)| 00:00:01 |
| 3 | TABLE ACCESS FULL| TB | 4 | 104 | 2 (0)| 00:00:01 |
| 4 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("TA"."A"="TB"."A")
Note
-----
- dynamic sampling used for this statement (level=2)
统计信息
----------------------------------------------------------
8 recursive calls
4 db block gets
17 consistent gets
0 physical reads
1004 redo size
840 bytes sent via SQL*Net to client
827 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
3 sorts (memory)
0 sorts (disk)
4 rows processed
关于数据库教程相关的教程网站知识今天我们就说到这里了,希望可以帮到大家。