怎么往数据库里插入一个树形结构的表,并且用一句SQL语句将其遍历出来
展开全部
树形结构统一使用下面的测试表与测试数据
CREATE TABLE test_tree (
test_id INT,
pid INT,
test_val VARCHAR(10),
PRIMARY KEY (test_id)
);
INSERT INTO test_tree VALUES(1, NULL, '.NET');
INSERT INTO test_tree VALUES(2, 1, 'C#');
INSERT INTO test_tree VALUES(3, 1, 'J#');
INSERT INTO test_tree VALUES(4, 1, 'ASP.NET');
INSERT INTO test_tree VALUES(5, 1, 'VB.NET');
INSERT INTO test_tree VALUES(6, NULL, 'J2EE');
INSERT INTO test_tree VALUES(7, 6, 'EJB');
INSERT INTO test_tree VALUES(8, 6, 'Servlet');
INSERT INTO test_tree VALUES(9, 6, 'JSP');
INSERT INTO test_tree VALUES(10, NULL, 'Database');
INSERT INTO test_tree VALUES(11, 10, 'DB2');
INSERT INTO test_tree VALUES(12, 10, 'MySQL');
INSERT INTO test_tree VALUES(13, 10, 'Oracle');
INSERT INTO test_tree VALUES(14, 10, 'SQL Server');
INSERT INTO test_tree VALUES(15, 13, 'PL/SQL');
INSERT INTO test_tree VALUES(16, 15, 'Function');
INSERT INTO test_tree VALUES(17, 15, 'Procedure');
INSERT INTO test_tree VALUES(18, 15, 'Package');
INSERT INTO test_tree VALUES(19, 15, 'Cursor');
INSERT INTO test_tree VALUES(20, 14, 'T-SQL');
Oracle
使用 START WITH CONNECT BY
语句实现树状查询
SQL> ed
Wrote file afiedt.buf
1 SELECT
2 LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
3 FROM
4 test_tree
5 START WITH
6 test_id IN (1, 6, 10)
7* CONNECT BY PRIOR test_id = pid
SQL> /
TEST_VAL
-----------------------------------------------------------
.NET
C#
J#
ASP.NET
VB.NET
J2EE
EJB
Servlet
JSP
Database
DB2
TEST_VAL
-----------------------------------------------------------
MySQL
Oracle
PL/SQL
Function
Procedure
Package
Cursor
SQL Server
T-SQL
20 rows selected.
SQL Server
使用 Common Table Expression (CTE) 来实现 递归调用。
1> WITH StepCTE
2> AS
3> (
4> SELECT
5> test_id,
6> pid,
7> test_val,
8> 1 as Lev
9> FROM
10> test_tree
11> WHERE
12> test_id IN (1,6,10)
13> UNION ALL
14> SELECT
15> T.test_id,
16> T.pid,
17> T.test_val,
18> CTE.Lev + 1
19> FROM
20> test_tree T INNER JOIN StepCTE CTE
21> ON T.pid = CTE.test_id
22> )
23> SELECT
24> test_id, pid, test_val, Lev
25> FROM StepCTE;
26> go
test_id pid test_val Lev
----------- ----------- ---------- -----------
1 NULL .NET 1
6 NULL J2EE 1
10 NULL Database 1
11 10 DB2 2
12 10 MySQL 2
13 10 Oracle 2
14 10 SQL Server 2
20 14 T-SQL 3
15 13 PL/SQL 3
16 15 Function 4
17 15 Procedure 4
18 15 Package 4
19 15 Cursor 4
7 6 EJB 2
8 6 Servlet 2
9 6 JSP 2
2 1 C# 2
3 1 J# 2
4 1 ASP.NET 2
5 1 VB.NET 2
(20 行受影响)
CREATE TABLE test_tree (
test_id INT,
pid INT,
test_val VARCHAR(10),
PRIMARY KEY (test_id)
);
INSERT INTO test_tree VALUES(1, NULL, '.NET');
INSERT INTO test_tree VALUES(2, 1, 'C#');
INSERT INTO test_tree VALUES(3, 1, 'J#');
INSERT INTO test_tree VALUES(4, 1, 'ASP.NET');
INSERT INTO test_tree VALUES(5, 1, 'VB.NET');
INSERT INTO test_tree VALUES(6, NULL, 'J2EE');
INSERT INTO test_tree VALUES(7, 6, 'EJB');
INSERT INTO test_tree VALUES(8, 6, 'Servlet');
INSERT INTO test_tree VALUES(9, 6, 'JSP');
INSERT INTO test_tree VALUES(10, NULL, 'Database');
INSERT INTO test_tree VALUES(11, 10, 'DB2');
INSERT INTO test_tree VALUES(12, 10, 'MySQL');
INSERT INTO test_tree VALUES(13, 10, 'Oracle');
INSERT INTO test_tree VALUES(14, 10, 'SQL Server');
INSERT INTO test_tree VALUES(15, 13, 'PL/SQL');
INSERT INTO test_tree VALUES(16, 15, 'Function');
INSERT INTO test_tree VALUES(17, 15, 'Procedure');
INSERT INTO test_tree VALUES(18, 15, 'Package');
INSERT INTO test_tree VALUES(19, 15, 'Cursor');
INSERT INTO test_tree VALUES(20, 14, 'T-SQL');
Oracle
使用 START WITH CONNECT BY
语句实现树状查询
SQL> ed
Wrote file afiedt.buf
1 SELECT
2 LPAD(' ', 2*(LEVEL-1)) || test_val AS test_val
3 FROM
4 test_tree
5 START WITH
6 test_id IN (1, 6, 10)
7* CONNECT BY PRIOR test_id = pid
SQL> /
TEST_VAL
-----------------------------------------------------------
.NET
C#
J#
ASP.NET
VB.NET
J2EE
EJB
Servlet
JSP
Database
DB2
TEST_VAL
-----------------------------------------------------------
MySQL
Oracle
PL/SQL
Function
Procedure
Package
Cursor
SQL Server
T-SQL
20 rows selected.
SQL Server
使用 Common Table Expression (CTE) 来实现 递归调用。
1> WITH StepCTE
2> AS
3> (
4> SELECT
5> test_id,
6> pid,
7> test_val,
8> 1 as Lev
9> FROM
10> test_tree
11> WHERE
12> test_id IN (1,6,10)
13> UNION ALL
14> SELECT
15> T.test_id,
16> T.pid,
17> T.test_val,
18> CTE.Lev + 1
19> FROM
20> test_tree T INNER JOIN StepCTE CTE
21> ON T.pid = CTE.test_id
22> )
23> SELECT
24> test_id, pid, test_val, Lev
25> FROM StepCTE;
26> go
test_id pid test_val Lev
----------- ----------- ---------- -----------
1 NULL .NET 1
6 NULL J2EE 1
10 NULL Database 1
11 10 DB2 2
12 10 MySQL 2
13 10 Oracle 2
14 10 SQL Server 2
20 14 T-SQL 3
15 13 PL/SQL 3
16 15 Function 4
17 15 Procedure 4
18 15 Package 4
19 15 Cursor 4
7 6 EJB 2
8 6 Servlet 2
9 6 JSP 2
2 1 C# 2
3 1 J# 2
4 1 ASP.NET 2
5 1 VB.NET 2
(20 行受影响)
展开全部
ID Value PID
0 根节点 null
1 节点1 0
2 节点2 0
3 节点3 1
..........
然后查询出来 再形成树就可以了
0 根节点 null
1 节点1 0
2 节点2 0
3 节点3 1
..........
然后查询出来 再形成树就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
现在最流行的商业数据库全是关系数据库,只是一对一的,虽然表以是B树的形式存储的,不过你如果只用数据库,那么无法完成树形存储,不过可以通过以文本形式存储XML来解决存取树形结构的问题... 就是把XML当做TEXT 存到数据库中,然后再用XML解析器来操作树,XML本来就是以树形存储的...
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询