當前位置:首頁 » 數據倉庫 » 遞歸資料庫好嗎
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

遞歸資料庫好嗎

發布時間: 2022-04-20 21:20:48

⑴ python中序列能被看作是遞歸的資料庫么,原話見補充

應該 不是這么理解哦。 我的理解是這樣的。

一個序列, 可以視為一組遞歸的數據收集行為。

換句話說,應該是可以用遞歸,產生一個系列的數據。

⑵ 資料庫語句的遞歸查詢求助

應該是這樣:

with qry as (select user_id,parent_id from tab where user_id = 32
union all
select tab.user_id,tab.parent_id from tab,qry
where tab.parent_id = qry.id)
select * from qry ;

我用mysql5.0.22,不支持上述語法,oracle就可以(sqlserver應該也可以):
create table tab1(user_id int, parent_id int);
insert into tab1 values(1,null);
insert into tab1 values(32,1);
insert into tab1 values(101,32);
insert into tab1 values(102,32);
insert into tab1 values(201,101);
insert into tab1 values(202,101);
insert into tab1 values(203,102);
insert into tab1 values(204,102);
select * from tab1;
with qry(user_id,parent_id) as (select user_id,parent_id from tab1 where user_id = 32
union all
select tab1.user_id,tab1.parent_id from tab1,qry
where tab1.parent_id = qry.user_id
)
select * from qry;

所以,mysql沒有辦法了,只有寫函數,用循環來實現了。

⑶ 怎麼用遞歸 綁定資料庫 顯示在treeview中

這是以前寫的,可以參考一下!

treeView裡面實現數據綁定

DataSetds;

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!IsPostBack)

{

//以下是綁定數據

SqlConnectioncon=new

SqlConnection("server=.;database=Test;uid=sa;pwd=sa");

SqlDataAdapteradapter=newSqlDataAdapter("select*fromTreeViewTest",con);

ds=newDataSet();

adapter.Fill(ds,"TreeViewTest");

//初始化TreeView:TreeView1.Nodes:表示TreeView1的節點集合,"0"表示的是最頂上節點沒有父節點

InitTree(TreeView1.Nodes,"0");

}

}

///<summary>

///初始化TreeView

///</summary>

///<paramname="Nds">TreeNodeCollection:節點的集合</param>

///<paramname="parentId">parentId:某一節點的父節點ID</param>

privatevoidInitTree(TreeNodeCollectionNds,stringparentId)

{

DataViewdv=newDataView();

TreeNodetmpNd;

//stringintId;

dv.Table=ds.Tables["TreeViewTest"];

//篩選出ParentId【父節點ID】為傳入值【parentId】的所有的數據行為"0"時表時選出所有的頂節點數據行

dv.RowFilter="ParentId='"+parentId+"'";

//將篩選出的所有的數據行添加到節點

foreach(DataRowViewdrvindv)

{

tmpNd=newTreeNode();

tmpNd.Value=drv["NodeId"].ToString();

tmpNd.Text=drv["NodeName"].ToString();

Nds.Add(tmpNd);

//判斷是不是存在drv["NodeId"]的ChildNodes,存在的話添加,不存在就繼續遍歷

DataViewdvTemp=newDataView();

dvTemp.Table=ds.Tables["TreeViewTest"];

dvTemp.RowFilter="ParentId='"+drv["NodeId"].ToString()+"'";

if(dvTemp.Count!=0)

{

InitTree(tmpNd.ChildNodes,tmpNd.Value);

}

}

}

⑷ c#語言遞歸顯示數據資料庫語句查詢

呵呵,算是有緣,這是一道公司面試題目吧,我以前碰到過一模一樣的題目,今天看到你提問就把那以前我弄不出來的題目翻出來寫了一次,我想他給你的資料庫文件和我的一模一樣,因為數據都是一樣的,下面是代碼,新鮮出爐的代碼,加油看,如果看不懂的話可以問我!

Default.aspx.cs文件中代碼

usingSystem;

usingSystem.Data;

usingSystem.Data.SqlClient;

usingSystem.Configuration;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Collections.Generic;

publicpartialclass_Default:System.Web.UI.Page

{

protectedDictionary<string,string>classdic=newDictionary<string,string>();//名稱字典

protectedList<string>lists=newList<string>();//編號泛型

protectedDictionary<string,int>orders=newDictionary<string,int>();//排序號字典

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!IsPostBack)

{

GetClassList("0",0);

stringisorder=Request.QueryString["isorder"];

stringid=Request.QueryString["id"];

switch(isorder)

{

case"del":

{

if(Del(id)>0)

{

Response.Write("<scripttype='text/javascript'>alert('刪除成功');location.href('default.aspx');</script>");

}

else

{

Response.Write("<scripttype='text/javascript'>alert('刪除失敗');location.href('default.aspx');</script>");

}

}

break;

case"up":

{

Updates(id,0);

Response.Write("<scripttype='text/javascript'>location.href('default.aspx');</script>");

}

break;

case"down":

{

Updates(id,1);

Response.Write("<scripttype='text/javascript'>location.href('default.aspx');</script>");

}

break;

}

}

}

///<summary>

///遞歸讀出資料庫方法

///</summary>

///<paramname="ParentID">父ID</param>

///<paramname="Layer">級數,這里只有一級,所以最後也只加了2個空格</param>

protectedvoidGetClassList(stringParentID,intLayer)

{

IDataReaderdr=ClassListDAL(ParentID);

Dictionary<string,string>classdic1=newDictionary<string,string>();

List<string>lists1=newList<string>();

while(dr.Read())

{

classdic1.Add(dr["categoryID"].ToString().Trim(),dr["categoryName"].ToString());

lists1.Add(dr["categoryID"].ToString().Trim());

orders.Add(dr["categoryID"].ToString().Trim(),int.Parse(dr["orderIndex"].ToString()));

}

dr.Close();

for(inti=0;i<classdic1.Count;i++)

{

stringstxt="";

if(Layer>0)

stxt="&nbsp;&nbsp;";

for(intj=1;j<Layer;j++)

{

stxt+="&nbsp;&nbsp;&nbsp;&nbsp;";

}

classdic.Add(lists1[i],stxt+classdic1[lists1[i]]);

lists.Add(lists1[i]);

GetClassList(lists1[i],(Layer+1));

}

}

///<summary>

///讀取資料庫信息

///</summary>

///<paramname="ParentID">父ID</param>

///<returns></returns>

(stringParentID)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//資料庫連接為我本地連接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

cmd.CommandText="select*from[shop_category]whereparentID=@parentIDorderbyorderIndexasc";

SqlParameterparam=newSqlParameter("@parentID",ParentID);

cmd.Parameters.Add(param);

conn.Open();

returncmd.ExecuteReader(CommandBehavior.CloseConnection);

}

///<summary>

///刪除方法,沒測試過,不過應該沒問題

///</summary>

///<paramname="ids">信息編號</param>

///<returns></returns>

protectedintDel(stringids)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//資料庫連接為我本地連接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

cmd.CommandText="deletefrom[shop_category]wherecategoryID=@categoryID";

SqlParameterparam=newSqlParameter("@categoryID",ids);

cmd.Parameters.Add(param);

conn.Open();

inti=cmd.ExecuteNonQuery();

conn.Close();

returni;

}

///<summary>

///升降方法

///</summary>

///<paramname="ids">信息ID</param>

///<paramname="i">升降編號0為升1為降</param>

protectedvoidUpdates(stringids,inti)

{

SqlConnectionconn=newSqlConnection("server=.\SQLEXPRESS;uid=sa;pwd=123456;database=interView");//資料庫連接為我本地連接

SqlCommandcmd=newSqlCommand();

cmd.Connection=conn;

cmd.CommandType=CommandType.Text;

SqlParameterparam=newSqlParameter("@id",ids);

cmd.Parameters.Add(param);

if(i==0)

{

cmd.CommandText="selecttop1*from[shop_category]whereparentID=(selectparentIDfrom[shop_category]wherecategoryID=@id)andorderIndex<(selectorderIndexfrom[shop_category]wherecategoryID=@id)orderbyorderIndexdesc";

}

else

{

cmd.CommandText="selecttop1*from[shop_category]whereparentID=(selectparentIDfrom[shop_category]wherecategoryID=@id)andorderIndex>(selectorderIndexfrom[shop_category]wherecategoryID=@id)orderbyorderIndexasc";

}

conn.Open();

SqlDataReaderread=cmd.ExecuteReader();

if(read.Read())

{

stringid=read["categoryID"].ToString();

intorderid=int.Parse(read["orderIndex"].ToString());

read.Close();

SqlParameterpas=newSqlParameter("@orderid",orderid);

cmd.Parameters.Add(pas);

cmd.CommandText="Update[shop_category]setorderIndex=@orderidwherecategoryID=@id";

if(cmd.ExecuteNonQuery()>0)

{

cmd.Parameters[0].Value=id;

cmd.Parameters[1].Value=int.Parse(Request.QueryString["orid"]);

cmd.CommandText="Update[shop_category]setorderIndex=@orderidwherecategoryID=@id";

cmd.ExecuteNonQuery();

conn.Close();

}

}

}

}

Default.aspx文件代碼

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headrunat="server">

<title>無標題頁</title>

</head>

<body>

<formid="form1"runat="server">

<tablecellpadding="3"cellspacing="3"border="1">

<trstyle="background-image:url(images/tablethbg.gif)">

<tdstyle="width:30%">類別名稱</td>

<tdstyle="width:20%">排序</td>

<tdstyle="width:20%">修改</td>

<tdstyle="width:30%">刪除</td>

</tr>

<%for(inti=0;i<lists.Count;i++)

{%>

<tr>

<td><%=classdic[lists[i]]%></td>

<td><ahref="Default.aspx?isorder=up&id=<%=lists[i]%>&orid=<%=orders[lists[i]]%>"><imgsrc="images/up.gif"alt=""/></a>&nbsp;<ahref="Default.aspx?isorder=down&id=<%=lists[i]%>&orid=<%=orders[lists[i]]%>"><imgsrc="images/down.gif"alt=""/></a></td>

<td><ahref="#">修改</a></td>

<td><ahref="javascript:if(window.confirm('你確定要刪除嗎?')){location.href('Default.aspx?isorder=del&id=<%=lists[i]%>');}">刪除</a></td>

</tr>

<%}%>

</table>

</form>

</body>

</html>

<scripttype="text/javascript">

</script>

測試結果如圖,我沒寫樣式的,希望你能通過面試,不過你還需要加強知識啊~~~

⑸ java 遞歸資料庫生成 樹形結構問題

1、准備表結構及對應的表數據
a、表結構:
create table TB_TREE
(
CID NUMBER not null,
CNAME VARCHAR2(50),
PID NUMBER //父節點
)

b、表數據:

insert into tb_tree (CID, CNAME, PID) values (1, '中國', 0);
insert into tb_tree (CID, CNAME, PID) values (2, '北京市', 1);
insert into tb_tree (CID, CNAME, PID) values (3, '廣東省', 1);
insert into tb_tree (CID, CNAME, PID) values (4, '上海市', 1);
insert into tb_tree (CID, CNAME, PID) values (5, '廣州市', 3);
insert into tb_tree (CID, CNAME, PID) values (6, '深圳市', 3);
insert into tb_tree (CID, CNAME, PID) values (7, '海珠區', 5);
insert into tb_tree (CID, CNAME, PID) values (8, '天河區', 5);
insert into tb_tree (CID, CNAME, PID) values (9, '福田區', 6);
insert into tb_tree (CID, CNAME, PID) values (10, '南山區', 6);
insert into tb_tree (CID, CNAME, PID) values (11, '密雲縣', 2);
insert into tb_tree (CID, CNAME, PID) values (12, '浦東', 4);

2、TreeNode對象,對應tb_tree

public class TreeNode implements Serializable {
private Integer cid;
private String cname;
private Integer pid;
private List nodes = new ArrayList();

public TreeNode() {
}

//getter、setter省略
}

3、測試數據

public class TreeNodeTest {
@Test
public void loadTree() throws Exception{
System.out.println(JsonUtils.javaToJson(recursiveTree(1)));
}

/**
* 遞歸演算法解析成樹形結構
*
* @param cid
* @return
* @author jiqinlin
*/
public TreeNode recursiveTree(int cid) {
//根據cid獲取節點對象(SELECT * FROM tb_tree t WHERE t.cid=?)
TreeNode node = personService.getreeNode(cid);
//查詢cid下的所有子節點(SELECT * FROM tb_tree t WHERE t.pid=?)
List childTreeNodes = personService.queryTreeNode(cid);
//遍歷子節點
for(TreeNode child : childTreeNodes){
TreeNode n = recursiveTree(child.getCid()); //遞歸
node.getNodes().add(n);
}

return node;
}
}

輸出的json格式如下:

{
"cid": 1,
"nodes": [
{
"cid": 2,
"nodes": [
{
"cid": 11,
"nodes": [

],
"cname": "密雲縣",
"pid": 2
}
],
"cname": "北京市",
"pid": 1
},
{
"cid": 3,
"nodes": [
{
"cid": 5,
"nodes": [
{
"cid": 7,
"nodes": [

],
"cname": "海珠區",
"pid": 5
},
{
"cid": 8,
"nodes": [

],
"cname": "天河區",
"pid": 5
}
],
"cname": "廣州市",
"pid": 3
},
{
"cid": 6,
"nodes": [
{
"cid": 9,
"nodes": [

],
"cname": "福田區",
"pid": 6
},
{
"cid": 10,
"nodes": [

],
"cname": "南山區",
"pid": 6
}
],
"cname": "深圳市",
"pid": 3
}
],
"cname": "廣東省",
"pid": 1
},
{
"cid": 4,
"nodes": [
{
"cid": 12,
"nodes": [

],
"cname": "浦東",
"pid": 4
}
],
"cname": "上海市",
"pid": 1
}
],
"cname": "中國",
"pid": 0
}

⑹ 高級資料庫設計中,什麼是遞歸關系遞歸關系有幾種什麼是交疊規則和分離規則

遞歸關系就是對自身的引用,多媒體資料庫中就是記錄與記錄之間的直接或間接的調用。遞歸關系有兩種 一對多 多對多 至於分離和交疊規則 就是 ,父類實例與子類實體之間的關系。若父類的實例屬於多個字類實體則是交疊規則,若父類實例至多屬於一個子類實體則是分離規則。不用謝!^_^

⑺ [PHP][THNIKPHP]如何遞歸查詢資料庫

function get_category($id){
$str=array();
//$sql = "select * from biao where id=$id";查詢節點,自己寫吧
$result = array('id'=>,'parent_id'=>);//查詢結果一個數組格式
if($result){
$str = get_category($result['parent_id']);
$str[]=$result;
}
return $str;
}
}
調用get_category()就行了,$str第一個元素是節點本身,去掉就行了。

⑻ 遞歸讀取資料庫的存儲過程

首先設計數據就得注意,比如說:表有3列 id,name,parentId.有這樣幾條數據1 'a' 02 'b' 03 'c 1隻要存儲過程寫成根據parentId查詢就行。先把parentId=0的數據取出來,然後再根據1取去3

⑼ 資料庫遞歸耗的資源比伺服器遞歸還要多嗎

那應該是查出後再程序里遞歸好一點,估計是樹形結構的數據吧,一般都是查出全部,然後程序里遞歸,不然你不停的執行命令肯定浪費性能。