├── README.md └── postgresql.sql /README.md: -------------------------------------------------------------------------------- 1 | # belajar-postgresql-dasar 2 | Belajar PostgreSQL Dasar 3 | -------------------------------------------------------------------------------- /postgresql.sql: -------------------------------------------------------------------------------- 1 | select * 2 | from pg_tables 3 | where schemaname = 'public'; 4 | 5 | create table barang 6 | ( 7 | kode int not null, 8 | name varchar(100) not null, 9 | harga int not null default 1000, 10 | jumlah int not null default 0, 11 | waktu_dibuat TIMESTAMP not null default current_timestamp 12 | ); 13 | 14 | alter table barang 15 | add column deskripsi text; 16 | 17 | alter table barang 18 | drop column deskripsi; 19 | 20 | alter table barang 21 | rename column name to nama; 22 | 23 | truncate barang; 24 | 25 | drop table barang; 26 | 27 | create table products 28 | ( 29 | id varchar(10) not null, 30 | name varchar(100) not null, 31 | description text, 32 | price int not null, 33 | quantity int not null default 0, 34 | created_at timestamp not null default current_timestamp 35 | ); 36 | 37 | insert into products(id, name, price, quantity) 38 | values ('P0001', 'Mie Ayam Original', 15000, 100); 39 | 40 | insert into products(id, name, description, price, quantity) 41 | values ('P0002', 'Mie Ayam Bakso Tahu', 'Mie Ayam Original + Bakso Tahu', 20000, 100); 42 | 43 | insert into products(id, name, price, quantity) 44 | values ('P0003', 'Mie Ayam Ceker', 20000, 100), 45 | ('P0004', 'Mie Ayam Spesial', 25000, 100), 46 | ('P0005', 'Mie Ayam Yamin', 15000, 100); 47 | 48 | select * 49 | from products; 50 | 51 | select id, name, price, quantity 52 | from products; 53 | 54 | alter table products 55 | add primary key (id); 56 | 57 | select id, name, price, quantity 58 | from products 59 | where quantity = 0; 60 | 61 | select id, name, price, quantity 62 | from products 63 | where price = 20000; 64 | 65 | select id, name, price, quantity 66 | from products 67 | where id = 'P0004'; 68 | 69 | select * 70 | from products; 71 | 72 | create type PRODUCT_CATEGORY as enum ('Makanan', 'Minuman', 'Lain-Lain'); 73 | 74 | alter table products 75 | add column category PRODUCT_CATEGORY; 76 | 77 | select * 78 | from products; 79 | 80 | update products 81 | set category = 'Makanan' 82 | where id = 'P0001'; 83 | 84 | update products 85 | set category = 'Makanan' 86 | where id = 'P0002'; 87 | 88 | update products 89 | set category = 'Makanan' 90 | where id = 'P0003'; 91 | 92 | update products 93 | set category = 'Makanan' 94 | where id = 'P0004'; 95 | 96 | update products 97 | set category = 'Makanan' 98 | where id = 'P0005'; 99 | 100 | update products 101 | set category = 'Makanan', 102 | description = 'Mie Ayam + Ceker' 103 | where id = 'P0003'; 104 | 105 | SELECT * 106 | from products; 107 | 108 | update products 109 | set price = price + 5000 110 | where id = 'P0004'; 111 | 112 | insert into products(id, name, price, quantity, category) 113 | values ('P0009', 'Contoh', 10000, 100, 'Minuman'); 114 | 115 | delete 116 | from products 117 | where id = 'P0009'; 118 | 119 | select id as "Kode Barang", price as "Harga Barang", description as "Deskripsi Barang" 120 | from products; 121 | 122 | select p.id as "Kode Barang", 123 | p.price as "Harga Barang", 124 | p.description as "Deskripsi Barang" 125 | from products as p; 126 | 127 | select * 128 | from products 129 | where price > 15000; 130 | 131 | select * 132 | from products 133 | where price <= 15000; 134 | 135 | select * 136 | from products 137 | where category != 'Minuman'; 138 | 139 | select * 140 | from products 141 | where price > 15000 142 | and category = 'Makanan'; 143 | 144 | SELECT * 145 | FROM products; 146 | 147 | insert into products(id, name, price, quantity, category) 148 | values ('P0006', 'Es teh tawar', 10000, 100, 'Minuman'), 149 | ('P0007', 'Es Campur', 20000, 100, 'Minuman'), 150 | ('P0008', 'Just Jeruk', 15000, 100, 'Minuman'); 151 | 152 | select * 153 | from products 154 | where price > 15000 155 | or category = 'Makanan'; 156 | 157 | select * 158 | from products 159 | where quantity > 100 160 | OR category = 'Makanan' and price > 15000; 161 | 162 | select * 163 | from products 164 | where category = 'Makanan' 165 | or (quantity > 100 and price > 15000); 166 | 167 | select * 168 | from products 169 | where name ilike '%es%'; 170 | 171 | select * 172 | from products 173 | where description is null; 174 | 175 | select * 176 | from products 177 | where description is not null; 178 | 179 | select * 180 | from products 181 | where price between 10000 and 20000; 182 | 183 | select * 184 | from products 185 | where price not between 10000 and 20000; 186 | 187 | select * 188 | from products 189 | where category in ('Makanan', 'Minuman'); 190 | 191 | select * 192 | from products 193 | order by price asc, id desc; 194 | 195 | select * 196 | from products 197 | where price > 0 198 | order by price asc, id desc; 199 | 200 | select * 201 | from products 202 | where price > 0 203 | order by price asc, id desc 204 | limit 2; 205 | 206 | -- 1 limit 2 offset 0, 2 limit 2 offset 2, 3 limit 2 offset 4 207 | 208 | select * 209 | from products 210 | where price > 0 211 | order by price asc, id desc 212 | limit 2 offset 2; 213 | 214 | select category 215 | from products; 216 | 217 | select distinct category 218 | from products; 219 | 220 | select 10 + 10 as hasil; 221 | 222 | select id, name, price / 1000 as price_in_k 223 | from products; 224 | 225 | select pi(); 226 | 227 | select power(10, 2); 228 | 229 | select cos(10), sin(10), tan(10); 230 | 231 | select id, name, power(quantity, 2) as quantity_power_2 232 | from products; 233 | 234 | create table admin 235 | ( 236 | id serial not null, 237 | first_name varchar(100) not null, 238 | last_name varchar(100), 239 | primary key (id) 240 | ); 241 | 242 | insert into admin(first_name, last_name) 243 | values ('Eko', 'Khannedy'), 244 | ('Budi', 'Nugraha'), 245 | ('Joko', 'Morro'); 246 | 247 | select * 248 | from admin; 249 | 250 | select currval('admin_id_seq'); 251 | 252 | create sequence contoh_sequence; 253 | 254 | select nextval('contoh_sequence'); 255 | 256 | select currval('contoh_sequence'); 257 | 258 | select id, name, description 259 | from products; 260 | 261 | select id, lower(name), length(name), lower(description) 262 | from products; 263 | 264 | select * 265 | from products; 266 | 267 | select id, extract(year from created_at), extract(month from created_at) 268 | from products; 269 | 270 | select id, category 271 | from products; 272 | 273 | select id, 274 | category, 275 | case category 276 | when 'Makanan' then 'Enak' 277 | when 'Minuman' then 'Seger' 278 | else 'Apa itu?' 279 | end as category_case 280 | from products; 281 | 282 | select id, 283 | price, 284 | case 285 | when price <= 15000 then 'Murah' 286 | when price <= 20000 then 'Mahal' 287 | else 'Mahal Banget' 288 | end as "apakah murah?" 289 | from products; 290 | 291 | select id, 292 | name, 293 | case 294 | when description is null then 'kosong' 295 | else description 296 | end as description 297 | from products; 298 | 299 | select count(id) 300 | from products; 301 | 302 | select avg(price) 303 | from products; 304 | 305 | select max(price) 306 | from products; 307 | 308 | select min(price) 309 | from products; 310 | 311 | select category, count(id) as "Total Product" 312 | from products 313 | group by category; 314 | 315 | select category, 316 | avg(price) as "Rata Rata Harga", 317 | min(price) as "Harga termurah", 318 | max(price) as "Harga termahal" 319 | from products 320 | group by category; 321 | 322 | select category, count(id) as "Total Product" 323 | from products 324 | group by category 325 | having count(id) > 3; 326 | 327 | select category, 328 | avg(price) as "Rata Rata Harga", 329 | min(price) as "Harga termurah", 330 | max(price) as "Harga termahal" 331 | from products 332 | group by category 333 | having avg(price) >= 20000; 334 | 335 | create table customer 336 | ( 337 | id serial not null, 338 | email varchar(100) not null, 339 | first_name varchar(100) not null, 340 | last_name varchar(100), 341 | primary key (id), 342 | constraint unique_email unique (email) 343 | ); 344 | 345 | select * 346 | from customer; 347 | 348 | insert into customer(email, first_name, last_name) 349 | values ('eko@pzn.com', 'Eko', 'Khannedy'); 350 | 351 | insert into customer(email, first_name, last_name) 352 | values ('budi@pzn.com', 'Budi', 'Nugraha'), 353 | ('joko@pzn.com', 'Joko', 'Morro'), 354 | ('rully@pzn.com', 'Rully', 'Irwansyah'); 355 | 356 | alter table customer 357 | drop constraint unique_email; 358 | 359 | alter table customer 360 | add constraint unique_email unique (email); 361 | 362 | alter table products 363 | add constraint price_check check ( price > 1000 ); 364 | 365 | alter table products 366 | add constraint quantity_check check ( quantity >= 0 ); 367 | 368 | insert into products(id, name, price, quantity, category) 369 | values ('XXX1', 'Contoh Gagal', 10, 0, 'Minuman'); 370 | 371 | insert into products(id, name, price, quantity, category) 372 | values ('XXX1', 'Contoh Gagal', 10000, -10, 'Minuman'); 373 | 374 | select * 375 | from products; 376 | 377 | create table sellers 378 | ( 379 | id serial not null, 380 | name varchar(100) not null, 381 | email varchar(100) not null, 382 | primary key (id), 383 | constraint email_unique unique (email) 384 | ); 385 | 386 | insert into sellers(name, email) 387 | values ('Galeri Olahraga', 'galeri@pzn.com'), 388 | ('Toko Tono', 'tono@pzn.com'), 389 | ('Toko Budi', 'budi@pzn.com'), 390 | ('Toko Rully', 'rully@pzn.com'); 391 | 392 | SELECT * 393 | From sellers; 394 | 395 | create index sellers_id_and_name_index ON sellers (id, name); 396 | create index sellers_email_and_name_index ON sellers (email, name); 397 | create index sellers_name_index ON sellers (name); 398 | 399 | select * 400 | from sellers 401 | where id = 1; 402 | select * 403 | from sellers 404 | where id = 1 405 | or name = 'Toko Tono'; 406 | select * 407 | from sellers 408 | where email = 'rully@pzn.com' 409 | or name = 'Toko Tono'; 410 | select * 411 | from sellers 412 | where name = 'Toko Tono'; 413 | 414 | select * 415 | from products 416 | where name ilike '%mie%'; 417 | 418 | select * 419 | from products 420 | where to_tsvector(name) @@ to_tsquery('mie'); 421 | 422 | select cfgname 423 | from pg_ts_config; 424 | 425 | create index products_name_search on products using gin (to_tsvector('indonesian', name)); 426 | create index products_description_search on products using gin (to_tsvector('indonesian', description)); 427 | 428 | select * 429 | from products 430 | where name @@ to_tsquery('ayam & tahu'); 431 | select * 432 | from products 433 | where description @@ to_tsquery('mie'); 434 | 435 | create table wishlist 436 | ( 437 | id serial not null, 438 | id_product varchar(10) not null, 439 | description text, 440 | primary key (id), 441 | constraint fk_wishlist_product foreign key (id_product) references products (id) 442 | ); 443 | 444 | insert into wishlist(id_product, description) 445 | values ('P0001', 'Mie ayam kesukaan'), 446 | ('P0002', 'Mie ayam kesukaan'), 447 | ('P0005', 'Mie ayam kesukaan'); 448 | 449 | SELECT * 450 | FROM wishlist; 451 | 452 | delete 453 | from products 454 | where id = 'P0005'; 455 | 456 | alter table wishlist 457 | drop constraint fk_wishlist_product; 458 | 459 | alter table wishlist 460 | add constraint fk_wishlist_product foreign key (id_product) references products (id) 461 | on delete cascade on update cascade; 462 | 463 | insert into products(id, name, price, quantity, category) 464 | values ('XXX', 'Xxx', 10000, 100, 'Minuman'); 465 | 466 | SELECT * 467 | FROM products; 468 | insert into wishlist(id_product, description) 469 | values ('XXX', 'Contoh'); 470 | 471 | select * 472 | from wishlist; 473 | 474 | delete 475 | from products 476 | where id = 'XXX'; 477 | 478 | select * 479 | from wishlist 480 | join products on wishlist.id_product = products.id; 481 | 482 | select p.id, p.name, w.description 483 | from wishlist as w 484 | join products as p on w.id_product = p.id; 485 | 486 | alter table wishlist 487 | add column id_customer int; 488 | 489 | alter table wishlist 490 | add constraint fk_wishlist_customer foreign key (id_customer) references customer (id); 491 | 492 | update wishlist 493 | set id_customer = 1 494 | where id in (2, 3); 495 | 496 | update wishlist 497 | set id_customer = 4 498 | where id = 4; 499 | 500 | select * 501 | From customer; 502 | 503 | select * 504 | from wishlist; 505 | 506 | select c.email, p.id, p.name, w.description 507 | from wishlist as w 508 | join products as p on w.id_product = p.id 509 | join customer as c on c.id = w.id_customer; 510 | 511 | create table wallet 512 | ( 513 | id serial not null, 514 | id_customer int not null, 515 | balance int not null default 0, 516 | primary key (id), 517 | constraint wallet_customer_unique unique (id_customer), 518 | constraint fk_wallet_customer foreign key (id_customer) references customer (id) 519 | ); 520 | 521 | select * 522 | from customer; 523 | 524 | insert into wallet(id_customer, balance) 525 | values (1, 1000000), 526 | (4, 2000000), 527 | (5, 3000000), 528 | (6, 4000000); 529 | 530 | select * 531 | from wallet; 532 | 533 | select * 534 | from customer 535 | join wallet on wallet.id_customer = customer.id; 536 | 537 | create table categories 538 | ( 539 | id varchar(10) not null, 540 | name varchar(100) not null, 541 | primary key (id) 542 | ); 543 | 544 | insert into categories(id, name) 545 | values ('C0001', 'Makanan'), 546 | ('C0002', 'Minuman'); 547 | 548 | select * 549 | from categories; 550 | 551 | alter table products 552 | add column id_category varchar(10); 553 | 554 | alter table products 555 | add constraint fk_product_category foreign key (id_category) references categories (id); 556 | 557 | select * 558 | from products; 559 | 560 | update products 561 | set id_category = 'C0001' 562 | where category = 'Makanan'; 563 | 564 | update products 565 | set id_category = 'C0002' 566 | where category = 'Minuman'; 567 | 568 | alter table products 569 | drop column category; 570 | 571 | select * 572 | from products 573 | join categories on products.id_category = categories.id; 574 | 575 | create table orders 576 | ( 577 | id serial not null, 578 | total int not null, 579 | order_date timestamp not null default current_timestamp, 580 | primary key (id) 581 | ); 582 | 583 | create table orders_detail 584 | ( 585 | id_product varchar(10) not null, 586 | id_order int not null, 587 | price int not null, 588 | quantity int not null, 589 | primary key (id_product, id_order) 590 | ); 591 | 592 | alter table orders_detail 593 | add constraint fk_orders_detail_product foreign key (id_product) references products (id); 594 | 595 | alter table orders_detail 596 | add constraint fk_orders_detail_order foreign key (id_order) references orders (id); 597 | 598 | insert into orders(total) 599 | values (1), 600 | (1), 601 | (1); 602 | 603 | select * 604 | from orders; 605 | 606 | select * 607 | from products 608 | order by id; 609 | 610 | insert into orders_detail (id_product, id_order, price, quantity) 611 | values ('P0001', 1, 1000, 2), 612 | ('P0002', 1, 1000, 2), 613 | ('P0003', 1, 1000, 2); 614 | 615 | insert into orders_detail (id_product, id_order, price, quantity) 616 | values ('P0004', 2, 1000, 2), 617 | ('P0006', 2, 1000, 2), 618 | ('P0007', 2, 1000, 2); 619 | 620 | insert into orders_detail (id_product, id_order, price, quantity) 621 | values ('P0001', 3, 1000, 2), 622 | ('P0004', 3, 1000, 2), 623 | ('P0005', 3, 1000, 2); 624 | 625 | select * 626 | from orders_detail; 627 | 628 | select * 629 | from orders 630 | join orders_detail on orders_detail.id_order = orders.id 631 | join products on orders_detail.id_product = products.id; 632 | 633 | 634 | select * 635 | from orders 636 | join orders_detail on orders_detail.id_order = orders.id 637 | join products on orders_detail.id_product = products.id 638 | where orders.id = 3; 639 | 640 | insert into categories (id, name) 641 | VALUES ('C0003', 'Gadget'), 642 | ('C0004', 'Laptop'), 643 | ('C0005', 'Pulsa'); 644 | 645 | select * 646 | from categories; 647 | 648 | select * 649 | from products; 650 | 651 | insert into products(id, name, price, quantity) 652 | values ('X0001', 'Contoh 1', 10000, 100), 653 | ('X0002', 'Contoh 2', 10000, 100); 654 | 655 | select * 656 | from categories 657 | inner join products on products.id_category = categories.id; 658 | 659 | select * 660 | from categories 661 | left join products on products.id_category = categories.id; 662 | 663 | select * 664 | from categories 665 | right join products on products.id_category = categories.id; 666 | 667 | select * 668 | from categories 669 | full join products on products.id_category = categories.id; 670 | 671 | select avg(price) 672 | from products; 673 | 674 | select * 675 | from products 676 | where price > (select avg(price) from products); 677 | 678 | select * 679 | from products; 680 | 681 | select max(price) 682 | from (select products.price as price 683 | from categories 684 | join products on products.id_category = categories.id) as contoh; 685 | 686 | create table guestbooks 687 | ( 688 | id serial not null, 689 | email varchar(100) not null, 690 | title varchar(100) not null, 691 | content text, 692 | primary key (id) 693 | ); 694 | 695 | select * 696 | from customer; 697 | 698 | insert into guestbooks(email, title, content) 699 | values ('eko@pzn.com', 'feedback eko', 'ini feedback eko'), 700 | ('eko@pzn.com', 'feedback eko', 'ini feedback eko'), 701 | ('budi@pzn.com', 'feedback budi', 'ini feedback budi'), 702 | ('rully@pzn.com', 'feedback rully', 'ini feedback rully'), 703 | ('tono@pzn.com', 'feedback tono', 'ini feedback tono'), 704 | ('tono@pzn.com', 'feedback tono', 'ini feedback tono'); 705 | 706 | select * 707 | from guestbooks; 708 | 709 | select distinct email 710 | from customer 711 | union 712 | select distinct email 713 | from guestbooks; 714 | 715 | select email 716 | from customer 717 | union all 718 | select email 719 | from guestbooks; 720 | 721 | select email, count(email) 722 | from (select email 723 | from customer 724 | union all 725 | select email 726 | from guestbooks) as contoh 727 | group by email; 728 | 729 | select email 730 | from customer 731 | intersect 732 | select email 733 | from guestbooks; 734 | 735 | select email 736 | from customer 737 | except 738 | select email 739 | from guestbooks; 740 | 741 | start transaction; 742 | 743 | insert into guestbooks(email, title, content) 744 | values ('transaction@pzn.com', 'transaction', 'transaction'); 745 | 746 | insert into guestbooks(email, title, content) 747 | values ('transaction@pzn.com', 'transaction', 'transaction 2'); 748 | 749 | insert into guestbooks(email, title, content) 750 | values ('transaction@pzn.com', 'transaction', 'transaction 3'); 751 | 752 | insert into guestbooks(email, title, content) 753 | values ('transaction@pzn.com', 'transaction', 'transaction 4'); 754 | 755 | insert into guestbooks(email, title, content) 756 | values ('transaction@pzn.com', 'transaction', 'transaction 5'); 757 | 758 | select * 759 | from guestbooks; 760 | 761 | commit; 762 | 763 | start transaction; 764 | 765 | insert into guestbooks(email, title, content) 766 | values ('transaction@pzn.com', 'transaction', 'rollback'); 767 | 768 | insert into guestbooks(email, title, content) 769 | values ('transaction@pzn.com', 'transaction', 'rollback 2'); 770 | 771 | insert into guestbooks(email, title, content) 772 | values ('transaction@pzn.com', 'transaction', 'rollback 3'); 773 | 774 | insert into guestbooks(email, title, content) 775 | values ('transaction@pzn.com', 'transaction', 'rollback 4'); 776 | 777 | insert into guestbooks(email, title, content) 778 | values ('transaction@pzn.com', 'transaction', 'rollback 5'); 779 | 780 | select * 781 | from guestbooks; 782 | 783 | rollback; 784 | 785 | select * 786 | from products; 787 | 788 | start transaction; 789 | 790 | update products 791 | set description = 'Mie ayam original enak' 792 | where id = 'P0001'; 793 | 794 | select * 795 | from products 796 | where id = 'P0001'; 797 | 798 | commit; 799 | 800 | start transaction; 801 | 802 | select * 803 | from products 804 | where id = 'P0001' for update; 805 | 806 | rollback; 807 | 808 | select * 809 | from products 810 | where id = 'P0001'; 811 | 812 | start transaction; 813 | 814 | select * 815 | from products 816 | where id = 'P0001' for update; 817 | 818 | select * 819 | from products 820 | where id = 'P0002' for update; 821 | 822 | rollback; 823 | 824 | select current_schema(); 825 | 826 | create schema contoh; 827 | 828 | drop schema contoh; 829 | 830 | SET search_path TO contoh; 831 | 832 | select current_schema(); 833 | 834 | select * 835 | from public.products; 836 | 837 | create table contoh.products 838 | ( 839 | id serial not null, 840 | name varchar(100) not null, 841 | primary key (id) 842 | ); 843 | 844 | select * from contoh.products; 845 | 846 | SET search_path TO public; 847 | 848 | insert into contoh.products(name) 849 | values ('iphone'), 850 | ('Play Station'); 851 | 852 | select * from contoh.products; 853 | 854 | create role eko; 855 | create role budi; 856 | 857 | drop role eko; 858 | drop role budi; 859 | 860 | alter role eko login password 'rahasia'; 861 | 862 | alter role budi login password 'rahasia'; 863 | 864 | grant insert, update, select on all tables in schema public to eko; 865 | grant usage, select, update ON guestbooks_id_seq TO eko; 866 | grant insert, update, select on customer to budi; 867 | 868 | create database belajar_restore; 869 | --------------------------------------------------------------------------------