Pages

Tuesday, May 20, 2014

Steps to Create Tables in SQL Server

You can create a new table, name it, and add it to an existing database in SQL Server 2014 by using SQL Server Management Studio or Transact-SQL.

1. Using SQL Server Management Studio

To create a table with Table Designer

  1. In Object Explorer, connect to the instance of Database Engine that contains the database to be modified.
  2. In Object Explorer, expand the Databases node and then expand the database that will contain the new table.
  3. In Object Explorer, right-click the Tables node of your database and then click New Table.
  4. Type column names, choose data types, and choose whether to allow nulls for each column as shown in the following illustration.
    AddColumnsinTableDesigner
  5. To specify more properties for a column, such as identity or computed column values, click the column and in the column properties tab, choose the appropriate properties.
  6. To specify a column as a primary key, right-click the column and select Set Primary Key
  7. To create foreign key relationships, check constraints, or indexes, right-click in the Table Designer pane and select an object from the list as shown in the following illustration.
    AddTableObjects

  8. By default, the table is contained in the dbo schema. To specify a different schema for the table, right-click in the Table Designer pane and select Properties as shown in the following illustration. From the Schema drop-down list, select the appropriate schema.
    Specifyatableschema

  9. From the File menu, choose Save table name.
  10. In the Choose Name dialog box, type a name for the table and click OK.
  11. To view the new table, in Object Explorer, expand the Tables node and press F5 to refresh the list of objects. The new table is displayed in the list of tables.
2.Using Transact-SQL

To create a table in the Query Editor


  1. In Object Explorer, connect to an instance of Database Engine.
  2. On the Standard bar, click New Query.
  3. Copy and paste the following example into the query window and click Execute.
    CREATE TABLE dbo.PurchaseOrderDetail
    (
        ID int NOT NULL
        ,LNumber smallint NOT NULL
        ,ProductID int NULL
        ,Price money NULL
        ,OrderQty smallint NULL
        ,ReceivedQty float NULL
        ,RejectedQty float NULL
        ,DueDate datetime NULL
    );

Friends, If you like this post, please share with your friends on Facebook and Google+ and recommend us on Google using the g+1 button on the top right hand corner.

No comments:

Post a Comment