1. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑํ๊ณ ์ฌ์ฉํ๊ธฐ (create) (MySQL๊ณผ ๋์ผ)
create database product_db;
use product_db;
2. ํ ์ด๋ธ ๋ง๋ค๊ธฐ (create)
ํ
์ด๋ธ์ ์์ฑํ ๋๋
์ด์ด๋ฆ๊ณผ ๋ฐ์ดํฐํ์
nullํ์ฉ์ฌ๋ถ (PK);
๋ก ์ ์ด์ฃผ๋ฉด ๋๋ค.
SQL Server๋ ์ ๋์ฝ๋๊ฐ ํฌํจ๋ nchar, nvarchar ์ฌ์ฉ์ ๊ถ์ฅํ๋ค.
create table product (
product_name nvarchar(20) not null primary key,
product_price int not null,
product_made date not null,
product_company nvarchar(20),
product_num int
);
๋ง์ฝ ์ด์ ์ด๋ฆ์ ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด
sp_rename 'ํ
์ด๋ธ์ด๋ฆ.์์ ์ ์ด์ด๋ฆ', 'ํ
์ด๋ธ์ด๋ฆ.์์ ํ์ด์ด๋ฆ', 'column';
์ด์ ๋ฐ์ดํฐํ์
์ ๋ณ๊ฒฝํ๊ณ ์ถ๋ค๋ฉด
alter table ํ
์ด๋ธ๋ช
alter column ์ด์ด๋ฆ ์๋ก์ด๋ฐ์ดํฐํ์
;
๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
sp_rename 'product.product_num', 'product_number', 'column';
alter table product alter column product_num tinyint;
3. ํ ์ด๋ธ์ ๋ฐ์ดํฐ ์ ๋ ฅ/์์ /์ญ์ ํ๊ธฐ (insert into / delete from / update)
MySQL๊ณผ ๋ช
๋ น๋ฌธ์ ๊ฐ์ง๋ง nvar, nchar ํ์
์๋ ์์ N์ ์ ์ด์ค์ผํ๋ค.
๋ฐ์ดํฐ ์
๋ ฅ์ ๋ค์๊ณผ ๊ฐ๋ค.
insert into ํ
์ด๋ธ์ด๋ฆ values (๋ฐ์ดํฐ);
insert into product values(N'๋ฐ๋๋', 1500, '2023-04-07', N'๋ธ๋ชฌํธ', 17);
insert into product values(N'์ฐธ์น๋ง์ ์ผ๊ฐ๊น๋ฐฅ', 900, '2023-04-06', '', 3);
insert into product values(N'์ง๋ผ๋ฉด ๋งค์ด๋ง', 600, '2023-03-20', N'์ค๋๊ธฐ', 37);
insert into product values(N'ํฌ์ผ๋ชฌ์คํฐ ์คํฐ์ปค', 500, '20230402', '', 1);
์
๋ ฅํ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ์ถ๋ค๋ฉด
delete from ํ
์ด๋ธ์ด๋ฆ where PK๊ฐ;
์ ์ฌ์ฉํด์ฃผ๋ฉด ๋๋๋ฐ, PK๊ฐ์ ์ฌ์ฉํด์ผ ์ํ๋ ๋ฐ์ดํฐ๊ฐ ๋ฑ ํ๋๋ง ์ญ์ ๋๋ค.
delete from product where product_name='ํฌ์ผ๋ชฌ์คํฐ ์คํฐ์ปค';
์
๋ ฅํ ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ณ ์ถ๋ค๋ฉด
update ํ
์ด๋ธ์ด๋ฆ set ์์ ํ๊ณ ์ถ์์ด์ด๋ฆ='์์ ํ๋ฐ์ดํฐ' where ์์ ํ๊ณ ์ถ์์ด์ด๋ฆ='์์ ์ ๋ฐ์ดํฐ';
update product set product_number=35 where product_number='37';
create database product_db;
use product_db;
create table product (
product_name nvarchar(20) not null primary key,
product_price int not null,
product_made date not null,
product_company nvarchar(20),
product_num int
);
alter table product alter column product_num tinyint not null;
sp_rename 'product.product_num', 'product_number', 'column';
drop table product;
insert into product values(N'๋ฐ๋๋', 1500, '2023-04-07', N'๋ธ๋ชฌํธ', 17);
insert into product values(N'์ฐธ์น๋ง์ ์ผ๊ฐ๊น๋ฐฅ', 900, '2023-04-06', '', 3);
insert into product values(N'์ง๋ผ๋ฉด ๋งค์ด๋ง', 600, '2023-03-20', N'์ค๋๊ธฐ', 37);
insert into product values(N'ํฌ์ผ๋ชฌ์คํฐ ์คํฐ์ปค', 500, '20230402', '', 1);
select len('์ฐธ์น๋ง์ ์ผ๊ฐ๊น๋ฐฅ');
select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='product';
select * from product;
'๐ฅ๏ธ > DBMS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[MySQL] ํ ์ด๋ธ ๋ง๋ค๊ธฐ (0) | 2023.04.10 |
---|---|
DB์ค๊ณ) ํ์ ํ ์ด๋ธ๊ณผ ์ฑ์ ํ ์ด๋ธ ์ค๊ณํ๊ธฐ (0) | 2023.04.09 |
SQL ๋ฐ์ดํฐ ํ์ ์ ๋ฆฌ (0) | 2023.04.07 |
DB์ค๊ณ) ์ง์ ํ ์ด๋ธ๊ณผ ๊ธ์ฌ ํ ์ด๋ธ ์ค๊ณํ๊ธฐ (0) | 2023.04.07 |
[MariaDB] Windows 10์ MariaDB ์ค์นํ๊ณ ์ธ๋ถ ์ ์ํ๊ธฐ(VMVirtualBox ์ฌ์ฉ) (0) | 2023.04.06 |