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

SQL Server CREATE TABLE Statement

The document discusses the SQL CREATE TABLE statement in SQL Server. It explains how to create tables, define data types, default values, and different types of constraints like primary keys, unique constraints, foreign keys, check constraints and more. Examples are provided for each. Table level constraints are also described. The summary ends with explaining how to drop or delete an existing table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

SQL Server CREATE TABLE Statement

The document discusses the SQL CREATE TABLE statement in SQL Server. It explains how to create tables, define data types, default values, and different types of constraints like primary keys, unique constraints, foreign keys, check constraints and more. Examples are provided for each. Table level constraints are also described. The summary ends with explaining how to drop or delete an existing table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TP2

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

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:
CREATE TABLE demo_tbl
(
salary DECIMAL(8,2) DEFAULT 9500,
hire_date DATE DEFAULT ’2011-01-27’ ,
birthdate DATE DEFAULT GETDATE()
)

INSERT INTO demo_tbl


VALUES (DEFAULT, DEFAULT, DEFAULT)

SELECT *
FROM demo_tbl

salary   hire_date   birthdate


------   ---------   ----------
9500     2011-01-27  2014-01-13

2
TP2

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>
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

3
TP2

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:
1 column_name column_DataType [DEFAULT value] [CONSTRAINT constraint_name] PRIMARY KEY

For example:
1 CREATE TABLE emps
2 (emp_id decimal(3) CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar(25))

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:
1 column_name column_DataType [DEFAULT value] [CONSTRAINT constraint_name] NOT NULL,

For example:
1 CREATE TABLE emps
2 (emp_id decimal(3)    CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar(25) CONSTRAINT emps_emnm_nn NOT NULL)

This constraint can only be defined at the column level

UNIQUE (UQ)
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:
1 column_name column_DataType [DEFAULT value] [CONSTRAINT constraint_name] UNIQUE,

For example:
1 CREATE TABLE emps
2 (emp_id decimal(3)      CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar(25) CONSTRAINT emps_emnm_nn NOT NULL,
 emp_phone varchar(25)CONSTRAINT emps_empn_uq UNIQUE)
4

4
TP2

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:
1 CREATE TABLE emps
2 (emp_id decimal(3)        CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar(25)   CONSTRAINT emps_emnm_nn NOT NULL,
4  emp_phone varchar(25)   CONSTRAINT emps_empn_uq UNIQUE,
 emp_mail    varchar(25) CONSTRAINT emps_emml_ck CHECK (emp_mail LIKE _%@%.%'))
5

Another example:
1 CREATE TABLE emps
2 (emp_id decimal(3)      CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar(25)    CONSTRAINT emps_emnm_nn NOT NULL,
4  emp_phone varchar(25)   CONSTRAINT emps_empn_uq UNIQUE,
 emp_mail    varchar(25) CONSTRAINT emps_emml_ck CHECK (emp_mail LIKE '_%@%.%'),
5  emp_sal      decimal(8,2)  CONSTRAINT emp_sal_ck CHECK (emp_sal &amp;amp;amp;lt;&a
6

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:
1 column_name  … [CONSTRAINT constraint_name] REFERENCES table_name(column_name) [ON D

Example:
The Parent Table
1 CREATE TABLE deps

5
TP2

2 (dep_id decimal(3) CONSTRAINT deps_id_pk PRIMARY KEY ,


3  dep_name varchar(25))

The Child Table


1 CREATE TABLE emps
2 (emp_id decimal(3)      CONSTRAINT emps_empid_pk PRIMARY KEY,
3  emp_name varchar2(25) CONSTRAINT emps_emnm_nn NOT NULL,
4  emp_phone varchar2(25)   CONSTRAINT emps_empn_uq UNIQUE,
5  emp_mail    varchar2(25) CONSTRAINT emps_emml_ck CHECK (emp_mail LIKE '_%@%.%'),
 emp_sal      decimal(8,2)  CONSTRAINT emp_sal_ck CHECK (emp_sal &amp;amp;amp;gt; 5
6  dep_id decimal(3) CONSTRAINT emp_depid_fk REFERENCES deps(dep_id))
7

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:
CREATE TABLE emps
(emp_id DECIMAL(3),
 emp_f_name VARCHAR(25) ,
 emp_l_name VARCHAR(25) ,
 emp_phone VARCHAR(25) CONSTRAINT emps_empn_nn NOT NULL,
 emp_mail    VARCHAR(25) ,
 emp_sal     DECIMAL(8,2) ,
 dep_id      DECIMAL(3),
 CONSTRAINT emps_empid_pk  PRIMARY KEY (emp_id),
 CONSTRAINT emps_empn_uq   UNIQUE(emp_f_name, emp_l_name),
 CONSTRAINT emps_emml_ck1 CHECK (emp_mail LIKE '_%@%.%'),
 CONSTRAINT emps_emml_ck2 CHECK (LENGTH(emp_mail) &amp;amp;amp;gt; 15),
 CONSTRAINT emps_emml_uq  UNIQUE (emp_mail) ,
 CONSTRAINT emp_sal_ck    CHECK (emp_sal &amp;amp;amp;gt; 5000),
 CONSTRAINT emp_depid_fk  FOREIGN KEY (dep_id)
 REFERENCES deps(dep_id)  )

Drop an Existing Table


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

For example

6
TP2

1 DROP TABLE employees

You might also like