Oracle数据库语法之【DDL】常用汇总-【更新】

Oracle数据库语法之【DDL】常用汇总

1)创建数据库

connect / as sysdba
  startup pfile= 'c:oracleadmininit_testorcl.ora' nomount; 
  create database testorcl 
       datafile '/u02/oracle/testorcl/system01.dbf' size 100m
       logfile group1 ('/u01/oracle/testorcl/redo1a.log',
       '/u02/oracle/testorcl/redo1b.log') size 500k,
       group2 ('/u01/oracle/testorcl/redo1a.log',
       '/u02/oracle/testorcl/redo1b.log') size 500k
  character set zhs16cgb231280;
2)创建数据库永久表空间
create tablespace mytablespace logging 
   datafile d:\oracle\product\10.1.0\oradata\mydb\mytablespace.dbf' size 10m  
reuse 
   autoextend on next 5m maxsize unlimited
   extent management local
3)创建数据库临时表空间
create temporary tablespace mytemp logging
   tempfile 'd:\oracle\product\10.1.0\oradata\mydb\mytemp.dbf' size 10m  
reuse 
   autoextend on next 5m maxsize unlimited
   extent management local
4)创建数据库用户
create user user_name identified by pword default tablespace you_tabspace temporary tablespace TEMP;
5)创建数据库角色
create role roleName;
6)创建数据库表
create table table_name(column1 number constraint pk_name primary key ,column3 date)
7)创建触发器
create or replace trigger trigger_name
    after insert or update on table_name for each row
declare
    t_varible varchar2(50); 
begin
    if inserting then 
        --insert table(:new.colnum)….;
        --t_varible:= new.cloumn;
   end if;
   if updating then 
        --update tbale_name set colnum = new.column where 1=1;
   end if;
end trigger_name;
8)创建包
create package emp_pkg is
    procedure emp_update_ename(v_empno varchar2,v_ename varchar2);
    function emp_get_sal(v_empno varchar2) return number;
end;
9)创建包体
create or replace package body emp_pkg
is
  procedure emp_update_ename (v_empno varchar2,v_ename varchar2)
  is
     vename varchar2(32);
  begin 
     update emp set ename=v_ename where empno=v_empno;
     commit;
     select ename into vename from emp where empno=v_empno;
   
     dbms_output.put_line('userName:'|| vename);
    
  end;
 
  function emp_get_sal (v_empno varchar2)
  return number is
     vsal number(7,2);
  begin
     select sal into vsal from emp where empno=v_empno;
     return vsal;
  end;
end;
10)创建存储过程
create or replace procedure emp_out_sal (v_empno in varchar2, v_sal out number) 
is
  vsal number(7,2);
begin
  select sal into vsal from emp where empno=v_empno;
  v_sal:=vsal;
end;
11)创建函数
create or replace function get_salary(dept_no number, emp_count out number)
   return number 
is
   v_sum number;
begin
   select sum(salary), count(*) into v_sum, emp_count
   from employees 
   where department_id = dept_no;
   return v_sum;
exception
   when no_data_found then 
      dbms_output.put_line('not find data');
   when others then 
      dbms_output.put_line(sqlcode||'---'||sqlerrm);
end get_salary;
12)创建链接库
create public database link DB_Link_Name connect to userName identified by password using 
'(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = OTHER_SNAME)
    )
)';
13)创建游标
declare
     cursor c_list is 
     select * from t_user 
begin 
     for my_cur in c_list loop
     --code
    end loop;
end
14)创建约束
constraint tabname_c1_pufck primay key/unique/foriegn key/check/not null
15)创建视图
create or replace view my_view as select * from table……
16)创建索引
create [bitmap] index index_name on table_name(column[,column_name[,...]]);
17)创建序列
create sequence myseqname start with 1000 increment by 1
18)创建同义词
create synonym you_syname for user.table/view...[@dblink]

声明:以上教程为博主原创,若需转载请注明出处,谢谢。

你可能感兴趣的:(Oracle数据库语法之【DDL】常用汇总-【更新】)