共計 2812 個字符,預計需要花費 8 分鐘才能閱讀完成。
自動寫代碼機器人,免費開通
丸趣 TV 小編給大家分享一下 oracle 數據庫的基本操作及語法是什么,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
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 新表名
4、增加字段
alter table 表名 add (字段名 字段類型 默認值 是否為空);
例:alter table tablename add (ID int);
5、修改字段
alter table 表名 modify (字段名 字段類型 默認值 是否為空);
6、重名字段
alter table 表名 rename column 列名 to 新列名(其中:column 是關鍵字)
7、刪除字段
說明:
alter table 表名 drop column 字段名;
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
復制表結構和數據
create table table1 select * from table2;
復制指定字段
create table table1 as select id, name from table2 where 1
四、數據庫復制命令
不同的數據庫語法不同(SQL Server 和 Oracle 為例),且復制包括目標表已存在和目標表不存在的情況,分別回答:
SQL Server 中,如果目標表存在:
insert into 目標表 select*from 原表;
SQL Server 中,,如果目標表不存在:
select*into 目標表 from 原表;(復制表結構和數據)select*from 目標表 from 原表 where1=0(只復制表結構)
Oracle 中,如果目標表存在:
insert into 目標表 select*from 原表;
commit;
Oracle 中,如果目標表不存在:
create table 目標表 as select * from 原表 where 1=0(只復制表結構)create table 目標表 as select * from 原表;復制表結構和數據
看完了這篇文章,相信你對 oracle 數據庫的基本操作及語法是什么有了一定的了解,想了解更多相關知識,歡迎關注丸趣 TV 行業資訊頻道,感謝各位的閱讀!
向 AI 問一下細節
丸趣 TV 網 – 提供最優質的資源集合!