SQL CREATE TABLE
Creating tables is fundamental — tables store your data in rows and columns.
🔹 Basic Syntax
CREATE TABLE table_name (
column1 datatype [constraints],
column2 datatype [constraints],
...
);
table_name
: Name of your table.column1, column2
: Columns with their data types.- Constraints like
PRIMARY KEY
,NOT NULL
can be added.
🔹 Example
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
hire_date DATE,
salary DECIMAL(10, 2)
);
Creates an employees
table with 5 columns and constraints.
🔹 Notes by DBMS
- Data types and syntax can slightly vary across DBMS.
- In Oracle, use
NUMBER
instead ofINT
. - Add
AUTO_INCREMENT
(MySQL) orIDENTITY
(SQL Server) for auto-generated IDs.
🧠 Quick Recap
Key Point | Explanation |
---|---|
Command | CREATE TABLE with columns |
Columns & Data Types | Define structure of data |
Constraints | Control data integrity |
💡 Always plan table structure carefully before creating it.