CREATE TABLE statement in SQL
CREATE TABLE is the SQL statement used to create the tables that will make up our relational databases. A table is divided into rows and columns and allows you to store data and relate it to other tables through Foreign Key.
The statement CREATE TABLE therefore allows defining the fields that will refer to the columns of the table together with the corresponding data type, as well as the pertinent restrictions, either the definition of the Primary Key to identify unique and unequivocally to each record and the Foreign Key to establish relationships with other tables in the model, to name a few examples.
In summary, the structure of the CREATE TABLE is as follows:
CREATE TABLE tableName (fieldName data type and properties) CONSTRAINT (constraints).
To have a better understanding on how the CREATE TABLE statement works, we are going to write and explain the statements for creating two new tables (Customers and Orders) in our database.
We can use the store database that we used as an example in the CREATE DATABASE section.
CREATE TABLE Customers(
customerId INT IDENTITY(1,1),
[name] VARCHAR(255) NOT NULL,
[lastName] VARCHAR(255) NOT NULL,
[address] VARCHAR(255),
[bankAccount] INT,
CONSTRAINT pk_customers_customerId PRIMARY KEY (customerId)
);
From this statement we can conclude the following:
-
The
customerIdfield is of typeINT, that is, it only admits integer numeric values and theIDENTITY (1,1)property tells us that the value of said field will progress sequentially one by one as we add the data, starting from one. In addition,customerIdis defined as the Primary Key of the table in theCONSTRAINT. -
The
name,lastNameandaddressfields accept alphanumeric values up to a maximum length of255characters. The first two,nameandlastNameshare the propertyNOT NULLso they will not accept null values, having to always be informed for each record. -
The
bankAccountfield is ofINTtype, which admits numeric values, and by not indicating property, admits nulls. -
In the
CONSTRAINT, a proper name for the Primary Key is defined:pk_customers_customerIdin order to identify it more easily.
CREATE TABLE Orders(
orderId INT IDENTITY(1,1),
product VARCHAR(255),
productDescription VARCHAR(255),
price FLOAT,
orderDate DATE,
productQuantity INT DEFAULT(0),
totalPrice AS price * productQuantity,
customerId INT,
CONSTRAINT pk_orders_orderId PRIMARY KEY(orderId),
CONSTRAINT uq_orders_product UNIQUE (product),
CONSTRAINT fk_customers_orders FOREIGN KEY (customerId) REFERENCES Customers(customerId)
);
We draw the following conclusions from this statement:
-
The
orderIdfield is of integer type and has theIDENTITY (1,1)property, so it will progress sequentially one by one for each record. In addition, this field is defined in theCONSTRAINTas Primary Key. -
The
productandproductDescriptionfields are of typeVARCHAR, so they accept alphanumeric values of up to 255 characters each. -
The
pricefield is of typeFLOAT, which supports numeric values with decimals. -
The
orderDatefield is of typeDATE, to register date format. The default format can be modified with different input masks to adjust it to the required needs. -
The
productQuantityfield is of typeINT, so it will only accept integer numeric values. TheDEFAULTproperty indicates a default value, in this case 0. -
The
totalPricefield is the result of an arithmetic operation of the multiplication of the values of thepriceandproductQuantityfields. -
In the
CONSTRAINTtheorderIdfield is defined as Primary Key and this key is assigned its own name:pk_orders_orderId -
In the
CONSTRAINTtheproductfield is defined as Unique Key and this key is assigned its own name:uk_orders_product -
In the
CONSTRAINTthe relationship between both tables (CustomersandOrders) is established through the Foreign Key (customerId) defined in theOrderstable. In addition, this key is assigned the name:fk_customers_orders
It is possible to define temporary tables from the newly created tables. As the name suggests, these tables will not be permanent and will allow us to temporarily handle the data without affecting the original tables and data.
The insertion of data in the tables is done through the INSERT INTO statement.