#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?