Oracle programming basics (DDL (data definition language), DML (data manipulation language), ORACLE functions)

Three aspects of Oracle programming: DDL (data definition language), DML (data manipulation language), ORACLE functions

DDL (data definition language)

1. Enter from the command line

cmd
sqlplus/nolog opens the workspace
conn username/password@service name as sysdba (he is an identity);
show user show user
shutdown immediate (forced database shutdown) closes the database. Note: When this command is used, the listener is forcibly closed.
startup force starts the database

2. Create table space      

Note (the file suffix of the table space is dbf and the specified directory must be the directory where the database is installed, that is:
create tablespace tbs_tablespacename datafile 'filename' size 100m autoextend on next 10mb
2.1 Adjust the size of the table space
alter database datafile 'filename' resize new size
   Change the read and write status of table space t
alter tablespace tbs_tablespacename read only/wtite; read-only/read-write
   Drop tablespace
dorp tablespace tbs_tablespacename;
   Delete data in table space
dorp tablespace tbs_tablespacename including contents;

3.Create user

Create a user and specify the size of the table space to use
create user test identified by 123
default tablespace tbs_school
temporary tablespace temp
quote 5m on tbs_school; (Note, specify the size of the table space used by the table)
 4. Modify user  
alter user userName attribute attribute value
alter user userName default tablespace tbs_

4. Delete user

drop user userName;
5. User authorization
grant DBA to userName; Three system predefined roles: connect resource dba 
6. Cancel authorization
revoke DBA from userName;

 5. Create table

create table tableName(
 userid    number(20),
 username  varchar(10);
 telephone number(11)
)

6. Modify table

  Change field
alter table tablename modify(name varchar2(25),...)
  Add field
alter table tablename add (name varchar2(10));
  Delete field
alter table tbl_school drop column (fieldName);
  Delete table
drop table tableName;
  View table structure
desc tableName; (Note: only used in the command line window)

7. Primary key constraints  

Generally used to constrain IDs
alter table student add constraint pk_id primary key(id);
Unique constraints are generally used to constrain names.
alter table student(table name) add constraint u_id(constraint name) unique(name field name);
Check constraints Constrain the domain of a field
alter table student add constraint chk_age check(age>10);

8. Referential integrity constraints

alter table child table 
dd constraint constraint name foreign key (foreign key of child table)
references main table (primary key of main table);
alter table student information table
add constraint fk_subtable name_foreing key (stu_cla_id)
references class information table (cla_id);
Delete constraints
alter table student(table name) drop constraint pk_id(constraint name);
View constraints
select * from user_constraints where table_name='Table name capitalized';
select constraint_name from user_constraints where table_name='表名大写';

9.User Unlock

Unlock ordinary users in Oracle with the province of the database administrator:
alter user scott  account unlock;

DML (Data Manipulation Language)

1. Insert

insert into school(id,name,studentCount,teacherCount,creatDate) values('00001','Beida Jade Bird Peking University Public School' 200,20,to_date('2006-hour 12-12','yyyy-mm-dd') );
    Adding table contents sequentially does not require the names of fields
insert into school('00002','Beida Jade Bird Peking University Public School' 200,20,to_date('2006-hour 12-12','yyyy-mm-dd'));//sysdate

2. Query

select * from es_user where usertype = 2 and  ( realname = '李思' or realname = '张三' ) ;//Query the member information of Zhang San and Li Si
select * from es_user where usertype is null; //Query whether the field name is a null character 
  // select * from es_user where realname like' 李%';
  // select * from es_user where realname like' 李_';
    % can match 0 to more characters
   - Can match any single character
  // select * from es_user where realname  in ('Zhang San','Zhang Xiaohong')
select * from school;

2.1 Query some fields

select name, createDate from tbl_school;
     Query the number of students between 100 and 200
select * from tbl_school where studentCount between 100 and 200;
select * from tbl_school where studentCount>100 and studentCount<200;

2.2 Sort in ascending order

select * from tbl_school order by createDate asc(optional)/desc;
     // asc sort in ascending order
     // desc sort in descending order
     //  distinct Check a group of unique unordered objects
select distinct * from es_user 

2.3 Alias ​​query

select u.realname as "user's real name" ,u.username  as  "user's real name" from es_user u where u.usertype = 2;

3. Copy table 

insert into tbl_school_pk   select t.*,sysdate from tbl_school t ; copy the table within the current system time
insert into tbl_school select id,name,studentCount ,teacherCount from tbl_school_pk;

4. Modify table

update tbl_school set studentCount = 256 ,teacherCount = 30 where id='00001';
update  (select d.qj ,r.creater from uf_dianzhanghr d left join requestbase r on d.requestid= r.id) set  qj =creater

5. Submission form

commit;

6. Delete table

delete from tbl_school where id = '0002';    commit  提交
delete from tbl_school delete all tables
truncate table tbl_school completely clear rollback rollback

7. Back up the table

insert into user_bak select * from es_user where ueertype = 2;
DCL (Database Control Language) grant rovoke commit rollback  

8. User role permissions

8.1 Permissions

Object: dba_sys_privs All System Privileges
Object: user_sys_privs System permissions owned by the user  
Query the system permissions owned by the current user: select * from user_sys_privs;

8.2 Grant system permissions

grant system permissions to user

8.3 Common system permissions

create session connection system permissions
create table create table
create view create view
create procedure create procedure
create sequence create sequence
Grant the object permissions grant select on table1 to user; Grant the object permissions of the query table 1 to the user user
Revoke object revoke select on table1 from user; revoke user's query permission on table one
Object permissions and data dictionary select * from user_tab_privs;

8.4 Grant role permissions to users

grant connect ,resource to user;
resource role: permission to operate the database
connect role: permission to connect to the database
DBA role: Has database administrator privileges

8.5 Custom roles

create role role1;
grant create session,create table to role1; 
user role
grant role1 to user; assign the role to the user (the user has the permissions of role1)
User roles and data dictionary
dba_role_privs all user roles
user_role_privs current user role
Role permissions and data dictionary
role_sys_privs System privileges for all roles

ORACLE function

1.orcl’s three connection methods

1. sqlplus --> username --> password
2.sqlplus scott/123@orcl
2.sqlplus/nolog  conn system/123@orcl as sysdba

2. Query table space

select * from dba_data_files;

3 field types

   Numeric data type : binary_double A new data type provided by Oracle 10G is used to store a double-precision 64-bit floating point number.
  Date data type : date stores date and time data in the table, timestamp stores the year, month, and day of the date as well as the minutes and seconds of the time.
  Large object data type : clob Large batches of text, blob binary objects such as pictures, etc.

4 functions

select p,name, concat(p.price,'元')  p.description from es_product p; // Concatenate strings
select p.name ,  p.price||元 from es_product p;
select p.name , nvl(p.content,'No detailed description yet')  from es_product p;
trunc(column|expression,n)  // Interception function
select trunc(45.926) from daul ;  45
select trunc(45.926,2) from dual; 45.92
select trunc(45.926,-1) from dual; 40
select round(45.926) from daul; 46    // 4 rounding function
select round(45.926,2) from dual; 45.93
select round(45.926,-1) from dual; 50
select round(p.price,2) as "rounded price", p.name as "product name"
from es_product p;
select sysdate from dual    // Date function:
select extract (year from sysdate) from dual
select extract (month from sysdate) from dual
select extract (day from sysdate) from dual
insert into es_product values(1000,2,apple,3783,null,null,sysdate,700);
select extract(year from p.saledate)||'-'|| extract(monthfrom p.saledate)||'-'||extract(day from p.saledate) from es_product p;
//conversion function
select  to_char(p.saledate,'yyyy-MM-DD') from es_product p;
select * from es_product p where  to_date('2012-2-29','yyyy-MM-DD') < p.saledate and  to_date('2012-4-1','yyyy-MM-DD') > p.saledate
//Aggregation function
select  sum(p.stockcount),p.sort_id from es_product p  group by p.sort_id;
select  count(p.stockcount),p.sort_id from es_product p group by p.sort_id;
select  max(p.price),p.sort_id from es_product p group by p.sort_id;
// The subquery returns the result as a single row
select id, name, price from es_product where sort_id = (select id from es_sort where sortname='图书');
select id, name,price  from es_product where(select avg(price) from  tb_sort )>price

5 Creation of stored procedures

create [or replace] procedure procudure_name
[{parameter_list}]
{ISIAS}
[local declarations]
begin
executable statments
[EXCEPTION]
executable handlers
END [produce_name]; 

5.1 demo:

create or replace procedure
find_emp(emp_no number) --Create a stored procedure with one parameter
AS
empname varchar2(20);
begin
select em.name into empname from employees em where em.id =emp_no;
dbms_output.put_line('employee name'||empname);
exception
when no_data found then
dbms_output.put_line('Employee number found');
end find_emp;

5.2 Execute stored procedures

execute procedure_name(param_list);
set serverountput on --display stored procedures
execute find_emp(3);

5.3 Parameter mode of stored procedures

in default mode If the parameter is not specified, the default int mode is specified.
out 
in out

5.4 Authorization of stored procedures

1.grant excute on procedure_name to user
2.grant excute on procedure to user with grent option

6 ORACLE stored procedure DEMO

create or replace procedure PRO_OA_KQ_SYNRECORDSDATA as
begin
    begin--请假
      insert into oa_kq_records(id,no,begintime,endtime,type,typeid,days,hours,savedate,reqdate,totalhours,LEAVEANDBUESSID)
            select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,lz.sjqjkssj,lz.sjqjjssj,1,lz.qjlx ,lz.sjqjts,lz.sjqjss,to_char(sysdate,'yyyy-MM-dd'),l.reqdate,lz.sjqjts*8+lz.sjqjss,lz.id
            from uf_kq_leave l
            left join uf__kq_leave_zib lz on l.requestid = lz.requestid
            left join uf_kq_xjdzib xj on lz.id=xj.sqr
            left join humres h on h.id = l.reqman
            left join requestbase rb on rb.id = lz.requestid
            where l.flag=0 and rb.isfinished =1 and rb.isdelete=0;
      update uf_kq_leave l set l.flag =1 where l.id in (select l.id from uf_kq_leave l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);
      commit;
      EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
    end;

    begin--销假
      insert into oa_kq_records(id,no,begintime,endtime,type,typeid,days,hours,requestid,savedate,reqdate,totalhours)
           select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,kqx.sjqjkssj,kqx.sjqjjssj,2,kqx.qjlx,kqx.sjqjts,kqx.sjqjss,kqx.sqr,to_char(sysdate,'yyyy-MM-dd'), kqxz.reqdate, kqx.sjqjts*8+kqx.sjqjss
           from uf_kq_xjsqd kqxz
           left join uf_kq_xjdzib kqx on kqxz.requestid=kqx.requestid
           left join humres h on h.id = kqx.reqman
           left join requestbase rb on rb.id=kqxz.requestid
           where kqxz.flag=0 and rb.isfinished=1 and rb.isdelete=0;
      ----用销请假的id更新 reuqest
      update oa_kq_records set requestid=1 where LEAVEANDBUESSID in(select kqx.sqr
           from uf_kq_xjsqd kqxz
           left join uf_kq_xjdzib kqx on kqxz.requestid=kqx.requestid
           left join humres h on h.id = kqx.reqman
           left join requestbase rb on rb.id=kqxz.requestid
           where kqxz.flag=0 and rb.isfinished=1 and rb.isdelete=0);
      update uf_kq_xjsqd l set l.flag =1 where l.id in (select l.id from uf_kq_xjsqd l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);

       commit;
       EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
     end;

    begin--出差
      insert into oa_kq_records(id,no,begintime,endtime,type,days,savedate,reqdate,LEAVEANDBUESSID)
        select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,kqwz.sjcckssj,kqwz.sjccjssj,3,kqwz.sjccts ,to_char(sysdate,'yyyy-MM-dd'),kqw.reqdate,kqwz.id
           from uf_kquf_workout kqw
           left join uf_kquf_workout_zib kqwz on kqw.requestid = kqwz.requestid
           left join humres h on h.id= kqw.reqman
           left join uf_kq_xccd_zib xc on xc.sqr=kqwz.id
           left join requestbase rb on rb.id = kqwz.requestid
           where kqw.flag=0 and rb.isfinished=1 and rb.isdelete=0;
      update uf_kquf_workout l set l.flag =1 where l.id in (select l.id from uf_kquf_workout l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);
       commit;
       EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
    end;

    begin--销出差
      insert into oa_kq_records(id,no,begintime,endtime,type,days,requestid,savedate,reqdate)
        select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,sccz.sjcckssj,sccz.sjccjssj,4,sccz.sjccts,sccz.sqr,to_char(sysdate,'yyyy-MM-dd'),kqsc.reqdate
           from uf_kq_xccsqd kqsc
           left join uf_kq_xccd_zib sccz on kqsc.requestid=sccz.requestid
           left join humres h on h.id= kqsc.reqman
           left join requestbase rb on rb.id = kqsc.requestid
           where kqsc.flag=0 and rb.isfinished=1 and rb.isdelete=0 ;

      ----用的销出差id更新 reuqest
     update oa_kq_records set requestid=1 where LEAVEANDBUESSID in(select sccz.sqr
           from uf_kq_xccsqd kqsc
           left join uf_kq_xccd_zib sccz on kqsc.requestid=sccz.requestid
           left join humres h on h.id= kqsc.reqman
           left join requestbase rb on rb.id = kqsc.requestid
           where kqsc.flag=0 and rb.isfinished=1 and rb.isdelete=0);

      update uf_kq_xccsqd l set l.flag =1 where l.id in (select l.id from uf_kq_xccsqd l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);

       commit;
       EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
    end;

    begin--加班核对
    insert into oa_kq_records(id,no,begintime,endtime,type,hours,savedate,reqdate)
          select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,jbzi.beintime,jbzi.endtime,5,jbzi.jbsc1,to_char(sysdate,'yyyy-MM-dd'),jbz.reqdate
           from uf_kq_jbhdzb jbz
           left join uf_kq_jbhdzib jbzi on jbz.requestid=jbzi.requestid
           left join humres h on h.id= jbzi.reqman
           left join requestbase rb on rb.id =jbz.requestid
           where jbz.flag=0 and rb.isfinished=1 and rb.isdelete=0;
      update uf_kq_jbhdzb l set l.flag =1 where l.id in (select l.id from uf_kq_jbhdzb l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);
      commit;
      EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
    end;

     begin--补打卡
      insert into oa_kq_records(id,no,bktime,bkqdtime,bkqttime,type,savedate,reqdate)
      select SE_OA_KQ_RECORDSID.NEXTVAL,h.objno,bkzi.bkrq,bkzi.qdsj,bkzi.qtsj ,6,to_char(sysdate,'yyyy-MM-dd'),bkz.reqdate
           from uf_kq_specialattendancezb bkz
           left join uf_kq_specialattendancezib bkzi on bkz.requestid=bkzi.requestid
           left join humres h on h.id= bkzi.reqman
           left join requestbase rb on rb.id =bkz.requestid
           where bkz.flag=0 and rb.isfinished=1 and rb.isdelete=0;
     update uf_kq_specialattendancezb l set l.flag =1 where l.id in (select l.id from uf_kq_specialattendancezb l left join requestbase r on l.requestid=r.id and l.flag=0 and r.isfinished =1 and r.isdelete=0);
       commit;
        EXCEPTION
          WHEN OTHERS THEN
          ROLLBACK;
    end;
end PRO_OA_KQ_SYNRECORDSDATA;

Guess you like

Origin blog.csdn.net/java_zhong1990/article/details/39613261#comments_28979377