Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Not only is the SQL Server node in SQL Server Object Explorer similar to SQL Server Management Studio (SSMS) visually, but you can create new objects using contextual menus that function like their SSMS counterparts.
For example, you can create a new database under the Databases node. Similarly, you can select a specific database and create or edit table definitions and their related programming objects on-the-fly using the new Table Designer. From the Table Designer, you can switch to a script pane, which allows you to directly edit the script that defines this table.
Create a new database
- In SQL Server Object Explorer, under the SQL Server node, expand your connected server instance.
- Right-click the Databases node and select Add New Database.
- Rename the new database to
Trade.
Create new tables using the Table Designer
Expand the newly created
Tradenode. Right-click the Tables node and select Add New Table.The Table Designer opens in a new window. The designer consists of the Columns Grid, Script pane, and Context pane. The Columns Grid lists all the columns in the table. We revisit other components of the designer in later procedures.
In the Script pane, rename the new table to
Suppliers. Specifically, replaceCREATE TABLE [dbo].[Table1]with
CREATE TABLE [dbo].[Suppliers]Select the empty row in the Columns Grid to add a new column to the table. Enter CompanyName for the Name field, nvarchar (128) for Data Type and uncheck the Allow Nulls field. As you tab away from the fields, notice that the Script pane is updated immediately.
Add another new column. Enter Address for the Name field, nvarchar (MAX) for Data Type and uncheck the Allow Nulls field.
Warning
When you're editing objects from a connected database, don't save them to your local drive. To save your changes to the database properly, follow the steps in the next How to: Update a connected database with Power Buffer procedure.
Repeat the above steps to create another table named Customer. This time, add the following columns to the Customer table using the Columns Grid. And remember to change the script so that the table's name is
[dbo].[Customer].Name Data type Allow nulls Idint unchecked Namenvarchar (128) unchecked Create one more table named
Products. Add the following columns to the Products table using the Columns Grid. And remember to change the script so that the table's name is[dbo].[Products].Name Data type Allow nulls Idint unchecked Namenvarchar (128) unchecked ShelfLifeint checked SupplierIdint checked CustomerIdint checked
Create a new check constraint using the Table Designer
The Context pane of the Table Designer gives you a logical view of the table definition (Keys, Constraints, Triggers, etc.), and enables you to select an object to highlight its relationships to individual columns.
For the Products table, right-click the Check Constraints node in the Context pane of the table designer, and select Add New Check Constraint.
The node count automatically increments by 1.
Select the Script pane, and replace the default definition of the constraint with the following.
CONSTRAINT [CK_Products_ShelfLife] CHECK ([ShelfLife] <5),This constraint limits the value of ShelfLife for a row to be under 5.
Create new foreign key references using the Table Designer
For the Products table, right-click the Foreign Keys node in the Context pane, and select Add New Foreign Key.
The node count automatically increments by 1.
Select the Script pane, and replace the default definition of the foreign key reference with the following code.
CONSTRAINT [FK_Products_SupplierId] FOREIGN KEY ([SupplierId]) REFERENCES [dbo].[Suppliers] ([Id]),Repeat the previous steps to add another foreign key reference to the Products table. This time, replace the default definition with the following code.
CONSTRAINT [FK_Products_CustomerId] FOREIGN KEY ([CustomerId]) REFERENCES [dbo].[Customer] ([Id])