โ 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
[MySQL] ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ ๋ณ๊ฒฝ (RENAME DATABASE)
MySQL ์์๋ ๋ฒ์ 5.1.7 ์์ RENAME DATABASE ๊ตฌ๋ฌธ์ด ์ถ๊ฐ๋์์ผ๋, database ์ ๋ด์ฉ์ด ์์ค๋ ์ํ์ฑ์ด ๋ฐ๊ฒฌ์ด ๋์ด ๋ฒ์ 5.1.23 ์ดํ๋ถํฐ ์ ๊ฑฐ๋๋ค๊ณ ํ๋ค. ๊ทธ๋์ database ์ด๋ฆ์ ๋ณ๊ฒฝํ๋ ค๋ฉด ์๋ก์ด datab
ryean.tistory.com
[DB] MySQL - ์์ฑ, ์กฐํ, ์์ , ์ญ์
create, select, update, alter, delete, drop
velog.io
'Database > SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] MySQL ๋ช ๋ น์ด (0) | 2022.10.18 |
---|---|
[MySQL] ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ธ๋ฑ์ค (0) | 2022.10.08 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Subquery, With, ๋ฌธ์์ด, Case (0) | 2022.07.16 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Join, Union (0) | 2022.07.16 |
[MySQL] [์คํ๋ฅดํ์ฝ๋ฉํด๋ฝ SQL] Group by, Order by (0) | 2022.07.09 |