How to create and delete an MSSQL table

Learn how to create and drop tables in the database. The first is created using SQL Server Management Studio, We will also explain how to use Transact-SQL as a method used by experts, so please try it out.

MS SQL table concept description

  • It’s easy to understand if you think of it as a set of data in a database consisting of the structure of table rows and columns.
  • Each row represents a record, and each column represents a field.
  • The table is the most basic unit for storing data structurally in a database.
  • For example, use table to store student information, grade content, etc.

How to create an MS SQL Table

How to create a table through SQL Server Management Studio (SSMS)

1-1. Select Object explorer - Database - Creation Database - Tables - Right mouse - new - Table. Select Object explorer.

1-2. TABLE and column input screen.

  • Enter id,name,age,hp_number,write,comment.
  • View and enter the Data Type capture screen.
  • Allow Nulls report and enter.
  • Click Save when you’re done typing. Click Save when you're done typing.

1-3. Table Name Input Pop-up.

  • Enter the desired name and click OK. Enter the desired name and click OK.

1-4. Refresh the Object explorer.

  • You can verify that the table has been created. You can verify.

Create a table using Transact-SQL

2-1. In the SQL Run window, type the source and click EXE.

USE [sampleDB]
GO
CREATE TABLE [dbo].[userInfo](
	[id] [nvarchar](50) NOT NULL,
	[name] [nvarchar](50) NULL,
	[age] [smallint] NULL,
	[hp_number] [nvarchar](50) NULL,
	[writeDate] [smalldatetime] NULL,
	[comment] [nvarchar](max) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

click EXE.

2-2. Check for errors and check the table created by Object explorer.

Check for errors.

How to rename a table using SSMS.

3-1. Right mouse - Rename in the table where you want to name Object explorer - Database - Create Database - Tables - Rename. Rename in the table.

3-2. If the name of the selected table changes to the modification mode, you can enter the name again and hit Enter to confirm. you can enter the name again and hit Enter.

How to delete MSSQL table

Delete table through SQL Server Management Studio (SSMS)

4-1. Select Object explorer - Database - Create Database - Tables - Right mouse - Delete from the table you want to delete. Select Object explorer.

4-2. Click the OK button in the Delete Object window. Delete Object window.

Delete table created using Transact-SQL

5-1. In the SQL Run window, type the source and click Excute.

USE [sampleDB]
GO 
DROP TABLE [dbo].[userInfoFortsql]
GO

click Excute.

5-2. Refresh Object explorer. Make sure the table is dropped. the table is dropped.

How to deal with an error

Msg 3701, Level 11, State 5, Line 6
Cannot delete table ‘dbo.userInfoFortsql1’ because it does not exist or does not have permission.

USE [sampleDB]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[userInfoFortsql]') AND type in (N'U'))
DROP TABLE [dbo].[userInfoFortsql]
GO

Categories:

Updated:

Leave a comment