Create New Table in SQL Server 2019
Tables are database objects that contain all the data in a database. In a table, data is logically organized in rows and columns. Each column represents a field and each row represents a unique record. Each column has a data type associated with it. It represents the type of data in that column. Each table is uniquely named in a database.
Number of tables in a database is only limited by the number of objects allowed in the database (2,147,483,647). A user-defined table can have up to 1024 columns.
There are two ways to create a new table in SQL Server:
Create Table using T-SQL Script
You can execute the SQL script in the query editor of SSMS to create a new table in SQL Server.
CREATE TABLE [database_name.][schema_name.]table_name ( pk_column_name data_type PRIMARY KEY, column_name2 data_type [NULL | NOT NULL], column_name3 data_type [NULL | NOT NULL], ..., [table_constraints] );
The following creates the
Employee table in the
CREATE TABLE HR.dbo.Employee ( EmployeeID int IDENTITY(1,1) PRIMARY KEY, FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL, EMail nvarchar(50), Phone varchar(15), HireDate date, ManagerID int, Salary float, DepartmentID int );
The above T-SQL script creates the
HRis the database name,
dbois the schema name in which a table will belongs to, and
Employeeis a table name. If the database name is not specified then it defaults to the current database that the query editor is targetting. If the schema name is not specified then the table will belongs to a default schema.
- A table name can be a maximum of 128 characters.
Make a column as a primary key by specifying
PRIMARY KEYwith the column name. If a table has only one primary key like above, then specify
PRIMARY KEY. If the Primary key is a combination of more than two or more columns then you need to specify it as a table constraint at the end of the CREAT statement.
The following drops the
Employee table if already exists and then creates the
USE HR GO DROP TABLE IF EXISTS HR.dbo.Employee; --drop table if already exists CREATE TABLE Employee ( EmployeeID int IDENTITY(1,1) PRIMARY KEY, FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL, EMail nvarchar(50), Phone varchar(15), HireDate date, ManagerID int, Salary float, DepartmentID int );
The following specifies the
Primary Key constraints at the end.
USE HR GO DROP TABLE IF EXISTS HR.dbo.Employee; --drop table if already exists CREATE TABLE Employee ( EmployeeID int IDENTITY(1,1), FirstName nvarchar(50) NOT NULL, LastName nvarchar(50) NOT NULL, EMail nvarchar(50), Phone varchar(15), HireDate date, ManagerID int, Salary float, DepartmentID int, CONSTRAINT pk_Employee_EmployeeID PRIMARY KEY(EmployeeID) );
Learn full syntax of CREATE TABLE.
Create Table using SSMS
You can also create a new table using SQL Server Management Studio.
Open SSMS and connect to the database instance.
In Object Explorer, expand the
Right-click on the Tables folder and select New Table.
This will open a table designer where you can enter a name of a column, its data type, and check the checkbox if a column allows null, as shown below.
Column Name: Type a unique column name.
Data type: Choose a data type for the column from the dropdown list. Choose the appropriate length for the string data types.
Allow Nulls: Choose whether to allow Nulls for each column by check the check-box.
Enter columns info in a separate row for all the columns you want to take in your table. The followings are columns of the
To set the properties for a column, such as identity specification, computed column specification, etc., select a column and set the property value in the
Column Properties tab in the bottom pane.
To specify a column as the Primary Key, right-click the column and select Set Primary Key.
You can configure the primary key that will auto generate unique integers by setting it as identity column, as shown below.
By default, the table is created in the dbo schema. To specify a different schema for the table, right-click in the Table-Designer pane and select properties. From the Schema drop-down list, select the appropriate schema.
Now, from the file menu, choose Save to create this table.
Employee as a table name and click OK.
To view the new table, refresh the Tables folder in the Object Explorer. The EMPLOYEE table is now available under the Tables node.
Thus, you can create a new table using SSMS.