Oracle 학습 노트 6 (DDL: 표, 색인, 보기, 동의어, 시퀀스 작업 관련 SQL)

18380 단어 oracle 학습
아래 의 이러한 기본 적 인 SQL 문 구 는 익숙 하고 유연 하 게 운용 할 수 있어 야 한다.자 료 를 찾 지 않 고 다음 과 같은 코드 를 쓸 수 있 는 것 이 좋 습 니 다.
1. 데이터 시트 작업 관련
--   
create table STUDENTS(
    STUNO number(10) not null,
    STUNAME varchar2(8),
    SEX char(2),
    AGE int,
    DEPARTNO varchar2(2) not null,
    CLASSNO varchar2(4) not null,
    REGDATE date default sysdate
);

--    
create table STUDENTS_2
    as select * from STUDENTS;

--     (    )
alter table STUDENTS add(PROVINCE varchar2(10));

--     (    )
alter table STUDENTS drop column PROVINCE;

--     (     )
alter table STUDENTS drop(SEX,AGE);

--     (    )
alter table STUDENTS modify DEPARTNO varchar2(4);

--    
alter table STUDENTS rename to STUDENTS_NEW;

--   
--drop table table_name [cascase contraints][purge];
drop table STUDENTS_NEW cascade constraints;

--  (        purge  ,       )
flashback table STUDENTS_NEW to before drop;

--      (  )
alter table STUDENTS_NEW read only;

--      (  )
alter table STUDENTS_NEW read write;

2. 구속 관련
--     :    、    、    、    、         。
create table BOOKS(
    BOOKNO number(4) not null, --    
    BOOKNAME varchar2(20),
    AUTHOR varchar2(10),
    SALEPRICE number(9,2) check(SALEPRICE>0 and SALEPRICE<1000), --    
    PUBLISHERNO varchar2(4) not null, --    
    PUBLISHDATE date,
    ISBN varchar2(20) not null, --    
    constraint PK_BOOKS primary key(BOOKNO)        --    
);

--      
alter table BOOKS modify BOOKNAME not null;

--      
alter table BOOKS modify BOOKNAME null;

--      
alter table BOOKS drop constraint PK_BOOKS;

--      
alter table BOOKS add constraint PK_BOOKS primary key(BOOKNO);

--      
alter table BOOKS add constraint UQ_BOOKS unique(ISBN);

--      
alter table BOOKS drop constraint UQ_BOOKS;

--      
alter table BBSDISTRICT drop constraint BBSDISTRICT_BBSPROVINCE_FK1;

--      
alter table BBSDISTRICT add constraint BBSDISTRICT_BBSPROVINCE_FK1
    foreign key(PROVINCE_ID)
    references BBSPROVINCE(PROVINCE_ID)
    on delete set null;--no action/set null/cascade

--      ,                   ,         
alter table BBSDISTRICT add constraint BBSDISTRICT_BBSPROVINCE_FK1
    foreign key(PROVINCE_ID)
    references BBSPROVINCE;

--    
alter table BBSDISTRICT disable constraint BBSDISTRICT_BBSPROVINCE_FK1;

--    
--alter table table_name enable [novalidate|validate] constraint con_name;
--novalidate                 ;validate                。
alter table BBSDISTRICT enable constraint BBSDISTRICT_BBSPROVINCE_FK1;

--    
--alter table table_name drop constraintcon_name;
alter table BBSDISTRICT drop constraint BBSDISTRICT_BBSPROVINCE_FK1;

3. 다른 대상
--    (B   ,      )
create index IX_BBSDETAIL_PRICE on BBSDETAIL(PRICE);

--      (  、              ,          7   )
create bitmap index IX_BITMAP_BBSDETAIL_CATEGORY on BBSDETAIL(CATEGORY_ID);

--

--       (             ,            )
create index IX_BBSDETAIL_CONIS on BBSDETAIL(round(NUMBER_OF_COINS,-1));

--    
alter index IX_BBSDETAIL_PRICE rebuild;

--    
drop index IX_BBSDETAIL_CONIS;

--    
--create [or replace] view <view_name> [alias[,alias]...]
--as <subquery>
--[with check option][constraint constraint_name]
--[with read only];
create or replace view VIEW_DETAIL
as select CATEGORY_NAME,TITLE,DISTRICT_NAME,ADDRESS from BBSDETAIL,BBSCATEGORY,BBSDISTRICT
    where BBSDETAIL.CATEGORY_ID=BBSCATEGORY.CATEGORY_ID and BBSDETAIL.DISTRICT_ID=BBSDISTRICT.DISTRICT_ID
with read only;

--      
desc VIEW_DETAIL;

--      
alter view VIEW_DETAIL compile;

--    
drop view VIEW_DETAIL;

--       (  )
create public synonym PUBLIC_DETAIL for BBS.BBSDETAIL;

--       
drop public synonym PUBLIC_DETAIL;

--    
create sequence SEQ_BBSDETAIL
    maxvalue 99999
    start with 10000
    increment by 100
    cache 50;

--       
select SEQ_BBSDETAIL.currval from dual;

--    
drop sequence SEQ_BBSDETAIL;

좋은 웹페이지 즐겨찾기