Create Table using Create Query

Admin
12 Mar 2024
Mysql

In SQL, you use the CREATE TABLE statement to make a new table in a database. This statement defines the table's name and the names and types of its columns. Remember, each table in a database must have a unique name.
Syntax,
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype,
PRIMARY KEY( one or more columns )
);

Example: Creating Table
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);

This SQL statement creates a new table named CUSTOMERS with the following structure:
ID: An integer column that cannot be null (NOT NULL). It's designated as the primary key of the table, meaning each value in this column must be unique.
NAME: A variable-length string column with a maximum length of 20 characters (VARCHAR (20)). It cannot be null.
AGE: An integer column that cannot be null.
ADDRESS: A fixed-length string column with a maximum length of 25 characters (CHAR (25)). It can be null, indicating that an address may not be provided for every customer.
SALARY: A decimal column with a precision of 18 digits, 2 of which are after the decimal point (DECIMAL (18, 2)). It can be null, indicating that salary information may not be provided for every customer.