MySQL: 첫 실습

#cd /usr/bin // mysql 명령어가 있는 디렉토리로 이동 합니다.

#mysql -u root -p mysql // mysql 접속

mysql -u 아이디 -p 패스워드

위와 같은 명령으로 접속

clip_image001

 

shell> mysql –host=127.0.0.1
shell> mysql –protocol=TCP

The –protocol option enables you to establish a particular type of connection even when the other options would normally default to some other protocol.

If the server is configured to accept IPv6 connections, clients can connect over IPv6 using –host=::1. See Section 5.1.8, IPv6 Support.

On Windows, you can force a MySQL client to use a named-pipe connection by specifying the –pipe or –protocol=PIPE option, or by specifying . (period) as the host name. If named-pipe connections are not enabled, an error occurs. Use the –socket option to specify the name of the pipe if you do not want to use the default pipe name.

Connections to remote servers always use TCP/IP. This command connects to the server running on remote.example.com using the default port number (3306):

貼り付け元  <https://dev.mysql.com/doc/refman/5.5/en/connecting.html>

MySQL 사용하기

 

clip_image002

mysql -u root // root 로그인

 

clip_image003

show databases; // 데이터베이스 목록 보기

 

clip_image004

use mysql; // 지정한 데이터베이스 사용

 

clip_image005

show tables; // 테이블 목록 보기

 

clip_image006

select * from db; // db 테이블의 모든 데이터 리턴

clip_image007

create database test1; // 데이터베이스 만들기

테이블 만들기

CREATE TABLE 테이블명(

속성명 자료형(길이) 옵션 옵션…,

속성명 자료형(길이) 옵션 옵션…,

속성명 자료형(길이) 옵션 옵션…,

속성명 자료형(길이) 옵션 옵션…,

속성명 자료형(길이) 옵션 옵션…);

 

 

실무에서는 내가 사용했던 쿼리문을 별도 파일 보관해놔야 한다.

 

clip_image008

CREATE TABLE goodsinfo(

code CHAR(5) PRIMARY KEY NOT NULL,

name VARCHAR(20) NOT NULL,

price INT(8),

maker VARCHAR(20));

  • PRIMARY KEY(기본 )
    • 테이블의 속성 입력된 레코드를 중복되지 않게 식별할 있는 속성에 지정한다.
    • 중복되거나 공백이 없음.

 

clip_image009

CREATE TABLE customer (

id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,

name VARCHAR(20) NOT NULL,

age INT NOT NULL,

tel VARCHAR(20) NOT NULL,

bloodtype VARCHAR(2),

gender CHAR(1));

 

INSERT INTO customer VALUES (NULL, ‘GolDongHong’, 20, ‘010-8888-9999’, ‘AB’, ‘F’);

INSERT INTO customer VALUES (NULL, ‘GooGleGoo’, 30, ‘010-7777-9999’, ‘B’, ‘F’);

INSERT INTO customer VALUES (NULL, ‘YaHooYa’, 30, ‘010-6666-9999’, ‘O’, ‘M’);

INSERT INTO customer VALUES (NULL, ‘MinJungKim’, 30, ‘010-5555-9999’, ‘A’, ‘F’);

INSERT INTO customer VALUES (NULL, ‘DoSukKwon’, 30, ‘010-4444-9999’, ‘B’, ‘M’);

INSERT INTO customer VALUES (NULL, ‘JongTakPark’, 30, ‘010-3333-9999’, ‘O’, ‘F’);

INSERT INTO customer VALUES (NULL, ‘JunSooPark’, 30, ‘010-2222-9999’, ‘AB’, ‘M’);

INSERT INTO customer VALUES (NULL, ‘KwangSooKim’, 30, ‘010-1111-9999’, ‘A’, ‘F’);

 

테이블의 구조를 변경

ALTER TABLE 테이블명 ADD COLUMN 속성명 자료형; // 칸을 추가

ALTER TABLE 테이블명 DROP COLUMN 속성명; // 제거

ALTER TABLE 테이블명 CHANGE COLUMN 기존속성명 바꿀속성명 자료형; // 속성이나 자료형을 바꿀

ALTER TABLE 테이블명 MODIFY 속성명 자료형; // 자료형만 바꿀 (자료형이 짧을 많이 사용한다고 )

 

clip_image010

ALTER TABLE goodsinfo ADD COLUMN point int;

ALTER TABLE goodsinfo CHANGE COLUMN name pname VARCHAR(20);

ALTER TABLE goodsinfo MODIFY pname VARCHAR(25);

 

테이블의 삭제

DROP TABLE 테이블명;

 

레코드의 입력

INSERT INTO 테이블명(속성명,속성명,..) VALUES (,,…);

 

INSERT INTO goodsinfo(code,pname,price,maker) VALUES (‘A0001’, ‘SM-9123’, 1150000, ‘SAMSUNG’);

(6개의 속성 5개만 입력할 때는 위와 같이 괄호속 어떤 자료들을 넣는지 입력해줘야 한다.)

 

clip_image011

INSERT INTO goodsinfo VALUES (‘A0001’, ‘SM-9123’, 1150000, ‘SAMSUNG’, 10);

모든 속성을 입력할 때에는 테이블명 괄호 생략 가능

 

clip_image012

대충 값을 아무 것이나 넣었다.

 

clip_image013

INSERT INTO customer VALUES (NULL, ‘YuMinSang’, 30, ‘010-9999-9999’, ‘A’, ‘M’);

(첫번째 값은 AUTO_INCREMENT 이므로 NULL 넣는다.)

 

clip_image014

대충 값을 넣었음.

 

레코드 수정

UPDATE 테이블명 SET 속성명= WHERE 조건식;

  • 조건식을 지정하지 앟을 경우 모든 레코드의 속성이 변경됨(주의!!)

 

UPDATE goodsinfo SET point=price/10000; // WHERE절이 없기 때문에 모든 속성의 값이 바뀜

clip_image015

모든 point 값이 price/10000으로 바뀜.

UPDATE customer SET age=35 WHERE id=3;

clip_image016

id 3 사용자가 35살로 바뀜.

 

레코드 삭제

DELETE FROM 테이블명 WHERE 조건식;

(이것도 마찬가지로 조건식을 주지 않으면 모든 레코드가 삭제되니 주의!)

 

레코드 검색

SELECT 속성명, 속성명,… FROM 테이블명 WHERE 조건식 GROUP BY 속성명 HAVING 조건식 ORDER BY 속성명, 속성명 DESC or ASC;

 

clip_image017

select name,age FROM customer;

 

clip_image018

SELECT code,pname FROM goodsinfo;

 

clip_image019

DISTINCT 중복 제거

clip_image020

SELECT * FROM customer WHERE age=30;

 

clip_image021

 SELECT * FROM customer WHERE age=30 AND gender=’M’; // 30세이고 남성만

 

clip_image022

SELECT * FROM customer WHERE age=30 OR gender=’M’; // 30 또는 여성을 검색할

 

clip_image023

SELECT * FROM customer WHERE bloodtype IN(‘A’,’B’); // 혈액형이 A이거나 B 자료

 

clip_image024

SELECT * FROM customer WHERE name LIKE ‘Y%’; // 이름이 Y 시작하는 자료(첫글자가 대문자 Y 시작하는 모든 문자)

clip_image025

SELECT * FROM customer WHERE name LIKE ‘%y%’; // 이름에 y 들어간 사람

clip_image026

SELECT * FROM customer WHERE name LIKE ‘_a%’; // 두번째 문자가 a 사람

clip_image027

SELECT * FROM customer WHERE age BETWEEN 30 AND 50; // 나이가 30~50 사이인 자료

clip_image028

SELECT COUNT(*) FROM customer; // 해당 테이블에 입력된 레코드의 개수를 보여줌

 

clip_image029

SELECT * FROM customer ORDER BY age DESC; // 나이순으로 정렬(내림차순)

 

clip_image030

SELECT * FROM customer ORDER BY name, age; // 이름순으로 정렬하되 같은 이름이 있으면 나이순으로

 

SELECT 이용한 테이블의 생성(내용복사)

 

clip_image031

 CREATE TABLE man AS SELECT * FROM CUSTOMER WHERE gender=’M’; // customer 테이블에서 남자만 뽑아 내용으로 man 테이블을 생성

 

 

 

 

 

 

 

 

이것도 살펴보세요!

Vue3: 프로젝트 폴더/파일 구성, ESLint

새로 만든 Vue 프로젝트의 폴더/파일 구성 폴더/파일내용.vscodeVisual Studio용 설정 파일dist배포용 파일 세트를 저장node_modules라이브러리를 저장public웹으로 공개할 …

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다