SQL (DML, 변환함수) PHP (arrays, functions)

Hyun Seo (Lucy) Lee 이현서·2020년 9월 25일
0
post-thumbnail

09/25/2020

DML (INSERT, UPDATE, DELETE)

  • DML - data manipulation language

    • sometimes SELECT is included in this, as some DBs support SELECT doing a transaction
  • transaction only involves one table at a time.

    > INSERT INTO table (col, col, ...) VALUES (val, val, ...) ;

    --> the number of cols and vals must match up.

UPDATE

> UPDATE table SET col=val, col=val, ... [WHERE condition];

DELETE

> DELETE FROM table [WHERE condition];

  • In Oracle, when you do DELETE FROM dept, it deletes the table dept right away, but stores the deleted data in a place called "Undo Segment". So that if the user does rollback, it can restore the dept table data back. --> 근데 업무용oracle은 rollback 안됨.
  • a system is either fast commit, slow rollback, or slow commit, fast rollback. --> oracle is the former.

restore vs. recovery

  • restore - 백업 받은거를 restore. the most recent 백업부터 지금까지는 다 날라감.
  • recovery - 이건 every action을 save해놔서 손실없는 복구를 할수 있음. --> redo and undo (참고로 MySQL은 recovery가 안됨. Oracle하고 DB2는 되는데. So for MySQL, they usually have 2 datas. one a copy of the other. there's less probability of both of them crashing.)

Back to INSERT

  • 날짜를 insert하려면 alter session set 으로 미리 correct format으로 세트 해놔야 함.
    ex:

    > SELECT sysdate FROM dual; --> to check what the current date format is.
    > ALTER ~~~;
    > INSERT INTO emp (eno, ename, ...) VALUES (val, val, ...) ;

변환 함수 (Conversion Functions)

  • YY RR 은 이제 잘 안씀.

  • 우리나라는 MM으로 씀.

  • HH24 더 많이 씀.

숫자 출력 형식

  • 9999 --> 10 입력하면 --> 10 (앞에 두자리 생략). 근데 9.99 --> 9.1 --> 9.10. 또 9.9 --> 9.89 입력하면 --> 9.9 로 반올림 함.
  • 0000 --> 10 입력하면 --> 0010 (무조건 네자리를 체움). 소수점 전에 0만 있으면 에러 남. 0.99나 00.
  • L --> 시스템의 설정에 따라 다름. 한국 시스템은 원 symbol, 미국은 $, 영국은 euro, etc.

  • MI: -50 이 아니라 50-

  • T_CHAR(123456789, '9.999EEEE') --> 1.235E+08

  • TO_NUMBER는 안씀. SQL이 저절로 해줌.

  • TO_CHAR는 보통 format 땜에 씀. 여기서 숫자 출력 형식 문자들을 씀.

  • WHENEVER you write a date in a query, ALWAYS use TO_DATE. 여기서 DON'T GET CONFUSED!!! You use TO_CHAR for the date as well if it's a formatting thing!! but if you're using a date for other purposes like hdate before or after a certain date, use TO_DATE().

    > SELECT TO_CHAR(sysdate, '"오늘은 "YYYY"년 "MM"월 "DD"일 입니다."') 날짜 FROM DUAL;

    --> ' ' because it's date and not number. The underlined part needs to have ' ' around it as long as it's either a date or a char.
    --> * also, don't forget to add NICKNAME after to_char() or to_date() functions!!

    > SELECT eno, ename, hdate FROM emp WHERE hdate < TO_DATE('19920101','YYYYMMDD');

    --> You use TO_DATE when you are comparing something to a DATE data-type column, since you can only compare dates to a date.
    --> OR when you are inserting an entry into a database that has a DATE data-type column.

PHP

  • 실제로 php에는 TRUE라는 값이 없음. 참 은 1 임. 거짓은 0 임.
  • array를 print 할때 만약 함수를 만들어 쓴다면 그 함수 안에 parameter는 &$parameter 하는게 좋음. 배열은 복사하는게 memory를 너무 많이 차지할수 있고 array는 어차피 it's made up of addresses. For PHP, an array is basically a list of variables.

0개의 댓글