溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

oracle數(shù)據(jù)庫(kù)的基本操作及語(yǔ)法是什么

發(fā)布時(shí)間:2020-10-26 13:44:17 來(lái)源:億速云 閱讀:350 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

小編給大家分享一下oracle數(shù)據(jù)庫(kù)的基本操作及語(yǔ)法是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

oracle數(shù)據(jù)庫(kù)基本語(yǔ)句

一、Oracle數(shù)據(jù)庫(kù)操作

1、創(chuàng)建數(shù)據(jù)庫(kù)

create database databasename

2、刪除數(shù)據(jù)庫(kù)

drop database dbname

3、備份數(shù)據(jù)庫(kù)

完全備份

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

demo:用戶(hù)名、密碼

buffer: 緩存大小

file: 具體的備份文件地址

full: 是否導(dǎo)出全部文件

ignore: 忽略錯(cuò)誤,如果表已經(jīng)存在,則也是覆蓋

將數(shù)據(jù)庫(kù)中system用戶(hù)與sys用戶(hù)的表導(dǎo)出

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

導(dǎo)出指定的表

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

按過(guò)濾條件,導(dǎo)出

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

導(dǎo)出時(shí)可以進(jìn)行壓縮;命令后面 加上 compress=y ;如果需要日志,后面: log=d:\log.txt

備份遠(yuǎn)程服務(wù)器的數(shù)據(jù)庫(kù)

exp 用戶(hù)名/密碼@遠(yuǎn)程的IP:端口/實(shí)例 file=存放的位置:\文件名稱(chēng).dmp full=y

4、數(shù)據(jù)庫(kù)還原

打開(kāi)cmd直接執(zhí)行如下命令,不用再登陸sqlplus。

完整還原

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

指定log很重要,便于分析錯(cuò)誤進(jìn)行補(bǔ)救。

導(dǎo)入指定表

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

還原到遠(yuǎn)程服務(wù)器

imp 用戶(hù)名/密碼@遠(yuǎn)程的IP:端口/實(shí)例 file=存放的位置:\文件名稱(chēng).dmp full=y

二、Oracle表操作

1、創(chuàng)建表

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

根據(jù)已有的表創(chuàng)建新表:

A:select * into table_new from table_old (使用舊表創(chuàng)建新表)
B:create table tab_new as select col1,col2… from tab_old definition only<僅適用于Oracle>

2、刪除表

drop table tabname

3、重命名表

alter table 表名 rename to 新表名

4、增加字段

alter table 表名 add (字段名 字段類(lèi)型 默認(rèn)值 是否為空);
例:alter table tablename add (ID int);

5、修改字段

alter table 表名 modify (字段名 字段類(lèi)型 默認(rèn)值 是否為空);

6、重名字段

alter table 表名 rename column 列名 to 新列名 (其中:column是關(guān)鍵字)

7、刪除字段

說(shuō)明:

alter table 表名 drop column 字段名;

8、添加主鍵

alter table tabname add primary key(col)

9、刪除主鍵

alter table tabname drop primary key(col)

10、創(chuàng)建索引

create [unique] index idxname on tabname(col….)

11、刪除索引

drop index idxname

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

12、創(chuàng)建視圖

create view viewname as select statement

13、刪除視圖

drop view viewname

三、Oracle操作數(shù)據(jù)

1、數(shù)據(jù)查詢(xún)

select <列名> from <表名> [where <查詢(xún)條件表達(dá)試>] [order by <排序的列名>[asc或desc]]

2、插入數(shù)據(jù)

insert into 表名 values(所有列的值);
insert into test values(1,'zhangsan',20);
insert into 表名(列) values(對(duì)應(yīng)的值);
insert into test(id,name) values(2,'lisi');

3、更新數(shù)據(jù)

update 表 set 列=新的值 [where 條件] -->更新滿(mǎn)足條件的記錄
update test set name='zhangsan2' where name='zhangsan'
update 表 set 列=新的值 -->更新所有的數(shù)據(jù)
update test set age =20;

4、刪除數(shù)據(jù)

delete from 表名 where 條件 -->刪除滿(mǎn)足條件的記錄
delete from test where id = 1;
delete from test -->刪除所有
commit; -->提交數(shù)據(jù)
rollback; -->回滾數(shù)據(jù)
delete方式可以恢復(fù)刪除的數(shù)據(jù),但是提交了,就沒(méi)辦法了 delete刪除的時(shí)候,會(huì)記錄日志 -->刪除會(huì)很慢很慢
truncate table 表名
刪除所有數(shù)據(jù),不會(huì)影響表結(jié)構(gòu),不會(huì)記錄日志,數(shù)據(jù)不能恢復(fù) -->刪除很快
drop table 表名
刪除所有數(shù)據(jù),包括表結(jié)構(gòu)一并刪除,不會(huì)記錄日志,數(shù)據(jù)不能恢復(fù)-->刪除很快

5、數(shù)據(jù)復(fù)制

表數(shù)據(jù)復(fù)制

insert into table1 (select * from table2);

復(fù)制表結(jié)構(gòu)

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

復(fù)制表結(jié)構(gòu)和數(shù)據(jù)

create table table1 select * from table2;

復(fù)制指定字段

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

四、數(shù)據(jù)庫(kù)復(fù)制命令

不同的數(shù)據(jù)庫(kù)語(yǔ)法不同(SQL Server和Oracle為例),且復(fù)制包括目標(biāo)表已存在和目標(biāo)表不存在的情況,分別回答:

SQL Server中,如果目標(biāo)表存在:

insert into 目標(biāo)表select*from原表;

SQL Server中,,如果目標(biāo)表不存在:

select*into 目標(biāo)表from原表;(復(fù)制表結(jié)構(gòu)和數(shù)據(jù))
select*from 目標(biāo)表from原表where1=0(只復(fù)制表結(jié)構(gòu))

Oracle中,如果目標(biāo)表存在:

insert into 目標(biāo)表select*from原表;
commit;

Oracle中,如果目標(biāo)表不存在:

create table 目標(biāo)表 as select * from 原表 where 1=0(只復(fù)制表結(jié)構(gòu))
create table 目標(biāo)表 as select * from 原表;復(fù)制表結(jié)構(gòu)和數(shù)據(jù)

看完了這篇文章,相信你對(duì)oracle數(shù)據(jù)庫(kù)的基本操作及語(yǔ)法是什么有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI