Langsung ke konten utama

Primary and Foreign Keys and Relationships


  1. Keys

    • Primary and Foreign Keys


     

  2. Relationships

    • One-to-Many and Many-to-Many Relationships






I would like to start by defining some basic conventions.
Primary Keys.


  • These are indicated by 'PK' alongside the field in a Table.

  • They show the field that identifies the record uniquely.

  • For example, the 'product_id' for a Product and an 'order_id' for an Order.

  • The values are usually generated by an 'Auto-Increment' field.

  • They don't have any intrinsic meaning and are simply integers generated automatically one after another.Primary/Foreign Keys.

  • These are indicated by 'PF' alongside the field in a Table.

  • In this case, the field also exists as a Primary Key in another Table.

  • In our example, the Primary Key in the 'Customer_Orders_Products' Table is a Composite Key, made up of two Fields.

  • The 'product_id' is the Primary Key in the 'Products' Table, and the 'order_id' is the Primary Key in the 'Customer_Orders' Table.Foreign Keys.

  • These are indicated by 'FK' alongside the field in a Table.

  • They show a reference to the Primary Key for a record in another Table.

  • For example, the 'customer_id' in the Customer_Orders Table is linked to a 'customer_id' in the Customer Table (not shown).One-to-Many Relationships.

  • One-to-Many Relationships are very common in the real world, and can be read that 'One' Parent can have 'Many' children.

  • In this diagram, One Product can be associated with Many Customer_Orders_Products.

  • The line between Products and Customer_Orders_Products is a continuous line (not a dotted line).

  • This means that the 'product_id' field is a Primary Key in the Customer_Orders_Products Table.

  • A dotted line would indicate the existence of a Foreign Key (which is defined above).
    'Crows-Feet'

  • At the Customer_Orders_Products end, there is what's called a 'Crows-Feet' symbol.

  • This indicates the 'Many' aspect of the relationship.

  • The little 'o' shows that this is an Optional relationship.

  • In other words, not every Product has an associated record in the Customer_Orders_Products Table.

  • This applies, of course, to Products that nobody orders.

  • At the other end of the line there is a small horizontal line. This means that this is essential.

  • In other words, every product_id in the Customer_Orders_Products Table must match a product_id in the products Table.

  • This means that a Customer cannot order a Product that does not exist.

  • In passing, let me say that this is different from the Product not being in stock.

  • This aspect is called Inventory Control and is covered by a different Database Schema.

  • The Relationship between Customer_Orders and Customer_Orders_Products shows a short horizontal line at each end.

  • This indicates that every Order must contain at least one Product.

  • It also indicates that Product in every Customer_Orders_Products must be associated with an Order in Customer_Orders.Many-to-Many Relationships.

  • Many-to-Many Relationships are very important in the real world, and we see them all around us every day.

  • In our example, many Customers can order the same Product.

  • Many Products can be ordered by the same Customer.

  • This is called a Many-to-Many Relationship. It cannot be implemented directly in a Table in a Relational Database.

  • The solution is to create an intermediate table that records all combinations that exist of Customer and Product.

  • For example, if a Customer ordered three Products, then there would be three records for that Customer in this intermediate table.

  • The combination of the Primary keys in the parent Tables become the Primary key in this intermediate table.

  • We also need to allow for Comments like 'Make it extra strong, please'.



  • If we get the structure to the Database right, then it's straightforward to extend it.


 

Komentar

  1. I hae been browsing on-line more than three hours these days,
    but I never found any fascinating article like
    yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you
    did, the net will probably be a lot more helpful tthan ever before.

    BalasHapus
  2. Hello buddy is see you aren't monetizing your blog.
    You can earn a lot using one simple method, search on youtube: how to make
    money online reselling seoclerks

    BalasHapus

Posting Komentar

Postingan populer dari blog ini

Totolink N300RT

Panduan singkat install OpenWrt Totolink N300RT Download FrmUpg :  http://www.anphat.vn/download/FirmwareUpgradeTool.zip Putty : http://www.putty.org/ Frimware totolink v2.1.6 :  http://www.totolink.net/include/download.asp?path=down/010300&file=N300RT-V2.1.6_20160516.zip Bootloder boot96E_32M:  https://drive.google.com/open?id=1p0SreEifzElS_cyngZZRL2lmGnR-Mvvi OpenWrt  :  https://drive.google.com/open?id=1ykv6QkrqXJ_8vnX-6imib44XGFVNJZoA A. Downgrade Frimware Totolink ke v2.1.6 Dalam router dalam keadaan mati tekan Tombol Reset lalu hidupkan power. tunggu sampai lampu indikator lan berkedip. Matikan Firewall dan Buka FrmUpg.exe dan colokan LAN ke Port 1,2,3,4 Di Frimware File pilih file Totolink v2.1.6 dan klik send, Tunggu sampai prosess selesai lalu restart router, cek 192.168.1.1 apakah sudah berhasil downgrade. B. Upgrade OpenWrt Ulangi langkah no 1, Pilih file bootloder boot96E_32M. klik send. tunggu sampai selesa...

How to customize tab colors in SSMS

Applies to ApexSQL Complete Summary This article describes how to set a custom connection color to a query tab in Microsoft  SQL Server Management Studio  (SSMS) using a feature called  Tab coloring  in ApexSQL Complete. Description Tab coloring can set custom connection colors for individual instances of SQL Server, down to the database level. The user has ability to assign SQL servers, and databases to a specific environment to help quickly identify which connection a tab is currently using.

DemoHelper tool buat Coret-coret Layar Komputer

Salah satu tool untuk membatu presentasi.  Sample : Zooming Mode up zooms in down zooms out enter or left mouse click activates drawing of the zoomed area escape cancels zooming mode Zooming is also possible using the mouse wheel. Drawing Mode up/down or Ctrl+Mousewheel increases/decreases the size of the drawing brush right or Mousewheel cycles through the available brush colors 0-9 select the brush colors Shift while drawing forces horizontal/vertical drawing Control while drawing drawing straight lines right mouse-drag draws arrow lines right mouse-drag with control key pressed draws straight lines escape quits drawing mode backspace removes the last line drawn delete removes the first line drawn E removes all drawn lines M toggles the marker on/off T toggles between solid and merged drawing Z select a rectangle to zoom in C clears the screen with the background color