Mario Contreras
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Garage Parking Project === [TOC] Model Entity Relationship == ![](https://i.imgur.com/XNFjOSY.png) Getting Started === ## Migrations ```sql= CREATE TABLE customers( id INTEGER NOT NULL, name VARCHAR2(100) NOT NULL, CONSTRAINT customers_id_pk PRIMARY KEY(id) ); CREATE TABLE vehicules( id INTEGER NOT NULL, customer_id INTEGER NOT NULL, brand VARCHAR2(100) NOT NULL, model VARCHAR2(100) NOT NULL, plate VARCHAR2(100) NOT NULL, CONSTRAINT vehicules_id_pk PRIMARY KEY(id), CONSTRAINT vehicules_customer_fk FOREIGN KEY (customer_id) REFERENCES customers(id), CONSTRAINT vehicules_plate_uk UNIQUE (plate) ); CREATE TABLE lots( id INTEGER NOT NULL, code VARCHAR2(100) NOT NULL, busy NUMBER NOT NULL, CONSTRAINT lots_id_pk PRIMARY KEY(id), CONSTRAINT lots_busy_chk CHECK (busy in (0, 1)), CONSTRAINT lots_code_uk UNIQUE (code) ); CREATE TABLE timings( id INTEGER NOT NULL, vehicule_id INTEGER NOT NULL, lot_id INTEGER NOT NULL, start_at TIMESTAMP NULL, end_at TIMESTAMP NULL, status VARCHAR2(100) NOT NULL, booked_at DATE NULL, paid NUMBER NULL, amount NUMBER NULL, CONSTRAINT timings_id_pk PRIMARY KEY(id), CONSTRAINT timings_vehicule_fk FOREIGN KEY (vehicule_id) REFERENCES vehicules(id), CONSTRAINT timings_lots_fk FOREIGN KEY (lot_id) REFERENCES lots(id), CONSTRAINT timings_status_chk CHECK (status in ('RESERVED', 'TAKEN', 'CANCELLED')), CONSTRAINT timings_paid_chk CHECK (paid in (0, 1)) ); CREATE TABLE sales_report( id INTEGER NOT NULL, day VARCHAR2(100) NOT NULL, total NUMBER NOT NULL, CONSTRAINT sr_id_pk PRIMARY KEY(id) ); ``` ## Seeds ```sql= INSERT INTO customers VALUES(1, 'Ada Ramos'); INSERT INTO customers VALUES(2, 'Mario Contreras'); INSERT INTO customers VALUES(3, 'Sukhkeet Singh'); INSERT INTO vehicules VALUES(1, 1, 'Tesla', '2021', 'E71 TZD'); INSERT INTO vehicules VALUES(2, 1, 'BMW', '2020', 'AAA 125'); INSERT INTO vehicules VALUES(3, 2, 'Chevrolet', '2017', 'ABC 123'); INSERT INTO vehicules VALUES(4, 3, 'Ford', '2020', 'X1X 2Y2'); ``` ## Triggers ```sql= CREATE OR REPLACE TRIGGER before_update_timings BEFORE UPDATE ON timings FOR EACH ROW BEGIN IF :OLD.start_at IS NOT NULL AND :OLD.start_at != :NEW.start_at THEN RAISE_APPLICATION_ERROR(-20000, 'ERR409: THE PARKING LOT CAN NOT BE UPDATED'); END IF; END; ``` ```sql= CREATE OR REPLACE TRIGGER after_update_timings AFTER INSERT OR UPDATE ON timings FOR EACH ROW BEGIN IF :NEW.STATUS ='CANCELLED' THEN UPDATE lots SET busy = 0 WHERE id = :NEW.lot_id; ELSIF :NEW.STATUS ='RESERVED' OR :NEW.STATUS ='TAKEN' THEN UPDATE lots SET busy = 1 WHERE id = :NEW.lot_id; END IF; IF :NEW.PAID = 1 THEN UPDATE lots SET busy = 0 WHERE id = :NEW.lot_id; END IF; END; ``` ## Sequences ```sql= CREATE SEQUENCE lots_id_seq INCREMENT BY 1 START WITH 1; CREATE SEQUENCE timings_id_seq INCREMENT BY 1 START WITH 1; CREATE SEQUENCE sales_report_id_seq INCREMENT BY 1 START WITH 1; ``` Features === ## Establish parking lots ```sql= CREATE OR REPLACE PROCEDURE parking_lot_set (in_code IN VARCHAR2) IS l_status NUMBER := 0; BEGIN INSERT INTO lots VALUES(lots_id_seq.NEXTVAL, LOWER(in_code), l_status); DBMS_OUTPUT.PUT_LINE('THE PARKING LOTS HAVE BEEN SET'); END; ``` ## Parking lot availables ```sql= CREATE OR REPLACE FUNCTION parking_lot_released (in_code IN VARCHAR2) RETURN NUMBER IS out_available NUMBER; BEGIN SELECT busy INTO out_available FROM lots WHERE LOWER(code) = LOWER(in_code); RETURN out_available; EXCEPTION WHEN NO_DATA_FOUND THEN RAISE; END; ``` ## Booking a parking lot ```sql= CREATE OR REPLACE PROCEDURE parking_lot_booking (in_plate IN VARCHAR2, in_code IN VARCHAR2, in_booked_at IN VARCHAR2) IS l_vehicule_id vehicules.id%TYPE; l_lot_id lots.id%TYPE; BEGIN SELECT id INTO l_vehicule_id FROM vehicules WHERE LOWER(plate) = LOWER(in_plate); SELECT id INTO l_lot_id FROM lots WHERE LOWER(code) = LOWER(in_code); INSERT INTO timings (id, vehicule_id, lot_id, status, booked_at) VALUES (timings_id_seq.NEXTVAL, l_vehicule_id, l_lot_id, 'RESERVED', TO_DATE(in_booked_at, 'YYYY-MM-DD')); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERR404: DATA NOT FOUND'); END; ``` ## Cancelling a booking ```sql= CREATE OR REPLACE PROCEDURE parking_lot_cancel (in_timing_id IN VARCHAR2) IS BEGIN UPDATE timings SET status = 'CANCELLED' WHERE id = in_timing_id; DBMS_OUTPUT.PUT_LINE('THE PARKING LOT HAS BEEN CANCELLED'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERR404: DATA NOT FOUND'); END; ``` ## Entering the parking lot ```sql= CREATE OR REPLACE PROCEDURE parking_lot_entry (in_plate IN VARCHAR2, in_code IN VARCHAR2) IS l_vehicule_id vehicules.id%TYPE; l_lot_id lots.id%TYPE; BEGIN SELECT id INTO l_vehicule_id FROM vehicules WHERE LOWER(plate) = LOWER(in_plate); SELECT id INTO l_lot_id FROM lots WHERE LOWER(code) = LOWER(in_code); UPDATE timings SET status = 'TAKEN', start_at = SYSDATE WHERE vehicule_id = l_vehicule_id AND lot_id = l_lot_id; IF sql%notfound THEN INSERT INTO timings (id, vehicule_id, lot_id, start_at, status) VALUES (timings_id_seq.NEXTVAL, l_vehicule_id, l_lot_id, SYSDATE, 'TAKEN'); END IF; DBMS_OUTPUT.PUT_LINE('THE PARKING LOT HAS BEEN TAKEN'); EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERR404: DATA NOT FOUND'); END; ``` ## Pay the used timing **Function to get the minutes spend into the parking lot** ```sql= CREATE OR REPLACE FUNCTION calculate_minutes(in_start_at IN VARCHAR2, in_end_at IN VARCHAR2) RETURN NUMBER IS out_minutes NUMBER; BEGIN out_minutes := (TO_DATE(in_start_at, 'YYYY-MM-DD HH24:MI') - TO_DATE(in_end_at, 'YYYY-MM-DD HH24:MI')) * 24 * 60; RETURN out_minutes; END; ``` ```sql= CREATE OR REPLACE PROCEDURE payment_process (in_plate IN VARCHAR2) IS l_vehicule_id vehicules.id%TYPE; l_timing_id timings.id%TYPE; l_price NUMBER := 2.50; l_minutes NUMBER; l_end_at DATE := SYSDATE; ex_start_date_null EXCEPTION; BEGIN SELECT id INTO l_vehicule_id FROM vehicules WHERE LOWER(plate) = LOWER(in_plate); SELECT id, calculate_minutes( TO_CHAR(start_at, 'YYYY-MM-DD HH24:MI'), TO_CHAR(l_end_at, 'YYYY-MM-DD HH24:MI') ) INTO l_timing_id, l_minutes FROM timings WHERE vehicule_id = l_vehicule_id; IF l_minutes IS NULL THEN RAISE ex_start_date_null; ELSE UPDATE timings SET amount = l_minutes * l_price, paid = 1, end_at = l_end_at WHERE id = l_timing_id; END IF; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERR404: DATA NOT FOUND'); WHEN ex_start_date_null THEN DBMS_OUTPUT.PUT_LINE('ERR409: START AT IS NULL'); END; ``` Cursor === ## Getting a Sales Report per day Write a procedure consolidate the sales per day and insert into the entity related ```sql= CREATE OR REPLACE PROCEDURE update_report (in_start_at IN VARCHAR2) IS c_amount timings.amount%TYPE; l_total timings.amount%TYPE := 0; CURSOR c_report IS SELECT amount FROM timings WHERE TO_DATE(TO_CHAR(start_at, 'YYYY-MM-DD'), 'YYYY-MM-DD') = TO_DATE(in_start_at, 'YYYY-MM-DD'); BEGIN OPEN c_report; LOOP FETCH c_report into c_amount; EXIT WHEN c_report%notfound; l_total := l_total + c_amount; END LOOP; CLOSE c_report; INSERT INTO sales_report VALUES(sales_report_id_seq.NEXTVAL, in_start_at, l_total); END; ``` References === **References:** * [Naming Conventions for PL/SQL](https://trivadis.github.io/plsql-and-sql-coding-guidelines/v4.0/2-naming-conventions/naming-conventions/) * [HTTP Status Codes](https://www.restapitutorial.com/httpstatuscodes.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully