昨天,一同事發(fā)過來的一道數(shù)據(jù)庫題目,就是哪種經(jīng)典的父子級 ID 在同一數(shù)據(jù)庫表中設(shè)計類型。需要在原表中添加一個字段,同時,將該節(jié)點的父子級詳細信息插入到原表新增的一字段中,具體效果如下圖。
AreaCode 、AreaName、ParentCode (原表三字段). Content __新增字段,更新該 AreaCode 下所有父級菜單信息至新增至原表的 Content 字段下面,用紅線框起來(意思應(yīng)該講明白了吧.)
AreaCode:地區(qū) ID AreaName:地區(qū)介紹 ParentCode:父級 AreaCode (Content---將該 AreaCode 下的所有父級 AreaName 拼成 類似:越城區(qū),紹興市,浙江省 字符串插入)
/*
Navicat SQL Server Data Transfer
Source Server : SQL
Source Server Version : 120000
Source Host : .:1433
Source Database : JKCRM
Source Schema : dbo
Target Server Type : SQL Server
Target Server Version : 120000
File Encoding : 65001
Date: 2015-06-12 11:20:40
*/
-- ----------------------------
-- Table structure for AreaRegion
-- ----------------------------
DROP TABLE [dbo].[AreaRegion]
GO
CREATE TABLE [dbo].[AreaRegion] (
[AreaCode] varchar(10) NOT NULL ,
[AreaName] varchar(50) NULL ,
[ParentCode] varchar(10) NULL ,
[Content] nvarchar(200) NULL
)
GO
-- ----------------------------
-- Records of AreaRegion
-- ----------------------------
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110000', N'北京市', N'0', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110100', N'東城區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110200', N'西城區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110300', N'崇文區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110400', N'宣武區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110500', N'朝陽區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110600', N'豐臺區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110700', N'石景山區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110800', N'海淀區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'110900', N'門頭溝區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111000', N'房山區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111100', N'通州區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111200', N'順義區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111300', N'昌平區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111400', N'大興區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111500', N'懷柔區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111600', N'平谷區(qū)', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111700', N'密云縣', N'110000', null)
GO
GO
INSERT INTO [dbo].[AreaRegion] ([AreaCode], [AreaName], [ParentCode], [Content]) VALUES (N'111800', N'延慶縣', N'110000', null)