Warning: main(/home/hosting_users/freelyi/www/menu_js.php) [function.main]: failed to open stream: No such file or directory in /home/hosting_users/freelyi/www/blog/archives/2004/08/04_mysql_정리.html on line 40

Warning: main(/home/hosting_users/freelyi/www/menu_js.php) [function.main]: failed to open stream: No such file or directory in /home/hosting_users/freelyi/www/blog/archives/2004/08/04_mysql_정리.html on line 40

Warning: main() [function.include]: Failed opening '/home/hosting_users/freelyi/www/menu_js.php' for inclusion (include_path='.:/usr/local/lib/php') in /home/hosting_users/freelyi/www/blog/archives/2004/08/04_mysql_정리.html on line 40

« STYLE SHEET 위치제어 | Main | 강해져라 »

mysql 정리

mysql 정리입니다.

 
mysql root 비밀번호 바꾸기
[root@brume /]/usr/local/mysql/bin/mysqladmin -u root password 1234
 
mysql 어디서든 실행가능하게 만들기
[root@brume /]cd /bin
[root@brume bin]ln -s /usr/local/mysql/bin/mysql mysql  // mysql의 심볼릭 링크를
                                                        // 일반 실행명령어가 있는 /bin에 만든다.
 
mysql에 접속하기
[root@brume /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 347 to server version: 3.23.53
 
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> 
mysql> show databases ;  // mysql에 있는 database의 정보를 보여준다. sql은 ';'로 끝남을 주의하자
+----------+
| Database |
+----------+
| brume    |
| linux    |
| mysql    |
| onmarket |
| test     |
+----------+
5 rows in set (0.00 sec)  // 5개의 db가 있음을 알 수 있다.
mysql> drop database test;  // test라는 이름의 db를 삭제
Query OK, 0 rows affected (0.03 sec)
mysql> use mysql; // mysql이라는 db를 사용한다.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed  // 사용하는  db가 바뀌었음을 알려준다.
mysql> show tables; // table의 정보를 보준다.
+-----------------+
| Tables_in_mysql |
+-----------------+
| columns_priv    |  // 컬럼 레벨의 권한에 대한 설정을 가지고 있다.
| db              |  // 데이터베이스 레벨의 권한에 대한 설정을 가지고 있다.
| func            |  // 사용자 함수에 대한 정보를 가지고 있다.
| host            |  // db테이블과 같이 데이터베이스 레벨의 권한에 대한 설정을 가지고 있다.
| tables_priv     |  // 테이블 레벨의 권한에 대한 설정을 가지고 있다.
| user            |  // 글로벌 레벨의 권한에 대한 설정을 가지고 있다.
+-----------------+
6 rows in set (0.00 sec)
mysql> describe db; //db table의 스키마 정보를 보여준다.
+-----------------+-----------------+------+-----+---------+-------+
| Field           | Type            | Null | Key | Default | Extra |
+-----------------+-----------------+------+-----+---------+-------+
| Host            | char(60) binary |      | PRI |         |       |
| Db              | char(64) binary |      | PRI |         |       |
| User            | char(16) binary |      | PRI |         |       |
| Select_priv     | enum('N','Y')   |      |     | N       |       |
| Insert_priv     | enum('N','Y')   |      |     | N       |       |
| Update_priv     | enum('N','Y')   |      |     | N       |       |
| Delete_priv     | enum('N','Y')   |      |     | N       |       |
| Create_priv     | enum('N','Y')   |      |     | N       |       |
| Drop_priv       | enum('N','Y')   |      |     | N       |       |
| Grant_priv      | enum('N','Y')   |      |     | N       |       |
| References_priv | enum('N','Y')   |      |     | N       |       |
| Index_priv      | enum('N','Y')   |      |     | N       |       |
| Alter_priv      | enum('N','Y')   |      |     | N       |       |
+-----------------+-----------------+------+-----+---------+-------+
13 rows in set (0.04 sec)
 
위에서 설명한  Mysql사용시 필수적인 명령어
명령어
설명
status;
현재 사용중인 데이터베이스와 서버의 정보를 보여준다.
show databases;
어떤 데이터베이스가 있는지 보여준다.
show tables from DB이름;
어떤 데이터베이스에 어떤 테이블이 있는지를 보여준다.
show tables;
어떤 테이블이 있는지를 보여준다.
show columns from Table이름;
어떤 테이블에 어떤 컬럼이 있는지 보여준다.
describe Table이름
어떤 테이블에 어떤 컬럼이 있는지 보여준다.(스키마 정보를 보여준다.)
 
MySQL 기본
 
데이터베이스 생성/삭제
 
- 데이터베이스 생성
create database 데이터베이스 이름;
 
- 데이터베이스 삭제
drop database 데이터베이스 이름;
 
mysql> create database mosq;
Query OK, 1 row affected (0.00 sec)
mysql> use mosq;
Database changed
 
 - 특정 사용자에게 데이터베이스 소유권 주기
grant 권한 on 데이터베이스 to 사용자ID@접속위치 iendtified by '비밀번호'
mysql> grant all on mosq.* to brume@'%' identified by 'brumepass'
brumepass라는 비밀번호를 가진 brume라는 사용자에게 mosq데이터 베이스에 모든 권한들 부여한다.
* 자세한 옵션 설명은 다음 버전의 문서에서 기대하시길 우선 여기까지.
 
테이블 생성/삭제
 
 - 테이블 생성
create table 테이블이름(
    컬럼명 타입 제한조건,
    컬럼명 타입 제한조건
)[테이블 옵션];
 
- 테이블 삭제
drop table 테이블이름;
 
mysql> create table orders (  // orders 테이블 생성
    -> id integer not null auto_increment,  // 정수타입, null값 허용안함, 데이터가 추가될 때 자동으로 증가
    -> cid varchar(8) not null, // 8자리 문자, null값 허용안함
    -> primary key (id) // id를 중복 불가능한 값으로 지정
    -> );
Query OK, 0 rows affected (0.00 sec)
 
* 파일에 있는 SQL문 사용하기 *
mysql> source /home/brume/orders.sql
 
 - 테이블에 값 넣기
insert into 테이블이름(추가할 컬럼항목) values (값,값...)
mysql> insert into orders values('','brume');
Query OK, 1 row affected (0.00 sec)
mysql> insert into orders values('','sepiru');
Query OK, 1 row affected (0.01 sec)
mysql> insert into orders values('','mosq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into orders values('','ryan');
Query OK, 1 row affected (0.00 sec)
mysql> select * from orders;
+----+--------+
| id | cid    |
+----+--------+
|  1 | brume  |
|  2 | sepiru |
|  3 | mosq   |
|  4 | ryan   |
+----+--------+
4 rows in set (0.00 sec)
 
컬럼타입
 - 불연형(참 또는 거짓의 두가지 형태를 저장) bool
mysql> create table typetest (
    -> invalue varchar(10),
    -> booltype bool
    -> );
Query OK, 0 rows affected (0.01 sec)
mysql> insert into typetest values ( 'true' , 1 );
Query OK, 1 row affected (0.01 sec)
mysql> insert into typetest values ( 'false' , 0 );
Query OK, 1 row affected (0.00 sec)
mysql> insert into typetest values ( 'false' , 'f' );
Query OK, 1 row affected (0.00 sec)
mysql> insert into typetest values ( 'false' , 't' );
Query OK, 1 row affected (0.00 sec)
mysql> insert into typetest values ( 'true' , 2 );
Query OK, 1 row affected (0.00 sec)
mysql> select * from typetest;
+---------+----------+
| invalue | booltype |
+---------+----------+
| true    |        1 |
| false   |        0 |
| false   |        0 |
| false   |        0 |
| true    |        2 |
+---------+----------+
5 rows in set (0.00 sec)
 - 숫자형
데이터타입
설명
저장공간
tinyint
0 ~ 255정수
1byte
smallint
-32768 ~ 32767정수 unsigned 붙이면 0 ~ 65535
2byte
mediumint
-8388608 ~ 8388607정수 0 ~ 16777215
3byte
int, integer
-2147483648 ~ 2147483647정수 unsigned시 0 ~ 4294967295
4byte
bigint
-9223372036854775808 ~ 9223372036854775807
unsigned시 0 ~ 188446744073709551615
8byte
float
-3.402823466E+38 에서-1.175494351E-38 사이의값과 0,
1.175494351E-38 에서 3.402823466E+38 의 부동소수점 숫자
4byte
double, real
찾아봐.. 숫자 치기 귀찮다.
8byte
decimal(m,d), numeric(m,d)
숫자를 스트링으로 저장하고 unsigned옵션 사용불가.
m자리수를 사용해서 숫자만을 저장하고 소수점 뒤에 d개의 
숫자가 있게 된다. float와 달리 정확한 값을 저장할 수 
있지만 효율적인 데이터 타입은 아니므로 신중히 사용
상황에따라
 
 - 문자형 : char와 varchar은 SQL표준이고 tinytext, mediumtext, longtext는 mysql전용
데이터 타입
설명
char
한 글자의 글자를 입력할 수 있다.
char(n)
n개의 고정 길이 문자열을 입력할 수 있다.
varchar(n)
n개의 가변 길이 문자열을 입력할 수 있다.
tinytext
255개의 문자열을 입력할 수 있다. varchar(255)와 유사하다.
mediumtext
65635개의 문자열을 입력할 수 있다.
longtext
2E+32 -1개의 문자열을 입력할 수 있다.
 
SQL로 데이터 접근하기
 - select : 데이터베이스에서 데이터를 선택할때
select 컬럼리스트 from 테이블리스트 [as 테이블 별칭] [where 필터링 조건] [group by 컬럼명] [order by 컬럼명]
 
 
- insert : 테이블에 데이터를 넣을때
insert into 테이블이름[(컬럼이름,..)] values (값,..)
 
 
- update : 기존의 데이터를 수정할 때
update 테이블이름 set 컬럼이름 = expr1, [컬럼이름 = expr2,...] [where 조건]
 
 
- delete : 특정행을 삭제할 때 
delete from 테이블이름 [where 조건]

TrackBack

TrackBack URL for this entry:
http://yongsang.com/blog/mt-tb.cgi/231

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on 2004年08月04日 21:23.

The previous post in this blog was STYLE SHEET 위치제어.

The next post in this blog is 강해져라.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.31