1. 程式人生 > >orcl創建表及管理表

orcl創建表及管理表

reat 圖片 進制數 表數據 set varchar 保存 大對象 長度

常用的字段數據類型:

.字符串(varchar2(n)) n表示保存最大長度,基本200作用。
.整數(number(n)) n位的整數,也可用int代替
.小數(number(n,m)) m為小數位,n-m為整數位,有時候用float代替
.日期(date) 存放日期
.大文本(clob) 存儲海量文字(4G)
.大對象(blob) 存放二進制數據,電影,圖片,文字,mp3

---------------------------------------------------------------------------------------------

1、創建表:

create table 表名稱 (字段1 數據類型[default 默認值][primary key],...)

create table bqh3 (name varchar2(20) not null, sfz number(18) ,dz varchar2(30),constraint pk_sfz primary key (sfz));

2、表數據及表的復制:

cerate table bqh (select * from 已知表)----從已知表中只復制數據

create table bqh as selec * from 已知表; -----從已知表中復制數據和表結構

create table bqh as select 字段1,字段2 from 已知表 where 1=2;----從已知表復制指定字段包含數據

create table bqh as select * from 已知表 where 1=2; ----從已知表中復制結構但不包含表數據

3、插入數據:

insert into 表名 [(字段1,字段2)] values (值1,值2)----字段與列一一對應

insert into bqh (name,sfz,dz) values (‘小白‘,130684,‘XX區1幢‘);

insert into 表名 values 所有列的值;--------可以省略字段

insert into bqh values (‘小白‘,130684,‘XX區1幢‘);

4、更新數據:

update 表 set 列=新的值 ----更新所有的數據

update bqh set sfz=130636

update 表 set 列=新的值 [where 條件] -----更新滿足條件的數據

update bqh set sfz=130636 where name=‘小白‘

--------------------------------------------------------------

select * from 表 for update ----手動更新的數據

select * from bqh for update

select * from 表 for update where 條件

select * from bqh for update where sfz=130636 -----手動更新滿足條件的數據

5、刪除數據:

delete from 表名稱 [where 刪除條件...]

delete from 表名 where 條件 -----刪除滿足條件的數據

delete from bqh where sfz=130636;

delete frombqh ----刪除所有數據

commit;

rollback;

查看回收站:show recyclebin

恢復表:flashback table 表名稱 to before drop

例如:flashback table bqh to before drop

恢復表數據:rollback

例如:delete from bqh;

rollback;

註意:

①事物的回滾:ROLLBACK 更新操作回到原點。

②事物的提交:COMMIT 真正的更新,一旦提交無法回滾。

6、修改表結構:

alter table 表名稱 add (列名稱 數據類型 [default 默認值,...]) -----增加表字段

alter table bqh add (jf number(3) default 20)

alter table bqh modify (zf number(2) default 10) ------修改表字段

alter table bqh drop zf --------刪除表字段

7、添加約束:

非空約束:not null不希望某字段為空

create table bqh (xh number,xm varcher2(20) not null)

唯一約束:unique 不希望某字段有重復值

create table bqh (xh number,xm varcher2(20) not null,email varchar2(50) unique)

指定唯一約束錯誤提醒:

create table bqh (xh number,xm varcher2(20) not null,email varchar2(50),constraint uk_email unique(email))

主鍵約束:primary key 非空約束+唯一約束例,如身份證

create table bqh (xm varcher2(20) not null,sfz number,constraint pk_sfz primary key (sfz))

檢查約束:為某數據增加過濾條件,例如:年齡範圍,性別等

create table bqh (xm varcher2(20) not null,xb varcher2(10) not null,age number(3),constraint ck_xb check (xb in (‘男‘,‘女‘)),constraint ck_age check (age between 0 and 150))

外鍵約束:約束在兩張(父子)表中進行,即字表某個字段的取值由父表決定。

例如:每個人有多本書,父表(人),子表(書)

create table xm (bh number,name varchar2(20) not null,constraint pk_bh primary key (bh))

create table book (bookname varchar2(20) not null,bh number,constraint fk_bh foreign key(bh) references xm(bh))

刪除數據時,如果主表中的數據有對應子表數據,應先刪除子表記錄,再刪主表記錄。但操作不方便,即可以建立外鍵約束的時候指定一個級聯刪除功能,當刪除主表數據後,對應的子表中的數據相關聯的數據希望將其設置為空。

create table xm (bh number,name varchar2(20) not null,constraint pk_bh primary key (bh))

create table book (bookname varchar2(20) not null,bh number,constraint fk_bh foreign key(bh) references xm(bh)on delete set null)

8、修改約束:

為表增加約束

alter table 表名 add constraint 約束名稱 約束類型(字段)

刪除表中的約束

alter table 表名 drop constraint 約束名稱

9、刪除表:

drop table 表

drop table xm cascade constraint purge; --------強制性刪除表,不關心約束條件

drop table book cascade constraint purge; --------強制性刪除表,不關心約束條件

感謝您的閱讀,如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕。本文歡迎各位轉載,但是轉載文章之後必須在文章頁面中給出作者和原文連接。

orcl創建表及管理表