Академический Документы
Профессиональный Документы
Культура Документы
You can explore basic tasks, such as adding tables and defining columns, by using Visual Studio to create
a local database file in SQL Server Express LocalDB. You perform these tasks by using the Table Designer,
which was significantly changed in 2012, and this topic reflects those updates. After you finish this
walkthrough, you can discover more advanced capabilities by using your local database as a starting point
for other walkthroughs that require it.
During this walkthrough, you'll explore the following tasks:
Create a project and a local database file.
Create tables, columns, primary keys, and foreign keys.
Populate the tables with data, and save them.
When you create a service-based database in Visual Studio, the SQL Server Express LocalDB engine is
used to access a SQL Server 2012 database file (.mdf). In earlier versions of Visual Studio, the SQL Server
Express engine is used to access a database file (.mdf). See Local Data Overview.
4. Open the shortcut menu for the CustomerID row, and then choose Set Primary Key.
5. Open the shortcut menu for the default row, and then choose Delete.
6. Name the Customers table by updating the first line in the script pane to match the following
sample:
7. CREATE TABLE [dbo].[Customers]
8. In the upper-left corner of the Table Designer, choose the Update button.
9. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.
To create the Orders table
1. In Server Explorer/Database Explorer, open the shortcut menu for Tables, and then
choose Add New Table.
2. In the grid, add a row for each entry in the following table:
3. Open the shortcut menu for the OrderID column, and then choose Set Primary Key.
4. Open the shortcut menu for the default row, and then choose Delete.
5. Name the Orders table by updating the first line in the script pane to match the following sample:
6. CREATE TABLE [dbo].[Orders]
7. In the upper-left corner of the Table Designer, choose the Update button.
8. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.
To create a foreign key
1. In the context pane on the right side of the grid, open the shortcut menu for Foreign Keys,
and then choose Add New Foreign Key.
2. In the text box that appears, replace ToTable with Customers.
3. In the script pane, update the last line to match the following sample:
4. CONSTRAINT [FK_Orders_Customers] FOREIGN KEY ([CustomerID]) REFERENCES
[Customers]([CustomerID])
5. In the upper-left corner of the Table Designer, choose the Update button.
6. In the Preview Database Updates dialog box, choose the Update Database button.
Your changes are saved to the local database file.
Important
Make sure that each value in the CustomerID column of the Orders table matches a value
in the CustomerID column of the Customers table.
Note
As a best practice, you can back up the database file that you just created by copying it
and then either pasting the copy in another location or giving the copy a different name.