By Wahyudi Robby Sutanto
Video overview: https://youtu.be/J-IRkQ1N330
The database for CS50 SQL includes all entities necessary to facilitate the process of tracking student progress and leaving feedback on student work. As such, included in the database's scope is:
- barang, including basic identifying information
- harga_beli, including the buying price of product inside table barang
- company_info, including basic identifying information
- employee, including basic identifying information
- kategori, including basic identifying information
- position, including basic identifying information
- cart, including content for invoice when submitted
- invoice, which includes basic information about the product that sold
- invoice_detail, which includes detail information about the product that sold that linked to invoice
This database will support:
- CRUD operations for inserting the product, its buying and selling price.
- CRUD operations for update the company_info.
- CRUD operations for inserting or updating employee.
- CRUD operations for inserting or updating kategori.
- CRUD operations for inserting or updating cart. And after that inserting to invoice.
- See history and make report from invoice and invoice_detail
Entities are captured in MySQL tables with the following schema.
The database includes the following entities:
the barang table includes:
id_barangwhich specifies the unique ID for the barang as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.id_kategoriwhich specifies the ID fromkategoritable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.nama_barangwhich specifies the barang's name asVARCHAR, givenVARCHARis appropriate for name fields.harga_jualwhich specifies the barang's selling price asINT, givenINTis appropriate for the price fields.stokwhich specifies the barang's stock asINT, givenINTis appropriate for the stock fields.statuswhich specifies the barang's status asTINYINT, givenTINYINTis appropriate for the status fields which got default value as1.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
the cart table includes:
nowhich specifies the unique ID for the cart as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.id_barangwhich specifies the ID frombarangtable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.id_employeewhich specifies the ID fromemployeetable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.harga_jualwhich got the value of the barang's selling price asINT, givenINTis appropriate for the price fields.jumlahwhich specifies the cart's qty asINT, givenINTis appropriate for the qty fields.totalwhich specifies the cart's total qty asINT, givenINTis appropriate for the total qty fields.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp).
the company_info table includes:
company_namewhich specifies the company_info's name asVARCHAR, givenVARCHARis appropriate for name fields.company_logowhich specifies the company_info's path for logo image asVARCHAR, givenVARCHARis appropriate for name fields.statuswhich specifies the company_info's selling price asTINYINT, givenTINYINTis appropriate for the status fields which got default value as1.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
the employee table includes:
nowhich specifies the unique ID for the employee as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.usernamewhich specifies the employee's username asVARCHAR, givenVARCHARis appropriate for username fields.passwordwhich specifies the employee's password asVARCHAR, givenVARCHARis appropriate for password fields.positionwhich specifies the ID frompositiontable as anINTEGER.statuswhich specifies the employee's status asTINYINT, givenTINYINTis appropriate for the status fields which got default value as1.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
the harga_beli table includes:
nowhich specifies the unique ID for the harga_beli as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.no_barangwhich specifies the ID frombarangtable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.harga_beliwhich got the value of the harga_beli's buying price of product asINT, givenINTis appropriate for the price fields.jumlah_stokwhich specifies the harga_beli's qty of product asINT, givenINTis appropriate for the qty fields.qtyleftwhich specifies the harga_beli's qty left of product asINT, givenINTis appropriate for the qty left fields.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
the invoice table includes:
invoice_nowhich specifies the unique ID for the invoice as anVARCHARbecause it is generate the invoice number that can use string too if need. This column thus has thePRIMARY KEYconstraint applied.nama_pelangganwhich specifies the invoice's customer's name asVARCHAR, givenVARCHARis appropriate for customer's name fields.jum_barangwhich specifies the invoice's total qty of product asINT, givenINTis appropriate for the total qty fields.totalwhich specifies the invoice's total amount of product asINT, givenINTis appropriate for the total amount fields.bayarwhich specifies the invoice's amount paid of product asINT, givenINTis appropriate for the amount paid fields.kembaliwhich specifies the invoice's return amount of product asINT, givenINTis appropriate for the return amount fields.tipe_pembayaranwhich specifies the invoice's payment method asVARCHAR, givenVARCHARis appropriate for the payment method fields.keteranganwhich specifies the invoice's description asTEXT, givenTEXTis appropriate for the description fields.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.
nowhich specifies the unique ID for the invoice_detail as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.invoice_nowhich got from invoice's invoice_no as anVARCHAR.id_barangwhich specifies the ID frombarangtable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.nama_barangwhich got from barang's nama_barang asVARCHAR, givenVARCHARis appropriate for barang's nama_barang fields.jumlahwhich specifies the invoice_detail's qty of product asINT, givenINTis appropriate for the qty fields.no_harga_beliwhich specifies the ID fromharga_belitable as anINTEGER. This column thus has theFOREIGN KEYconstraint applied.,harga_beliwhich got from harga_beli's harga_beli asINTEGER, givenINTEGERis appropriate for barang's harga_beli fields.harga_jualwhich got from barang's harga_jual asINTEGER, givenINTEGERis appropriate for barang's harga_jual fields.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.
the kategori table includes:
id_kategoriwhich specifies the unique ID for the kategori as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.nama_kategoriwhich specifies the kategori's name asVARCHAR, givenVARCHARis appropriate for name fields.statuswhich specifies the kategori's status asTINYINT, givenTINYINTis appropriate for the status fields which got default value as1.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
the kategori table includes:
nowhich specifies the unique ID for the kategori as anINTEGER. This column thus has thePRIMARY KEYconstraint applied.namawhich specifies the kategori's name asVARCHAR, givenVARCHARis appropriate for name fields.created_datewhich specifies record's created date asDATE, givenDATEis appropriate for the created date fields which got default(current_timestamp),created_bywhich specifies the record's created by asVARCHAR, givenVARCHARis appropriate for created by fields.modified_datewhich specifies record's modified date asDATE, givenDATEis appropriate for the modified date fields which got default(current_timestamp),modified_bywhich specifies the record's modified by asVARCHAR, givenVARCHARis appropriate for modified by fields.
The below entity relationship diagram describes the relationships among the entities in the database.
The foreign key that we created is for if someone try to delete the master record like barang table. it can't be done because it need to delete all the foreign key that used. It is same as other that got foreign key.
And for the invoice_detail, i use the foreign key there for id_barang and no_harga_beli is for when inserting it is not randomly insert any value. so that's why it is linked like that.
The current schema assumes only 1 card for creating invoice.
