id:主键、无意义
owner:说说的主人
sendtime:发布时间
content:说说内容
-- -- 创建说说表
-- create table saysay(
-- id int unsigned auto_increment primary key,
-- owner varchar(20) not null,
-- sendtime datetime,
-- content text
-- );
-- insert into saysay (owner,sendtime,content) values ('张三','2016-03-15 15:05:10','今天天气有点凉');
-- insert into saysay (owner,sendtime,content) values ('王梅','2016-03-15 15:10:10','今天天气真凉');
评论表(comments)
id:主键、无意义(id 为0 代表一级评论)
owner:该评论的发出者
toname: 该评论是回复哪个人的
sendtime:评论的时间
content:评论的内容
ssid:是哪条说说的下面的评论
pid:一级评论的id
-- -- 创建评论表
-- create table comments (
-- id int unsigned auto_increment primary key,
-- owner varchar(20) not null,
-- toname varchar(20) not null,
-- sendtime datetime not null,
-- content text not null,
-- ssid int unsigned not null,
-- pid int,
-- foreign key (ssid) references saysay(id),
-- foreign key (pid) references comments(id),
-- foreign key (toname) references comments(owner)
-- );
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','张三','2016-03-15 15:08:10','就是,听说有雷阵雨',1,0);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('王五','李四','2016-03-15 15:09:30','哎呦,真的吗?没带伞咋办?',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('张三','李四','2016-03-15 15:10:10','好烦,又要下雨',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','王五','2016-03-15 15:16:00','那只能自求多福了',1,1);
-- insert into comments (owner,toname,sendtime,content,ssid,pid) values ('李四','张三','2016-03-15 15:16:30','确实挺烦人的',1,1);
--
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王五','2016-03-15 15:10:00','冻成煞笔了吧!',1,0);
-- insert into comments (owner,sendtime,content,ssid,pid) values ('张三','2016-03-15 15:13:10','你才是煞笔呢',1,16);
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王五','2016-03-15 15:15:30','煞笔说谁呢?',1,16);
--
-- insert into comments (owner,sendtime,content,ssid,pid) values ('王红','2016-03-15 15:17:30','穿厚点',1,0);
java代码逻辑:
1、获取说说内容之后,得到说说的id
2、根据说说的id,获取该说说下的评论(按照发布时间递增的顺序查询),并且输出一级评论
3、在每输出一条一级评论之后,获取该一级评论的id,获取该一级评论下的所有评论即可。
——————————————————————————————————————————————————————
CREATE DATABASE qzone;
#用户表(用户,姓名)
CREATE TABLE quser(
uid INT PRIMARY KEY AUTO_INCREMENT,
uno INT UNIQUE NOT NULL,
uname VARCHAR(10) NOT NULL
);
INSERT INTO quser(uno,uname)
VALUES (1,'桃子'),
(2,'祁玉'),
(3,'欢姐'),
(4,'梦雅'),
(5,'小诺');
SELECT * FROM quser;
#说说表(说说编号,用户,内容,时间)
CREATE TABLE topic(
tid INT PRIMARY KEY AUTO_INCREMENT,
tno INT UNIQUE NOT NULL,
tuno INT NOT NULL,
tcontent VARCHAR(100) NOT NULL,
tdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (tuno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO topic(tno,tuno,tcontent)
VALUES (1,1,'举杯邀明月,对影成三人'),
(2,1,'假如生活欺骗了你,不要悲伤,不要着急'),
(3,2,'月是故乡明'),
(4,3,'今天吃的好饱呀'),
(5,4,'我已经控制不住体内的洪荒之力了!');
SELECT * FROM topic;
#评论表(评论编号,说说编号,用户,内容,时间)
CREATE TABLE discuss(
did INT PRIMARY KEY AUTO_INCREMENT,
dno INT UNIQUE NOT NULL,
dtno INT NOT NULL,
duno INT NOT NULL,
dcontent VARCHAR(100) NOT NULL,
ddate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (dtno) REFERENCES topic (tno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (duno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO discuss(dno,dtno,duno,dcontent)
VALUES(1,2,5,'普希金的诗,我喜欢!'),
(2,4,1,'今天吃啥了呀欢姐'),
(3,3,4,'祁玉又文艺了');
SELECT * FROM discuss;
#回复表replay(回复编号,评论编号,被回复用户,用户,内容,时间)
CREATE TABLE replay(
rid INT PRIMARY KEY AUTO_INCREMENT,
rno INT UNIQUE NOT NULL,
rdno INT NOT NULL,
runum INT NOT NULL,
runo INT NOT NULL,
rcontent VARCHAR(100) NOT NULL,
rdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (rdno) REFERENCES discuss (dno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (runum) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (runo) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO replay(rno,rdno,runum,runo,rcontent)
VALUES (1,1,5,1,'小诺你知道的太多了,你是alphago的亲戚吧'),
(2,1,1,5,'对呀桃子,我亲戚老厉害了'),
(3,1,5,1,'给你点颜色就开染坊。。。'),
(4,2,1,3,'豆浆油条'),
(5,3,4,2,'总要有诗和远方');
SELECT * FROM replay;
#点赞表admire(点赞编号,说说编号,用户编号,时间)
CREATE TABLE admire(
aid INT PRIMARY KEY AUTO_INCREMENT,
ano INT UNIQUE NOT NULL,
atno INT NOT NULL,
auno INT NOT NULL,
adate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (atno) REFERENCES topic (tno) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (auno) REFERENCES quser (uno) ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO admire(ano,atno,auno)
VALUES(1,1,1),
(2,1,2),
(3,1,3),
(4,3,2),
(5,4,3);
SELECT * FROM admire;
#查询用户说说
SELECT uname '用户',tcontent '说说',tdate '时间'
FROM topic,quser
WHERE quser.`uno`=topic.`tuno`
#创建说说视图
CREATE VIEW shuo
AS
SELECT uno,uname,tno,tcontent,tdate
FROM topic,quser
WHERE quser.`uno`=topic.`tuno` ;
SELECT * FROM shuo;
#查询评论
SELECT uname,tcontent
FROM discuss,topic
WHERE quser.`uno`=discuss.`dtno` AND