0% found this document useful (0 votes)
113 views

SQL Server CREATE TABLE Statement

The document discusses how to use SQL statements to create and manage database tables. It explains how to create tables using the CREATE TABLE statement and define column data types, default values, and constraints like primary keys, unique keys, foreign keys, and check constraints. It also describes how to drop or delete existing tables using the DROP TABLE statement.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

SQL Server CREATE TABLE Statement

The document discusses how to use SQL statements to create and manage database tables. It explains how to create tables using the CREATE TABLE statement and define column data types, default values, and constraints like primary keys, unique keys, foreign keys, and check constraints. It also describes how to drop or delete existing tables using the DROP TABLE statement.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TP2 BD INFO 2

SQL Server CREATE TABLE Statement


This SQL tutorial explains how to use the CREATE TABLE statement in
SQL Server. This tutorial is the first part of two posts describing DDL (Data
Definition Language) statements in SQL Server.
The DDL statements are a subset of SQL statements used to create,
modify, or remove database structures. In this post you will learn how to
create and delete tables.
This tutorial allows you to become familiar with the following topics:
 Creating new tables
 Defining Data Types
 Defining Default Value
 Defining Constraints
 Column Level Constraints
 Primary Key
 Unique
 Not Null
 Check
 Foreign Key
 Table Level Constraints
o Drop Existing Table

SQL Server CREATE TABLE Statement


SQL Server CREATE TABLE statement is used to create new tables in the
database.

Data Types
1
TP2 BD INFO 2

Column Type Description Example

String column. The value within the


brackets indicates the maximum VARCHAR(3) → ‘ABC’
VARCHAR (size) size of each field in the column (in VARCHAR(3) → ‘AB’
characters)

Numeric column. Precision –
number of digits, Scale – how many DECIMAL(5,2) → 476.29
Decimal (p,s)
of the digits are located after the DECIMAL(5,2) → 6.29
decimal point

DATE Date format column ‘YYYY-MM-DD’

SQL Server Default Value


A column can be given a default value using the DEFAULT keyword. The
DEFAULT keyword provides a default value to a column when the SQL
Server INSERT INTO statement does not provide a specific value. The
default value can be a literal value, an expression, or a SQL Function, such
as GETDATE().
To define a Default value, use this syntax:
1 DEFAULT default_value

For example:

2
TP2 BD INFO 2

Creating SQL Server Constraints


Constraints enforce rules on the data in a table whenever a row is inserted,
deleted, or updated. Constraints can be defined at the column or table
level.

Defining Constraints at the Column Level


Constraint enforced at the column level:
 Is created as part of the column definition
 Always refers to a single column
 A constraint at the column level has the following structure:
1 CONSTRAINT constraint_name constraint_type

 Constraint_type – the type of the constraint to be enforced on the


column (for example, Unique or Not Null)
 Constraint_name – although not mandatory, it is always advisable
to give the constraint a name, thereby allowing you to easily identify it.
The following naming convention is commonly used by many database
developers :
<table name>_<column_name>_<constraint abbreviation>

3
TP2 BD INFO 2

For example:

Primary Key (PK)


In SQL Server, the Primary Key constraint is a column (or a set of columns)
that uniquely identifies each row in the table, this constraint enforces
uniqueness and ensures that no column that is part of the Primary Key can
hold a NULL value. Only one Primary Key can be created for each table.
The syntax for defining a Primary Key Constraint is as follows:
Please note – the square brackets in this demonstration (and in those that
follow) indicate that what enclosed within them is optional, the square
brackets are not part of the CREATE TABLE statement.

Not Null (NN)


In SQL Server, the Not Null constraint ensures that the column contains no
NULL values. The syntax for defining a Not Null constraint is as follows:

This constraint can only be defined at the column level

UNIQUE (UQ)

4
TP2 BD INFO 2

In SQL Server, the Unique constraint requires that every value in a column
(or set of columns) be unique. The syntax for defining a UNIQUE
Constraint is as follows:

CHECK (CK)
In SQL Server, the Check constraint defines a condition that each row must
satisfy. The syntax for defining a Check Constraint is as follows:
1 column_name column_DataType [DEFAULT value] [CONSTRAINT constraint_name] CHECK (Cond

 The condition written in the CHECK is quite similar in its structure


to each of the conditions written in a WHERE statement.
 The condition in the CHECK part must not include:
 Values that are returned as a result of using SEQUENCES
 Functions such as GETDATE()
 Subqueries
For Example:

FOREIGN KEY (FK)


In SQL Server, the Foreign Key constraint designates a column (or a set of
columns) as a Foreign Key and establishes a relationship between a
Primary Key (or Unique) in different table (or in the same table). The syntax
for defining a Check Constraint is as follows:

5
TP2 BD INFO 2

Table Level Constraints


 Created after defining the various column.
 Can refer to more than one column (a constraint that comprises two
columns together).
 Allows creating several constraints on the same column.
 It is not possible to create a NOT NULL constraint by using this
method.
For example:

Drop an Existing Table


6
TP2 BD INFO 2

The syntax used for deleting an existing table in SQL Server is as follows :
DROP TABLE table_name

For example
DROP TABLE employees

You might also like