In this blog, I will show how to create Table in MS SQL using T-SQL command.  explain how its works and why we need to do this. Let’s Go.

Subscribe to my mailing List to notify every new posted topics:

I. What is Table?

Table is where data dump or stored in Database. A table is a collection of related data held in a table format within a database. It consists of columns and rows. In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect.

Create Statement

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

CREATE TABLE table_name (
    pk_column data_type PRIMARY KEY,
    column_1 data_type NOT NULL,
    column_2 data_type,
    ...,
    table_constraints
);

As we begin to create table, I expect that you are already logging on in your MS SQL Management studio. For more topics about SQL just click  HERE.

I just want to create the table belong in my testDB Database . so to open the Query tab. Just Right click the database name “testDB” and select or click “New Query”.

This is my sample T-SQL State to create table. I just want to create student table in my testDB database.

  • Create Table stable is a command statement to create a table in the database.
  • Student is the name of table which I want to create in my Database name testDB.
  • Columns – student_id, firstname, lastname, middlename, and TDT is the sample column belong the table student I created.
  • Datatype – this is the assign datatype of each column. Int for student_id to use is a primary key. Varchar and datetime for the others. there many datatype but this is what I commonly use of those.
  • Not Null means table column is not open for null value. so every new row those column must have data. it is required. to create column that considered null value is to use only Null.
  • Key Identify(1,1) mean is to enable autoincrement number.

This is the SQL statement.


Create Table student (
	student_id INT Primary KEY IDENTITY (1, 1),
	firstname VARCHAR(50) NOT NULL, 
	lastname VARCHAR(50) NOT NULL,
	middlename VARCHAR(50) NOT NULL,
	TDT datetime
);

Here is it, table successfully created.

After refreshing the database, the student table appear.

this is the table design. the student_id set as primary key and also autoincrement. and the other column is set correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *