Ticker

6/recent/ticker-posts

Create Table Statement in SQL

Create Table Statement


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:

sql
CREATE 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 as NOT 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."

sql
CREATE 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.

Post a Comment

0 Comments