假如你的小说如以下结构:目录 第一章标题 第一章内容 第二章标题 第二章内容 第三章标题 第三章内容 第四章标题 第四章内容 第五章标题 第五章内容 第六章标题 第六章内容 第七章标题 第七章内容可以这样创建数据表if object_id('tbl_contents') is not null drop table tbl_contents; gocreate table tbl_contents ( cont_id int not null identity(1, 1) primary key, --内容主键 cont_content nvarchar(max), --存储每个章节的内容 chpt_id int not null ); goif object_id('tbl_chapters') is not null drop table tbl_chapters; go--创建章节表 create table tbl_chapters ( chpt_id int not null primary key, --章节id chpt_title nvarchar(50) not null --章节标题 ); go--创建外键 alter table tbl_contents add constraint FK_CONTENT_CHAPTER foreign key(chpt_id) references tbl_chapters(chpt_id); go--插入测试数据 insert into tbl_chapters select 1, '第一章标题' union all select 2, '第二章标题' union all select 3, '第三章标题' union all select 4, '第四章标题' union all select 5, '第五章标题' union all select 6, '第六章标题' union all select 7, '第七章标题'; goinsert into tbl_contents(cont_content, chpt_id) select '第一章内容', 1 union all select '第二章内容', 2 union all select '第三章内容', 3 union all select '第四章内容', 4 union all select '第五章内容', 5 union all select '第六章内容', 6 union all select '第七章内容', 7; go定位章节的时候可以以下查询,这里定位第三章的内容select chpt_title, cont_content from tbl_chapters cp inner join tbl_contents ct on cp.chpt_id = ct.chpt_id where cp.chpt_id = 3; go
问题:1.如果用数据库,应该怎样建表?不同的书章节数不一样
2.有什么更好的办法把一本书变成单独的文件,又能快速定位章节?
第一章标题
第一章内容
第二章标题
第二章内容
第三章标题
第三章内容
第四章标题
第四章内容
第五章标题
第五章内容
第六章标题
第六章内容
第七章标题
第七章内容可以这样创建数据表if object_id('tbl_contents') is not null
drop table tbl_contents;
gocreate table tbl_contents
(
cont_id int not null identity(1, 1) primary key, --内容主键
cont_content nvarchar(max), --存储每个章节的内容
chpt_id int not null
);
goif object_id('tbl_chapters') is not null
drop table tbl_chapters;
go--创建章节表
create table tbl_chapters
(
chpt_id int not null primary key, --章节id
chpt_title nvarchar(50) not null --章节标题
);
go--创建外键
alter table tbl_contents
add constraint FK_CONTENT_CHAPTER foreign key(chpt_id) references tbl_chapters(chpt_id);
go--插入测试数据
insert into tbl_chapters
select 1, '第一章标题' union all
select 2, '第二章标题' union all
select 3, '第三章标题' union all
select 4, '第四章标题' union all
select 5, '第五章标题' union all
select 6, '第六章标题' union all
select 7, '第七章标题';
goinsert into tbl_contents(cont_content, chpt_id)
select '第一章内容', 1 union all
select '第二章内容', 2 union all
select '第三章内容', 3 union all
select '第四章内容', 4 union all
select '第五章内容', 5 union all
select '第六章内容', 6 union all
select '第七章内容', 7;
go定位章节的时候可以以下查询,这里定位第三章的内容select chpt_title, cont_content
from tbl_chapters cp
inner join tbl_contents ct
on cp.chpt_id = ct.chpt_id
where cp.chpt_id = 3;
go