征集高手思路!数据库设计!

APOLLO_TS 2011-03-29 04:09:57
...全文
174 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
APOLLO_TS 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wxf163 的回复:]
去书店看一会吧
主要看看思路就行
不过这本书买来看也值。
[/Quote]
以上几位给的树形数据设计不能满足要求。没办法,图书馆也没有,还是买了一本,正在看!看了几页。的确值得购买。
qgqch2008 2011-03-29
  • 打赏
  • 举报
回复
擦擦,学习……
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 acherat 的回复:]
树形数据。。。


create table tb
(
id int, --节点
pid int, --父节点
lrFlag bit,--左右树位
name varchar(10)
)
[/Quote]
gengchenhui 2011-03-29
  • 打赏
  • 举报
回复
标记一下,明天看看。。。
快溜 2011-03-29
  • 打赏
  • 举报
回复
创建树形结构的表吧,子id,父ID,单连接就做外键连接。
王向飞 2011-03-29
  • 打赏
  • 举报
回复
去书店看一会吧
主要看看思路就行
不过这本书买来看也值。
APOLLO_TS 2011-03-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wxf163 的回复:]

http://product.dangdang.com/product.aspx?product_id=20246199&ref=search-1-pub
第九章
[/Quote]
9.3.2 天文数据 也不知道里面写的什么东西。
这倒不错!但是没书,但是看标题,这里面的确有,我这个类似天文星体,随意联线形成星座,父子节点的设计恐怕很难实现。多次试验,发现树形探索的路径深度有限。
APOLLO_TS 2011-03-29
  • 打赏
  • 举报
回复
树形结构的路径自上向下。而且层次分明,对数据的要求较高。

比如以上我想要A-->B-->这条路径,如果是树形设计,sql检索往往得到的是 B->A和B->C这种。

ILOVE_ASPNET 2011-03-29
  • 打赏
  • 举报
回复
哇前面二楼的好历害呀, 有勋章的就是历害呀 赞一个
王向飞 2011-03-29
  • 打赏
  • 举报
回复
http://product.dangdang.com/product.aspx?product_id=20246199&ref=search-1-pub
第九章
APOLLO_TS 2011-03-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 fredrickhu 的回复:]

SQL code
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 08:52:38
set nocount on
if object_id('tb','U')is not null drop table tb
go
create table tb(ID int, ParentID int)
inser……
[/Quote]

还是问直接点吧!存储无向图怎么设计?遍历无向图的设计是什么样的?。
Rotel-刘志东 2011-03-29
  • 打赏
  • 举报
回复
父节点与子节点间关系。
liangCK 2011-03-29
  • 打赏
  • 举报
回复
/...
--小F-- 2011-03-29
  • 打赏
  • 举报
回复
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-09-30 08:52:38
set nocount on
if object_id('tb','U')is not null drop table tb
go
create table tb(ID int, ParentID int)
insert into tb select 1,0
insert into tb select 2,1
insert into tb select 3,1
insert into tb select 4,2
insert into tb select 5,3
insert into tb select 6,5
insert into tb select 7,6
-->Title:查找指定節點下的子結點
if object_id('Uf_GetChildID')is not null drop function Uf_GetChildID
go
create function Uf_GetChildID(@ParentID int)
returns @t table(ID int)
as
begin
insert @t select ID from tb where ParentID=@ParentID
while @@rowcount<>0
begin
insert @t select a.ID from tb a inner join @t b
on a.ParentID=b.id and
not exists(select 1 from @t where id=a.id)
end
return
end
go
select * from dbo.Uf_GetChildID(5)
/*
ID
-----------
6
7
*/
-->Title:查找指定節點的所有父結點
if object_id('Uf_GetParentID')is not null drop function Uf_GetParentID
go
create function Uf_GetParentID(@ID int)
returns @t table(ParentID int)
as
begin
insert @t select ParentID from tb where ID=@ID
while @@rowcount!=0
begin
insert @t select a.ParentID from tb a inner join @t b
on a.id=b.ParentID and
not exists(select 1 from @t where ParentID=a.ParentID)
end
return
end
go
select * from dbo.Uf_GetParentID(2)
/*
ParentID
-----------
1
0
*/



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/09/30/4619995.aspx
AcHerat 2011-03-29
  • 打赏
  • 举报
回复
树形数据。。。


create table tb
(
id int, --节点
pid int, --父节点
lrFlag bit,--左右树位
name varchar(10)
)
dawugui 2011-03-29
  • 打赏
  • 举报
回复

/*
标题:SQL SERVER 2000中查询指定节点及其所有子节点的函数(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/

--生成测试数据
create table tb(id varchar(3) , pid varchar(3) , name varchar(10))
insert into tb values('001' , null , '广东省')
insert into tb values('002' , '001' , '广州市')
insert into tb values('003' , '001' , '深圳市')
insert into tb values('004' , '002' , '天河区')
insert into tb values('005' , '003' , '罗湖区')
insert into tb values('006' , '003' , '福田区')
insert into tb values('007' , '003' , '宝安区')
insert into tb values('008' , '007' , '西乡镇')
insert into tb values('009' , '007' , '龙华镇')
insert into tb values('010' , '007' , '松岗镇')
go

--创建用户定义函数
create function f_cid(@id varchar(10)) returns varchar(8000)
as
begin
declare @i int , @ret varchar(8000)
declare @t table(id varchar(10) , pid varchar(10) , level int)
set @i = 1
insert into @t select id , pid , @i from tb where id = @id
while @@rowcount <> 0
begin
set @i = @i + 1
insert into @t select a.id , a.pid , @i from tb a , @t b where a.pid = b.id and b.level = @i - 1
end
select @ret = isnull(@ret , '') + id + ',' from @t
return left(@ret , len(@ret) - 1)
end
go

--执行查询
select id , children = isnull(dbo.f_cid(id) , '') from tb group by id

drop table tb
drop function f_cid

/*
id children
---- ---------------------------------------
001 001,002,003,004,005,006,007,008,009,010
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010

(所影响的行数为 10 行)
*/




/*
标题:SQL SERVER 2005中查询指定节点及其所有子节点的方法(字符串形式显示)
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2010-02-02
地点:新疆乌鲁木齐
*/

create table tb(id varchar(3) , pid varchar(3) , name nvarchar(10))
insert into tb values('001' , null , N'广东省')
insert into tb values('002' , '001' , N'广州市')
insert into tb values('003' , '001' , N'深圳市')
insert into tb values('004' , '002' , N'天河区')
insert into tb values('005' , '003' , N'罗湖区')
insert into tb values('006' , '003' , N'福田区')
insert into tb values('007' , '003' , N'宝安区')
insert into tb values('008' , '007' , N'西乡镇')
insert into tb values('009' , '007' , N'龙华镇')
insert into tb values('010' , '007' , N'松岗镇')
go

;with t as
(
select id , cid = id from tb
union all
select t.id , cid = tb.id
from t join tb on tb.pid = t.cid
)
select id , cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id
order by id
/*
id cid
---- ---------------------------------------
001 001,002,003,005,006,007,008,009,010,004
002 002,004
003 003,005,006,007,008,009,010
004 004
005 005
006 006
007 007,008,009,010
008 008
009 009
010 010

(10 行受影响)
*/

;with t as
(
select id , name , cid = id , path = cast(name as nvarchar(100)) from tb
union all
select t.id , t.name , cid = tb.id , path = cast(tb.name as nvarchar(100))
from t join tb on tb.pid = t.cid
)
select id , name ,
cid = STUFF((SELECT ',' + rtrim(cid) FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , ''),
path = STUFF((SELECT ',' + path FROM t WHERE id = tb.id FOR XML PATH('')) , 1 , 1 , '')
from tb
group by id , name
order by id
/*
id name cid path
---- ---------- ------------------------------------------- ---------------------------------------------------------------------
001 广东省 001,002,003,005,006,007,008,009,010,004 广东省,广州市,深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇,天河区
002 广州市 002,004 广州市,天河区
003 深圳市 003,005,006,007,008,009,010 深圳市,罗湖区,福田区,宝安区,西乡镇,龙华镇,松岗镇
004 天河区 004 天河区
005 罗湖区 005 罗湖区
006 福田区 006 福田区
007 宝安区 007,008,009,010 宝安区,西乡镇,龙华镇,松岗镇
008 西乡镇 008 西乡镇
009 龙华镇 009 龙华镇
010 松岗镇 010 松岗镇

(10 行受影响)
*/

drop table tb

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧