Introduction The CREATE TABLE statement is used in SQL (Structured Query Language) to create a new table in a relational database. It defines the structure and attributes of the table, including column names, data types, and constraints. This documentation provides a brief overview of the CREATE TABLE statement along with code examples and explanations.
Syntax The basic syntax of the CREATE TABLE statement is as follows:
sqlCREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
columnN datatype constraint
);
Explanation
- The
CREATE TABLE
keyword indicates the start of the statement. table_name
refers to the name of the table that is being created.column1
,column2
, ...,columnN
represent the column names in the table.datatype
specifies the data type of each column.constraint
defines any constraints or rules applied to the column, such asNOT NULL
,UNIQUE
,PRIMARY KEY
,FOREIGN KEY
, etc.
Code Example Consider an example where we create a table named "Customers" with three columns: "CustomerID," "FirstName," and "LastName."
sqlCREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL
);
In the above example:
- The "CustomerID" column is defined as an integer data type and is set as the primary key for the table.
- The "FirstName" and "LastName" columns are defined as variable-length character data types (VARCHAR) with a maximum length of 50 characters. They are also marked as
NOT NULL
, meaning that these columns must have a value for every record.
Conclusion The CREATE TABLE statement is a fundamental SQL command used to create tables in a relational database. It allows you to define the structure and attributes of a table, including column names, data types, and constraints. By following the syntax and using appropriate code examples, you can easily create tables tailored to your specific requirements.
0 Comments