#10 Nomalization
#10.1 Normalizing Status
-- status ๋นผ๊ธฐ
CREATE TABLE statuses (
status_id BIGINT UNSIGNED PRIMARY KEY auto_increment,
status_name ENUM (
'Canceled',
'In Production',
'Planned',
'Post Production',
'Released',
'Rumored'
) NOT NULL,
explanation TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
)
INSERT INTO statuses (status_name) SELECT status from movies GROUP BY status;
-- value ๋์ select ๋ฌธ ์ฌ์ฉ ๊ฐ๋ฅ
-- status_id ์์ฑ ํ ์ฐ๊ฒฐ
ALTER TABLE movies ADD COLUMN status_id BIGINT UNSIGNED;
ALTER TABLE movies ADD CONSTRAINT fk_status FOREIGN KEY (status_id) REFERENCES statuses (status_id) ON DELETE SET NULL;
-- ๊ธฐ์กด ๋ฐ์ดํฐ ๊ธฐ์ค์ผ๋ก id ๊ฐ ์ ์ฉ
UPDATE movies SET status_id = (SELECT status_id FROM statuses WHERE status_name = movies.status);
-- ๊ธฐ์กด status ์ ๊ฑฐ
ALTER TABLE movies DROP COLUMN status;#10.2 Normalizing Directors
#10.3 Normalizing Original Language
#10.4 Normalizing Countries
#10.5 Unions
#10.6 Conclusions
Last updated
Was this helpful?