ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 데이터베이스#8
    데이터베이스 2023. 2. 7. 09:10
    • Insert all
      • 모든 조건을 실행
    • Insert first
      • 첫 조건을 실행했다면 두번째 조건에서는 실행안함
    • PIVOTING INSERT
      • 통계적인 분석이 안되므로 차트가 안된다
      • 원장성
    • DROP
    • DELETE
    • UPDATE
    • COMMIT & ROLLBACK & SAVEPOINT
    • SEQUENCE
    •  
    TRUNCATE TABLE WEIGHT_INFO; 
    TRUNCATE TABLE HEIGHT_INFO;
    
    -- 학생 테이블에서 2학년 이상의 학생을 검색하여 height_info 테이블에는 키가 170보다 큰 학생의 학번, 이름, 키를 입력하고
    -- weight_info 테이블에는 몸무게가 70보다 큰 학생의 학번, 이름, 몸무게를 각각 입력하여라.
    
    insert FIRST
    when height > 170 then into height_info values(studno, name, height)
    when weight > 70 then into weight_info values(studno, name, weight)
    select *
    from student
    where grade>=2;
    
    select * from weight_info;
    select * from height_info;
    
    create table sales(
    sales_no number(4),
    week_no number(2),
    sales_mon number(7,2),
    sales_tue number(7,2),
    sales_wed number(7,2),
    sales_thu number(7,2),
    sales_fri number(7,2));
    
    
    insert into sales values(1101, 4, 100, 150, 80, 60, 120);
    insert into sales values(1102, 5, 300, 300, 230, 120, 150);
    drop table sales_data;
    create table sales_data(
    sales_no number(4),
    week_no number(2),
    day_no number(2),
    sales number(7,2)
    );
    
    select * from sales;
    select * from sales_data;
    
    
    -- 시험문제
    insert all
    into sales_data values(sales_no, week_no, 1, sales_mon)
    into sales_data values(sales_no, week_no, 2, sales_tue)
    into sales_data values(sales_no, week_no, 3, sales_wed)
    into sales_data values(sales_no, week_no, 4, sales_thu)
    into sales_data values(sales_no, week_no, 5, sales_fri)
    select * from sales;
    
    select sales_no, week_no, 
    -- DAY_NO
    max(case when day_no = 1 then sales end) sale_mon,
    max(case when day_no = 2 then sales end) sale_tue,
    max(case when day_no = 3 then sales end) sale_wed,
    max(case when day_no = 4 then sales end) sale_thu,
    max(decode(day_no, 5, sales)) sale_fri
    from sales_data
    group by sales_no, week_no;
    
    select 
        MIN(DECODE(MOD(rownum, 7),1,rownum)) SUN,
        MIN(DECODE(MOD(rownum, 7),2,rownum)) MON,
        MIN(DECODE(MOD(rownum, 7),3,rownum)) TUE,
        MIN(DECODE(MOD(rownum, 7),4,rownum)) WED,
        MIN(DECODE(MOD(rownum, 7),5,rownum)) THU,
        MIN(DECODE(MOD(rownum, 7),6,rownum)) FRI,
        MIN(DECODE(MOD(rownum, 7),0,rownum)) SAT
    from dict
    where rownum <= to_char(LAST_DAY(SYSDATE), 'DD')
    GROUP BY  TRUNC((ROWNUM -1) / 7)
    ORDER BY  TRUNC((ROWNUM -1) / 7);
    
    
    -- 교수 번호가 9903인 교수의 혂재 직급을 ?부교수?로 수정하여라
    select * from professor;
    update professor set position='부교수' where profno='9903';
    
    -- 서브쿼리를 이용하여 학번이 10201인 학생의 학년과 학과 번호를 10103 학번 학생의 학년과 학과 번호와 동일하게 수정하여라.
    -- 시험에나옴!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    
    update student set (grade, deptno) = (select grade, deptno from student where studno='10103')
    where studno = '10201';
    
    -- 학생 테이블에서 학번이 20103인 학생의 데이터를 삭제하여라.
    delete from student where studno = '20103';
    select * from student;
    
    -- 학생 테이블에서 컴퓨터공학과에 소속된 학생을 모두 삭제하여라.
    select * from student ;
    delete from student 
    where deptno = (select deptno from department where dname='컴퓨터공학과');
    
    
    -- professor 테이블과 professor_temp 테이블을 비교하여 professor 테이블에 있는 기존 데이터는 professor_temp 테이블
    -- 의 데이터에 의해 수정하고, professor 테이블에 없는 데이터는
    -- 신규로 입력한다.
    
    create table professor_tmp as
    select * 
    from professor
    where position ='교수';
    
    update professor_tmp set position ='명예교수'
    where position = '교수';
    
    insert into professor_tmp
    values(9999, '김도경', 'arom21','전임강사',200,sysdate,10,101);
    
    
    
    
    merge into professor p
    using professor_tmp f
    on(p.profno = f.profno)
    when matched then
        update set p.position = f.position
    when not matched then
        insert values(f.profno,f.name, f.userid, f.position, f.sal, f.hiredate, f.comm, f.deptno);
    
    select * from professor;
    select * from professor_tmp;
    commit;
    --save point
    Insert into professor(profno, name) values(9991,'양찬용');
    savepoint s1;
    delete  from professor where profno='9991';
    Insert into professor(profno, name) values(9992,'천은경');
    savepoint s2;
    
    Insert into professor(profno, name) values(9993,'김설하');
    select * from professor;
    
    rollback to s2;
    
    commit;
    
    create table TBL_BOARD(
        BNO NUMBER,
        TITLE VARCHAR2(1000),
        CONTENT VARCHAR2(4000),
        WRITER VARCHAR2(1000),
        REGDATE DATE DEFAULT SYSDATE
    );
    
    INSERT INTO TBL_BOARD (BNO, TITLE, CONTENT, WRITER) VALUES(SEQ_BOARD.NEXTVAL, '글제목','글내용','USER1');
    
    SELECT * FROM TBL_BOARD;
    ROLLBACK;
    
    CREATE SEQUENCE SEQ_BOARD;
    
    SELECT * FROM SAMPLE.STUDENT;

    '데이터베이스' 카테고리의 다른 글

    데이터베이스#3  (0) 2023.02.07
    데이터베이스#4  (0) 2023.02.07
    데이터베이스#7  (0) 2023.02.07
    데이터베이스#6  (0) 2023.02.03
    데이터베이스#5  (0) 2023.02.02
Designed by Tistory.