SQL 기본 명령어

알파로그·2023년 3월 12일
0

Database

목록 보기
8/19

✏️ Setting 관련 명령어

  • 모든 DB 리스팅
show databases;
  • DB 생성
create database {db};
  • DB 삭제
drop database {db};

# 조건 걸기
drop database if exists {db};
  • DB 선택
use {db};
  • DB 내의 모든 Table 리스팅
show tables;
  • Table 내의 Column 의 상세 설정 확인
desc {table};

✏️ CRUD

📍 Create

  • Table 생성
create table {table}(
    id int auto_increment,
    primary key(id),
    ...
);
  • Column 옵션
not null - null 불가
unsigned - 음수 불가
unique - 중복값 불가
auto_increment - 자동 값 증가
as '별칭' - column 별칭 설정
PRIMARY KEY - UNIQUE + NOT NULL

📍 Read

  • Table 조회
select * from {table};
  • 특정 column 조회
select {column} from {table};
  • 특정 row 조회
select * from {table}
where {column} = '...'
and {column} = 0
or {column} > 10;
  • 오름차순 (ASC) , 내림차순 (DESC)
select * from {table}
order by {column} desc;
  • like
    • 홍길 ~ 인 row 가 조회됨
select * from {table}
where {column} like '홍길%';

📍 update

  • Column 추가
    • after : 새로운 column 위치 정해주기
alter table {table}
add column {column} 속성;
after {column};
  • Column 명 수정
alter table {table}
change column {기존이름} {새로운이름} 속성;
  • Column 속성 수정
alter table {table}
modify column {column} 속성;
  • Column 삭제
alter table {table}
drop column {column};

  • row 수정
update {table}
set {column} = {변경될 값}
where {조건 걸기};

📍 row 삭제

delete from {talbe} where id = 1;

✏️ 정리

DB 생성 : create database
DB 삭제 : drop database

TB 생성 : create table
TB 삭제 : drop table
TB 수정 : alter table

데이터 생성 : INSERT
데이터 조회 : SELECT
데이터 수정 : UPDATE
데이터 삭제 : DELETE
profile
잘못된 내용 PR 환영

0개의 댓글