본문 바로가기

About Security/PHP & MYSQL

mysql table 생성,수정 & 자료형 12월 7일

*테이블 생성/수정/삭제

1.테이블생성 
create table 테이블명(컬럼명 자료형, 컬럼명 자료형, ..)
ex:) create table exam1 (name varchar(10), tel varchar(30));
=========================dtype1.sql=========================
create table dype1(
c1 tinyint,
c2 tinyint(3),
c3 tinyint signed,
c4 tinyint unsigned);
=========================dtype1.sql=========================

C:\ mysql -u root -papmsetup examdb < c:\php\dytpe1.sql (첨부했음)
==>mysql에 접속하지않고 db에 table까지 생성

C:\ mysql -u root -pampsetup

mysql> show databases;

mysql> use examdb;

mysql> show tables;

mysql> desc dtype1;

mysql> insert into dtype1 valuses('128','128','255','255');

mysql> select * from dtype1;

mysql> alter table dtype1 modify c2 tinyint(2);

mysql> create table dtype2(c1 int(2), c2 int(2) zerofill);  *zerofill: 남은 자릿수를 0으로 채운다

mysql> insert into dtype2 values('1','1');

mysql> select * from dtype2;

=========================dtype4.sql=========================
create table dtype4(
c1 double(5,2),
c2 real(5,2));
=========================dtype4.sql=========================

C:\ mysql -u root -papmsetup examdb < c:\php\dytpe4.sql (첨부했음)==>mysql에 접속하지않고 db에 table까지 생성


#실수형 double옆에 (5,2)라는 숫자의 의미

⑤라는 숫자의 의미는 소수점을 포함한 자릿수.
②라는 숫자의 의미는 소주점의 자릿수.

즉. 1234.56이라고 하면 총 6자릿수 이지만 지금 double(5,2)라고 했으니  5자리에서 나타낼수있는 값인
999.99라는 최대값을 나타내준다.

=========================dtype6.sql=========================
create table dtype6(
c1 enum('Y', 'N'),
c2 set('Y','N'))
=========================dtype6.sql=========================
C:\ mysql -u root -papmsetup examdb < c:\php\dytpe6.sql (첨부했음)==>mysql에 접속하지않고 db에 table까지 생성
mysql 접속

mysql> insert into dtype6 values('1','1');

mysql> select * from dtype6;


mysql> insert into dtype6 values('2','2');

mysql> select * from dtype6;


mysql> insert into dtype6 values('3','3');

mysql> select * from dtype6;

ENUM문자열 저장된 문자열 목록중에 오직 한가지만

SET 문자열 저장된 문자열 목록 중에 0,1개 이상을 얻을수 있다.

=========================dtype7.sql=========================
create table dtype7(
c1 date,
c2 datetime,
c3 timestamp)
=========================dtype7.sql=========================

C:\ mysql -u root -papmsetup examdb < c:\php\dytpe7.sql (첨부했음)==>mysql에 접속하지않고 db에 table까지 생성


mysql > select now(); => 현재 서버의 date를 보여준다.

mysql > insert into dtype7 values(now(),now(), unix_timestamp());

mysql> select * from dtype7;

mysql> insert into dtype7(c1,c2) values(now(),now());

2.테이블 수정 
=> alter table 테이블명 [add/drop/rename/change/modify] [패턴]

2.1 컬럼추가
alter table 테이블명 add 컬럼명 자료형;

2.2 컬럼삭제
alter table 테이블명 drop 컬럼명;

2.3 테이블명 변경
alter table 테이블명 rename 변경할테이블명;