数据库基础入门(二)——具体项目实现

宠物社交网站数据库设计

 项目简介:用户通过给自己的宠物申请账号,从而在网路上以宠物为第一人称与其他宠物进行社交(类似微博)。

    数据库基本架构:

 

  用户(ID,用户名,密码,邮箱,电话,地址,身份证号,性别,姓名,生日)

 

  宠物(ID,名字,性别,生日,智商,简介,用户ID品种ID

 

  科目(ID,科目名)

 

  品种(ID,品种名,科目ID

 

  动态(ID用户ID,内容,时间,赞数)

 

  评论(ID用户ID,内容,时间,动态ID

 

  回复(ID用户ID,内容,时间,评论ID

 

  管理员(ID,用户名,密码)

用户表
create table user(
    -> id int not null auto_increment,
    -> usr_name varchar(20) not null,
    ->  password varchar(20) not null,
    ->  email varchar(20) not null,
    ->  tel varchar(11),
    ->  address varchar(50),
    ->  sex boolean,
    ->  true_name varchar(20),
    ->  birthday Date,
    ->  primary key(id));
宠物表

create table pet(
    -> id int not null auto_increment,
    -> name varchar(20) not null,
    -> sex boolean,
    -> birthday Date,
    -> IQ int,
    -> intro varchar(200),
    -> user_id int not null,
    -> kind_id int not null,
-> primary key(id));
 //设置外键
alter table pet add constraint fk_pet_user foreign key(user_id) references user(id) on delete cascade on update cascade;
alter table pet add constraint fk_pet_kind foreign key(kind _id) references kind (id) on delete cascade on update cascade;



 

科目表



  

 

mysql> create table family(
 
    -> id int not null primary key auto_increment,
 
    -> family varchar(20));

 


 

品种表



 

 

mysql> create table kind(
 
    -> id int not null primary key auto_increment,
 
    -> kind varchar(20) not null,
 
-> family_id int not null);
//设置外键
 
alter table kind add constraint fk_breed_family foreign key(family_id) references family(id) on  delete cascade on update cascade;

 



 

动态表



 

mysql> create table dynamic(
    -> id int not null primary key auto_increment,
    -> user_id int not null,
    -> content varchar(280) not null,
    -> time time not null,
-> praise int);
//设置外键
alter table dongtai add constraint fk_dynamic_user foreign key(user_id)
 
references user(id) on delete cascade on update cascade;


评论表


 

mysql> create table comment(
    -> id int not null primary key auto_increment,
    -> user_id int not null,
    -> content varchar(280) not null,
    -> time time not null,
-> from_id int not null);
//设置外键
alter table comment add constraint fk_comment_user foreign key(user_id) references user(id) on delete cascade on update cascade;
alter table comment add constraint fk_comment_dongtai foreign key(from_id) references dongtai(id) on delete cascade on update cascade;

管理员表

 

mysql> create table admin(
    -> id int not null primary key auto_increment,
    -> name varchar(20) not null,
    -> password varchar(20) not null);

 

回复表


 
create table reply_comment(
     id int not null auto_increment,
     user_id int not null,
     time Time not null,
     owner_id int not null,
  primary key(id));
//设置外键
  alter table reply_comment add constraint fk_reply_user foreign key(user_id) references user(id) on delete cascade on update cascade;
  alter table reply_comment add constraint fk_reply_comment foreign key(owner_id) references comment(id) on delete cascade on update cascade;



 

猜你喜欢

转载自815222418.iteye.com/blog/2244395