我正在创建一个标有类别的表,其中主类别(父列)包含 0,子类别包含父类别的 ID。我听说它叫做引用。我的问题:这张桌子的结构是否正确?或者有没有更好的方法,比如实现遍历树或类似的方法?
CREATE TABLE `categories` (
`primary_id` int(11) NOT NULL auto_increment,
`master_id` int(11) NOT NULL default '0',
`name` varchar(50) NOT NULL default '',
PRIMARY KEY (`c_id`)
)
INSERT INTO `categories` (`primary_id`, `master_id`, `name`) VALUES
(1, '0', 'Games'),
(2, '0', 'Technology'),
(3, '0', 'Science'),
(4, '0', 'Pop Culture'),
(5, '0', 'Jobs/Services'),
(6, '0', 'Blogs'),
(7, '1', 'Computer'),
(8, '1', 'Game Console'),
(9, '2', 'Cellphone'),
(10, '2', 'Audio/Video/Photos'),
(11, '2', 'Laptop'),
(12, '2', 'Others >'),
(13, '3', 'Human Body'),
(14, '3', 'Ocean Life'),
(15, '3', 'Plant Life'),
(16, '3', 'Animal Life'),
(17, '4', 'Artist'),
(18, '4', 'Upcoming'),
(19, '5', 'Jobs'),
(20, '5', 'Tutoring'),
(21, '5', 'Seminars'),
(22, '5', 'Haircuts'),
(23, '9', 'Iphone'),
(24, '9', 'Android'),
(25, '9', 'Windows Mobile'),
(26, '11', 'Toshiba'),
(27, '11', 'HP'),
(28, '11', 'Apple'),