본문 바로가기

About Security/PHP & MYSQL

mysql 컬럼,자료형 변경 12월 8일 수요일

1.컬럼의 자료형 변경
alter table 테이블명 modify 컬럼명 변경할 자료형
ex)alter table dtype2 modify c1 varchar(100);

2.컬럼명 변경
alter table 테이블명 change 컬럼명 변경할컬럼명 변경할자료형
ex)alter table dtype2 change c1 c10 varchar(100);


3.삭제
drop table 테이블명


*************데이터 제어*************
1.데이터 삽입(insert)

1.1 insert into 테이블명(컬럼명,...) values(값

c:\ mysql -u root -papmsetup examdb < c:\php\exam2.sql
 => examdb라는 db에 exam2라는 테이블을 만들어서 name,age,tel이란 컬럼명을 만들어준다.

========================exam2.sql========================
create table exam2(
name varchar(15),
age int,
tel varchar(11))
========================exam2.sql========================

insert into exam2(name,age,tel) values('jungho' ,'30', '01012345678);
=>name,age,tel이란 컬럼에 각각의 데이터를 삽입한다.

1.2 insert into 테이블명(컬럼명,...) values(값,...),values(값,...)..
insert into 테이블명(컬럼명....) values(값,...),(값....),(값...);
ex)insert into exam2 values('park',35,'01012345678'),('lee',15,'234234234');
=>한꺼번에 여러가지의 데이터를 넣는법

1.3 text파일의 데이터를 테이블에 넣을때
=>LOAD DATA LOCAL INFILE 'c:/php/xls_exam2.txt' INTO TABLE exam2;
1.4 다른 테이블의 데이터를 insert할때
insert into 테이블B(컬럼명...) select 컬럼명.. from 테이블A;
ex) mysql> insert into exam2(name,age,tel) select name,age,tel from tmp_exam2;


2.데이터 조회(select)

1.1 select 컬럼명,... from 테이블명 
mysql> select age, name,tel from exam2;

번외: select age, name, tel as phone from exam2;
as라는 명령어를 통하여 컬럼명을 바꿔줄수있다. tel-> phone

1.2 테이블의 데이터를 파일로 옮기고자 할때 
select 컬럼,.. into outfile '파일명' from 테이블;
ex) mysql> select * into outfile 'c:/php/select_outfile_exam2.txt' from exam2;

1.3 조건절
연산자: >, <, =, >=,<=, and, or, like, is null, in, between
mysql> select * from employee where dept_no = '010';
=> employee라는 테이블에서 010으로 시작하는것을 뽑아라.

mysql> select * from employee where pay > 3000;
=> employee라는 테이블에서 연봉 3000이상되는사람을 뽑아라.

mysql> select * from employee where dept_no = '010' and pay > 3000 or job='사원'

mysql> select saname from employee where saname like '이%';

=>employee라는 테이블에서 saname컬럼에서 '이'로 시작되는 모든것을 뽑아내라.

mysql> select address from employee where address like '%APT%';
=>APT라는 단어가 들어가는것을 뽑아라.

================================================================
mysql> SELECT saname,job FROM employee where job in ('사장','과장','대리','부장'); 
mysql> SELECT saname,job FROM employee where job ='사장' or job='과장' or job='대리';
================================================================
mysql> select saname,job from employee where job<>'사장';
=사장이 아닌 모든사람을 뽑아라

================================================================
mysql> select saname,job,pay from employee where pay >=2000 and pay<=3000;
=
mysql> select saname,job,pay from employee where pay between 2000 and 3000;
연봉이 2000~3000사이인 사람을 뽑아라.
================================================================