MySQL 명령어

김병현·2022년 4월 4일
0

MySQL

목록 보기
1/1

SQL

Structured Query Language. 관계형 데이터베이스 관리 시스템의 데이터를 관리(데이터 생성, 읽기, 수정, 삭제)하기 위해 설계된 특수 목적의 프로그래밍 언어

Database Server > Schema(=Database) > Table 포괄적인 구조를 나타냄
Schema와 같은 의미를 나타내는 Database는 포괄적인 의미의 Database를 의미하는 것이 아님

Table은 row(행)과 column(열)로 구성되어 있음


MySQL

  • 명령어 마지막 뒤에 항상 ; 를 작성할 것

실행 (Database Server 접속)

mysql -uroot -p;
# mysql -u(계정명) -p;
# -u : user, -p : password
# root : 최고 관리자
 
password 입력창이 활성화되면 입력 후 Enter

비밀번호 변경

set password = password('000000');
# set password = password('새로운 비밀번호');

Schema 생성

create database example;
# create database (생성하고자 하는 Schema 이름);

Schema 편집 사용

use example;
# use (편집하고자 하는 Schema 이름);

Schema 목록

show databases;

Schema 삭제

drop database example;
# drop database (삭제하고자 하는 Schema 이름);

Table and Column 생성

create table example(
	-> id INT(11) NOT NULL auto_increment,
    -> title VARCHAR(100) NOT NULL,
    -> description TEXT NULL,
    -> PRIMARY KEY(id));
   
# create table (생성하고자 하는 Table 이름)(
	-> (Column 이름) (Data Type)(데이터 노출 한도 수) NOT NULL auto_increment,
    -> (Column 이름) (Data Type)(텍스트 입력 제한) NOT NULL
    -> (Column 이름) (Data Type) Null
    -> PRIMARY KEY({Column 이름}));
    
# NOT NULL : 빈 값 허용 X. 필수적으로 데이터를 요청
# NULL : 빈 값 허용
# auto_increment : row가 증가하면 해당 column의 데이터 값이 타입에 알맞게 순차적으로 증가된 값을 자동으로 입력 설정
	# => ex. 행이 증가하면 id 열의 데이터들이 자동적으로 1, 2, 3 ··· 과 같이 증가
# PRIMARY KEY(Column 이름)) : 해당 Column의 값들은 서로 중복되지 않게 설정

Table 확인

show tables;

Table 구조 확인

DESC example;

# DESC (table 이름);

Table 이름 수정

rename table example to example_backup;

# rename table (기존 table 이름) to (수정할 table 이름);

Row 생성

insert into example(title, description, created, author, rofile)
	-> values("MySQL", "MySQL is ...", now(), "man", "developer");
    
# insert into (table 이름)(column1 이름, column2 이름 ···)
	-> values(column1 값, column2 값 ···);
    
# now() : 현재 시간 자동 입력

Table Data 전체 확인

select * from example;

# select * from (table 이름);

Table Data 선택적으로 일부 확인

select id, title, author from example;	# 원하는 column만 확인

# select (확인하려는 column), (확인하려는 column) ··· from (table 이름)

select id, title, author from example where author="man";

# select (확인하려는 column), (확인하려는 column) ··· from (table 이름) where (필터 적용할 column) = "(필터 기준 값)";
# where : 필터 기능

select id, title, author from example where author="man" order by id desc;

# select (확인하려는 column), (확인하려는 column) ··· from (table 이름) where (필터 적용할 column) = "(필터 기준 값)" order by (정렬 기준 column) (desc or asc);
# order by : 정렬 기능
# desc(내림차순), asc(오름차순)

select id, title, author from example where author="man" order by id desc limit 2;

# select (확인하려는 column), (확인하려는 column) ··· from (table 이름) where (필터 적용할 column) = "(필터 기준 값)" order by (정렬 기준 column) (desc or asc) limit (제한 row 수);
# limit : 출력 데이터 제한 기능

Table Data Update

update example set
	-> title = "ORACLE" where id = 2;
    
# update (table 이름) set
	-> (변경할 데이터 column 이름) = "(변경할 데이터)" where (검색 column) = (검색 column);

!! where 매우매우 중요 !!

Table Data Delete

delete from example
	-> where id = 6;
    
# delete from (table 이름)    
	-> where (삭제할 데이터 column 이름) = (삭제할 데이터 column);
    
!! where 매우매우 중요 !!
profile
Without haste, but without rest.

0개의 댓글