0% found this document useful (0 votes)
49 views5 pages

Data Types

This document provides an overview of SQL Server data types and constraints. It describes numeric, date/time, character string, and Unicode character string data types including their storage sizes, ranges, and precisions. It also explains the six main constraints in SQL Server - NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT - and provides examples of how each constraint is defined and used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views5 pages

Data Types

This document provides an overview of SQL Server data types and constraints. It describes numeric, date/time, character string, and Unicode character string data types including their storage sizes, ranges, and precisions. It also explains the six main constraints in SQL Server - NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DEFAULT - and provides examples of how each constraint is defined and used.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Server data types Overview

A data type is an attribute that specifies the type of data that these objects can store. It
can be an integer, character string, monetary, date and time, and so on.
Exact numeric data types
Data Type Lower limit Upper limit Memory
bigint −2^63 (−9,223,372, 2^63−1 (−9,223,372, 8 bytes
036,854,775,808) 036,854,775,807)

int −2^31 (−2,147, 483,648) 2^31−1 (−2,147, 4 bytes


483,647)

smallint −2^15 (−32,767) 2^15 (−32,768) 2 bytes


tinyint 0 255 1 byte
bit 0 1 1 byte/8bit column
decimal −10^38+1 10^381−1 5 to 17 bytes
numeric −10^38+1 10^381−1 5 to 17 bytes
money −922,337, 203, +922,337, 203, 8 bytes
685,477.5808 685,477.5807

smallmoney −214,478.3648 214,478.36 4 bytes

Approximate numeric data types


Data Lower limit Upper Memory Precisio
Type limit n
−1.79E+30 1.79E+308 Depends on the value of n 7 Digit
float(n) 8
−3.40E+38 3.4E+38 4 bytes 15 Digit
real

Date & Time data types

Data Type Storage size Accuracy Lower Range Upper Range


datetime 8 bytes Rounded to 1753-01-01 2958465
increments of .000,
.003, .007
smalldatetime 4 bytes, fixed 1 minute 1 65537
date 3 bytes, fixed 1 day 0001-01-01 2958465
time 5 bytes 100 nanoseconds 0 1
datetimeoffset 10 bytes 100 nanoseconds 0001-01-01 2958465
datetime2 6 bytes 100 nanoseconds 0001-01-01 2958465

Character strings data types


Data Type Lower limit Upper limit Memory

char 0 chars 8000 chars n bytes

varchar 0 chars 8000 chars n bytes + 2 bytes

varchar (max) 0 chars 2^31 chars n bytes + 2 bytes

text 0 chars 2,147,483,647 chars n bytes + 4 bytes

Unicode character string data types


Data Type Lower limit Upper limit Memory
nchar 0 chars 4000 chars 2 times n bytes
nvarchar 0 chars 4000 chars 2 times n bytes + 2 bytes
ntext 0 chars 1,073,741,823 char 2 times the string length

SQL Server Constraints

Constraints in SQL Server are predefined rules and restrictions that are enforced in a single column or
multiple columns, regarding the values allowed in the columns, to maintain the integrity, accuracy, and
reliability of that column’s data. 

There are six main constraints that are commonly used in SQL Server that we will describe deeply with
examples within this article and the next one. These constraints are:

● SQL NOT NULL


● UNIQUE
● PRIMARY KEY
● FOREIGN KEY
● CHECK
● DEFAULT

NOT NULL Constraint


By default, the columns are able to hold NULL values. A NOT NULL constraint in SQL is used to prevent
inserting NULL values into the specified column,
CREATE TABLE ConstraintDemo1
(
ID INT NOT NULL,
Name VARCHAR(50) NULL
)

INSERT INTO ConstraintDemo1 ([ID],[NAME]) VALUES (1,'Ali')


GO
INSERT INTO ConstraintDemo1 ([ID]) VALUES (2)
GO
INSERT INTO ConstraintDemo1 ([NAME]) VALUES ('Fadi')
GO

UNIQUE Constraints

‘The UNIQUE constraint in SQL is used to ensure that no duplicate values will be inserted into a specific
column or combination of columns that are participating in the UNIQUE constraint and not part of the
PRIMARY KEY. In other words, the index that is automatically created when you define a UNIQUE
constraint will guarantee that no two rows in that table can have the same value for the columns
participating in that index, with the ability to insert only one unique NULL value to these columns, if the
column allows NULL.
CREATE TABLE ConstraintDemo2
(
ID INT UNIQUE,
Name VARCHAR(50) NULL
)

INSERT INTO ConstraintDemo2 ([ID],[NAME]) VALUES (1,'Ali')


GO
INSERT INTO ConstraintDemo2 ([ID],[NAME]) VALUES (2,'Ali')
GO
INSERT INTO ConstraintDemo2 ([ID],[NAME]) VALUES (NULL,'Adel')
GO
INSERT INTO ConstraintDemo2 ([ID],[NAME]) VALUES (1,'Faris')
GO

PRIMARY KEY Constraint


The PRIMARY KEY constraint consists of one column or multiple columns with values that uniquely
identify each row in the table.
The SQL PRIMARY KEY constraint combines between the UNIQUE and SQL NOT NULL constraints,
where the column or set of columns that are participating in the PRIMARY KEY cannot accept a NULL
value. If the PRIMARY KEY is defined in multiple columns, you can insert duplicate values on each
column individually, but the combination values of all PRIMARY KEY columns must be unique.
CREATE TABLE ConstraintDemo3
(
ID INT PRIMARY KEY,
Name VARCHAR(50) NULL
)

INSERT INTO ConstraintDemo3 ([ID],[NAME]) VALUES (1,'John')


GO
INSERT INTO ConstraintDemo3 ([NAME]) VALUES ('Fadi')
GO
INSERT INTO ConstraintDemo3 ([ID],[NAME]) VALUES (1,'Saeed')
GO

FOREIGN KEY Constraint

A Foreign Key is a database key that is used to link two tables together. The FOREIGN KEY constraint
identifies the relationships between the database tables by referencing a column, or set of columns, in
the Child table that contains the foreign key, to the PRIMARY KEY column or set of columns, in
the Parent table.

The relationship between the child and the parent tables is maintained by checking the existence of the
child table FOREIGN KEY values in the referenced parent table’s PRIMARY KEY before inserting these
values into the child table.
CREATE TABLE ConstraintDemoParent
(
ID INT PRIMARY KEY,
Name VARCHAR(50) NULL
)
GO
CREATE TABLE ConstraintDemoChild
(
CID INT PRIMARY KEY,
ID INT FOREIGN KEY REFERENCES ConstraintDemoParent(ID)
)

INSERT INTO ConstraintDemoParent ([ID],[NAME]) VALUES (1,'John'),(2,'Mika'),(3,'Sanya')


GO
INSERT INTO ConstraintDemoChild (CID,ID) VALUES (1,1)
GO
INSERT INTO ConstraintDemoChild (CID,ID) VALUES (2,4)
GO

CHECK Constraint
A CHECK constraint is defined on a column or set of columns to limit the range of values, that can be
inserted into these columns, using a predefined condition. The CHECK constraint comes into action to
evaluate the inserted or modified values, where the value that satisfies the condition will be inserted
into the table, otherwise, the insert operation will be discarded.

CREATE TABLE ConstraintDemo4


(
ID INT PRIMARY KEY,
Name VARCHAR(50) NULL,
Salary INT CHECK (Salary>1000 && Salary <20000)
)
GO
INSERT INTO ConstraintDemo4 ([ID],[NAME],Salary) VALUES (1,'John',350)
GO
INSERT INTO ConstraintDemo4 ([ID],[NAME],Salary) VALUES (2,'Mike',0)
GO
INSERT INTO ConstraintDemo4 ([ID],[NAME],Salary) VALUES (3,'Nikola',-72)
GO
DEFAULT Constraint
A DEFAULT constraint is used to provide a default column value for the inserted rows if no value is
specified for that column in the INSERT statement. The Default constraint helps in maintaining the
domain integrity by providing proper values for the column, in case the user does not provide a value
for it. The default value can be a constant value, a system function value or NULL.
CREATE TABLE ConstraintDemo5
(
ID INT PRIMARY KEY,
Name VARCHAR(50) NULL,
EmployeeDate DATETIME NOT NULL DEFAULT GETDATE()
)
GO

INSERT INTO ConstraintDemo5 ([ID],[NAME],EmployeeDate) VALUES (1,'Lorance','2016/10/22')


GO
INSERT INTO ConstraintDemo5 ([ID],[NAME]) VALUES (2,'Shady')
GO

You might also like