SQL代码
-- 创建数据库并设置编码
create database blog default character set utf8 collate utf8_general_ci;
-- 创建博主信息表
create table user(
uid int(1) primary key,
uname varchar(255) not null,
upass varchar(255) not null,
unname varchar(255) not null,
utime date,
uimg varchar(255),
ubname varchar(255)
);
-- 创建分类表
create table categorie(
cid int(10) primary key auto_increment,
cname varchar(255) not null
);
-- 创建文章表
create table article(
aid int(10) primary key auto_increment,
atitle varchar(255) not null,
cid int(10),
atime date not null,
aatime date not null,
anum int not null,
aclick int default 0,
acontent text not null,
aflag int default 1,
foreign key (cid) references categorie(cid)
);
-- 创建标签表
create table tag(
tid int(10) primary key auto_increment,
tname varchar(255) not null,
aid int(10),
foreign key (aid) references article(aid)
);
-- 创建留言表
create table message(
mid int(10) primary key auto_increment,
mname varchar(255) not null,
mcontact varchar(255) not null,
mflag int default 1
);
insert into user values(1,'fan','123','F先森','2019-06-27','1','LIVE_下一秒');
insert into categorie values(1,'随笔');
insert into article(atitle,cid,atime,aatime,anum,acontent)
values('我的第一篇文章',1,'2019-06-27','2019-06-27',300,'这是测试内容');
-- 获取首页的文章信息
select aid,atitle,article.cid,cname,atime,aatime,anum,aclick,acontent
from article,categorie
where article.cid = categorie.cid
-- 获取博主的信息
select unname,TIMESTAMPDIFF(day,utime,CURRENT_DATE) utime,uimg,ubname from user
-- 查询密码
select uname,upass from user
-- 获取文章总数
select count(1) num from article
-- 获取分类总数
select count(1) num from categorie
-- 获取标签总数
select count(1) num from tag
-- 获取文字总数
select sum(anum) num from article
-- 查询最热的10篇文章
select aid,atitle from article order by aclick desc limit 0,10
-- 获取所有分类
select * from categorie