目标;第一步;创建两张表 用户表和文章表
第二步;发表文章
1,建表;
---用户表 BlogUsers
--userID唯一的
--userName
--pwd
--sex
create table BlogUsers(
userID number(10) primary key not null,
userName varchar2(20),
pwd varchar2(20)
)
---文章表 Article
--articleID唯一
--articleTittle
--articleContent
--UpTime 时间
--userID 发表文章的用户ID
create table Article(
articleID number(10) primary key not null,
articleTittle varchar2(40),
articleContent varchar2(4000),
UpTime Date,
userID number(10)
);
2, 建立索引;索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息
--建立索引
根据blogusers中的表创建索引,以便快速的根据username字段快速的查找
create unique index CUS_uniquel on BLOGUSERS (username);
3,联机删除;
cascade : 删除子表中所有的相关记录 set null : 将所有相关记录的外部码字段值设置为 no action: 不做任何操作
4, 设置主外键和联机删除(由于在建表时就已经设置了主键,现在只需要设置外键即可);
文章表的userid就是用户表中的userid只用主外键关联起来,文章表示属于某个用户的,发表文章前需要指定用户的存在
主键;唯一标示
外键;外键中的数据就是主键中的数据
alter table ARTICLE
add constraint CUS_FOREIGN foreign key (USERID)
references BLOGUSERS (USERID) on delete cascade;
5,插入数据;
insert into article values(2,'我在这里','是否会存在这样的事情呢?',to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') ,1182);
此时会提示找不到父级
原因是没有创建用户
创建用户
insert into blogusers values(1181,'王辉','wanghui');
再执行发表文章
insert into article values(1,'我在这里','是否会存在这样的事情呢?',to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') ,1181);
查询插入的数据;
select * from blogusers;
USERID USERNAME PWD
----------- -------------------- --------------------
1181 王辉 wanghui
select * from article;
ARTICLEID ARTICLETITTLE ARTICLECONTENT UPTIME USERID
----------- ---------------------------------------- -------------------------------------------------------------------------------- ----------- -----------
1 我在这里 是否会存在这样的事情呢? 2005/1/1 13 1181
本文详细介绍了如何从零开始搭建一个简单的博客系统,包括创建用户表和文章表、建立索引、设置主外键关联以及实现联机删除等功能。通过具体实例展示了SQL语句的应用。
461

被折叠的 条评论
为什么被折叠?



