Janoš Vidali
    • 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
    • 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
    • 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 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
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: vaje, opb, insert, update, delete, select hackmd: https://hackmd.io/tq2EDkavS_G0b5k9W7Ji4w --- # Osnove podatkovnih baz - vaje 17.2.2022 --- ## Osnovna uporaba SQL ### Naloga 3: Ukaz `INSERT` ```sql INSERT INTO tabela [(st1, st2, ...)] VALUES (vr1, vr2, ...); ``` 1. Vsak naj se vpiše v tabelo `oseba`. Po potrebi je treba prej vpisati še kraj v tabelo `kraj`. Vpiši se z ukazom `INSERT`. (Najprej klikni na zbirko `banka` in izberi zavihek *SQL*. Odpre se polje, v katerega lahko pišeš ukaze SQL in jih izvajaš.) ```sql INSERT INTO kraj (posta, kraj) VALUES (6281, 'Škofije'); INSERT INTO oseba VALUES ('2305939505123', 'Moja', 'Nona', '1939-05-23', 'Stara ulica 80', 6281); ``` 2. V tabeli `racun` ustvari še svoj bančni račun. ```sql INSERT INTO racun (lastnik) VALUES ('2305939505123'); ``` 3. V tabeli `transakcija` naredi nekaj transakcij na svojem računu, na primer vsaj dva dviga in dva pologa. ```sql INSERT INTO transakcija (znesek, racun, opis) VALUES (800, 6, 'Penzija'); INSERT INTO transakcija (znesek, racun) VALUES (-100, 6); ``` --- ### Naloga 4: Ukaz `UPDATE` ```sql UPDATE tabela SET stolpec1 = vrednost1, st2 = izraz, ... WHERE stolpec = vrednost AND (pogoj1 OR pogoj2); <> > >= <= LIKE 'a_b%' -- niz, ki ima na 1. mestu a in na 3. mestu b IS [NOT] NULL ``` V tabeli `oseba` poskusi spremeniti EMŠO v zapisu, ki si ga naredil v nalogi 3. Uporabi ukaz `UPDATE`. Ali ti baza to dovoli? Poskusi spremeniti še kakšno drugo polje v tabeli. ```sql UPDATE oseba SET emso = '0802986500999' WHERE emso = '0802986500666'; -- ne gre, ker imamo račun s tem EMŠOm UPDATE oseba SET ime = priimek, priimek = ime WHERE emso LIKE '%3'; -- zamenjamo ime in priimek vsem, ki se jim EMŠO konča s 3 ``` --- ### Naloga 5: Ukaz `DELETE` ```sql DELETE FROM tabela WHERE pogoj; ``` Iz tabele `transakcija` zbriši eno transakcijo (vseeno katero). Glej, da ne pobrišeš celotne tabele! ```sql DELETE FROM transakcija WHERE id = 49; ``` --- ### Naloga 6: Ukaz `SELECT` ```sql SELECT [DISTINCT] [*, ] stolpec1, st2, izraz AS ime, ... FROM tabela1 [NATURAL] JOIN tabela2 [ON pogoj | USING (stolpec1, stolpec2, ...)] WHERE pogoj; ``` Naredi naslednje poizvedbe: 1. Za vse osebe, ki živijo v kraju s poštno številko 2000, izpiši celotne pripadajoče vrstice. ```sql SELECT * FROM oseba WHERE posta = 6000; ``` 2. Vse osebe v tabeli `oseba`, vendar izpiši samo ime, priimek in poštno številko. ```sql SELECT ime, priimek, posta FROM oseba; ``` 3. Vse transakcije, pri katerih je znesek manjši od -1000. ```sql SELECT * FROM transakcija WHERE znesek < -1000; ``` 4. Vse transakcije, pri katerih je znesek manjši od -1000. Zneski transakcij naj bodo pri izpisu zaokroženi na najbližjo stotico proti ničli. (Uporabi celoštevilsko deljenje.) ```sql SELECT *, (znesek / 100) * 100 AS zaokrozen_znesek FROM transakcija WHERE znesek < -1000; ``` 5. Vse osebe v tabeli `oseba`, vendar izpiši samo ime, priimek in ime kraja, v katerem živijo (pomagati si bo treba s tabelo `kraj`). ```sql SELECT ime, priimek, kraj FROM oseba JOIN kraj ON oseba.posta = kraj.posta; ``` 6. Vse osebe, ki živijo v kraju *Maribor*, pri čemer poštno številko dobiš iz tabele `kraj`. Nato naredi še bolj komplicirane poizvedbe: 1. Z uporabo funkcije `count` preštej, koliko računov je odprtih na banki. ```sql SELECT count(*) FROM racun; ``` 2. Preštej, koliko je oseb, ki živijo v kraju s poštno številko 1000. ```sql SELECT count(*) FROM oseba WHERE posta = 1000; ``` 3. Z uporabo funkcije `sum` izračunaj, koliko denarja je trenutno v banki (seštej stolpec `znesek` v tabeli `transakcija`). ```sql SELECT sum(znesek) FROM transakcija; ``` 4. Z uporabo funkcije `sum` izračunaj, koliko denarja imaš trenutno na svojem računu. ```sql SELECT sum(znesek) FROM transakcija WHERE racun = 6; ``` 5. Izpiši imena in priimke vseh tistih ljudi, ki so naredili vsaj eno transakcijo z absolutno vrednostjo 1000 ali več. Pri izpisu ne podvajaj oseb, ki so opravile več takih transakcij; uporabi določilo `DISTINCT`. ```sql SELECT DISTINCT ime, priimek FROM oseba JOIN racun ON emso = lastnik JOIN transakcija ON stevilka = racun WHERE abs(znesek) >= 1000; ``` 6. Nadgradnja prejšnje naloge: za vsako osebo, ki je opravila vsaj eno transakcijo z absolutno vrednostjo 1000 ali več, izpiši niz *__Ime Priimek__ iz kraja __Kraj__*. Uporabi operator `||` za stikanje nizov. 7. **Dodatna naloga**: Z uporabo funkcij `sum` in `pow` ter ustrezne funkcije, ki izračuna razliko med dvema časoma (poišči jo v dokumentaciji!), izračunaj, koliko denarja imaš trenutno na svojem računu, pri predpostavki, da se obrestuje z 1% na 30 dni. (Obračunavanje pa je zvezno - po enem dnevu in pol recimo profitiraš za faktor 1.01<sup>1.5/30</sup>).

    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