系統交付、環境遷移、測試數據重置等場景,常需實現「一鍵式」數據導入。本文提供生產可用的批處理方案,覆蓋用戶創建、權限授予、數據導入全鏈路。
@echo off
sqlplus system/manager @createuser
imp system/manager file=initial.dmp fromuser=initialuser touser=initialuser ignore=y
CREATE USER initialuser IDENTIFIED BY test;
GRANT DBA, RESOURCE, CONNECT TO initialuser;
EXIT;
| 參數 | 含義 |
|---|---|
@echo off | 隱藏命令回顯 |
system/manager | 默認 DBA 帳號(若鎖定/改密需替換) |
@createuser | 執行同目錄下 SQL 腳本 |
file=initial.dmp | 導入文件路徑 |
fromuser/touser | 源/目標 Schema 映射 |
ignore=y | 忽略創建對象錯誤(如已存在) |
DROP USER powercc CASCADE;
-- 對象級授權
GRANT SELECT ON scott.dept TO news;
-- 連帶授權 (WITH GRANT OPTION)
GRANT SELECT ON scott.dept TO news WITH GRANT OPTION;
-- 全量授權
GRANT ALL ON scott.dept TO news;
-- 撤銷權限
REVOKE SELECT ON scott.dept FROM news;
CREATE ROLE myrole;
GRANT UNLIMITED TABLESPACE, DBA TO myrole;
GRANT myrole TO news;
-- 默認用戶表空間
CREATE TABLE tt(t VARCHAR2(10));
-- 指定表空間
CREATE TABLE tt(t VARCHAR2(10)) TABLESPACE othertabs;
EXP scott/tiger FILE=e:\oracle_new_scott\scott.dmpIMP user2/user2@db2 FILE=scott.dmp FROMUSER=scott TOUSER=user2關鍵點:導入時首次輸入 user2@db2(目標用戶@全局DB名),第二次輸入 scott(源 Schema)。