MySQL | 시작

파과·2022년 8월 1일
0

MySQL

목록 보기
2/2

MySQL 기본

show databases;

실행해 DB목록 확인.

여기서는 root계정으로 작업한다.


DB 생성

company라는 db를 만든다.

create database company;

schemas 탭에서 새로고침해 company가 뜨는지 확인.

use company;

위 코드를 통해 company db를 사용할 수 있는 상태가 된다.

select database();

현재 사용중인 db 정보를 확인할 수 있다.


주석

한 줄은 #
여러 줄은 /* */로 표기한다.


테이블 생성

default character set utf8 collate utf8_general_ci;
MySQL에서는 테이블 생성시 끝에 위 문구를 붙여줘야 한글 깨짐을 방지할 수 있다.

MySQL에서는 sequence 대신 auto_increment를 사용한다.

create table products(
	id int auto_increment primary key,
    name varchar(50) not null,
    modelnumber varchar(15) not null,
    series varchar(30) not null
) default character set utf8 collate utf8_general_ci;

테이블에 데이터 삽입

insert into products(name, modelnumber, series)
values('Eric Clapton Stratocaster', '0117602806', 'Artist');

insert into products(name, modelnumber, series)
values('American Deluxe Stratocaster', '011900', 'American Deluxe'),
	('American Deluxe Tele', '011950', 'American Deluxe'),
    ('Jeff Back Stratocaster', '0119600805', 'Artist');

데이터 조회

select * from products;

select * from products where series='Artist';

select * from products where modelnumber like '0119%';

MySQL에만 있는 기능 limit : 인덱스 2부터 2개 데이터 조회하기

select * from products limit 2,2;

select * from products order by id asc limit 2;

테이블, 데이터베이스 삭제

drop table products;
drop database company;

0개의 댓글