CREATE TABLE builds a new, empty table and defines its columns, data types, and constraints in SQL Server.
CREATE TABLE lets you persist structured data, enforce data types, and set rules such as primary keys, defaults, and constraints in SQL Server. Every relational object—orders, customers, products—starts with this command.
The minimal form requires a table name and one column.Real-world tables usually include multiple columns, a primary key, and indexes for performance.
CREATE TABLE schema.table_name ( column_name data_type [NULL|NOT NULL] [constraint], ...);
Place constraints inline or at the bottom of the column list. Use PRIMARY KEY for uniqueness, FOREIGN KEY to link tables, CHECK for business rules, and DEFAULT for automatic values.
Define Customers, Products, Orders, and OrderItems with proper keys.Link foreign keys so deleting a customer can cascade to orders when needed.
Pick the narrowest data type, name constraints explicitly, and store dates in DATETIME2
. Keep DDL scripts in version control and include IF NOT EXISTS checks in migrations.
Forgetting a primary key causes duplicate rows—always declare one. Using float
for money leads to rounding errors—use DECIMAL(10,2)
instead.
.
Yes. Wrap the statement in an IF block: IF NOT EXISTS (SELECT * FROM sys.tables WHERE name = 'Customers') BEGIN CREATE TABLE ... END
.
Use ALTER TABLE dbo.Customers ADD phone NVARCHAR(20);
to append a new column after creation.
Prefer DATETIME2
; it has higher precision and a wider date range than DATETIME
.