Careeroza
CREATE TABLE
The SQL CREATE TABLE Statement
CREATE TABLE creates a new table, with a name and data type for each column.
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
...
);Create a Table
Example
Create a Suppliers table, add a row and read it back:
CREATE TABLE Suppliers (
SupplierID INTEGER PRIMARY KEY,
SupplierName VARCHAR(100) NOT NULL,
Phone VARCHAR(20),
Country VARCHAR(50) DEFAULT 'India'
);
INSERT INTO Suppliers (SupplierID, SupplierName, Phone)
VALUES (1, 'Spice Route Exports', '+91-22-5550-1234');
SELECT * FROM Suppliers;Result: 1 row
| SupplierID | SupplierName | Phone | Country |
|---|---|---|---|
| 1 | Spice Route Exports | +91-22-5550-1234 | India |
Create Table Using Another Table
A copy of an existing table can be created from a SELECT. The new table gets the selected columns and their data.
Example
Copy the Indian customers into a new table, then read it:
CREATE TABLE IndianCustomers AS
SELECT CustomerID, CustomerName, City
FROM Customers
WHERE Country = 'India';
SELECT * FROM IndianCustomers;Result: 2 rows
| CustomerID | CustomerName | City |
|---|---|---|
| 1 | Aarav Traders | Mumbai |
| 4 | Delta Mart | Bengaluru |
Note: SQL Server uses SELECT ... INTO NewTable FROM ... instead of CREATE TABLE ... AS. Constraints and indexes are not copied.
IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Suppliers (
SupplierID INT PRIMARY KEY,
SupplierName VARCHAR(100)
);Skips creation instead of failing when the table is already there (MySQL, PostgreSQL, SQLite; SQL Server 2016+ uses DROP TABLE IF EXISTS and checks against sys.tables).