比如要创建user,blog,category,comment表

 

 

用户表(user):存放用户名和密码

user:

  id

  username  用户名

  password 密码

 

 

博文表(blog):存放博文标题,内容,博文创建时间

blog:

  id

  title 文章标题

  content 内容

  created_time 创建时间

 

 

评论表(comment):存放评论人名称,评论内容,评论时间

comment:

  id

  com_person 评论人

  com_content 评论内容

  com_time 评论时间

 

 

分类表(category):存放分类名称和等级

category:

  id

  cate_name 分类名称

  level 等级

 

1.创建表的语句:create table user(
   id                  int NOT NULL auto_increment,//不为空,主键自动增加
   username             varchar(200),
   password             varchar(200),
   primary key (id)//定义id为主键
);

 

create table blog
(
   id                 int NOT NULL auto_increment,
   title                varchar(400),
   content              varchar(4000),
   created_time         datetime,
   primary key (id)
);

 

create table category
(
   id                   int NOT NULL auto_increment,
   cate_name                  varchar(200),
   level                int,
   primary key (id)
);

 

create table comment
(
   id                  int NOT NULL  auto_increment,
   com_person             varchar(200),
   com_content              varchar(1000),

   com_time               datetime,
   primary key (id)
);

 

2.考虑到表之间的关系,要对表进行修改:

user-->blog:一个用户可以写多篇文章,所以user对blog为一对多,在blog中应该有个外键,引用指向user表的id字段

故写:先给blog添加一个字段user_id,其中有文章必须知道是谁写的,所以user_id为not null

       alter table blog add user_id int not null;

添加完整性约束:

       alter table blog add constraint FK_blog_user_id foreign key (user_id)
       references user (id) on delete restrict on update restrict;

 

category-->blog:一个分类中可以有多篇博文,所以category对blog为一对多,在blog表中应该有个外键,引用指向category表的id字段

故写:alter table blog add category_id int ;其中该字段可以为null,因为没有分类的时候文章可以为无分类下的

添加完整性约束:

        alter table blog add constraint FK_blog_category_id foreign key (category_id)

        references category (id) on delete restrict on update restrict;

 

blog--->comment:一篇博文可以有多条评论,所以blog对comment是一对多的关系,在comment表中应该有个外键,引用指向blog的id字段

故写:alter table comment add blog_id int not null;//有评论就得有文章,是针对哪篇文章的评论,所以blog_id不为空

添加完整性约束:

        alter table comment add constraint FK_comment_blog_id foreign key (blog_id) references blog (id)

        on delete restrict on update restrict;

 

 

2.往表中插入数据:

insert into user(username,password) values('zhangsan','11111111');

insert into category(cate_name,level) values('新闻故事',5);

insert into blog(title,content,created_time,user_id,category_id) values('今天天气很好','心情自然好了!',now(),1,1);

insert into comment (com_person,com_content,com_time) values('匿名','这文章写得不错',now());

 

3.查询语句

 select * from user;//查询user表中的所有字段内容

 select b.title , b.content c.cate_name,c.level from blog b, category c where b.category_id = c.id;//连接查询

 

4.更新表中内容:

update user set username='张三' where id=1;

 

 

5.删除表中的内容:

delete from user;//删除user表中所有记录

delete from category where level=2;//删除category表中level=2的记录

 

 

6.删除表的语句:

drop table user;

drop table blog;

drop table category;

drop table comment; 

 

 

7.显示数据库中的表:

show tables;

 

8.查看表的结构:

desc blog;

 

Logo

更多推荐