數據庫(學習整理)----3--Oracle創建表和設置約束


  1. BBS論壇表設計
  2. 包含的表:BBSusers(用戶表),BBSsection(版塊表),BBStopic(主貼表),BBSreply(跟帖表)
  3. 表結構

    1)BBSusers

字段名

字段說明

數據類型

字段約束

備注

U_ID

用戶ID

number

主鍵,非空

非空

UName

用戶名

Varchar2(32)

 

非空

UPassword

密碼

Varchar2(16)

默認6個8

 

UEmail

email

Varchar2(32)

默認p@p.com,必須有“@”和“.”,“@”必須在“.”前面

 

UBirthday

生日

Date

 

非空

USex

性別

Char(2)

默認’男’

非空

UClass

用戶等級

Number

值只能是1,2,3中任意一個,默認1

 

UStatement

備注

Varchar2(255)

 

 

URegDate

注冊時間

Date

 

非空

UState

用戶狀態

Number

值只能是1,2,3,4中任意一個

非空

UPoint

積分

Number

不能為負。默認20

 

 

    2)BBSsection

字段名

字段說明

數據類型

字段約束

備注

S_ID

版塊ID

Number

主鍵,非空

非空

SName

版塊名稱

Varchar2(32)

 

非空

SMasterID

用戶ID

Number

外鍵,關聯用戶表主鍵

非空

SStatement

備注

Varchar2(255)

 

 

SClickCount

點擊次數

Number

不能為負

非空

STopicCount

主貼數目

Number

不能為負。默認為0

 

 

    3)BBStopic

 

字段名

字段說明

數據類型

字段約束

備注

TID

主貼ID

Number

主鍵,非空

非空

TNumber

主貼標號

Varchar2(32)

必須以”t”開頭的8個字符

非空

TSID

版塊ID

Number

外鍵,關聯版塊ID

非空

TUID

用戶ID

Number

外鍵,關聯用戶ID

非空

TTopic

主貼標題

Varchar2(255)

 

非空

TContents

主貼正文

Varchar2(2000)

 

非空

TTime

發帖時間

Date

 

非空

TClickCount

點擊數量

Number

不能為負,默認0

 

TFlag

主貼狀態

Number

只能為1,2,3

非空

TLastCLickT

最后點擊時間

Date

 

非空

 

    4)BBSreply

字段名

字段說明

數據類型

字段約束

備注

RID

跟帖表主鍵

Number

主鍵,非空

非空

RNumber

跟貼標號

Varchar2(32)

必須以”r”開頭的8個字符

非空

RTID

主貼ID

Number

外鍵,關聯主貼ID

非空

RSID

版塊ID

Number

外鍵,關聯版塊ID

非空

RUID

用戶ID

Number

外鍵,關聯用戶ID

非空

RTopic

跟帖標題

Varchar2(255)

 

非空

RContents

跟帖正文

Varchar2(2000)

 

非空

RTime

發帖時間

Date

 

非空

RClickCount

點擊數量

Number

不能為負,默認0

 

 

2、Oracle建表和設置約束:

-----------------------------【1】----------------------------------------------
------創建:用戶表
create table BBSusers
(
       U_ID number,           -----用戶ID
       UName Varchar2(32),    -----用戶名
       UPassword Varchar2(16),-----密碼
       UEmail Varchar2(32),   -----email
       UBirthday Date,        -----生日
       USex Char(2),          -----性別
       UClass Number,         -----用戶等級
       UStatement Varchar2(255),---備注
       URegDate Date,         -----注冊時間
       UState Number,         -----用戶狀態
       UPoint Number         -----積分
);
------創建:表約束
----(1)主鍵,非空 
       alter table BBSusers
             add constraint PK_BBSusers_uid primary key(U_ID);
       alter table BBSusers 
             modify(U_ID number not null);   
       
---用戶名 :非空
       alter table BBSusers 
             modify(UName number not null);   
       
---密碼:默認6個8
       alter table BBSusers 
            modify(UPassword  Varchar2(16) default '888888');        
       
---email:默認p@p.com,必須有“@”和“.”,“@”必須在“.”前面  
       alter table BBSusers
             add constraint CK_BBSusers_UEmail check(UEmail like '%@%.%');     
       alter table BBSusers 
            modify(UEmail  Varchar2(32) default 'p@p.com');    
       
---生日:非空          
       alter table BBSusers 
             modify(UBirthday number not null); 
             
---性別:默認’男’  非空
       alter table BBSusers 
             modify(USex Char(2) default '男' not null);                   
             
---用戶等級:值只能是1,2,3中任意一個,默認1
       alter table BBSusers 
             modify(UClass number default 1 check(UClass in(1,2,3)));                  
---注冊時間:非空
       alter table BBSusers 
             modify(URegDate Date  not null);                 
---用戶狀態:值只能是1,2,3,4中任意一個   非空
       alter table BBSusers 
             modify(UState number check(UState in(1,2,3,4)) not null);                                   
---積分:不能為負。默認20             
       alter table BBSusers 
             modify(UPoint number default 20 check(UPoint>0));
             
     
---------------------------------------------------------------------------
-----------------------------【2】----------------------------------------------
-----創建:版塊表
create table BBSsection
(
       S_ID number,           -----版塊ID(主鍵)
       SName Varchar2(32),    -----版塊名稱
       SMasterID Number,      -----用戶ID(外鍵)
       SStatement Varchar2(255),-----備注
       SClickCount Number,     -----點擊次數
       STopicCount Number    -----主貼數目
);

-----設置:約束
   
-----(1):版塊ID      主鍵,非空
       alter table BBSsection
             add constraint PK_BBSsection_sid primary key(S_ID);
       alter table BBSsection 
             modify(S_ID number not null);       

-----版塊名稱:非空
       alter table BBSsection 
              modify(SName number not null);    

-----用戶ID:外鍵,關聯用戶表主鍵       非空
       alter table BBSsection       
             add constraint FK_BBSsection_SMasterID foreign key(SMasterID) references BBSusers(U_ID);
       alter table BBSsection 
             modify(SMasterID number not null);      

-----點擊次數:不能為負              非空
       alter table BBSsection 
              modify(SClickCount check(SClickCount>0) not null);

----主貼數目:不能為負。默認為0
       alter table BBSsection 
              modify(STopicCount default 0 check(STopicCount>0));            
---------------------------------------------------------------------------
-----------------------------【3】---------------------------------------------- 
-----創建:主貼表
create table BBStopic
(
       TID number,           -----主貼ID(主鍵)
       TNumber Varchar2(32), -----主貼標號
       TSID Number,          -----版塊ID(外鍵)
       TUID Number,          -----用戶ID(外鍵)
       TTopic Varchar2(255), -----主貼標題
       TContents Varchar2(2000),-----主貼正文
       TTime Date,           -----發帖時間
       TClickCount Number,   -----點擊數量
       TFlag Number,         -----主貼狀態
       TLastCLickT Date      -----最后點擊時間
);

------設置:約束
-----(1)主貼ID:主鍵,非空
       alter table BBStopic
             add constraint PK_BBStopic_sid primary key(TID);
       alter table BBStopic 
             modify(TID number not null); 

-----主貼標號:必須以”t”開頭的8個字符 非空
       alter table BBStopic
             add constraint CK_BBStopic_TNumber check(TNumber like 't%' and length(TNumber)=8);     
       alter table BBStopic 
             modify(TNumber  not null); 
-----版塊ID:外鍵,關聯版塊ID      非空
      alter table BBStopic
             add constraint FK_BBStopic_TSID foreign key(TSID) references BBSsection(S_ID);
      alter table BBStopic 
             modify(TSID number not null); 

-----用戶ID:外鍵,關聯用戶ID      非空        
      alter table BBStopic
             add constraint FK_BBStopic_TUID foreign key(TUID) references BBSusers(U_ID);
      alter table BBStopic 
             modify(TUID number not null); 
-----主貼標題:非空 
      alter table BBStopic 
             modify(TTopic Varchar2(255) not null); 
-----主貼正文:非空             
      alter table BBStopic 
             modify(TContents Varchar2(2000) not null); 
-----發帖時間:非空 
      alter table BBStopic 
             modify(TTime Date not null);

-----點擊數量:不能為負,默認0
      alter table BBStopic 
             modify(TClickCount number default 0 check(TClickCount>0) );       

------主貼狀態:只能為1,2,3 非空
      alter table BBStopic 
             modify(TFlag number  check(TFlag in(1,2,3)) not null );       

-----最后點擊時間:非空
      alter table BBStopic 
             modify(TLastCLickT Date not null);      

---------------------------------------------------------------------------
-----------------------------【4】---------------------------------------------- 
-----創建:跟帖表
create table BBSreply
(
       RID number,           -----跟帖表主鍵(主鍵)
       RNumber Varchar2(32), -----跟貼標號
       RTID Number,          -----主貼ID(外鍵)
       RSID Number,          -----版塊ID(外鍵)
       RUID Number,          -----用戶ID(外鍵)
       RTopic Varchar2(255), -----跟帖標題
       RContents Varchar2(2000),-----跟帖正文
       RTime Date,           -----發帖時間
       RClickCount Number    -----點擊數量
);

------設置:約束
-----(1)跟帖表主鍵:跟帖表主鍵
       alter table BBSreply
             add constraint PK_BBSreply_RID primary key(RID);
       alter table BBSreply 
             modify(RID number not null); 

-----跟貼標號:必須以”r”開頭的8個字符 非空
       alter table BBSreply
             add constraint CK_BBSreply_RNumber check(RNumber like 'r%' and length(RNumber)=8);     
       alter table BBSreply 
             modify(RNumber  not null); 

-----主貼ID:外鍵,關聯主貼ID          非空
      alter table BBSreply
             add constraint FK_BBSreply_RTID foreign key(RTID) references BBStopic(TID);
      alter table BBSreply 
             modify(RTID number not null); 

-----版塊ID:外鍵,關聯版塊ID          非空
      alter table BBSreply
             add constraint FK_BBSreply_RSID foreign key(RSID) references BBSsection(S_ID);
      alter table BBSreply 
             modify(RSID number not null);
-----用戶ID:外鍵,關聯用戶ID         非空   
      alter table BBSreply
             add constraint FK_BBSreply_RUID foreign key(RUID) references BBSusers(U_ID);
      alter table BBSreply 
             modify(RUID number not null);

-----跟帖標題:非空
      alter table BBSreply 
             modify(RTopic number not null);
-----跟帖正文:非空 
      alter table BBSreply 
             modify(RContents number not null);
-----發帖時間:非空             
      alter table BBSreply 
             modify(RTime number not null);     
-----點擊數量:不能為負,默認0
      alter table BBSreply 
             modify(RClickCount number default 0 check(RClickCount>0) );       

                    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM