oracle基本命令使用

2021-09-02 00:26:53 字數 4177 閱讀 8436

oracle 資料庫常用操作語句大全

一、oracle資料庫操作

1、建立資料庫

create database databasename
2、刪除資料庫

drop database dbname
3、備份資料庫

完全備份

exp demo/demo@orcl buffer=1024 file=d:\back.dmp full=y

demo:使用者名稱、密碼

buffer: 快取大小

file: 具體的備份檔案位址

full: 是否匯出全部檔案

ignore: 忽略錯誤,如果表已經存在,則也是覆蓋

將資料庫中system使用者與sys使用者的表匯出

exp demo/demo@orcl file=d:\backup\1.dmp owner=(system,sys)

匯出指定的表

exp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)

按過濾條件,匯出

exp demo/demo@orcl file=d:\back.dmp tables=(table1) query=" where filed1 like 『fg%』"

匯出時可以進行壓縮;命令後面 加上 compress=y ;如果需要日誌,後面: log=d:\log.txt
備份遠端伺服器的資料庫

exp 使用者名稱/密碼@遠端的ip:埠/例項 file=存放的位置:\檔名稱.dmp full=y

4、資料庫還原

開啟cmd直接執行如下命令,不用再登陸sqlplus。
完整還原

imp demo/demo@orcl file=d:\back.dmp full=y ignore=y log=d:\implog.txt

指定log很重要,便於分析錯誤進行補救。
匯入指定表

imp demo/demo@orcl file=d:\backup2.dmp tables=(teachers,students)

還原到遠端伺服器

imp 使用者名稱/密碼@遠端的ip:埠/例項 file=存放的位置:\檔名稱.dmp full=y

二、oracle表操作

1、建立表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根據已有的表建立新錶:

a:select * into table_new from table_old (使用舊表建立新錶)

b:create table tab_new as select col1,col2… from tab_old definition only《僅適用於oracle>

2、刪除表

drop table tabname
3、重新命名表

說明:alter table 表名 rename to 新錶名

eg:alter table tablename rename to newtablename

4、增加字段

說明:alter table 表名 add (欄位名 字段型別 預設值 是否為空);

例:alter table tablename add (id int);

eg:alter table tablename add (id varchar2(30) default '空' not null);

5、修改字段

說明:alter table 表名 modify (欄位名 字段型別 預設值 是否為空);

eg:alter table tablename modify (id number(4));

6、重名字段

說明:alter table 表名 rename column 列名 to 新列名 (其中:column是關鍵字)

eg:alter table tablename rename column id to newid;

7、刪除字段

說明:alter table 表名 drop column 欄位名;

eg:alter table tablename drop column id;

8、新增主鍵

alter table tabname add primary key(col)
9、刪除主鍵

alter table tabname drop primary key(col)
10、建立索引

create [unique] index idxname on tabname(col….)
11、刪除索引

drop index idxname

注:索引是不可更改的,想更改必須刪除重新建。

12、建立檢視

create view viewname as select statement
13、刪除檢視

drop view viewname
三、oracle運算元據

1、資料查詢

select 《列名》 from 《表名》 [where 《查詢條件表達試》] [order by 《排序的列名》[asc或desc]]
2、插入資料

insert into 表名 values(所有列的值);

insert into test values(1,'zhangsan',20);

insert into 表名(列) values(對應的值);

insert into test(id,name) values(2,'lisi');

3、更新資料

update 表 set 列=新的值 [where 條件] -->更新滿足條件的記錄

update test set name='zhangsan2' where name='zhangsan'

update 表 set 列=新的值 -->更新所有的資料

update test set age =20;

4、刪除資料

delete from 表名 where 條件 -->刪除滿足條件的記錄

delete from test where id = 1;

delete from test -->刪除所有

commit; -->提交資料

rollback; -->回滾資料

delete方式可以恢復刪除的資料,但是提交了,就沒辦法了 delete刪除的時候,會記錄日誌 -->刪除會很慢很慢

truncate table 表名

刪除所有資料,不會影響表結構,不會記錄日誌,資料不能恢復 -->刪除很快

drop table 表名

刪除所有資料,包括表結構一併刪除,不會記錄日誌,資料不能恢復–>刪除很快

5、資料複製

表資料複製

insert into table1 (select * from table2);

複製表結構

create table table1 select * from table2 where 1>1;

複製表結構和資料

create table table1 select * from table2;

複製指定字段

create table table1 as select id, name from table2 where 1>1;

四、資料庫複製命令

Oracle基本使用和基本命令

1.oracle自動生成的sys使用者和system使用者 sys使用者是超級使用者,具有最高許可權,具有sysdba角色,有create database的許可權。system使用者是管理操作員,許可權也很大。具有sysoper角色,沒有create database的許可權。一般講,對資料庫維護...

oracle基本命令

1 describe查詢表結構 describe xuesheng 名稱 空值 型別 id number 38 xing ming varchar2 25 yu wen number shu xue number 2 select 列名稱 from 表名稱 select from xuesheng ...

oracle基本命令

sqlplus 鏈結oracle資料庫 sqlplus as sysdba 1 檢視當前的資料庫檔案一般都是放在 select name from v datafile 2 建立表空間 create tablespace 表空間名 datafile 對應的檔名 size 大小 3 建立使用者 cre...