728x90
Snowflake❄️의 Data Warehousing Workshop을 진행하면서 작업한 내용을 정리하고자 합니다.
Badge 1: 데이터 웨어하우징 워크샵 - KR
이번 내용은 Lesson 7: Data Storage Structures 과정을 진행하면서 작성한 내용입니다.
🥋 Create the Library Card Catalog DB, Author Table & a Sequence
🥋 Create a New Database and Table
use role sysadmin;
// Create a new database and set the context to use the new database
CREATE DATABASE LIBRARY_CARD_CATALOG COMMENT = 'DWW Lesson 9 ';
USE DATABASE LIBRARY_CARD_CATALOG;
// Create and Author table
CREATE OR REPLACE TABLE AUTHOR (
AUTHOR_UID NUMBER
,FIRST_NAME VARCHAR(50)
,MIDDLE_NAME VARCHAR(50)
,LAST_NAME VARCHAR(50)
);
// Insert the first two authors into the Author table
INSERT INTO AUTHOR(AUTHOR_UID,FIRST_NAME,MIDDLE_NAME, LAST_NAME)
Values
(1, 'Fiona', '','Macdonald')
,(2, 'Gian','Paulo','Faleschini');
// Look at your table with it's new rows
SELECT *
FROM AUTHOR;
🥋 Create a Sequence and Query It A Few Times
classic console을 사용해 Sequence 만들기
NAME : SEQ_AUTHOR_UID
select SEQ_AUTHOR_UID.nextval, SEQ_AUTHOR_UID.nextval;
🥋 Reset the Sequence & Add Rows to the Author Table
🥋 Reset the Sequence then Add Rows to Author
시퀀스를 3으로 시작하게 재설정 후 Author에 행을 추가합니다.
use role sysadmin;
//Drop and recreate the counter (sequence) so that it starts at 3
// then we'll add the other author records to our author table
CREATE OR REPLACE SEQUENCE "LIBRARY_CARD_CATALOG"."PUBLIC"."SEQ_AUTHOR_UID"
START 3
INCREMENT 1
COMMENT = 'Use this to fill in the AUTHOR_UID every time you add a row';
//Add the remaining author records and use the nextval function instead
//of putting in the numbers
INSERT INTO AUTHOR(AUTHOR_UID,FIRST_NAME,MIDDLE_NAME, LAST_NAME)
Values
(SEQ_AUTHOR_UID.nextval, 'Laura', 'K','Egendorf')
,(SEQ_AUTHOR_UID.nextval, 'Jan', '','Grover')
,(SEQ_AUTHOR_UID.nextval, 'Jennifer', '','Clapp')
,(SEQ_AUTHOR_UID.nextval, 'Kathleen', '','Petelinsek');
🥋 The NextVal Function
AUTHOR_UID
를 하드코딩 하지않고도 자동으로 시퀀스가 생성된 것을 볼 수 있습니다.
🥋 Create and Fill the Books Table and Mapping Table
🥋 Create a 2nd Counter, a Book Table, and a Mapping Table
USE DATABASE LIBRARY_CARD_CATALOG;
// Create a new sequence, this one will be a counter for the book table
CREATE OR REPLACE SEQUENCE "LIBRARY_CARD_CATALOG"."PUBLIC"."SEQ_BOOK_UID"
START 1
INCREMENT 1
COMMENT = 'Use this to fill in the BOOK_UID everytime you add a row';
// Create the book table and use the NEXTVAL as the
// default value each time a row is added to the table
CREATE OR REPLACE TABLE BOOK
( BOOK_UID NUMBER DEFAULT SEQ_BOOK_UID.nextval
,TITLE VARCHAR(50)
,YEAR_PUBLISHED NUMBER(4,0)
);
// Insert records into the book table
// You don't have to list anything for the
// BOOK_UID field because the default setting
// will take care of it for you
INSERT INTO BOOK(TITLE,YEAR_PUBLISHED)
VALUES
('Food',2001)
,('Food',2006)
,('Food',2008)
,('Food',2016)
,('Food',2015);
// Create the relationships table
// this is sometimes called a "Many-to-Many table"
CREATE TABLE BOOK_TO_AUTHOR
( BOOK_UID NUMBER
,AUTHOR_UID NUMBER
);
//Insert rows of the known relationships
INSERT INTO BOOK_TO_AUTHOR(BOOK_UID,AUTHOR_UID)
VALUES
(1,1) // This row links the 2001 book to Fiona Macdonald
,(1,2) // This row links the 2001 book to Gian Paulo Faleschini
,(2,3) // Links 2006 book to Laura K Egendorf
,(3,4) // Links 2008 book to Jan Grover
,(4,5) // Links 2016 book to Jennifer Clapp
,(5,6);// Links 2015 book to Kathleen Petelinsek
//Check your work by joining the 3 tables together
//You should get 1 row for every author
select *
from book_to_author ba
join author a
on ba.author_uid = a.author_uid
join book b
on b.book_uid=ba.book_uid;
💯 Lesson 7: Data Storage Structures 완료!
728x90