There are many infinite classification types on the network, but there are only two types: recursive algorithm and non-recursive algorithm ..
There are many infinite classification types on the network, but there are only two types: recursive algorithm and non-recursive algorithm ..
The following are my statistical solutions:
Solution 1 (recursive ):
Simple Table Structure:
CategoryID int (4 ),
CategoryName nvarchar (50 ),
ParentID int (4 ),
Depth int (4)
In this way, the parent directory of ParentID is obtained recursively based on the first-level application of ParentID.
You can also add CategoryLeft and CategoryRight to save the parent directory or sub-directory.
Solution 2:
Set a varchar CategoryPath field to save the complete directory path and separate the parent directory IDs with symbols. For example :,
Solution 3:
Method for increasing the number of double digits for each classification
Example:
Level 1 category: 01,02, 03, 04...
Level 2 Category: 0101,0102, 0103,010 4...
Level 3 classification: 010101,010102, 010103...
After analysis, in fact, the third solution cannot be infinitely classified. However, in the second solution, although it is easy to obtain classification information of superiors and lower levels. However, adding and transferring a category can be difficult.
Furthermore, it completely violates the database design paradigm.
In fact, I have been using the second solution. For ease of searching, I sometimes add CategoryID and CategoryPath to the news table.
The algorithm I want to talk about today is actually the ultimate version of the second solution. Generally, classification uses a table to store classification information.
Here, I want to create two new tables. One is to save the classification information table and the other is to save the classification relationship table.
The table structure is as follows:
Table 1: tomi_Category
CategoryID int (4), 'No.
CategoryName nvarchar (50), 'category name
Depth int (4), 'depth
Table 2: tomi_CategoryBind
CategoryID int (4 ),
BindCategoryID int (4 ),
Depth int (4 ),
Adding, editing, and deleting operations are troublesome .. I directly use stored procedures .. I don't know if you can understand it .. Haha.
1. Add a category (Category_Add)
The Code is as follows:
CREATE proc [dbo]. [Category_Add]
@ CategoryName nvarchar (50 ),
@ BindCategoryID int,
@ CategoryID int output
As
Declare @ Success bit
Set @ Success = 1
-- Generate non-repeated CategoryID
Declare @ I bit
Set @ I = 0
While @ I = 0
Begin
Set @ CategoryID = LEFT (10000000 + CONVERT (bigint, ABS (CHECKSUM (NEWID (), 8)
If (not exists (select CategoryID from tomi_Category where CategoryID = @ CategoryID ))
Set @ I = 1
End
-- Get depth
Declare @ depth int
Set @ depth = 0
Select @ depth = depth from tomi_Category where CategoryID = @ BindCategoryID
Set @ depth = @ depth + 1
-- Insert
BEGIN TRAN
Insert into tomi_Category (categoryID, CategoryName, Depth) values (@ CategoryID, @ CategoryName, @ Depth)
If (@ ERROR <> 0)
BEGIN
ROLLBACK TRAN
Set @ Success = 0
END
Insert into tomi_CategoryBind (CategoryID, BindCategoryID, Depth) values (@ CategoryID, @ CategoryID, @ Depth)
If (@ ERROR <> 0)
BEGIN
ROLLBACK TRAN
Set @ Success = 0
END
Insert into tomi_CategoryBind (CategoryID, BindCategoryID, Depth) select @ CategoryID, BindCategoryID, Depth from tomi_CategoryBind where CategoryID = @ BindCategoryID
If (@ ERROR <> 0)
BEGIN
ROLLBACK TRAN
Set @ Success = 0
END
COMMIT TRAN
Print @ CategoryID
Each category has a complete directory structure in tomi_CategoryBind .. The number of records of a category in tomi_CategoryBind is equal to the value of depth in tomi_Category.
Image:
2. Edit and modify a category (Category_Edit)
The Code is as follows:
CREATE proc [dbo]. [Category_Edit]
@ CategoryID int,
@ CategoryName nvarchar (50 ),
@ BindCategoryID int
As
-- Update
BEGIN TRAN
Update tomi_Category set CategoryName = @ CategoryName where CategoryID = @ CategoryID
IF @ ERROR <> 0
BEGIN
ROLLBACK TRAN
Return 0
END
COMMIT TRAN
-- Check whether the parent directory has been changed
Declare @ is bit
Set @ is = 0
If (exists (select CategoryID from tomi_CategoryBind where CategoryID = @ CategoryID and BindCategoryID = @ BindCategoryID and Depth = (select Depth-1 from tomi_Category where CategoryID = @ CategoryID )))
Set @ is = 1
Print @ is
-- Changed the depth
If (@ is = 0)
BEGIN
-- Obtain the depth of the parent directory.
Declare @ depth int
Set @ depth = 0
Select @ depth = depth from tomi_Category where CategoryID = @ BindCategoryID
Set @ depth = @ depth + 1
-- Print @ depth
-- Change the subdirectory
Declare @ I int
Declare @ sCategoryID int
Declare @ sBindCategoryID int
Declare @ tCategoryIDList Table
(
CategoryID int,
FlagID tinyint
)
Insert @ tCategoryIDList select c. CategoryID, 0 from tomi_Category c left join tomi_CategoryBind B on c. CategoryID = B. CategoryID where B. BindCategoryID = @ CategoryID order by c. Depth
Set @ I = 1
Set @ sBindCategoryID = @ BindCategoryID
Declare @ errs int
Set @ errs = 0
BEGIN TRAN
While (@ I> = 1)
BEGIN
Select @ sCategoryID = 0
Select Top 1 @ sCategoryID = CategoryID from @ tCategoryIDList where FlagID = 0
Set @ I =rowcount
-- Print @ sCategoryID
If @ sCategoryID> 0
BEGIN
-- Delete and update
Delete from tomi_CategoryBind where CategoryID = @ sCategoryID
Set @ errs = @ errs + @ error
Update tomi_Category set depth = @ depth where CategoryID = @ sCategoryID
Set @ errs = @ errs + @ error
-- Insert
Insert into tomi_CategoryBind (CategoryID, BindCategoryID, Depth) values (@ sCategoryID, @ sCategoryID, @ Depth)
Set @ errs = @ errs + @ error
Insert into tomi_CategoryBind (CategoryID, BindCategoryID, Depth) select @ sCategoryID, BindCategoryID, Depth from tomi_CategoryBind where CategoryID = @ sBindCategoryID
Set @ errs = @ errs + @ error
Set @ sBindCategoryID = @ sCategoryID
Set @ Depth = @ Depth + 1
-- Print @ sCategoryID
-- Print @ sBindCategoryID
-- Print @ Depth
-- Print '--'
END
Update @ tCategoryIDList set FlagID = 1 where CategoryID = @ sCategoryID
END
If (@ errs> 0)
BEGIN
ROLLBACK TRAN
Return 0
END
Else
COMMIT TRAN
END
3. Deleting a category (Category_Del) directly deletes a subcategory.
The Code is as follows:
Create proc Category_Del
@ CategoryID int
As
BEGIN TRAN
Delete from tomi_Category where CategoryID in (select CategoryID from tomi_CategoryBind where CategoryID = @ CategoryID or BindCategoryID = @ CategoryID)
If (@ ERROR <> 0)
BEGIN
ROLLBACK TRAN
Return 0
END
Delete from tomi_CategoryBind where CategoryID in (select CategoryID from tomi_CategoryBind where CategoryID = @ CategoryID or BindCategoryID = @ CategoryID)
If (@ ERROR <> 0)
BEGIN
ROLLBACK TRAN
Return 0
END
COMMIT TRAN
4. Category list, displaying category (Category_List)
The Code is as follows:
CREATE proc Category_List
As
Select c. * from tomi_Category c left join tomi_CategoryBind B on c. CategoryID = B. CategoryID where B. Depth = 1 order by B. BindCategoryID, c. Depth
GO
Exec Category_List can be directly used to query classification levels. If all data is displayed, you only need to judge the depth value for one query.
Image:
5. List of parent subcategories (Category_upTree)
The Code is as follows:
Create Proc Category_UpTree
@ CategoryID int
As
Select c. * from tomi_Category c left join tomi_CategoryBind B on c. CategoryID = B. BindCategoryID where B. CategoryID = @ CategoryID order by c. Depth
GO
Exec Category_UpTree 63919523 so that you can obtain a complete sub-directory set of the classification. For convenience, you only need an SQL statement.
Image:
6. subcategory list (Category_downTree)
The Code is as follows:
Create Proc Category_DownTree
@ CategoryID int
As
Select c. * from tomi_Category c left join tomi_CategoryBind B on c. CategoryID = B. CategoryID where B. BindCategoryID = @ CategoryID order by c. Depth
GO
Exec Category_DownTree 21779652 to obtain a complete sub-directory of the classification. For example, you can use this to obtain all products under a category and its subcategories .. Convenience: one SQL statement.
Image:
The above is the first draft, just randomly tested several times... If there is any error, please note it ..
Haha. For more information, see the link. Blog is the first in the blog community. Thank you.
Author: TomiWong
Time: 2010.07.18