|
1 | 1 | ---
|
2 |
| -title: "Create a SQL database by using a script | Microsoft Docs" |
3 |
| -ms.custom: "" |
4 |
| -ms.date: "11/04/2016" |
5 |
| -ms.reviewer: "" |
6 |
| -ms.suite: "" |
7 |
| -ms.tgt_pltfrm: "" |
8 |
| -ms.topic: "article" |
9 |
| -ms.assetid: 36f913c0-f5a7-4831-83a0-baba721ac95c |
10 |
| -caps.latest.revision: 14 |
11 |
| -author: "gewarren" |
12 |
| -ms.author: "gewarren" |
13 |
| -manager: "ghogen" |
14 |
| -translation.priority.ht: |
15 |
| - - "de-de" |
16 |
| - - "es-es" |
17 |
| - - "fr-fr" |
18 |
| - - "it-it" |
19 |
| - - "ja-jp" |
20 |
| - - "ko-kr" |
21 |
| - - "ru-ru" |
22 |
| - - "zh-cn" |
23 |
| - - "zh-tw" |
24 |
| -translation.priority.mt: |
25 |
| - - "cs-cz" |
26 |
| - - "pl-pl" |
27 |
| - - "pt-br" |
28 |
| - - "tr-tr" |
29 |
| ---- |
30 |
| -# Create a SQL database by using a script |
31 |
| -In this walkthrough, you use Visual Studio to create a small database that contains the sample code for [Create a simple data application by using ADO.NET](../data-tools/create-a-simple-data-application-by-using-adonet.md). |
32 |
| - |
33 |
| - **In this topic** |
34 |
| - |
35 |
| -- [Create a script that contains a database schema](../data-tools/create-a-sql-database-by-using-a-script.md#CreateScript) |
36 |
| - |
37 |
| -- [Create a database project and import a schema](../data-tools/create-a-sql-database-by-using-a-script.md#CreateProject) |
38 |
| - |
39 |
| -- [Deploy the database](../data-tools/create-a-sql-database-by-using-a-script.md#DeployDatabase) |
40 |
| - |
41 |
| -## Prerequisites |
42 |
| - To complete this walkthrough, you must have SQL Server Express LocalDB, or another SQL database, installed. |
43 |
| - |
44 |
| -## <a name="CreateScript"></a> Create a script that contains a database schema |
45 |
| - |
46 |
| -#### To create a script from which you can import a schema |
47 |
| - |
48 |
| -1. In [!INCLUDE[vsprvs](../code-quality/includes/vsprvs_md.md)], on the menu bar, select **File** > **New** > **File**. |
49 |
| - |
50 |
| - The **New File** dialog box appears. |
51 |
| - |
52 |
| -2. In the **Categories** list, select **General**. |
53 |
| - |
54 |
| -3. In the **Templates** list, select **Sql File**, and then select the **Open** button. |
55 |
| - |
56 |
| - The Transact-SQL editor opens. |
57 |
| - |
58 |
| -4. Copy the following Transact-SQL code, and then paste it into the Transact-SQL editor. |
59 |
| - |
60 |
| - ``` |
61 |
| - PRINT N'Creating Sales...'; |
62 |
| - GO |
63 |
| - CREATE SCHEMA [Sales] |
64 |
| - AUTHORIZATION [dbo]; |
65 |
| - GO |
66 |
| - PRINT N'Creating Sales.Customer...'; |
67 |
| - GO |
68 |
| - CREATE TABLE [Sales].[Customer] ( |
69 |
| - [CustomerID] INT IDENTITY (1, 1) NOT NULL, |
70 |
| - [CustomerName] NVARCHAR (40) NOT NULL, |
71 |
| - [YTDOrders] INT NOT NULL, |
72 |
| - [YTDSales] INT NOT NULL |
73 |
| - ); |
74 |
| - GO |
75 |
| - PRINT N'Creating Sales.Orders...'; |
76 |
| - GO |
77 |
| - CREATE TABLE [Sales].[Orders] ( |
78 |
| - [CustomerID] INT NOT NULL, |
79 |
| - [OrderID] INT IDENTITY (1, 1) NOT NULL, |
80 |
| - [OrderDate] DATETIME NOT NULL, |
81 |
| - [FilledDate] DATETIME NULL, |
82 |
| - [Status] CHAR (1) NOT NULL, |
83 |
| - [Amount] INT NOT NULL |
84 |
| - ); |
85 |
| - GO |
86 |
| - PRINT N'Creating Sales.Def_Customer_YTDOrders...'; |
87 |
| - GO |
88 |
| - ALTER TABLE [Sales].[Customer] |
89 |
| - ADD CONSTRAINT [Def_Customer_YTDOrders] DEFAULT 0 FOR [YTDOrders]; |
90 |
| - GO |
91 |
| - PRINT N'Creating Sales.Def_Customer_YTDSales...'; |
92 |
| - GO |
93 |
| - ALTER TABLE [Sales].[Customer] |
94 |
| - ADD CONSTRAINT [Def_Customer_YTDSales] DEFAULT 0 FOR [YTDSales]; |
95 |
| - GO |
96 |
| - PRINT N'Creating Sales.Def_Orders_OrderDate...'; |
97 |
| - GO |
98 |
| - ALTER TABLE [Sales].[Orders] |
99 |
| - ADD CONSTRAINT [Def_Orders_OrderDate] DEFAULT GetDate() FOR [OrderDate]; |
100 |
| - GO |
101 |
| - PRINT N'Creating Sales.Def_Orders_Status...'; |
102 |
| - GO |
103 |
| - ALTER TABLE [Sales].[Orders] |
104 |
| - ADD CONSTRAINT [Def_Orders_Status] DEFAULT 'O' FOR [Status]; |
105 |
| - GO |
106 |
| - PRINT N'Creating Sales.PK_Customer_CustID...'; |
107 |
| - GO |
108 |
| - ALTER TABLE [Sales].[Customer] |
109 |
| - ADD CONSTRAINT [PK_Customer_CustID] PRIMARY KEY CLUSTERED ([CustomerID] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF); |
110 |
| - GO |
111 |
| - PRINT N'Creating Sales.PK_Orders_OrderID...'; |
112 |
| - GO |
113 |
| - ALTER TABLE [Sales].[Orders] |
114 |
| - ADD CONSTRAINT [PK_Orders_OrderID] PRIMARY KEY CLUSTERED ([OrderID] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF); |
115 |
| - GO |
116 |
| - PRINT N'Creating Sales.FK_Orders_Customer_CustID...'; |
117 |
| - GO |
118 |
| - ALTER TABLE [Sales].[Orders] |
119 |
| - ADD CONSTRAINT [FK_Orders_Customer_CustID] FOREIGN KEY ([CustomerID]) REFERENCES [Sales].[Customer] ([CustomerID]) ON DELETE NO ACTION ON UPDATE NO ACTION; |
120 |
| - GO |
121 |
| - PRINT N'Creating Sales.CK_Orders_FilledDate...'; |
122 |
| - GO |
123 |
| - ALTER TABLE [Sales].[Orders] |
124 |
| - ADD CONSTRAINT [CK_Orders_FilledDate] CHECK ((FilledDate >= OrderDate) AND (FilledDate < '01/01/2020')); |
125 |
| - GO |
126 |
| - PRINT N'Creating Sales.CK_Orders_OrderDate...'; |
127 |
| - GO |
128 |
| - ALTER TABLE [Sales].[Orders] |
129 |
| - ADD CONSTRAINT [CK_Orders_OrderDate] CHECK ((OrderDate > '01/01/2005') and (OrderDate < '01/01/2020')); |
130 |
| - GO |
131 |
| - PRINT N'Creating Sales.uspCancelOrder...'; |
132 |
| - GO |
133 |
| - CREATE PROCEDURE [Sales].[uspCancelOrder] |
134 |
| - @OrderID INT |
135 |
| - AS |
136 |
| - BEGIN |
137 |
| - DECLARE @Delta INT, @CustomerID INT |
138 |
| - BEGIN TRANSACTION |
139 |
| - SELECT @Delta = [Amount], @CustomerID = [CustomerID] |
140 |
| - FROM [Sales].[Orders] WHERE [OrderID] = @OrderID; |
141 |
| - |
142 |
| - UPDATE [Sales].[Orders] |
143 |
| - SET [Status] = 'X' |
144 |
| - WHERE [OrderID] = @OrderID; |
145 |
| - |
146 |
| - UPDATE [Sales].[Customer] |
147 |
| - SET |
148 |
| - YTDOrders = YTDOrders - @Delta |
149 |
| - WHERE [CustomerID] = @CustomerID |
150 |
| - COMMIT TRANSACTION |
151 |
| - END |
152 |
| - GO |
153 |
| - PRINT N'Creating Sales.uspFillOrder...'; |
154 |
| - GO |
155 |
| - CREATE PROCEDURE [Sales].[uspFillOrder] |
156 |
| - @OrderID INT, @FilledDate DATETIME |
157 |
| - AS |
158 |
| - BEGIN |
159 |
| - DECLARE @Delta INT, @CustomerID INT |
160 |
| - BEGIN TRANSACTION |
161 |
| - SELECT @Delta = [Amount], @CustomerID = [CustomerID] |
162 |
| - FROM [Sales].[Orders] WHERE [OrderID] = @OrderID; |
163 |
| - |
164 |
| - UPDATE [Sales].[Orders] |
165 |
| - SET [Status] = 'F', |
166 |
| - [FilledDate] = @FilledDate |
167 |
| - WHERE [OrderID] = @OrderID; |
168 |
| - |
169 |
| - UPDATE [Sales].[Customer] |
170 |
| - SET |
171 |
| - YTDSales = YTDSales + @Delta |
172 |
| - WHERE [CustomerID] = @CustomerID |
173 |
| - COMMIT TRANSACTION |
174 |
| - END |
175 |
| - GO |
176 |
| - PRINT N'Creating Sales.uspNewCustomer...'; |
177 |
| - GO |
178 |
| - CREATE PROCEDURE [Sales].[uspNewCustomer] |
179 |
| - @CustomerName NVARCHAR (40), |
180 |
| - @CustomerID INT OUTPUT |
181 |
| - AS |
182 |
| - BEGIN |
183 |
| - INSERT INTO [Sales].[Customer] (CustomerName) VALUES (@CustomerName); |
184 |
| - SET @CustomerID = SCOPE_IDENTITY(); |
185 |
| - RETURN @@ERROR |
186 |
| - END |
187 |
| - GO |
188 |
| - PRINT N'Creating Sales.uspPlaceNewOrder...'; |
189 |
| - GO |
190 |
| - CREATE PROCEDURE [Sales].[uspPlaceNewOrder] |
191 |
| - @CustomerID INT, @Amount INT, @OrderDate DATETIME, @Status CHAR (1)='O' |
192 |
| - AS |
193 |
| - BEGIN |
194 |
| - DECLARE @RC INT |
195 |
| - BEGIN TRANSACTION |
196 |
| - INSERT INTO [Sales].[Orders] (CustomerID, OrderDate, FilledDate, Status, Amount) |
197 |
| - VALUES (@CustomerID, @OrderDate, NULL, @Status, @Amount) |
198 |
| - SELECT @RC = SCOPE_IDENTITY(); |
199 |
| - UPDATE [Sales].[Customer] |
200 |
| - SET |
201 |
| - YTDOrders = YTDOrders + @Amount |
202 |
| - WHERE [CustomerID] = @CustomerID |
203 |
| - COMMIT TRANSACTION |
204 |
| - RETURN @RC |
205 |
| - END |
206 |
| - GO |
207 |
| - CREATE PROCEDURE [Sales].[uspShowOrderDetails] |
208 |
| - @CustomerID INT=0 |
209 |
| - AS |
210 |
| - BEGIN |
211 |
| - SELECT [C].[CustomerName], CONVERT(date, [O].[OrderDate]), CONVERT(date, [O].[FilledDate]), [O].[Status], [O].[Amount] |
212 |
| - FROM [Sales].[Customer] AS C |
213 |
| - INNER JOIN [Sales].[Orders] AS O |
214 |
| - ON [O].[CustomerID] = [C].[CustomerID] |
215 |
| - WHERE [C].[CustomerID] = @CustomerID |
216 |
| - END |
217 |
| - GO |
218 |
| - ``` |
219 |
| - |
220 |
| -5. On the menu bar, select **File** > **Save SqlQuery_1.sql As...**. |
221 |
| - |
222 |
| - The **Save File As** dialog box appears. |
223 |
| - |
224 |
| -6. In the **File Name** box, enter `SampleImportScript.sql`, note the location where you'll save the file, and then select the **Save** button. |
225 |
| - |
226 |
| -7. On the menu bar, select **File** > **Close Solution**. |
227 |
| - |
228 |
| - Next, create a database project, and then import the schema from the script that you've created. |
229 |
| - |
230 |
| -## <a name="CreateProject"></a> Create a database project and import a schema |
231 |
| - |
232 |
| -#### To create a database project |
233 |
| - |
234 |
| -1. On the menu bar, select **File** > **New** > **Project**. |
235 |
| - |
236 |
| - The **New Project** dialog box appears. |
237 |
| - |
238 |
| -2. Under **Installed**, expand the **Templates** node, expand the **Other Languages** node, select the **SQL Server** category, and then select the **SQL Server Database Project** template. |
239 |
| - |
240 |
| - > [!NOTE] |
241 |
| - > The **Other Languages** node doesn't appear in all installations of Visual Studio. |
242 |
| - |
243 |
| -3. In the **Name** box, enter `Small Database`. |
244 |
| - |
245 |
| -4. Select the **Create directory for solution** check box if it isn't already selected. |
246 |
| - |
247 |
| -5. Clear the **Add to source control** check box if it isn't already cleared, and then select the **OK** button. |
248 |
| - |
249 |
| - The database project is created and appears in **Solution Explorer**. |
250 |
| - |
251 |
| - Next, import the database schema from the script. |
252 |
| - |
253 |
| -#### To import a database schema from a script |
254 |
| - |
255 |
| -1. On the menu bar, select **Project** > **Import** > **Script**. |
256 |
| - |
257 |
| -2. On the **Welcome** page, review the text, and then select the **Next** button. |
258 |
| - |
259 |
| -3. Select the **Single File** option button, and then select the **Browse** button. |
260 |
| - |
261 |
| - The **Import SQL Script** dialog box appears. |
262 |
| - |
263 |
| -4. Open the folder where you saved the SampleImportScript.sql file, select the file, and then select the **Open** button. |
264 |
| - |
265 |
| -5. Select the **Finish** button to close the **Import SQL Script** dialog box. |
266 |
| - |
267 |
| - The script is imported, and the objects that the script defines are added to your database project. |
268 |
| - |
269 |
| -6. Review the summary, and then click the **Finish** button to close the **Import SQL Script File** dialog box. |
270 |
| - |
271 |
| -7. In **Solution Explorer**, expand the Sales, Scripts, and Security folders of your project, and verify that they contain .sql files. |
272 |
| - |
273 |
| -8. In **SQL Server Object Explorer**, verify that the database appears under the **Projects** node. |
274 |
| - |
275 |
| - At this point, the database contains only system objects, such as tables and stored procedures. After you deploy the database, it will contain the user tables and stored procedures that the scripts define. |
276 |
| - |
277 |
| -## <a name="DeployDatabase"></a> Deploy the database |
278 |
| - When you press the **F5** key, you deploy (or publish) the database to a LocalDB database by default. You can deploy the database to a different location by opening the properties page for the project, selecting the **Debug** tab, and then changing the connection string. |
| 2 | +redirect_url: https://msdn.microsoft.com/en-us/library/hh864423(v=vs.103).aspx |
| 3 | +--- |
0 commit comments