小说网站 数据库

小说网站 数据库 SQL语句

一、创建数据库

create database novel;

二、数据表的建立

小说表
create table `n_book`(
	`book_id` int auto_increment,
	`book_name` char(10) unique not null,
	`book_intro` tinytext not null,
	`book_author_id` int,
	`book_class_id` int,  
	`book_star` int default 0,
	primary key (`book_id`),
	foreign key (`book_author_id`) references n_author(`author_id`),
	foreign key (`book_class_id`) references n_class(`class_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
章节表
create table n_chapter(
	`chapter_id` int auto_increment,
	`chapter_number` int not null,
	`chapter_name` char(10) not null,
	`chapter_content` text not null,
	`chapter_book_id` int,
	primary key (`chapter_id`),
	foreign key (`chapter_book_id`) references n_book(`book_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
读者表
create table n_reader(
	`reader_id` int auto_increment,
	`reader_name` char(10) unique not null,
	primary key(`reader_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
作者表
create table n_author(
	`author_id` int auto_increment,
	`author_name` char(10) unique not null,
	primary key (`author_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
评论表
create table n_comment(
	`comment_id` int auto_increment,
	`comment_content` tinytext not null,
	`comment_reader_id` int,
	`comment_book_id` int,
	primary key(`comment_id`),
	foreign key(`comment_reader_id`) references n_reader(`reader_id`),
	foreign key(``comment_book_id) references n_book(`book_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
类别表
create table n_class(
	`class_id` int auto_increment,
	`class_name` char(2) unique not null,
	primary key (`class_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
收藏表
create table n_star(
	`star_id` int auto_increment,
	`star_reader_id` int,
	`star_book_id` int,
	primary key(`star_id`),
	foreign key(`star_reader_id`) references n_reader(`reader_id`),
	foreign key(`star_book_id`) references n_book(`book_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
好的,这里是一个基于 Django 的小说网站数据库代码示例: ```python from django.db import models from django.contrib.auth.models import User class Category(models.Model): name = models.CharField(max_length=50) class Novel(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=50) category = models.ForeignKey(Category, on_delete=models.CASCADE) intro = models.TextField() cover_image = models.ImageField(upload_to='covers/') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Chapter(models.Model): novel = models.ForeignKey(Novel, on_delete=models.CASCADE) title = models.CharField(max_length=100) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class UserNovel(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) novel = models.ForeignKey(Novel, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) class UserChapter(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) chapter = models.ForeignKey(Chapter, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) class Comment(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) novel = models.ForeignKey(Novel, on_delete=models.CASCADE) content = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) ``` 以上代码定义了小说网站的基本数据表,包括:小说分类(Category)、小说(Novel)、章节(Chapter)、用户阅读记录(UserNovel、UserChapter)、评论(Comment)等。其中,Novel 表中包含了封面图片,需要使用 Pillow 库来处理。 当然,具体的数据库设计也可能会因为业务需求而有所不同。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值