Updated March 16, 2023
Introduction to T-SQL CREATE TABLE
T-SQL CREATE TABLE is defined as, we can create a table by providing a name to the table and also data type for its columns, as the ‘CREATE TABLE’ is the statement which can be utilized for creating a table by following its syntax, the tables are used to reserve data in the database. They have special names in the database; every table can carry more than one column, and the data present in the columns related to the data type means it can reserve numbers, strings, etc., types of data in the table.
Overview of T-SQL CREATE TABLE
In T-SQL, creating a table can be done by using the keyword ‘CREATE TABLE’ in the statement, which can ask for the system, and what the system has to do, which we can see in the syntax given below. First, the unique name identifier can observe the CREATE TABLE statement; then, it comes back in the bracket with a list of defining columns in the table. Also, we can keep the data classification, so from the syntax given below, we can understand the process of creating the table. It also has an option to create a copy of an existing table that can be generated by combining the CREATE TABLE and SELECT statements.
T-SQL CREATE NEW TABLE Statement
To generate a new table, we can able to utilize the below syntax:
CREATE TABLE [database_name. schema_name. table_name] ( pk_colmn data_type PRIMARY KEY, colmn1 data_type NOT NULL, colmn2 data_type, ..., table_constraints );
Below are the steps while creating the create table statement:
Step 1: We have to describe the database name to which we can create a table so that the database name will be similar to the surviving database name, and if we have not described it, then by default, it gets selected as the surviving database name.
Step 2: We have to describe the schema’s name in the new table can have.
Step 3: We have to describe the new table.
Step 4: In this step, the table we will create can have the primary key that may carry out more than one column. Generally, we can index the primary key column first and other columns after that; if the primary key column carries one column, then we can able to utilize the ‘PRIMARY KEY’ keyword next to the column name, and if the primary key contains more than one column then we have to describe ‘PRIMARY KEY’ constraint as a table constraint, every column of the table can have particular data type next to its name in the statement. A column can have more than one column constraint, such as ‘NOT NULL,’ ‘UNIQE,’ and ‘CHECK.’
Step 5: A table can have the table constraint described in the table constraint part like, ‘FOREIGN KEY,’ ‘PRIMARY KEY,’ ‘UNIQUE,’ and ‘CHECK,’ and the ‘CREATE TABLE’ is composite.
Using T-SQL CREATE TABLE
Let us now see the example:
CREATE TABLE vendor. meeting ( meeting_id INT PRIMARY KEY IDENTITY (1, 1), f_name VARCHAR (60) NOT NULL, l_name VARCHAR (60) NOT NULL, meeting_at DATETIME, phone VARCHAR (30), shop_id INT NOT NULL, FOREIGN KEY (shop_id) REFERENCES vendor. shop (shop_id) );
In the above example, we have not defined the name of the database precisely on which the table has been generated, as we try to create a table on the database, and we have explained the schema correctly; hence the meeting table is brought about in the ‘vendor’ structural outline.
The ‘meeting’ table carries six columns:
- The primary key column will be the ‘vendor_id’ which is a column of the table in which ‘IDENTITY (1, 1)’ can direct to the SQL server to bring about a number impulsively for the columns that can begin when a new row from one and also expanded by one.
- Then the ‘f_name’ and ‘l_name’ columns have a varchar data type, so it will accept the string as input with 60 characters as we can reserve up to 60 characters string.
- The ‘meeting at’ is a column with the ‘DateTime’ data type, which means it can reserve the date and time when the customer has a meeting at the shop.
- The ‘phone’ is the column as it is an assorted character string in which it can accept the ‘NULL.’
- The ‘shop_id’ is the column that can reserve an identification number that can recognize the shop when the vendor is going to have a meeting.
- At last, the ‘FOREIGN KEY’ constraint can ensure that the entity in the vendor table’s column of ‘shop _id’ will have to access the ‘shop _id’ column in the shop table.
As we see the term above that IDENTITY (1, 1), the term identity shows that the new column will be the identity column, in which when we add a new row to the table then the system can give a unique column, in which we can say that the identity columns are generally utilized with the PRIMARY KEY constraint to provide the unique row identifier for the table, and the properties of it can be allocated to various columns.
The increased value can be added to the identity value of the prior row.
It is a non-compulsory code name that can show the beginning of the definition of a primary key, and the constraint name is also the name of the constraint, which is unique inside the schema in which the table is.
In this article, we conclude that the ‘CREATE TABLE’ is a statement that can be executed with a specific format and particular values for creating a table in the database; the table can have a special name, and also it can reserve various kinds of data depending on the data type.
This is a guide to the T-SQL CREATE TABLE. Here we discuss the introduction, overview and T-SQL create a new table statement for better understanding. You may also have a look at the following articles to learn more –