Create new table - DB first

Creating a basic table involves naming the table and defining its columns and each column's data type.

The SQL CREATE TABLE statement is used to create a new table.

Syntax

The basic syntax of the CREATE TABLE statement is as follows


CREATE TABLE table_name(
   column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   columnN datatype,
   PRIMARY KEY( one or more columns )
);

 

CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement.

Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer with the following example.

A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. You can check the complete details at Create Table Using another Table

Example:


CREATE TABLE tblCustomers(
   ID			INT              NOT NULL,
   NAME			NVARCHAR (20)     NOT NULL,
   AGE			INT              NOT NULL,
   ADDRESS		NVARCHAR (150) ,
   SALARY		DECIMAL (18, 2),       
   PRIMARY KEY (ID)
);

You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the sp_help command as follows


sp_help 'tblcustomers'

Summary:

Creating a basic table involves naming the table and defining its columns and each column's data type.