Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a simple example of a classic Order example. Each <em>Customer</em> can have multiple <em>Order</em>s, and each <em>Order</em> can consist of multiple <em>OrderLine</em>s.</p> <p>You create a relation by adding a <a href="http://en.wikipedia.org/wiki/Foreign_key" rel="nofollow noreferrer">foreign key</a> column. Each Order record has a CustomerID in it, that points to the ID of the Customer. Similarly, each OrderLine has an OrderID value. This is how the database diagram looks:</p> <p><a href="https://i.stack.imgur.com/y7gjY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y7gjY.png" alt="enter image description here"></a></p> <p>In this diagram, there are actual <a href="http://msdn.microsoft.com/en-us/library/ms175464.aspx" rel="nofollow noreferrer">foreign key constraints</a>. They are optional, but they ensure integrity of your data. Also, they make the structure of your database clearer to anyone using it.</p> <p>I assume you know how to create the tables themselves. Then you just need to define the relationships between them. You can of course define constraints in T-SQL (as posted by several people), but they're also easily added using the designer. Using SQL Management Studio, you can right-click the <em>Order</em> table, click <strong>Design</strong> (I think it may be called Edit under 2005). Then anywhere in the window that opens right-click and select <strong>Relationships</strong>.</p> <p>You will get another dialog, on the right there should be a grid view. One of the first lines reads "<strong>Tables and Columns Specification</strong>". Click that line, then click again on the little [...] button that appears on the right. You will get this dialog:</p> <p><a href="https://i.stack.imgur.com/lWPmd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lWPmd.png" alt="Foreign key constraint"></a></p> <p>The <em>Order</em> table should already be selected on the right. Select the Customer table on the left dropdown. Then in the left grid, select the <code>ID</code> column. In the right grid, select the <code>CustomerID</code> column. Close the dialog, and the next. Press <strong>Ctrl+S</strong> to save.</p> <p>Having this constraint will ensure that no Order records can exist without an accompanying Customer record.</p> <p>To effectively query a database like this, you might want to <a href="http://en.wikipedia.org/wiki/Join_%28SQL%29" rel="nofollow noreferrer">read up on JOINs</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload