SQL Server CREATE, ALTER, DROP Table [T-SQL Examples]

โšก Smart Summary

SQL Server tables store data in rows and columns. Transact-SQL uses CREATE TABLE to build a table, ALTER TABLE to add or change columns, and DROP TABLE to remove the table, its data, and its structure.

  • ๐Ÿงฑ What is a table: A table stores data in rows and columns and is the basic object for holding data in a database.
  • ๐Ÿ› ๏ธ CREATE TABLE: The CREATE TABLE statement defines each column with a name, a data type, and a NULL or NOT NULL setting.
  • ๐Ÿ“‹ Create from existing: SELECT … INTO copies chosen columns and rows from an existing table into a brand-new table.
  • โž• ALTER TABLE: ALTER TABLE … ADD adds new columns to a table without recreating it.
  • ๐Ÿ—‘๏ธ DROP TABLE: DROP TABLE removes the entire table, including its data and its structure.
  • ๐Ÿ–ฑ๏ธ Table Designer: SQL Server Management Studio can also create and alter tables visually through Table Designer.

SQL Server CREATE, ALTER and DROP Table with T-SQL Examples

What is a Table?

A table is an object in SQL Server that stores data in a row-and-column format. The diagram below shows rows and columns respectively.

Diagram of a table storing data in rows and columns

How to Create a Table in SQL Server with T-SQL

The first step to storing data is to create a table in the database where the data will reside. After creating the table, you can keep inserting rows into it.

You can create a table in the following ways:

  • T-SQL: create a new table by defining all columns and their data type.
  • T-SQL: create a new table using an existing table.
  • Using Table Designer.

Let us study this in detail.

T-SQL: Create a New Table by defining all columns and their data type

Below is the syntax to create a table in SQL Server.

Syntax:

CREATE TABLE tableName
( 
  column_1 datatype [ NULL | NOT NULL ],
  column_2 datatype [ NULL | NOT NULL ],
  ...
);

Here,

  • The parameter tableName denotes the name of the table that you are going to create.
  • The parameters column_1, column_2… denote the columns to be added to the table.
  • A column should be specified as either NULL or NOT NULL. If you do not specify, SQL Server takes NULL as the default.

Let us create a basic table with two columns.

Query:

CREATE TABLE COURSE
(
Course_ID Int,
Course_Name Varchar(10)
)

Pre-Requisite: Select the database where you need to create the table.

Step 1) Run the query by clicking Execute. Before running the query, no table exists, as shown below.

Object Explorer showing no Course table before the CREATE statement runs

After running the query, click the Refresh button; the new table now exists as Course, as shown below.

Course table listed in Object Explorer after execution and refresh

Step 2) Insert some data. The screenshots below show the newly created Course table ready to receive rows.

Newly created Course table selected in Object Explorer before inserting data

Course table structure with its two columns shown before data insertion

Until now the table only exists, but there is no data in it yet.

Transact-SQL provides the INSERT statement to insert data into a table. With this statement, you can insert one or more rows into a table.

Syntax:

INSERT INTO tableName
(column_1, column_2, ... )
VALUES
(expression_1, expression_2, ... ),
(expression_1, expression_2, ... ),
...;

The syntax above shows the basic form of the command when using the VALUES keyword to insert data into a table.

Let us insert four rows using the INSERT command.

Insert into COURSE values (1,'SQL');
Insert into COURSE values (2,'Python');
Insert into COURSE values (3,'SQL');
Insert into COURSE values (4,'C');

The snapshot below shows that the insertion of four rows is successful.

Messages pane confirming four rows inserted into the Course table

Step 3) Use the SELECT statement to view the data. To view data in SQL Server, use the following syntax:

SELECT expression
FROM tableName
[WHERE condition];

Example Query:

SELECT * FROM COURSE;

The table was created and now holds four records. Note that you can create records with a duplicate value in any of the columns, because there are no constraints.

Results grid showing the four Course rows returned by SELECT

T-SQL: Create a New Table using an existing table

Suppose you want another table like the COURSE table created above, but you need only the Course_Name column and not Course_ID.

Syntax:

SELECT (Column 1, โ€ฆ) INTO <New Table name> FROM <Old Table name>;

Example Query:

SELECT COURSE_NAME INTO COURSE_NAMES FROM COURSE;

Step 1) Run the query by clicking Execute. A new table named COURSE_NAMES is created with one column and four records copied from the older table, as shown below.

New COURSE_NAMES table created from an existing table using SELECT INTO

Example Query:

SELECT * FROM COURSE_NAMES;

The result below lists the four Course_Name records in the new COURSE_NAMES table.

Results grid showing the Course_Name values in the COURSE_NAMES table

Using Table Designer

You can also create a table from the SQL Server Management Studio IDE using Table Designer.

Step 1) Right-click Table > New > Table, as shown below.

Right-click Table then New then Table in Object Explorer

Step 2) Enter the column names and data types, and choose whether each column allows nulls.

Table Designer toolbar for defining a new table

Table Designer grid with column names, data types, and Allow Nulls options

Step 3) Press CTRL+S to save the table, and enter the name as Course_Title.

Choose Name dialog to save the new table as Course_Title

After clicking Refresh, the new table appears with the name Course_Title and one column named Course_Name, as shown below.

Course_Title table with a single Course_Name column in Object Explorer

Alter Table in SQL Server

There are two ways to alter a table in SQL Server:

  • T-SQL: alter the table by adding new columns.
  • Using Table Designer.

T-SQL: Alter Table by inserting columns

Below is the syntax of the ALTER command in SQL:

Syntax:

 Alter TABLE <Table name> ADD Column1 datatype, Column2 datatype;

Query:

ALTER TABLE dbo.Course_Title ADD Course_Duration VARCHAR(20);

Step 1) Run the query by clicking Execute. A new column named Course_Duration now exists within the Course_Title table, as shown below.

ALTER TABLE ADD statement adding the Course_Duration column

Course_Title columns including the newly added Course_Duration column

Using Table Designer

You can also alter a table from the UI using Table Designer.

Step 1) Right-click the existing table > Design, as shown below.

Right-click the Course_Title table and choose Design

Step 2) Add the column name as Course_Start_Date and select its data type.

Adding the Course_Start_Date column and its data type in Table Designer

Step 3) Press CTRL+S to save the newly added column. A new column named Course_Start_Date is created in the Course_Title table, as shown below.

Course_Title table showing the saved Course_Start_Date column

Delete Table in SQL Server

You delete a table when it is no longer required.

There are two ways to delete a table in SQL Server:

  • Using SQL Server Management Studio.
  • T-SQL: delete the table with DROP TABLE.

Using SQL Server Management Studio

Step 1) Right-click the existing table > Delete, for the table named Course_Title, as shown below.

Right-click the Course_Title table and choose Delete

Step 2) On the Delete Object window, click OK.

Delete Object confirmation window with the OK button

The Course_Title table is now deleted from the table list, as shown below.

Object Explorer table list after the Course_Title table is deleted

T-SQL: Delete Table

Below is the syntax to drop a table.

Syntax:

DROP TABLE <tableName>;

The parameter tableName is the name of the table that is to be deleted.

Query:

DROP TABLE COURSE_NAMES;

Step 1) Run the query by clicking Execute. The COURSE_NAMES table is deleted from the table list, as shown below.

DROP TABLE statement removing the COURSE_NAMES table

Object Explorer table list after the COURSE_NAMES table is dropped

Alternatively, the source shows a DELETE TABLE command intended to remove only rows (data) while leaving the table structure intact, so the table can be reused later.

Syntax:

DELETE TABLE <Table name>;

Note: In standard T-SQL, rows are removed with DELETE FROM <table>, and a table is emptied quickly with TRUNCATE TABLE <table>; the whole table is removed with DROP TABLE, shown above.

Interesting Facts!

  • Large files such as XML or images can be stored in a column using large-object data types like VARBINARY(MAX), VARCHAR(MAX), or the XML type (SQL Server does not use the Oracle-style BLOB and CLOB names).
  • DELETE removes only rows and can be rolled back within a transaction, while DROP removes the whole table structure; in SQL Server, even DROP can be rolled back when it runs inside an explicit transaction.

FAQs

DELETE removes chosen rows and is fully logged. TRUNCATE quickly removes all rows and resets identity values. DROP removes the whole table, its data, and its structure. In SQL Server, all three can be rolled back inside an explicit transaction.

Use DROP TABLE IF EXISTS table_name, available since SQL Server 2016. It removes the table when present and does nothing when it is absent, avoiding an error. On older versions, check OBJECT_ID before dropping.

SQL Server has no RENAME TABLE statement. Instead run the system stored procedure sp_rename, for example EXEC sp_rename ‘old_name’, ‘new_name’. Update dependent views, procedures, and code afterward, because sp_rename does not change those references automatically.

Yes. ALTER TABLE table ALTER COLUMN column new_type changes a column’s data type, and ALTER TABLE table DROP COLUMN column removes it. The change succeeds only when existing data is compatible, so test conversions and back up first.

Define constraints inside CREATE TABLE, such as Course_ID INT PRIMARY KEY, or add them later with ALTER TABLE ADD CONSTRAINT. A primary key enforces uniqueness, while a foreign key links rows across tables.

A temporary table, named with a # prefix, is created in tempdb and dropped automatically when the session ends. A regular table is permanent until you drop it. Temporary tables suit short-lived intermediate results inside a batch or procedure.

Yes. GitHub Copilot can produce CREATE, ALTER, and DROP TABLE scripts from natural-language prompts and suggest columns, data types, and constraints. Always review the generated schema, keys, and nullability before running it in production.

AI and machine learning tools recommend table structures, data types, and indexes from sample data and query patterns, flag redundant or oversized columns, and warn about risky DROP or ALTER operations. The developer confirms each suggestion before applying it.

Summarize this post with: