The document shows the creation of tables, functions, and insertion of data into tables in a PostgreSQL database. Specifically:
1) Several tables are created - book, member, borrower, mahasiswa (students), barang (items) - with various columns and constraints.
2) Functions are created to count and summarize data from the tables, such as counting students by religion code or postal code.
3) Data is inserted into the tables, such as student records and item inventory.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
975 views
SQL
The document shows the creation of tables, functions, and insertion of data into tables in a PostgreSQL database. Specifically:
1) Several tables are created - book, member, borrower, mahasiswa (students), barang (items) - with various columns and constraints.
2) Functions are created to count and summarize data from the tables, such as counting students by religion code or postal code.
3) Data is inserted into the tables, such as student records and item inventory.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5
Server [localhost]: localhost
Database [postgres]: postgres
Port [5432]: 5432 Username [postgres]: postgres psql (9.2.4) WARNING: Console code page (850) differs from Windows code page (1252) 8-bit characters might not work correctly. See psql reference page "Notes for Windows users" for details. Type "help" for help. postgres=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | ady | table | postgres (1 row) postgres=# create table book (book_id char(6) primary key, book_title varchar(40 ), year_published date, author varchar(50)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "book_pkey" for t able "book" CREATE TABLE postgres=# create table member(member_id char(5) primary key, member_nama varcha r(50), address varchar(50), gender char(1), telp varchar(12), instance varchar(5 0)); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "member_pkey" for table "member" CREATE TABLE postgres=# create table borrower (book_id char(6) references book, member_id cha r(5) references member, date_start date, date_finish date)); ERROR: syntax error at or near ")" LINE 1: ...r(5) references member, date_start date, date_finish date)); ^ postgres=# create table borrower (book_id char(6) references book, member_id cha r(5) references member, date_start date, date_finish date); CREATE TABLE postgres=# \d List of relations Schema | Name | Type | Owner --------+----------+-------+---------- public | ady | table | postgres public | book | table | postgres public | borrower | table | postgres public | member | table | postgres (4 rows) postgres=# create table mahasiswa (nim char(10) primary key, nama varchar(30), j k char, kode_agama int, nama_jalan varchar(50), kode_pos char(5), kelas char(6)) ; NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "mahasiswa_pkey" for table "mahasiswa" CREATE TABLE postgres=# \d List of relations Schema | Name | Type | Owner --------+-----------+-------+---------- public | ady | table | postgres public | book | table | postgres public | borrower | table | postgres public | mahasiswa | table | postgres public | member | table | postgres (5 rows) postgres=# select*from mahasiswa; nim | nama | jk | kode_agama | nama_jalan | kode_pos | kelas -----+------+----+------------+------------+----------+------- (0 rows) postgres=# create function jumlah (char(4)) postgres-# returns int as postgres-# $$ postgres$# select count(*) from mahasiswa where right($1,2)=left(nim,1); postgres$# $$ postgres-# language sql; ERROR: return type mismatch in function declared to return integer DETAIL: Actual return type is bigint. CONTEXT: SQL function "jumlah" postgres=# create function jumlah (char(4)) postgres-# returns bigint as postgres-# $$ postgres$# select count(*) from mahasiswa where right($1,2)=left(nim,1); postgres$# $$ postgres-# language sql; CREATE FUNCTION postgres=# select jumlah; ERROR: column "jumlah" does not exist LINE 1: select jumlah; ^ postgres=# select jumlah('2013'); jumlah -------- 0 (1 row) postgres=# create function jumlahmhsagama() postgres-# returns table (kode_agama int, jumlah int) as postgres-# $$ postgres$# select kode_agama, count(nim) as jumlah from mahasiswa group by kode_ agama; postgres$# $$ postgres-# language sql; ERROR: return type mismatch in function declared to return record DETAIL: Final statement returns bigint instead of integer at column 2. CONTEXT: SQL function "jumlahmhsagama" postgres=# create function jumlahmhsagama() postgres-# returns table (kode_agama int, jumlah bigint) as postgres-# $$ postgres$# select kode_agama, count(nim) as jumlah from mahasiswa group by kode_ agama; postgres$# $$ postgres-# language sql; CREATE FUNCTION postgres=# insert into mahasiswa values ('13051234','ady','L','1431','jalan jala n','12345','lima')); ERROR: syntax error at or near ")" LINE 1: ...('13051234','ady','L','1431','jalan jalan','12345','lima')); ^ postgres=# insert into mahasiswa values ('13051234','ady','L','1431','jalan jala n','12345','lima'); INSERT 0 1 postgres=# insert into mahasiswa values ('13132423','jar','L','1431','jalan jala n','12345','lima'); INSERT 0 1 postgres=# insert into mahasiswa values ('1313433','deby','L','1431','jalan jala n','12345','lima'); INSERT 0 1 postgres=# create function jumlahmhspos() postgres-# returns table (kode_pos char(5), jumlah bigint) as postgres-# $$ postgres$# begin postgres$# return query select mahasiswa.kode_pos, count(mahasiswa.nim) as jumla h from mahasiswa group by mahasiswa.kode_pos; postgres$# return; postgres$# end; postgres$# $$ postgres-# language plpgsql; CREATE FUNCTION postgres=# create table barang(id char(10) primary key, nama_barang varchar(30), stock int)); ERROR: syntax error at or near ")" LINE 1: ... char(10) primary key, nama_barang varchar(30), stock int)); ^ postgres=# create table barang(id char(10) primary key, nama_barang varchar(30), stock int); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "barang_pkey" for table "barang" CREATE TABLE postgres=# create table penjualan(id char(10) primary key, jumlah bigint, tangga l_jual date); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "penjualan_pkey" for table "penjualan" CREATE TABLE postgres=# insert into barang ('001','lemari','20'); ERROR: syntax error at or near "'001'" LINE 1: insert into barang ('001','lemari','20'); ^ postgres=# insert into barang values ('001','lemari','20'); INSERT 0 1 postgres=# insert into barang values ('002','kursi','10'); INSERT 0 1 postgres=# insert into barang values ('003','polpen','12'); INSERT 0 1 postgres=# insert into penjualan values('001','120','12-08-08'); INSERT 0 1 postgres=# insert into penjualan values('002','140','12-09-08'); INSERT 0 1 postgres=# create function jumlah_barang() postgres-# returns table (nama_barang char(30), stock int) as postgres-# $$ postgres$# select nama_barang, sum(stock) from barang group by id having sum(sto ck)<100; postgres$# $$ postgres-# language sql; ERROR: return type mismatch in function declared to return record DETAIL: Final statement returns bigint instead of integer at column 2. CONTEXT: SQL function "jumlah_barang" postgres=# create function banyakbarang() postgres-# returns table(nama_barang char(30), stock int) as postgres-# $$ postgres$# select nama_barang, stok from barang where stok<100; postgres$# $$ postgres-# language sql; ERROR: column "stok" does not exist LINE 4: select nama_barang, stok from barang where stok<100; ^ postgres=# \d List of relations Schema | Name | Type | Owner --------+-----------+-------+---------- public | ady | table | postgres public | barang | table | postgres public | book | table | postgres public | borrower | table | postgres public | mahasiswa | table | postgres public | member | table | postgres public | penjualan | table | postgres (7 rows) postgres=# drop table penjualan; DROP TABLE postgres=# create table penjualan (id char(10) references barang, jumlah int, ta nggal_jual date); CREATE TABLE postgres=# create function banyakbarang() postgres-# returns table (nama_barang char(30), stock int) as postgres-# $$ postgres$# select nama_barang, stok from barang where stok<100; postgres$# $$ postgres-# language sql; ERROR: column "stok" does not exist LINE 4: select nama_barang, stok from barang where stok<100; ^ postgres=# select*from barang; id | nama_barang | stock ------------+-------------+------- 001 | lemari | 20 002 | kursi | 10 003 | polpen | 12 (3 rows) postgres=# create function banyakbarang() postgres-# returns table (nama_barang char(30), stock int) as postgres-# $$ postgres$# select nama_barang, stok from barang where stok<100; postgres$# $$ postgres-# language sql; ERROR: column "stok" does not exist LINE 4: select nama_barang, stok from barang where stok<100; ^ postgres=# create function banyakbarang() postgres-# returns table (nama_barang char(30), stock int) as postgres-# select nama_barang, stock from barang where stock<100; ERROR: syntax error at or near "select" LINE 3: select nama_barang, stock from barang where stock<100; ^ postgres=# create function banyakbarang() postgres-# returns table (nama_barang char(30), stock int) as postgres-# $$ postgres$# select nama_barang, stock from barang where stock<100; postgres$# $$ postgres-# language sql; CREATE FUNCTION postgres=#