[ Web Proxy ]
URL:
Viewing: https://www.sqlservertutorial.net/sql-server-basics/sql-server-create-table/ [Back]  [Original]

SQL Server CREATE TABLE: Creating a New Table in the Database
Skip to content

SQL Server CREATE TABLE

Savigo [Savigo]
You organize your data. Now organize your savings.
Set savings goals, track your progress, and stay on course.
Get Savigo for iPhone 

Summary: in this tutorial, you will learn how to use the SQL Server CREATE TABLE statement to create a new table.

Introduction to the SQL Server CREATE TABLE statement #

Tables are used to store data in the database. Tables are uniquely named within a database and schema. Each table contains one or more columns. And each column has an associated data type that defines the kind of data it can store e.g., numbers, strings, or temporal data.

To create a new table, you use the CREATE TABLE statement as follows:

CREATE TABLE [database_name.][schema_name.]table_name (
    pk_column data_type PRIMARY KEY,
    column_1 data_type NOT NULL,
    column_2 data_type,
    ...,
    table_constraints
);
Code language: SQL (Structured Query Language) (sql)

In this syntax:

Note that CREATE TABLE is complex and has more options than the syntax above. We will gradually introduce you to each individual options in the subsequent tutorials.

SQL Server CREATE TABLE example #

The following statement creates a new table named sales.visits to track the customer in-store visits:

CREATE TABLE sales.visits (
    visit_id INT PRIMARY KEY IDENTITY (1, 1),
    first_name VARCHAR (50) NOT NULL,
    last_name VARCHAR (50) NOT NULL,
    visited_at DATETIME,
    phone VARCHAR(20),
    store_id INT NOT NULL,
    FOREIGN KEY (store_id) REFERENCES sales.stores (store_id)
);
Code language: SQL (Structured Query Language) (sql)

In this example:

Because we do not specify the name of the database explicitly in which the table is created, the visits table is created in the BikeStores database. For the schema, we specify it explicitly, therefore, the visits table is created in the sales schema.

The visits table contains six columns:

In this tutorial, you have learned how to use the SQL Server CREATE TABLE statement to create a new table in a database.

Was this tutorial helpful?
Yes No
Search …:

Getting Started

Data Manipulation

Data Definition

Data Types

Expressions

Copyright © 2025 by www.sqlservertutorial.net. All Rights Reserved.

Microsoft Corporation does not sponsor the tutorials on sqlservertutorial.net, and this website has no relationship with Microsoft Corporation.

Web Proxy Viewer  |  New URL  |  Original Page