SQL (WHERE, LIKE) & PHP (WHILE)

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

09/21/2020

SQL

  • **Always make sure that your printed chart shows what kind of data you intended to print.**
  • 검색과 정렬이 있어야 Database라고 할수 있음. 이 두가지 성능은 굉장히 중요함.
  • 만약 명단을 출력 한다면 이름과 번호. 그냥 정보를 원한다면 그냥 다 출력 (*).
  • **In SQL, = is not an assignment, but a comparison operator. There's no == in SQL (note, there is == in PHP).**
  • In SQL, except for 별명, all other strings are denoted with single quotes ('');
  • When you 검색, always make sure to add '' to varchar-type columns. Only when it's a Number-type column you don't need the ''.
  • **in WHERE, you can't use the 별명 like you did in ORDER BY because WHERE phrase gets executed first before any other parts of the query.
  • 오라클 (ORACLE)이나 MySQL 은 year가 9,999 밖에 못늠. (그래서 NASA 같은 데는 자기가 알아서 database를 만듬. year 9999가 필요하니까.) --> 그치만 기업들은 많아봐야 10년 정도를 앞에 보고 기획을 세움. 그러니까 왠만한 기업들은 다 오라클과 MySQL 써도 상관 없음.

DATE

  • we gotta set up the 날짜 Format (**but note, for best practice you gotta use the 단일행함수 (single row function) that converts a data into DATE form).

    > ALTER SESSION SET nls_date_format = 'YY/MM/DD';

    But in general, the DATE type is in this form:
    YYYY/MM/DD:HH24:MI:SS
    --> the default for 시/분/초 is 00:00:00

    > ALTER SESSION SET nls_date_format = 'YYYY/MM/DD';

  • 요즘엔 YEAR를 꼭 4자리 씀. 1920년에 태어나 아직 살아있는 사람들도 많으니까.

  • 실제론 RDB (Relational DataBase) 에선 data 못 지움. 지웠다고 할땐 그냥 식별할수 없게 만드는 거임.

  • sometimes, to avoid NULL, people use -1 instead.

  • type NUMBER means it's likely to be involved in a math operation, while VARCHAR means it's not.

    • ex: 학번, 군번, 주민등록번호 --> 문자
    • ex2: 사번 --> 문자일수도 있고 숫자 일수도 있음. If the individual digits have some kind of meaning, then it's 문자. if not, 숫자.
  • Null 은 비교연산자도 안 통함. 그래서 IS NULL & IS NOT NULL function을 써야함. 그치만 첨부터 NULL이 있었다는 거는 이 database에 심각한 문제가 있다는 거임.

관계연산자와 LIKE 연산자

  • WHERE 조건이 여러개 있을땐 AND 와 OR 를 써야함.
    ex: SELECT * FROM emp WHERE dno = '20' AND sal >= 2000;
  • In this case: SELECT * FROM [table] WHERE 조건 OR 조건 AND 조건;
    --> 조건 AND 조건 gets executed first, and then the OR
  • AND 가 OR 보다 더 높은 priority 를 가지고 있음. 그래서 AND 와 OR를 둘다 쓰개 될땐 무조건 괄호 쳐줘야 함.

LIKE 연산자 (also known as 패턴 연산자)

> SELECT ... FROM ... WHERE 컬럼 LIKE '비교 문자열' ... ORDER BY ... ;

  • '%김%' --> '김' 이란 글자가 들어간 모든 문자열을 return.
  • '' --> '동' 이 가운데에 들어간 세글자 단어.
  • if you want to actually search the '%' or '_', use the # excape letter.

(side note: 원래 이름은 성과 이름으로 나눠 넣어져야 함. 더 좋게는 3개까지 컬럼이 있어야함. First/Middle/Last Name.)

  • ex: 이름과 성이 각각 한글자: '__'
  • ex2: 이름에 '%' 가 들어간 사람: '%#%%' ESCAPE '#';

**If SQL doesn't return anything after a query, that means the query wasn't executed.
**Be careful not to confuse 'LIKE' with '='! '=' only returns something if it's identical to the phrase that comes after it.

DATE

  • date is confusing because 365.24251 is the exact # of days in a year. So irregularity like February's date numbers make it tricky.
  • You gotta do hiredate < '1995/01/01', and NOT hiredate <= '1994/12/31' (cuz default 시분초 is 00 00 00)

(side note: Windows의 기초적인 에디터는 Note Pad. if you type in SQL> ed, it opens up Note Pad with the query that was typed in last in the cmd prompt. Linux의 기초적인 에디터는 Vi 임.)

SQL에서 sqlplus는 브라우저 와 같은 개념임. SELECT query를 type하면, sqlplus가 그거를 Server Processor (SP) 한테 보냄. 그럼 SP가 Oracle에서 검색을 하고 그렇게 찾은 data를 다시 sqlplus 에게 보냄. 그러면 sqlplus는 그걸 loop을 통해서 우리가 볼수 있는 형태로 cmd prompt에 띠움.

PHP

  • 반복문 (while, for)
    --> 보통 for loop 을 제일 많이 씀. everything written in while loop can be written in for loop, and vice versa. and for loop has higher readability, so everyone uses for loop whenever they can rather than while loops

  • 초기식 - initializer (ex: $a = 1;)

  • 증감식 - increment operator (ex; $a++;)

  • 4 main components of any loops:

    1. 초기식
    2. 조건
    3. 명령문
    4. 증감식
  • Because the increment operator comes at the end of the while loop, you can't figure out how long the while loop is going to go until unless you read the whole while loop block. That's one reason why for-loops have higher readability.

  • To do a client-side validation of sorts for the form:
    <script>
    window.alert('2에서 9사이에 값을 입력하세요!'); //알림창을 띠움
    history.go(-1); //goes back to the previous page on browser
    </script>
    --> 이걸 echo() 로 php code 안에다가 적음.

    exit; --> you do this after that script because this will clear the memory and stuff like that right away so that's good in general rather than not doing it and waiting for the system to clean it up some time later.
    (side note: PHP의 주석은 // 로 하면 됨!)

0개의 댓글