Jiayong
    • 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
    • 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 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
    --- disqus: hackmd --- RDBMS Assignment === #### 題目要求: You will use a modified version of the data for the project, so to succeed in the project, download the linked files when prompted in the instructions. You do not need to use any data from the original source. In your scenario, you will be working with data from the following sources: * Staff information held in a spreadsheet at HQ. * Sales outlet information held in a spreadsheet at HQ. * Sales data output as a CSV file from the POS system in the sales outlets. * Customer data output as a CSV file from a bespoke customer relationship management system. * Product information maintained in a spreadsheet exported from your supplier's database. After completing this lab, you will be able to: * Identify entities. * Identity attributes. * Create an entity relationship diagram (ERD) using the pgAdmin ERD Tool. * Normalize tables. * Define keys and relationships. * Create database objects by generating and running the SQL script from the ERD Tool. * Create a view and export the data. * Create a materialized view and export the data. * Import data into a MySQL database. ## 事前準備: 1. Open a new terminal from the side-by-side Cloud IDE. 2. Use the `start_postgres` command to start a PostgreSQL service session in the Cloud IDE. 3. Use the pgAdmin weblink to open pgAdmin in a new tab in your browser. ## 資料庫設計的需求單 * Product table: <br>![](https://i.imgur.com/8ey9wis.png =500x)<br><br> ## 1. Create an ERD #### 題目要求: Now that you have defined some of your attributes and entities, you can determine the tables and columns for them and create an ERD. 1. Create a new database named `COFFEE`, view the schemas in the new `COFFEE` database, and then start a new ERD project. 2. Add a table to the ERD for the sale transactions entity using the information in the following table. Consider what naming convention to use so that your colleagues will be able to understand your data and to ensure that the names are valid in other RDBMS. And use the sample data shown in the image in Task 1 to determine appropriate data types for each column. <br>![](https://i.imgur.com/DyHxp21.png =150x)<br><br> 3. Add a table to the ERD for the product entity using the information in the following table. Consider what naming convention to use so that your colleagues will be able to understand your data and to ensure that the names are valid in other RDBMS. And use the sample data shown in the image in Task 1 to determine appropriate data types for each column. <br>![](https://i.imgur.com/waurBdu.png =150x)<br><br> --- * pgAdmin 的介面 <br>![](https://i.imgur.com/b8gR4Ci.png =500x)<br><br> * 新增 Table <br>![](https://i.imgur.com/f7UdoMw.png =500x)<br><br> <br>![](https://i.imgur.com/TwLhpZd.png =500x)<br><br> * 設定後的結果 <br>![](https://i.imgur.com/xHswfaW.png =500x)<br><br> ## 2. Normalize tables #### 題目要求: When reviewing your ERD you notice that it does not conform to second normal form. In this task, you will normalize some of the tables within the database. 1. Review the data in the sales transaction table. Note that the transaction id column does not contain unique values because some transactions include multiple products. 2. Determine which columns should be stored in a separate table to remove the repeating rows and to put this table into second normal form. 3. Add a new table named `sales_detail` to the ERD, define the columns in the new table, and delete the moved columns from the sales transaction table, leaving a matching column in each of two tables to later create a relationship between them. * 重新整理 table 裡的資料,把資料在細分到新的 table 上。 <br>![](https://i.imgur.com/Kq3SJOf.png =500x)<br><br> * sales_transaction 的 product_id, quantity 以及 price 資料存到新的 sales_detail table。 * 為了 sales_detail table 與 sales_transaction 保持關聯性,所以新增 transaction_id 資料。 4. Review the data in the product table. Note that the product category and product type columns contain redundant data. 5. Determine which columns should be stored in a separate table to reduce redundant data and to put this table into second normal form. 6. Add a new table named product_type to the ERD, define the columns in the new table, and delete the moved columns from the product table, leaving a matching column in each of two tables to later create a relationship between them. * 參考product table 新增 product_type 的 table,並把多餘的資料清除。 <br>![](https://i.imgur.com/lv87f2t.png =500x)<br><br> --- ## 3. Define keys and relationships #### 題目要求: After normalizing your tables, you can define their primary keys and define relationships between the tables in your ERD. 1. Identify an appropriate column in each table to be a primary key and create the primary keys in the tables in your ER * 在各個table建立primary key。 <br>![](https://i.imgur.com/nLZ2Kdb.png =500x)<br><br> * 開啟primary key設定。 <br>![](https://i.imgur.com/tcIFclm.png =500x)<br><br> * 設定後的結果。 <br>![](https://i.imgur.com/ShkwbiF.png =500x)<br><br> 2. Identify the relationships between the following pairs of tables and then create the relationships in your ERD: * `sales_detail` to `sales_transaction` * `sales_detail` to `product` * `product` to `product_type` * 繪製關係圖 <br>![](https://i.imgur.com/MFu9k1a.png =500x)<br><br> * 設定後的結果 <br>![](https://i.imgur.com/u7h9zS2.png =500x)<br><br> --- ## 4. Create database objects by generating and running the SQL script from the ERD Tool #### 題目要求: Now that your design is complete, you will generate an SQL script from your ERD which you could use to create your database schema. For the purposes of this project, you will then use a provided SQL script to ensure that you will be able to successfully load the sample data into the schema. Finally, you will load the existing data from the various data sources into your new database schema. 1. Use the Generate SQL functionality in the ERD Tool to create an SQL script from your ERD. 2. Download the `GeneratedScript.sql` file below to your local computer storage. * [GeneratedScript.sql](https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/datasets/CoffeeData/GeneratedScript.sql) 3. In pgAdmin, open the Query Tool, upload and open the `GeneratedScript.sql` file from your local computer storage, and then execute the script to create the tables defined in the ERD. Verify that the tables now exist in the `public` schema of the `COFFEE` database. * 上傳GeneratedScript.sql檔案 <br>![](https://i.imgur.com/HHANnl1.png =500x)<br><br> * 打開已上傳檔案,並執行SQL文本 <br>![](https://i.imgur.com/b7p0Kwr.png =500x)<br><br> * 重新整理頁面,就會看到SQL執行後的結果。 <br>![](https://i.imgur.com/adI4d0r.png =500x)<br><br> 5. Download the `CoffeeData.sq` file below to your local computer storage. * [CoffeeData.sql](https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/datasets/CoffeeData/CoffeeData.sql) 6. In pgAdmin, open another instance of the Query Tool, upload and open the `CoffeeData.sql` file from your local computer storage, and then execute the script to populate the tables you just created. * 依照以上的步驟上傳`CoffeeData.sql`檔案。 7. In pgAdmin, view the first 100 rows of the `sales_detail` table. * ```SQL= SELECT * FROM sales_detail LIMIT 100; ``` * 回傳結果 <br>![](https://i.imgur.com/f36fM5X.png =500x)<br><br> --- ## 5. Create a view and export the data #### 題目要求: The external payroll company have requested a list of employees and the locations at which they work. This should not include the CEO or CFO who own the company. In this task, you will create a view in your PostgreSQL database that returns this information and export the results to a CSV file. 1. In your COFFEE database, create a new view named staff_locations_view using the following SQL: ```SQL= SELECT staff.staff_id, staff.first_name, staff.last_name, staff.location FROM staff WHERE "position" NOT IN ('CEO', 'CFO'); ``` 2. View all the rows returned from the view. 3. Save the results of the query to a file named `staff_locations_view`.csv on your local computer storage. * 下載回傳結果的CSV檔 <br>![](https://i.imgur.com/es94tgV.png =500x)<br><br> --- ## 6. Create a materialized view and export the data #### 題目要求: A marketing consultant requires access to your product data in their MySQL database for a marketing campaign. You will create a materialized view in your PostgreSQL database that returns this information and export the results to a CSV file. 1. In your `COFFEE` database, create a new materialized view named `product_info_m-view` using the following SQL: ```SQL= SELECT product.product_name, product.description, product_type.product_category FROM product JOIN product_type ON product.product_type_id = product_type.product_type_id; ``` 2. Refresh the materialized view with data. 3. View all the rows returned from the view. 4. Save the results of the query to a file named `product_info_m-view.csv` on your local computer storage. * 依照以上的步驟下載檔案,並把檔案命名為product_info_m-view.csv <br>![](https://i.imgur.com/sCUwals.png =500x)<br><br> --- ## 7. Import data into a MySQL database #### 題目要求: The marketing consultant has asked you to upload the product information to their MySQL database. 1. In the terminal from the side-by-side Cloud IDE, use the `start_mysql` command to start a My SQL service session in the Cloud IDE. 2. Use the browser weblink to open phpMyAdmin in a new tab in your browser. 3. In phpMyAdmin, create a new database named `coffee_shop_products`, and then import the product information saved in the `product_info_m-view.csv` file from your materialized view into a new table in the `coffee_shop_products` database. * 創建新的資料庫,命名為 `coffee_shop_products` <br>![](https://i.imgur.com/rVe8b1e.png =500x)<br><br> * 把 `product_info_m-view.csv` 檔案上載至資料庫中 <br>![](https://i.imgur.com/Ilt8gQL.png =500x)<br><br> * 測試上傳是否有成功 <br>![](https://i.imgur.com/JECX7fH.png =500x)<br><br> * 成功取得回傳 <br><br> --- ## 總結: 這次的練習讓我了解到如何使用pgAdmin和phpMyAdmin操作資料庫。

    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