Oracle基本語句執行個體代碼介紹

來源:互聯網
上載者:User

Oracle基本語句

 

1 進入介面

 

在cmd裡面進入oracle的sqlplus介面:sqlplus scott/orcl@orcl

 

2 串連管理

 

串連命令 conn[ect] sys/orcl@orcl as sysdba

 

中斷連線 disc[onnect]

 

修改密碼 psssw[ord]

 

顯示使用者 show user

 

退出介面 exit

 

 

3 執行編輯sql語句

 

執行sql語句 start D:1.sql 或者 @ D:1.sql

 

編輯sql語句 edit D:1.sql

 

截取螢幕上的內容 spool D:1.sql(開始截取) spool off(停止截取)

 

4 使用者管理

 

建立使用者 create user ssy identified by ssy

 

修改密碼 alter user ssy identified by orcl

 

刪除使用者 drop user ssy(cascade) cascade代表刪除這個使用者對應的所有對象

 

賦予許可權 grant create session to ssy

 

         grant all on emp to ssy

 

許可權傳遞 grant all on emp to ssy with grant option(對象許可權) 根表有關的許可權

 

         grant create session to ssy with admin option(系統許可權)其他的許可權

 

收回許可權 revoke all on emp to ssy(株連制度)

 

 

5 使用者口令管理profile

 

6 表操作

 

建立表 create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null)

 

修改表 添加一個欄位 alter table student add(Address nvarchar2(100) [default value][null/not null]);

 

       修改一個欄位的長度 alter table student modify(Name nvarchar2(10),Address nvarchar2(10));

 

       修改一個欄位的類型 alter table student modify(Name varchar2(10));

 

       修改一個欄位的名稱 alter table student rename column Name to Name2;

 

       刪除一個欄位 alter table student drop column Salary;

 

修改表的名字 rename student to stu;

 

刪除表 drop table student;

 

       delete from student;刪除所有記錄,表結構還在,寫日誌,可以恢複的,速度慢,Delete 的資料可以恢複。

 

查看錶欄位結構 desc student;

 

 

7 增刪改查

 

增 insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'史守陽','男','21-8月-12',2000);

 

   修改日期的預設格式(臨時修改,資料庫重啟後仍為預設;如要修改需要修改註冊表)

 

   alter session set nls_date_format='yyyy-mm-dd';

 

   insert into student(SNo,Name,Sex,Birthday,Salary) values(1002,'史守陽','男','2012-08-21',2000);

 

   插入部分欄位和空值

 

   快速加大表中資料 insert into student(SNo,Name,Sex,Birthday,Salary) select * from student;

 

改 update student set Name='陳慧琳',Sex='女' where SNo=1002;

 

刪 savepoint a;

 

   delete from student where SNo=1003;

 

   rollback to a;

 

總結:刪除表的三種方式 delete from student;     刪除所有記錄,表結構還在,寫日誌,可以恢複的,速度慢。

 

                       truncate table student;  刪除表中的所有記錄,表結構還在,不寫日誌,無法找回刪除的記錄,速度快。

 

                       drop table student;      刪除表的結構和資料。

 

查(查詢相對較複雜,完善中。。。)

 

開啟計時 set timing on;

 

取消重複行 select distinct * from emp;

 

空值計算   select  sal*13+nvl(comm,0)*13 as 年薪 from emp;

 

子查詢(巢狀查詢)

 

單行子查詢

 

多行子查詢

 

多列子查詢

 

分頁查詢

 

合并查詢

 

 

8 資料備份和恢複

 

備份(多表多檔案加上大括弧)

 

匯出整個資料庫 exp userid=system/orcl@orcl file=d:all.dmp full=y log=d:all.log

 

匯出自己的方案 exp userid=scott/orcl@orcl owner=scott file=d:scott.dmp log=d:scott.log

 

匯出其它方案   exp userid=system/orcl@orcl owner=scott file=d:scott2.dmp log=d:scott2.log

 

匯出自己的表   exp userid=scott/orcl@orcl tables=emp file=d:emp.dmp log=d:emp.log

 

匯出其它方案的表 exp userid=system/orcl@orcl tables=scott.emp file=d:emp.dmp log=d:emp.log

 

匯出表的結構   exp userid=scott/orcl@orcl tables=emp file=d:emp.dmp rows=n log=d:emp.log

 

使用直接匯出方式 exp userid=scott/orcl@orcl tables=emp file=d:emp.dmp direct=y log=d:emp.log     

 

這種方式比預設的常規方式速度要快,當資料量大時,可以考慮使用這樣的方法。

 

 

這時需要資料庫的字元集要與用戶端字元集完全一致,否則會報錯

 

恢複(多表多檔案加上大括弧)

 

匯入整個資料庫 imp userid=system/orcl@orcl file=d:all.dmp full=y log=d:allimp.log ignore=y

 

匯入自己的方案 imp userid=scott/orcl@orcl file=d:emp.dmp log=d:empimp.log

 

匯入其它方案   imp userid=system/orcl@orcl file=d:emp.dmp fromuser=system touser=scott log=d:empimp.log

 

匯入自己的表   imp userid=scott/orcl@orcl tables=emp file=d:emp.dmp

 

匯入表到其它使用者 imp userid=system/orcl@orcl tables=emp file=d:emp.dmp fromuser=system touser=scott log=d:empimp.log

 

匯入表的結構   imp userid=scott/orcl@orcl tables=emp file=d:emp.dmp rows=n log=d:empimp.log

 

匯入資料 如果對象(如比表)已經存在可以只匯入表的資料

 

imp userid=scott/orcl@orcl tables=emp file=d:emp.dmp ignore=y log=d:empimp.log

 

注意formuser是表本來屬於哪個使用者 touser現在傳遞給哪個使用者

 

 

9 資料表空間

 

建立資料表空間 create tablespace ssy datafile 'C:oracleproduct10.2.0oradatassy.dbf'

 

size 50m autoextend on next 50m maxsize unlimited extent management local;

 

create user ssy identified by ssy default tablespace ssy;

 

create table student(SNo number(4),Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null) tablespace ssy;

 

知道資料表空間名,顯示該資料表空間包括的所有表

 

select * from all_tables where tablespace_name='ssy';

 

知道表名,查看該表屬於那個資料表空間

 

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

 

刪除資料表空間 drop tablespace ssy including contents and datafiles cascade constraints;

 

 

10 約束

 

not null unique primary key foreign key check

 

alter table class add constraint class_key primary key (classid);

 

11 主鍵

 

自動成長

 

先建立一個表

 

create table student(SNo number(4) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null)

 

自訂一個sequence

 

create sequence student_sequence increment by 1 start with 1 nomaxvalue nocycle nocache;

 

建立一個觸發器

 

create trigger student_trigger before insert on student for each row when(new.SNo is null) begin select student_sequence.nextval into:new.SNo from dual;end;/

 

最近插入一行資料

 

insert into student(Name,Sex,Birthday,Salary) values('史守陽','男','21-8月-12',2000);

 

GUID

 

先建立一個表

 

create table student2(SNo char(32) primary key,Name nvarchar2(50),Sex char(2),Birthday date,Salary number(7,2) default 1000 not null)

 

然後插入一行資料

 

insert into student2(SNo,Name,Sex,Birthday,Salary) values(sys_guid(),'史守陽','男','21-8月-12',2000);

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.