'MySQL'에 해당되는 글 5건

  1. 2007.06.03 mysql의 update문에서 variable를 사용하기 2 by 홍사마
  2. 2007.05.13 Implement Oracle's rownum using mysql by 홍사마
  3. 2007.02.23 MySQL에서 limit를 사용할 때 전체 row갯수를 구하는 방법 by 홍사마
  4. 2006.10.22 [펌.mysql] jdbc 한글 문제 by 홍사마
  5. 2006.10.17 [MySql] auto_increment 값 알아오기 3 by 홍사마

아래의 rownum과 같은 방법으로 update에서 variable을 사용하기. 이걸 사용하기 위해서 set 안에 variable 세팅을 넣었는데 안 되더니만 where에 넣으니깐 되네.

update table, (SELECT @a:=0) p SET val1=@a
where @a:=@a+1
Posted by 홍사마

Sometimes it needs to exactly mimic Oracle's ROWNUM where is no possibility to initiate a counter in previous statement by SET @rownum:=0;.
It is still possible in a single SQL.

SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r, mytable t;

Posted by 홍사마

여기는 블로그가 아니라 메모장으로 쓰는군...ㅡ.ㅡ;;;
많은 사람들(나만 그런가?)이 paged list를 만들 때 전체 row수를 페이지에 나타나는 갯수로 나누어서 페이지를 나눠놓고 가져올 때는 limit로 가져올것이다. 그러면 페이지를 나타낼 때 전체수를 나누는 쿼리를 한번 더해야되는 수가 있고 그 쿼리도 만들어야 되는 경우가 발생한다. 물론 아래의 방법도 쿼리를 한번 더 해야되나 그 쿼리를 만들지 않아도 되고 그냥 found_rows()라는 것을 사용하면 된다.

문제는 이것이 subquery 안에서는 동작이 안된다. 가장 바깥에 있는 쿼리에서만 사용해야된다. 해보니 잘된다. 이걸로 totalcount를 가져오는 쿼리를 다 바꿔야겠다..흐극...


MySQL: Get total number of rows when using LIMIT August 11, 2006

Posted by Slobodan Kovacevic in : Programming , trackback

Every now and then you need to limit the number of rows MySQL returns, i.e. use the LIMIT clause. Result set pagination is by far the most often usage of LIMIT clause, since you usually want to select only rows you’ll be displaying on certain page.

The problem is that for pagination you also need total number of rows in a result set, so you know how many pages you’ll have. This usually means that you need to execute query two times. First query is for counting total number of rows without LIMIT. Second query is exactly the same as the first, just without LIMIT and it will actually retrieve required data. You would need two queries like these:

SELECT COUNT(*) FROM users WHERE name LIKE 'a%';

SELECT name, email FROM users WHERE name LIKE 'a%' LIMIT 10;

Now, this is not such a big problem when you have small result sets and/or simple queries. But if you have a complex query that joins several tables and takes a while to execute - well, you probably wouldn’t want to execute it twice and waste server resources.

Luckily since MySQL 4.0.0 you can use SQL_CALC_FOUND_ROWS option in your query which will tell MySQL to count total number of rows disregarding LIMIT clause. You still need to execute a second query in order to retrieve row count, but it’s a simple query and not as complex as your query which retrieved the data.

Usage is pretty simple. In you main query you need to add SQL_CALC_FOUND_ROWS option just after SELECT and in second query you need to use FOUND_ROWS() function to get total number of rows. Queries would look like this:

SELECT SQL_CALC_FOUND_ROWS name, email FROM users WHERE name LIKE 'a%' LIMIT 10;

SELECT FOUND_ROWS();

The only limitation is that you must call second query immediately after the first one because SQL_CALC_FOUND_ROWS does not save number of rows anywhere.

Although this solution also requires two queries it’s much more faster, as you execute the main query only once.

You can read more about SQL_CALC_FOUND_ROWS and FOUND_ROWS() in MySQL docs.

Posted by 홍사마

[펌.mysql] jdbc 한글 문제

DEVEL : 2006. 10. 22. 19:27

퍼온 글...

주제는 my.cnf파일을 수정한 후에 create database를 euckr로 생성해야함.

====================================================

/etc/mysql/my.cnf 파일에서..

[client]
...
default-character-set = euckr

[mysqld]
...
default-character-set=euckr
init_connect = SET collation_connection = euckr_korean_ci
init_connect = SET NAMES euckr
charater-set-server = euckr
collation-server = euckr_korean_ci

[mysqldump]
...
default-character-set=euckr

[mysql]
default-character-set=euckr

수정후에 mysql을 다시 띄움


==============================================

mysql이 4.1로 업그레이드 되면서 서브쿼리가 지원되게 되어

아마도 많은 사람들이 개발시점에서 한 번쯤은 mysql4.1을 사용할지

고민했으리라 생각합니다.

그러나 아직 제대로된 버전이 아닌관계로 버젓한 메뉴얼이 없어

변화된 localization셋팅때문에 많은 분들이 고생했을 것이라 예상합니다.


mysql4.1에서는 4.0에서처럼 default character set하나만 바꿔주면 끝나질

않습니다. 서버/데이터베이스/테이블/connection/ 심지어 필드

하나하나, 스트링 하나하나까지도 character set과 collation을 설정할 수

있게 되었습니다. 그래서 4.0에서 서비스하던 사이트를 4.1로

데이터를 옮기는 과정에서 한글이 깨지는 현상이 발생합니다.


쉽게 말씀드리면 4단계에 걸쳐서 각각 character set과 collation을

변경해주시면 됩니다. 우선 데이터베이스를 다음과 같이 새로 생성합니다.


>> create database DB_NAME character set euckr collation euckr_korean_ci


그리고 4.0의 데이터를 스크립트로 복사한 다음 각각의 테이블 끝에

붙어있는 character set을 euc-kr 이나 latin1에서 euckr로 변경해줍니다.

(4.1에서 한글의 character set이름이 euc-kr에서 euckr로 변경되었습니다.)


그런 다음 mysql 을 --default-character-set=euckr --default-collation=euckr_korean_ci 의 옵션을 주어 실행시키면서 자료를 덤프받습니다.


>> mysql -uroot -p --default-character-set=euckr --default-collation=euckr_korean_ci < DB_SCRIPT.SQL 

(DB_SCRIPT.SQL파일은 4.0에서 받은 데이터 스크립트파일이겠죠.)


그렇게 하면 일단 데이터베이스 어필리케이션(예를 들면 SQLyog같은 프로그램)에서는 정상적으로 데이터를 볼 수 있을 것입니다.

하지만 JDBC를 사용해서 접속하는 경우 connection characterset을  따로

설정해주지 않으면 한글이 깨질 것입니다.

저같은 경우 어플리케이션에서 connection을 얻어오는 즉시

해당 connection을 이용하여 "SET CHARACTER SET euckr"이란

SQL명령을 executeUpdate 시켰서 해결했습니다만 아마도 더 깔끔한

방법이 있을 줄 압니다.


그렇게 하면 mysql4.1.1알파에서도 한글을 문제없이 사용할 수 있습니다.

참고로 그냥 latin1으로 설정한 상태에서도 한글이 사용이 가능하긴 합니다만 order by나 일부 한글로의 검색시에 제대로 검색이 안되는 경우가 발생합니다. 그런 경우 해당 필드만 character set을 설정해서 사용할 수도 있습니다. 예를 들면 latin1으로 저장된 한글을 euckr의 코드셋 순서로 정렬하고 싶을 때 다음과 같이 하면 됩니다.


>> SELECT * FROM table_name ORDER BY k COLLATE euckr_korean_ci


그러면 한글 순서로 정렬이 됩니다.

이 말은 만약 이제까지 latin1으로 잘 써오신 분이라면 정렬을 위해서만 추가적으로 코드를 추가해줄 경우 문제없이 잘 정렬이 됨을 의미하는 것이니 데이터베이스를 통째로 수정할 필요가 없겠지요.


저도 초보라 고생끝에 알아낸 팁입니다. 유용하게 참고하셨으면 좋겠습니다.


Posted by 홍사마

네이년에서는 copy&paste가 안되니 직접 쓸 수밖에..ㅡ.ㅡ;;;

=======================================

컬럼타입이 auto_increment이면 insert 시 값을 지정하지 않아도 알아서 자동으로 증가된 값을 사용한다. 그러나 문제는 PK로 사용한 경우 insert 후 그 값을 알 수가 없다는 것이다. 그래서 MySQL에서는 가장 최근에 사용된 auto_increment값을 알수 있게 해주는 함수를 제공한다.

LAST_INSERT_ID()

이 함수는 connection기반으로 가장 최근에 사용된 auto_increment 값을 리턴한다. 사용할 대는 아래와 같이 간단하게.

select LAST_INSERT_ID();

이런 식으로 사용하면 된다.

참고로 auto_increment를 사용할 때 임의로 시작값을 설정하기 위한 것..
alter table "tbl_name" auto_increment = xxx

=================================================

3.6.9. Using AUTO_INCREMENT

The AUTO_INCREMENT attribute can be used to generate a unique identity for new rows:

CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO animals (name) VALUES ('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;

Which returns:

+----+---------+
| id | name |
+----+---------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
+----+---------+

You can retrieve the most recent AUTO_INCREMENT value with the LAST_INSERT_ID() SQL function or the mysql_insert_id() C API function. These functions are connection-specific, so their return value is not affected by another connection also doing inserts.

Note: For a multiple-row insert, LAST_INSERT_ID()/mysql_insert_id() actually returns the AUTO_INCREMENT key from the first of the inserted rows. This allows multiple-row inserts to be reproduced correctly on other servers in a replication setup.

For MyISAM and BDB tables you can specify AUTO_INCREMENT on a secondary column in a multiple-column index. In this case, the generated value for the AUTO_INCREMENT column is calculated as MAX(auto_increment_column)+1 WHERE prefix=given-prefix. This is useful when you want to put data into ordered groups.

CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
);
INSERT INTO animals (grp,name) VALUES('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');SELECT * FROM animals ORDER BY grp,id;

Which returns:

+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+

Note that in this case (when the AUTO_INCREMENT column is part of a multiple-column index), AUTO_INCREMENT values are reused if you delete the row with the biggest AUTO_INCREMENT value in any group. This happens even for MyISAM tables, for which AUTO_INCREMENT values normally are not reused.)

If the AUTO_INCREMENT column is part of multiple indexes, MySQL will generate sequence values using the index that begins with the AUTO_INCREMENT column, if there is one. For example, if the animals table contained indexes PRIMARY KEY (grp, id) and INDEX (id), MySQL would ignore the PRIMARY KEY for generating sequence values. As a result, the table would contain a single sequence, not a sequence per grp value.

==================================================================

LAST_INSERT_ID()
LAST_INSERT_ID(expr)
Returns the last automatically generated value that was inserted into an AUTO_INCREMENT column.
mysql> SELECT LAST_INSERT_ID();        -> 195
커넥트 단워로 값이 유지됨으로 다른 사용자에 의해서 값이 바뀌지 않습니다.
The last ID that was generated is maintained in the server on a per-connection basis. This means the value the function returns to a given client is the most recent AUTO_INCREMENT value generated by that client. The value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that you can retrieve your own ID without concern for the activity of other clients, and without the need for locks or transactions. The value of LAST_INSERT_ID() is not changed if you update the AUTO_INCREMENT column of a row with a non-magic value (that is, a value that is not NULL and not 0). If you insert many rows at the same time with an insert statement, LAST_INSERT_ID() returns the value for the first inserted row. The reason for this is to make it possible to easily reproduce the same INSERT statement against some other server. If you use INSERT IGNORE and the record is ignored, the AUTO_INCREMENT counter still is incremented and LAST_INSERT_ID() returns the new value. If expr is given as an argument to LAST_INSERT_ID(), the value of the argument is returned by the function and is remembered as the next value to be returned by LAST_INSERT_ID(). This can be used to simulate sequences:
  • Create a table to hold the sequence counter and initialize it:
    mysql> CREATE TABLE sequence (id INT NOT NULL);mysql> INSERT INTO sequence VALUES (0);
  • Use the table to generate sequence numbers like this:
    mysql> UPDATE sequence SET id=LAST_INSERT_ID(id+1);mysql> SELECT LAST_INSERT_ID();
    The UPDATE statement increments the sequence counter and causes the next call to LAST_INSERT_ID() to return the updated value. The SELECT statement retrieves that value. The mysql_insert_id() C API function can also be used to get the value. See section 21.2.3.33 mysql_insert_id().
You can generate sequences without calling LAST_INSERT_ID(), but the utility of using the function this way is that the ID value is maintained in the server as the last automatically generated value. It is multi-user safe because multiple clients can issue the UPDATE statement and get their own sequence value with the SELECT statement (or mysql_insert_id()), without affecting or being affected by other clients that generate their own sequence values. Note that mysql_insert_id() is only updated after INSERT and UPDATE statements, so you cannot use the C API function to retrieve the value for LAST_INSERT_ID(expr) after executing other SQL statements like SELECT or SET.
Posted by 홍사마