설문조사
PostgreSQL/PPAS 관련 듣고 싶은 교육은


총 게시물 94건, 최근 0 건
   

CHAR 와 VARCHAR 의 동등비교

글쓴이 : PostgresDBA 날짜 : 2013-01-24 (목) 09:57 조회 : 5997
퀴즈를 하나 내보겠습니다. 

scott@[local]:5432:scottdb] 
SQL> create table test(i int, x varchar(10), y varchar(10), z char(10));
CREATE TABLE
scott@[local]:5432:scottdb] 
SQL> insert into test(i,x,y,z) values(1,'abc','abc','abc');
INSERT 0 1
scott@[local]:5432:scottdb] 
SQL> insert into test(i,x,y,z) values(2,'abc       ','abc','abc');  #abc문자뒤에 공백7개
INSERT 0 1
scott@[local]:5432:scottdb] 
SQL> insert into test(i,x,y,z) values(3,'abc    ','abc','abc');  #abc문자뒤에 공백4개
INSERT 0 1
scott@[local]:5432:scottdb] 
SQL> insert into test(i,x,y,z) values(4,'abc    ','abc','abc     '); #abc문자뒤에 공백4개
INSERT 0 1
scott@[local]:5432:scottdb] 

이제 부터 아래 쿼리 세개의 쿼리 결과를 맞혀보세요.
select i,x,y,z, length(x), length(y), length(z) from test order by i;
select i,x,y,z from test where x=y;
select i,x,y,z from test where x=z;




...... 정답은 다음과 같습니다.
SQL>  select i,x,y,z, length(x), length(y), length(z) from test order by i;
 i |     x      |  y  |     z      | length | length | length 
---+------------+-----+------------+--------+--------+--------
 1 | abc        | abc | abc        |      3 |      3 |      3
 2 | abc        | abc | abc        |     10 |      3 |      3
 3 | abc        | abc | abc        |      7 |      3 |      3
 4 | abc        | abc | abc        |      7 |      3 |      3
(4 rows)


scott@[local]:5432:scottdb] 
SQL> select i,x,y,z from test where x=y;
 i |  x  |  y  |     z      
---+-----+-----+------------
 1 | abc | abc | abc       
(1 row)

scott@[local]:5432:scottdb] 
SQL> select i,x,y,z from test where x=z;
 i |     x      |  y  |     z      
---+------------+-----+------------
 1 | abc        | abc | abc       
 2 | abc        | abc | abc       
 3 | abc        | abc | abc       
 4 | abc        | abc | abc       
(4 rows)

생각하신 답과 같으신가요? ^^
이렇듯 데이터타입이 다른 VARCHAR / CHAR  컬럼간에 비교할때는 항상 주의하셔야 합니다!!

   

postgresdba.com