0% found this document useful (0 votes)
19 views3 pages

Message

Uploaded by

Jalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Message

Uploaded by

Jalu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

```sql

CREATE TABLE "users" (


"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"email" varchar,
"password" varchar,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp DEFAULT 'now()'
);

CREATE TABLE "books" (


"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"title" varchar,
"author" varchar,
"year" integer,
"user_id" integer,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp DEFAULT 'now()'
);

CREATE TABLE "genres" (


"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"title" varchar,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp DEFAULT 'now()'
);

CREATE TABLE "genre_books" (


"id" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"genre_id" integer,
"book_id" integer,
"created_at" timestamp DEFAULT 'now()',
"updated_at" timestamp DEFAULT 'now()'
);

ALTER TABLE "genre_books" ADD FOREIGN KEY ("book_id") REFERENCES "books" ("id");

ALTER TABLE "genre_books" ADD FOREIGN KEY ("genre_id") REFERENCES "genres" ("id");

ALTER TABLE "books" ADD FOREIGN KEY ("user_id") REFERENCES "users" ("id");

-- INSERT DATA KE DATABASE

INSERT INTO users(email, password)


VALUES
('[email protected]',
'$2a$12$yqSdKKUGCnp9F7x5J13lgeHd31CSw1Ym4KccnngRbW9VcejvTnvfS'),
('[email protected]',
'$2a$12$tTyTMBhuFTEXBRvhhuUql.PR3qlHhGNwRkH53NqnRbLPPYNnMZRra');
INSERT INTO books(title, author, year, user_id)
VALUES
('Red Rising', 'Pierce Brown', 2014, 1),
('Morning Star', 'Pierce Brown', 2016, 2),
('The Two Towers', 'Tolkien', 2001, 1),
('Steve Jobs', 'Walter Isaacson', 2011, 1);

INSERT INTO books(title, author, year, user_id)


VALUES
('Genghis Khan', 'Jack Weatherford', 2004, 2),
('SPQR', 'Mary Beard', 2015, 1),
('The Shining', 'Stephen King', 1977, 1),
('Pet Sematary', 'Stephen King', 1983, 1);

SELECT * FROM users;

SELECT * FROM books;

SELECT
users.id AS id,
users.email AS email,
users.created_at AS created_at,
users.updated_at AS updated_at,
books.title AS title,
books.author AS author,
books.year AS year
FROM users
INNER JOIN books
ON users.id = books.user_id
WHERE
books.user_id = 2;

INSERT INTO genres(title)


VALUES
('Biography'), -- 1
('History'), -- 2
('Science Fiction'), -- 3
('Drama'), -- 4
('Horror'), -- 5
('Action'); -- 6

SELECT * FROM genres;

SELECT * FROM books;

INSERT INTO genre_books(genre_id, book_id)


VALUES
(3, 1),
(6, 1),
(3, 2),
(6, 2),
(3, 3),
(6, 3),
(1, 4),
(2, 5),
(2, 6),
(5, 7),
(5, 8);

SELECT
books.id AS id,
books.title AS title,
books.author AS author,
books.year AS year,
books.user_id AS user_id,

genres.title AS genre,
books.created_at AS created_at,
books.updated_at AS updated_at
FROM books
INNER JOIN genre_books
ON books.id = genre_books.book_id
INNER JOIN genres
ON genres.id = genre_books.genre_id;

SELECT
*
FROM genres
INNER JOIN genre_books
ON genres.id = genre_books.genre_id;

UPDATE books
SET title = 'Golden Son',
year = 2015,
updated_at = NOW()
WHERE
id = 1;

UPDATE books
SET user_id = 2,
updated_at = NOW();

DELETE FROM books


WHERE id = 5;

DELETE FROM genre_books


WHERE id = 8;
```

You might also like