Create Table in SQL
? CREATE TABLE in SQL
The CREATE TABLE statement is used to create a new table in the database.
? Basic Syntax
CREATE TABLE table_name ( column1 datatype [constraints], column2 datatype [constraints], ...);table_name: Name of the new table.column1,column2, ... : Names of columns.datatype: Data type of the column (e.g., INT, VARCHAR, DATE).[constraints]: Optional rules likePRIMARY KEY,NOT NULL,UNIQUE, etc.
? Example
CREATE TABLE employees ( id INT PRIMARY KEY, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) UNIQUE, hire_date DATE, salary DECIMAL(10, 2) CHECK (salary > 0));? Common Data Types
| Data Type | Description |
|---|---|
| INT | Integer numbers |
| VARCHAR(n) | Variable-length string (max n characters) |
| CHAR(n) | Fixed-length string (n characters) |
| DATE | Date |
| DECIMAL(p, s) | Decimal number with precision and scale |
| BOOLEAN | True or False |
? Common Constraints
PRIMARY KEY— Unique identifier for rows.NOT NULL— Column must have a value.UNIQUE— Values must be unique.CHECK— Condition that values must satisfy.FOREIGN KEY— Enforces referential integrity.
If you want, I can provide examples for specific SQL databases or more advanced table creation!