Database/SQL

[MySQL] DB, Table, Data, Column ์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ

Ella_K 2022. 9. 26. 22:20

โœ… DATABASE

๐Ÿ“Œ DB ์ƒ์„ฑ

create database <db ๋ช…> default character set utf8mb4
default collate utf8mab4_general_ci;

create database <db ๋ช…>;

 

๐Ÿ“Œ DB ์กฐํšŒ

show databases;

 

๐Ÿ“Œ RENAME TABLE ์„ ์ด์šฉํ•œ DB ์ด๋ฆ„ ๋ณ€๊ฒฝ

select concat('rename table ',table_schema,'.',table_name,' to ','์ƒˆDB๋ช….',table_name,';')
from information_schema.tables
where table_schema like '๊ธฐ์กดDB๋ช…';

 

๐Ÿ“Œ DB ์‚ญ์ œ

drop database <db๋ช…>;

 

โœ… TABLE

๐Ÿ“Œ TABLE ์ƒ์„ฑ

create table <table๋ช…> (
id bigint primary key auto_increment,
name varchar(50) not null,
age int)
default character set utf8mb4 collate utf8mb4_general_ci;

 

๐Ÿ“Œ ์‹œํ€€์Šค

  • auto_increment: row ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์ž๋™์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ๊ฐ’์ด ์ €์žฅ๋จ 

 

๐Ÿ“Œ ์ œ์•ฝ ์กฐ๊ฑด

  • primary key
์นผ๋Ÿผ์— ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์—†์œผ๋ฉฐ null ๊ฐ’์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ.
๊ฐ ํ–‰(๋ฐ์ดํ„ฐ)์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•œ ์œ ์ผํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์ปฌ๋Ÿผ์— ์‚ฌ์šฉ
๊ธฐ๋ณธํ‚ค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
  • foreign key
์ง€์ •๋œ ํ…Œ์ด๋ธ”์˜ ๊ธฐ๋ณธํ‚ค ์ปฌ๋Ÿผ์˜ ๊ฐ’๋งŒ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ.
์ฐธ์กฐํ‚ค, ์™ธ๋ž˜ํ‚ค๋ผ๊ณ  ๋ถ€๋ฆ„.
null ๊ฐ’์„ ํ—ˆ์šฉํ•จ.
  • not null
์ปฌ๋Ÿผ์— null ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ฟผ๋ฆฌ๋ฌธ์„ ํ†ตํ•ด ๋ฐ˜๋“œ์‹œ ๊ฐ’์ด ์ง€์ •๋˜์–ด์•ผ ํ•œ๋‹ค.
  • unique
์ปฌ๋Ÿผ์— ์ค‘๋ณต๋œ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์—†๋‹ค.
null ๊ฐ’์€ ํ—ˆ์šฉ(null ๊ฐ’์€ ์ค‘๋ณต์œผ๋กœ ์ €์žฅ๋  ์ˆ˜ ์žˆ์Œ)
create table <table ๋ช…> (
id int,
data1 int not null,
unique (data1)
);
  • default
null์ด ๋“ค์–ด์˜ฌ ๊ฒฝ์šฐ ๊ธฐ๋ณธ์œผ๋กœ ์„ค์ •๋˜๋Š” ๊ฐ’์„ ์ง€์ •.
default๋ฅผ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์ปฌ๋Ÿผ์— null์„ ์ €์žฅํ•  ์ˆ˜ ์—†์Œ.
create table test_table6 (
id int,
data1 int not null,
data2 varchar not null default 'person'
);

 

๐Ÿ“Œ TABLE ์กฐํšŒ

show tables;

 

๐Ÿ“Œ TABLE ์ด๋ฆ„ ๋ณ€๊ฒฝ

rename table <db ๋ช…>.<๊ธฐ์กด table ๋ช…> to <db ๋ช…>.<๋ณ€๊ฒฝํ•  table ๋ช…>;

 

๐Ÿ“Œ TABLE ์‚ญ์ œ

drop table naver_wiki;

 

โœ… DATA

๐Ÿ“Œ ๋ฐ์ดํ„ฐ ์ €์žฅ

insert into <table ๋ช…> <column ๋ช…> values <๊ฐ’>;

 

๐Ÿ“Œ ๋ฐ์ดํ„ฐ ์ˆ˜์ •

update <table ๋ช…> set <column ๋ช…>=<๊ฐ’> where <์กฐ๊ฑด>

 

๐Ÿ“Œ ๋ฐ์ดํ„ฐ ๋ชจ๋‘ ์‚ญ์ œ(ํ…Œ์ด๋ธ”์ด ์‚ญ์ œ๋˜๋Š” ๊ฒƒ ์•„๋‹˜)

delete from <table ๋ช…> where <์กฐ๊ฑด>

 

โœ… COLUMN

๐Ÿ“Œ ์ปฌ๋Ÿผ๋ช… ์ˆ˜์ •

alter table <table ๋ช…> change <๊ธฐ์กด column ๋ช…> <์ƒˆ๋กœ์šด column ๋ช…> <์ž๋ฃŒํ˜•>;

 

๐Ÿ“Œ ์ปฌ๋Ÿผ์˜ ์ž๋ฃŒํ˜• ์ˆ˜์ •

alter table <table ๋ช…> modify <column ๋ช…> <์ž๋ฃŒํ˜•>

 

๐Ÿ“Œ ์ƒˆ๋กœ์šด ์ปฌ๋Ÿผ ์ƒ์„ฑ

alter table <table ๋ช…> add <column ๋ช…> <์ž๋ฃŒํ˜•>;

 

๐Ÿ“Œ ํŠน์ • ์ปฌ๋Ÿผ ์‚ญ์ œ

alter table <table ๋ช…> drop <column ๋ช…>;

 


Source

https://ryean.tistory.com/41

 

[MySQL] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ด๋ฆ„ ๋ณ€๊ฒฝ (RENAME DATABASE)

MySQL ์—์„œ๋Š” ๋ฒ„์ „ 5.1.7 ์—์„œ RENAME DATABASE ๊ตฌ๋ฌธ์ด ์ถ”๊ฐ€๋˜์—ˆ์œผ๋‚˜, database ์˜ ๋‚ด์šฉ์ด ์†์‹ค๋  ์œ„ํ—˜์„ฑ์ด ๋ฐœ๊ฒฌ์ด ๋˜์–ด ๋ฒ„์ „ 5.1.23 ์ดํ›„๋ถ€ํ„ฐ ์ œ๊ฑฐ๋๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ž˜์„œ database ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์ƒˆ๋กœ์šด datab

ryean.tistory.com

https://velog.io/@bmoyeon/MySQL-%EC%83%9D%EC%84%B1-%EC%A1%B0%ED%9A%8C-%EC%88%98%EC%A0%95-%EC%82%AD%EC%A0%9C

 

[DB] MySQL - ์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ

create, select, update, alter, delete, drop

velog.io