oracle的一些SQL命令

2022-04-01 00:18:01 字數 4708 閱讀 5263

第四章

1 邏輯備份:

(1)必備引數:

exp system/abc123 file=d:/b.dmp;

該命令並未指定登陸到哪個資料庫例項,因此,將使用系統環境變數

oracle_sid

所指定的資料庫例項。(2)

owner

引數:exp system/abc123 owner=(test,oracle) file=d:/b.dmp;//

指定僅僅匯出

test

和oracle

兩個使用者所擁有的物件。(3)

tables

引數:exp system/abc123 tables=(people,employees) file=d:/b.dmp;//tables

用於指定匯出哪些資料表。

2 對於邏輯備份,相應的恢復命令為

imp:

imp system/abc123 files=d:/b.dmp;

例項:exp system/abc123@orcl owner=(scott) file=d:/scott.dmp; //system/abc123@orcl指定了欲進行備份的資料庫的本地

net服務名及登陸使用者名稱,密碼;

owner=(scott)

指定了備份使用者

scott

的所有物件;

file=d:/scott.dmp

指定了備份的目標檔案。

第五章1 建立名為

user1

的表空間:

create tablespace user1 datafile 'f:\database\user1_data.dbf' size 20m;

2 指定資料檔案的大小可以自動擴充套件:

create tablespace user2 datafile:'f:\database\user2_data.dbf' size 20m autoextend on;

3 指定資料檔案的增長幅度:

create tablespace user3 datafile 'f:\database\user3_dbf' size 20m autoextend on next 5m;

4 指定資料檔案的最大尺寸:

create tablespace user4 datafile 'f:\database\user4_dbf' size 20m autoextend on next 5m maxsize 500m;

5 檢視表空間是否建立成功:

select file_name,tablespace_name from dba_data_files order by file_name;//檢視

dba_data_files

儲存了資料庫中所有表空間的資料檔案資訊。其中

tablespace_name

列儲存了表空間的名稱;

file_name

則儲存了對應表空間的資料檔案的完整路徑。

6 為乙個表空間建立多個資料檔案:

create tablespace multiple_data_file datafile 'f:\database\data_1.dbf' size 20m, 'f:\database\data_2.dbf' size 20m;

7 檢視所有表空間資訊: 

select tablespace_name,status,allocation_type from dba_tablespaces;

8 查詢每個使用者的預設表空間:

select user_id,username,default_tablespace from dba_users;

9 修改資料庫的預設表空間:

alter database default tablespace user1;//alter database 

是修改資料庫屬性命令,

default tablespace user1

則指定了新的表空間

user1.

該語句執行後,資料庫的預設表空間被設定為

user1

。10 修改表空間名稱:

alter tablespace user2 rename to user20;

11 刪除表空間:

drop tablespace user20;

12 刪除表空間及其資料檔案: 

drop tablespace user20 including contents and datafiles;

12 建立資料庫表:

create table t_user (user_id number not null,user_name varchar2(20) not null, user_email varchar2(30));

13 檢視表結構: 

select table_name,tablespace_name from user_tables where table_name='t_user';

14 修改表結構:

alter tablet t_user rename column user_email to email;

15 修改列屬性: 

alter table t_user modify (user_name varchar2(15));

16 為表新增一列: 

alter table t_user add (remark varchar2(50));

17 刪除表中的一列: 

alter table t_user drop column remark;

18 修改表名: 

alter table t_user rename to t_users;

19 刪除資料表:

drop table t_drop;

第六章:

1 所有使用者建立的主鍵的基本資訊都儲存在表

user_constraint

表中。檢視主鍵約束:select table_name,constraint_name,constraint_type,status from user_constraints where table_name='student';  //

檢視表student

的約束;

2 顯示命名主鍵約束:

create table student (student_id number constraint pk_student primary key, student_name varchar2(20),student_birthday date, student_address varchar2(50), student_phone varchar2(20));

3 建立主鍵的另一種寫法:

create table student (student_id number, student_name varchar2(20),student_birthday date, student_address varchar2(50), student_phone varchar2(20), constraint pk_student primary key(student_id));

4 為表新增主鍵:

alter table student modify(student_id number primary key);

5 為表新增多列主鍵:

alter table student add constraint pk_student primary key(student_name.student_birthday,student_address);

6 刪除主鍵:

alter table student drop primary key;

7 禁用表

student

的主鍵約束:

alter table student disable primary kry;

啟用表student

的主鍵約束:

alter table student enable primary key;

8 修改主鍵的名稱:

alter table student rename constraint sys_c005145 to pk_student;//rename constraint

用於修改約束的名稱;

9 建立表

orders

到表customers

的外來鍵關聯:

alter table orders add constraint fk_orders_customers foreign key (customer_id ) references customers(customer_id);

10 為已存在的表新增唯一性約束:

alter table users add constraint uq_phone unique (customer_phone);//uq_phone 

是該唯一性約束的名稱

11 刪除唯一性約束:

alter table users drop constraint uq_phone;

12 新增檢查約束:

alter table employees add constraint chk_name check(length(employee_name)<=4);

13 新增預設值約束:

alter table sales modify quantity number default 1;

oracle常用的一些sql命令

檢視系統當前時間 hh24 24小時制 mi是正確的分鐘 select to char sysdate,yyyy mm dd hh24 mi ss from dual hh非24 mm不區分大小寫 日期中mm系統認為是月份 select to char sysdate,yyyy mm dd hh m...

oracle常用的一些sql命令

檢視系統當前時間 hh24 24小時制 mi是正確的分鐘 select to char sysdate,yyyy mm dd hh24 mi ss from dual hh非24 mm不區分大小寫 日期中mm系統認為是月份 select to char sysdate,yyyy mm dd hh m...

oracle的一些命令

create tablespace datafile data 00.dbf size 500m create user identified by passwd default tablespace tbs temporary tablespace temp create table id int...