Oracle資料庫常用命令

2021-09-25 23:38:03 字數 2771 閱讀 8428

1、主鍵和外來鍵

主鍵:關係型資料庫中的一條記錄中有若干個屬性,若其中的某乙個屬性組(注意是組,可以是乙個,也可以是多個)能唯一標識一條記錄,那麼該屬性組就是主鍵

外來鍵:關係型資料庫表中的一列或者某幾列的組合,它的值與另外一張表的某一列或者某幾列相匹配,且為另一張表的主鍵(即這張表的某一列或某幾列是另外一張表的主鍵,稱這一列或幾列為另外一張表的外來鍵)

注1:一張表主鍵只能有乙個,可以有多個外來鍵以及唯一索引

注2:oracle資料庫共有5個約束:主鍵、外來鍵、非空、唯

一、條件

非空:這個列的值不能為空(not null)

唯一:這個列的值在表中是唯一存在的,不能重複,但可以為空值(null)

條件:可以對列的值設定在某個範圍內,如人的年齡就不能為負數等。

注3:主鍵和唯一約束的區別

表的主鍵是列的值為表中的唯一標識,不能為空值(null),而表的唯一約束是列的值在表中唯一存在,可以為空值(null)

2、表的建立及刪除

無約束建立:

create table items(itemno number(2),itemname varchar2(20));

刪除:drop table items;

有主鍵約束建立:

複製** **如下:

create table items(itemno number(2) constraint pk_items primary key,itemname varchar2(20) not null);

有外來鍵約束建立:

create table business(busino number(2) constraint pk_business primary key,

businame varchar2(32) not null,itemno number(2),constraint fk_business

foreign key(itemno) references items(itemno),starttime date);

注:constraint:定義表中約束所必須的關鍵字

primary key:主鍵約束關鍵字

foreign key…references…:建立表的外來鍵關鍵字

3、to_date:oracle的乙個內部函式,可以把字串變成時間

insert into business(busino,businame,itemno,starttime)

values(4,『supermarket』,2,to_date(『2008-08-08』,『yyyy-mm-dd』));

4、建立有「唯一」和「條件」約束的表

create table computers(

compno number(4) constraint pk_comp primary key,

compmodel varchar2(64) unique,

buytime date,

price number(7,2) constraint ch_price check(price>0 and price<=30000),

owner varchar2(32));

注:unique:為唯一約束關鍵字

constraint…check…:為條件約束的關鍵字

5、建立新錶business_copy,並複製business表的資料

create table business_copy as select * from business;

注:create table:建立表的關鍵字

as select * from business:把business中的資料全部複製到business_copy中(不複製表的約束)

6、把備份表中的資料匯入新錶

insert into business(busino,businame,itemno,starttime) select * from business_copy;

7、常用的字段資料型別

number(p,s):數值型別,其中p最小值為1,最大值為38,s最小值為-84,最大值為124

date:日期型別,用於記錄時間

char(size):定長字串型別,知道規定的長度,可以節省很大空間,比如性別,f表示女,m表示男

varchar(size):可變長字串型別

blob(二進位製大物件型別):用於儲存二進位制物件,比如**、文件資料等

clob(字元大物件型別):用於儲存位元組的大物件資料,比如簡歷之類

bfile(二進位制檔案):儲存大物件,比如電影膠片等

8、修改表結構

增加乙個表字段

alter table items add(manager varchar2(6));

注:alter,這是oracle資料庫中更改資料庫引數、表結構等均會使用到

add:這裡是增加乙個列的關鍵字

修改表的字段最大值

alter table items modify(manager varchar2(8));

刪除表的某一列

alter table items drop column manager;

Oracle資料庫常用命令

export oracle sid db name 伺服器 啟動資料庫伺服器 lsnrctl start sqlplus as sysdba sql startup 資料庫 建立資料庫 oracle home bin dbca 或者 oracle home bin dbassist 連線資料庫 co...

Oracle資料庫常用命令

誤刪正在使用的表空間後處理辦法 1 connect sys as sysdba 用sys登陸資料庫 2 shutdown 解除安裝資料庫,關閉資料庫 3 startup mount 例程啟動 4 alter database open 開啟資料庫 5 alter database datafile ...

oracle資料庫常用命令

登入資料庫例項使用者使用者 2.許可權 表空間刪除 drop tablespace testtablespace1 include contents and datafiles 刪除表空間及表空間檔案 drop tablespace testtablespace1 including content...