sql語句創建數據庫
/*創建bbsDB數據庫*/use masterif exists(select * from sysdatabases where name='bbsDB')drop database bbsDBcreate database bbsDBon(name='bbsDB_data',filename='D:\project\bbsDB_*',size=10,filegrowth=20%)log on(name='bbsDB_log',filename='D:\project\bbsDB_*',size=3,maxsize=20,filegrowth=10%)/*創建bbsUsers表*/use bbsdbif exists(select * from sysobjects where name='bbsUsers')drop table bbsUserscreate table bbsUsers(UID int identity(1,1) not null,--學號,標識列Uname varchar(15) not null,--用戶昵稱Upassword varchar(10) not null,--用戶密碼Uemail varchar(20),--郵箱地址Usex bit not null,--用戶性別Uclass int,--等級Uremark varchar(20),--備注UregDate datetime not null,--注冊日期Ustate int null, --狀態Upoint int null--用戶積分 )/*創建bbsUsers表中的約束*/alter table bbsUsersadd constraint PK_uid primary key(uid),--主鍵constraint DF_Upassword default(888888) for Upassword,--初始密碼為888888constraint DF_Usex default (1) for Usex,--性別默認為男constraint DF_UregDate default (getdate()) for UregDate,--注冊日期默認為系統日期constraint DF_Ustate default(0) for Ustate,--狀態默認為離線constraint DF_Upoint default(20) for Upoint,--積分默認為20點constraint CK_Uemail check(Uemail like '%@%'),--電子郵件必須含有@符號constraint CK_Upassword check (len(Upassword)>=6)--密碼至少為六位/*創建bbsSection表*/use bbsdbif exists(select * from sysobjects where name='bbsSection')drop table bbsSectioncreate table bbsSection(SID int identity(1,1) not null,--板塊標號,自動增長Sname varchar(32) not null,--版塊名稱SmasterID int not null,--版主用戶IDSprofile varchar(20) null,--版面簡介SclickCount int null, --點擊率StopicCount int null--發帖數)/*創建bbsSection表中的約束*/alter table bbsSectionadd constraint PK_sid primary key(sid),--主鍵constraint DF_SclickCount default(0) for SclickCount,--點擊率默認為0constraint DF_StopicCount default(0) for StopicCount,--發帖數默認為0constraint DF_SmasterID foreign key(SmasterID)references bbsUsers (UID)--外鍵/*創建bbsTopic表*/use bbsdbif exists(select * from sysobjects where name='bbsTopic')drop table bbsTopiccreate table bbsTopic(TID int identity(1,1) not null,--帖子編號,自動增長TsID int not null,--發帖人IDTuID int not null,--版主用戶IDTreplyCount int null,--回復數量Tface int null, --發帖表情Ttopic varchar(20) not null,--標題Tcontents varchar(30) not null,--正文Ttime datetime null,--發帖時間TclickCount int null,--點擊數Tstate int not null,--狀態TlastReply datetime null--回復時間)/*創建bbsTopic表的約束*/alter table bbsTopicadd constraint DF_TreplyCount default(0) for TreplyCount,--回復數量默認為0constraint PK_tid primary key(tid),--主鍵constraint DF_TclickCount default (0) for TclickCount,--點擊數默認為0constraint DF_Tstate default (1) for Tstate,--狀態默認為1constraint DF_Ttime default (getdate()) for Ttime,--發帖時間默認為系統日期constraint CK_Tcontents check (len(Tcontents)>=6),--正文必須大于六個字符constraint CK_TlastReply check ((TlastReply)>(Ttime)),--最后回復時間必須晚于發帖時間constraint DF_TsID foreign key(TsID)references bbsSection (SID),--外鍵constraint DF_TuID foreign key(TuID)references bbsUsers (UID)--外鍵/*創建bbsReply表*/use bbsdbif exists(select * from sysobjects where name='bbsReply')drop table bbsReplycreate table bbsReply(RID int identity(1,1) not null,--自動編號,帖子編號RtID int not null,--主貼IDRsID int not null,--板塊IDRuID int not null,--回帖人IDRface int null, --回帖表情Rcontents varchar(30) not null,--正文Rtime datetime null,--回帖時間RclickCount int null--點擊數)/*創建bbsReply表的約束*/alter table bbsReplyadd constraint DF_Rtime default (getdate()) for Rtime,--回帖時間默認為系統日期constraint CK_Rcontents check (len(Rcontents)>=6),--正文必須大于六個字符constraint DF_RtID foreign key(RtID)references bbsTopic (TID),--外鍵constraint DF_RsID foreign key(RsID)references bbsSection (SID),--外鍵constraint DF_RuID foreign key(RuID)references bbsUsers (UID)--外鍵差不多,你改一下吧!。
SQL語句創建的表在哪
所謂臨時表,即我們臨時創建來用一下的表,也就是說這個表只是臨時創建來用一會兒的,再我們使用完之后就會進行刪除的表。
我們在對數據庫操作的時候,會常常用到一些臨時的數據,這時候使用臨時表來存放這些東西最合適不過了。我們可以使用create語句來創建一個臨時表,具體語法如:create global trmprary table temp_emp(DSN Number,DNAME VARCHAR(20)) On Commit Rows;其中On Commit Rows表示關機之后就刪除這個臨時表,或許你會覺得這樣不好,當然我們還有其他的方式,比如:如果你想在關閉表的時候就刪除的話,我們可以寫上這條語句:On Commit PRESERVE Rows。
用sql語句生成數據庫
oracle中的寫法
WITH t1 AS ( --建立臨時表
SELECT 1 AS L1,'A' AS L2, 'n' AS L3 FROM dual
UNION ALL
SELECT 2 AS L1,'A' AS L2, 'n' AS L3 FROM dual
UNION ALL
SELECT 3 AS L1,'A' AS L2, 'n' AS L3 FROM dual
UNION ALL
SELECT 4 AS L1,'B' AS L2, 'm' AS L3 FROM dual
UNION ALL
SELECT 5 AS L1,'B' AS L2, 'm' AS L3 FROM dual
UNION ALL
SELECT 6 AS L1,'B' AS L2, 'm' AS L3 FROM dual
)
SELECT t.*,
row_number() over(PARTITION BY t.L2 ORDER BY t.L1) row_ --按照規則排序
FROM t1 t;
你在oracle下試試看應該在嵌套一層就可以得出你想要的結果了!