이클립스에 worknech에서 작성한 SQL 쿼리문 저장할 폴더 만들기

1. 이클립스에서 저장할 폴더 만들기

new - project - maven project - create a simple project (skip archetype selection) 체크
group id : com.douzone
artifact id : mariadb-practices
packaging : pom
finish

mariadb-practices 프로젝트 안 src삭제 (필요없음)

저장할 폴더 2개만들기

mariadb-practices 우클릭 - new - 폴더
폴더 이름 :
1. sql연습
2. 연습문제 (앞으로 과제 저장할 위치)

gitignore 생성 (깃 연동위해)

mariadb-practices 우클릭 - new - 파일 - 파일이름 : .gitignore
.gitignore 내용
**/.classpath
**/.project
**/.settings
**/.target
**/.build

README.md 생성

mariadb-practices 우클릭 - new - 파일 - 파일이름 : README.md

깃 연동

1. 깃 홈피이지에서 레포지토리 생성

이름 : mariadb-practices
깃 링크 복사하기

2. 연동하기

mariadb-practices 우클릭 - Team - Share project - Use or create repository in parent folder of project(워크벤치 sql문서 저장할 위치) - 밑에 create Repository 클릭 - Finish

깃 Perspective에서 test01의 Remotes 우클릭 - Create Remotes - Configure fetch - Create - (깃 링크 클립보드에 저장된상태에서)Change 클릭 - finish - save and Fetch
문서 깃 스테이징하고 푸쉬하면 깃에 올라가있음

워크벤치 사용법

원하는 행동을 명시하는 라인에서 Ctrl + Enter
ex)
create table pet( // 테이블생성
select name, owner, species, gender, birth, death from pet; // 조회

워크벤치 쿼리문 작성하기

커넥션된 webdb 계정 열기 - 밑의 쿼리문 작성하기

사전에 해야할 것

1.에러) * sql 문 실행도중에 Key 에러

Edit - Preferences - Sql Editor - 맨밑에 Safe Updates (rejects UPDATEs and DELETEs with no restrictions) 체크해제

2.에러) 연결권한 설정하기

워크벤치 메인화면 - MySQL Connections의 webdb 우클릭 - Edit Connection - Advanced - Others:의 맨위에 OPT_LOCAL_INFILE=1 추가
권한 허용 후에 OPT_LOCAL_INFILE=1 지우고 껐다 켜도 잘 실행됨
webdb 계정 연결을 해제하고 다시 연결하려면 권한 설정 다시해줘야함

3. 쿼리문에서 임포트할 데이터 다운받기

더존 구글 드라이브 - 자료 - MySQL - pet.txt 다운받기 - C드라이브에 넣기

-- Basic Query						// -- : 주석

drop table pet;		

create table pet(					//테이블 생성하고싶을때 커서올리고 Ctrl Enter
   name varchar(20),				//컬럼지정
   owner varchar(20),
   species varchar(20),				//varchar은 가변적	(네임같은것에 쓰임)

   gender char(1),					//char 고정적	(주민번호같은것에 쓰임)
   birth DATE,
   death DATE
);

-- 조회								
select name, owner, species, gender, birth, death from pet;

-- 데이터 넣기(생성, create)
insert
  into pet
 value ('성탄이', '안대혁', 'dog', 'm', '2018-12-25', null);

-- 데이터 삭제(delete)
delete
  from pet
 where name = '성탄이';

-- load data local infile
load data local infile 'C:\\pet.txt' into table pet;

--- update death
update pet 
  set death = null
  where name != 'Bowser';

-- 조회연습1: where
-- 1990년 이후에 태어난 아이들은?
select name, species, birth
  from pet
 where birth > '1990-12-31';
 
 --- Gwen과 함께 사는 아이들은 ?
 select name, species, owner
  from pet
 where owner = 'Gwen';
 
 --- null 다루기 1 : 살아있는 애들은?
 select name, birth, death
   from pet 
 where death is null;
 
 --- null 다루기 2 : 죽은 애들은?
 select name, birth, death
   from pet 
 where death is not null;
 
 -- like 검색(패턴 매칭) : 이름이 b로 시작하는 아이들은?
 select name from pet where name like 'b%';
 
 -- like 검색(패턴 매칭) : 이름이 b로 시작하는 아이들중에 이름이 6자인 아이는?
 select name from pet where name like 'b_____';
 
 -- 집계(통계) 함수 
 select count(*) from pet;

 
 select count(death) from pet;
 select count(*) from pet where death is not null;

저장

File - Save script as - 폴더 경로 찾기
폴더경로 : eclips workspace - mariadb-practices- sql 연습에저장

불러오기

커넥션된 webdb 계정 열기
File - open sql script - 저장햇던위치의 sql 문서 열기

백업된 db데이터(employees) workbench로 가져오기

1. 연습용으로 백업된 데이터 파일 다운받기

더존 구글 드라이브 - 자료 - MySQL - employees_db.zip 다운로드받기 - C드라이브에 옮김

2. sftp로 서버에 zip파일 옮기기

환경 : xshell
[c:~]$ cd c:\
[C:]$ sftp webmaster@127.0.0.1
sftp : put employees_db.zip
[C:]$ open
[webmaster@lx ~]$ ls -l
[webmaster@lx ~]$ su -
[root@lx ~]# mv /home/webmaster/employees_db.zip .
[root@lx ~]# ls -l

3. xsehll에 unzip 기능 설치하기

환경 : xshell
[root@lx ~]# yum -y install unzip
[root@lx ~]# unzip employees_db.zip

4. db데이터 workbench로 가져오기

환경 : xshell
새 세션
[c:~]$ open
[webmaster@lx ~]$ su -
[root@lx ~]# mysql -p
create user 'hr'@'10.0.2.2' identified by 'hr';

MariaDB [(none)]> create database employees;
MariaDB [(none)]> create user 'hr'@'10.0.2.2' identified by 'hr';
MariaDB [(none)]> grant all privileges on employees.* to 'hr'@'10.0.2.2';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit

[root@lx ~]# cd employees_db
[root@lx employees_db]# mysql -p < employees.sql
Enter password: root 비밀번호

4. 생성된 hr 계정 workbench에 연결하기

워크벤치 메인화면에 MySQL Connections + 클릭

  • Connection Name(메인화면 보여주기식닉네임) : hr
  • Hostname : 127.0.0.1 - Port : 3306
  • Username : hr
  • Default Schema : hr
    Test Connection - xshell에서 지정했던 비밀번호 hr 입력후 확인
    워크벤치에서 hr 계정 접속해서
    빈문서에
    desc employees; //데이터베이스스키마확인
    select count(*) from employees; //집계확인
    Ctrl Enter 치면 확인가능

0개의 댓글