DAY-2
09 March 2022 19:59
SQL Server Data Types
String Data Types
Data type Description Max size Storage
char(n) Fixed width character string 8,000 characters Defined width
varchar(n) Variable width character string 8,000 characters 2 bytes + number of chars
varchar(max) Variable width character string 1,073,741,824 characters 2 bytes + number of chars
text Variable width character string 2GB of text data 4 bytes + number of chars
nchar Fixed width Unicode string 4,000 characters Defined width x 2
nvarchar Variable width Unicode string 4,000 characters
nvarchar(max) Variable width Unicode string 536,870,912 characters
ntext Variable width Unicode string 2GB of text data
binary(n) Fixed width binary string 8,000 bytes
varbinary Variable width binary string 8,000 bytes
varbinary(max) Variable width binary string 2GB
image Variable width binary string 2GB
Numeric Data Types
Data type Description Storage
bit Integer that can be 0, 1, or NULL
tinyint Allows whole numbers from 0 to 255 1 byte
smallint Allows whole numbers between -32,768 and 32,767 2 bytes
int Allows whole numbers between -2,147,483,648 and 2,147,483,647 4 bytes
bigint Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 8 bytes
decimal(p,s) Fixed precision and scale numbers. 5-17 bytes
Allows numbers from -10^38 +1 to 10^38 –1.
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1
to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0
numeric(p,s) Fixed precision and scale numbers. 5-17 bytes
Allows numbers from -10^38 +1 to 10^38 –1.
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal point). p must be a value from 1
to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p. Default value is 0
smallmoney Monetary data from -214,748.3648 to 214,748.3647 4 bytes
money Monetary data from -922,337,203,685,477.5808 to 922,337,203,685,477.5807 8 bytes
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 bytes
The n parameter indicates whether the field should hold 4 or 8 bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53.
real Floating precision number data from -3.40E + 38 to 3.40E + 38 4 bytes
Date and Time Data Types
Data type Description Storage
datetime From January 1, 1753 to December 31, 9999 with an accuracy of 3.33 milliseconds 8 bytes
datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds 6-8 bytes
smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute 4 bytes
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes
time Store a time only to an accuracy of 100 nanoseconds 3-5 bytes
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
timestamp Stores a unique number that gets updated every time a row gets created or modified. The timestamp value is based upon an internal clock and does not
correspond to real time. Each table may have only one timestamp variable
Other Data Types
Data type Description
sql_variant Stores up to 8,000 bytes of data of various data types, except text, ntext, and timestamp
uniqueidentifier Stores a globally unique identifier (GUID)
xml Stores XML formatted data. Maximum 2GB
cursor Stores a reference to a cursor used for database operations
table Stores a result-set for later processing
CONSTRAINTS in SQL Server
New Section 1 Page 1
SQL Server Constraints
Constraints in SQL Server are rules and restrictions applied on a column or a table such that unwanted data can't be inserted into tables. This ensures the accuracy and reliability of the data in
the database. We can create constraints on single or multiple columns of any table. Constraints maintain the data integrity and accuracy in the table.
Constraints can be classified into the following two types.
Column Types Constraints
Definitions of these types of constraints is given when the table is created.
1. Create Table My_Constraint
2. (
3. IID int NOT NULL,
4. Salary int CHECK(Salary>5000)
5. )
Table Types Constraints
Definitions of these types of constraints is given after the creation of the table using the Alter Command.
1. Alter Table My_Cosntraint
2. Add constraint Check_Constraint Check(Age>50)
SQL Server contains the following 6 types of constraints:
• Not Null Constraint
• Check Constraint
• Default Constraint
• Unique Constraint
• Primary Constraint
• Foreign Constraint
Let us understand each constraint briefly.
Not Null Constraint
A Not null constraint restricts the insertion of null values into a column. If we are using a Not Null Constraint for a column then we cannot ignore the value of this column during an insert of
data into the table.
Column Level
Syntax
1. CREATE TABLE Table_Name
2. (
3. Column_Name Datatype CONSTRAINT Constraint_Name NOT NULL,
4. );
Example
1. Create Table My_Constraint
2. (
3. IID int NOT NULL,
4. Name nvarchar(50) CONSTRAINT Cons_NotNull not null,
5. Age int Not Null,
6. )
Table Level
Syntax
1. ALTER TABLE Table_Name
2. ALTER COLUMN Column_Name Datatype NOT NULL
Example
1. Alter Table My_Constraint
2. Alter Column IId int Not Null
Without SQL Command
We can also create a Not Null constraint in Microsoft SQL Server without execution of a SQL query.
First right-click on the table and select and click on the design option. Now check all the columns in the “Allow Nulls” option that should have a Null Value.
Figure 1: Table
Check Constraint
A Check constraint checks for a specific condition before inserting data into a table. If the data passes all the Check constraints then the data will be inserted into the table otherwise the data
for insertion will be discarded. The CHECK constraint ensures that all values in a column satisfies certain conditions.
Column Level
Syntax
1. Create Table Table_Name
2. (
3. Column_Name Datatype Constraint Constraint_Name Check(Condition)
4. )
New Section 1 Page 2
4. )
Example
1. Create Table Constraint_
2. (
3. IId int Constraint Constraint_Name Check(IId>100)
4. )
Table Level
Syntax
1. Alter Table Table_Name
2. Add Constraint Constraint_Name Check(Condition)
Example
1. Alter table Constraint_
2. Add constraint Cons_Name Check(IId>150)
Without SQL Command
First go to Table Design then right-click on the Column_Name that will contain a check constraint and select the “Check Constraint” option then a new window will be shown. In this window add
a constraint and provide its definition in the Expression Field.
Figure 2: Check Constraint
Figure 3: Select Check Constraint
Default Constraint
Specifies a default value for when a value is not specified for this column. If in an insertion query any value is not specified for this column then the default value will be inserted into the
column.
Column Level
Syntax
1. Create Table Table_Name
2. (
3. Column_Name DataType Constraint Constraint_Name Default(Value),
4. )
Example
1. Create Table My_Table1
2. (
3. IId int default(1500),
4. Name Nvarchar(50)Constraint Name_Default Default('Pankaj'),
5. Age Int,
6. Salary Int Default(100)
7. )
Table Level
Syntax
1. Alter Table Tabel_Name
2. Add Constraint Constraint_Name Default(Value) for[Column_Name]
Example
1. Alter Table My_Table1
2. Add Constraint cons_Default Default(40) for[Age]
Without SQL Command
New Section 1 Page 3
Without SQL Command
Go to Table Design then click on the specific column name that should have a default value and go to the column Property and provide the default value.
Figure 4: Column Property
Unique Constraint
It ensures that each row for a column must have a unique value. It is like a Primary key but it can accept only one null value. In a table one or more column can contain a Unique Constraint.
Column Level
Syntax
1. Create Table Table_Name
2. (
3. Column_Name Datatype Constraint Constraint_Name Unique
4. )
Example
1. Create Table MY_Tab
2. (
3. IId int constraint Unique_Cons Unique ,
4. Name nvarchar(50)
5. )
Table Level
Syntax
1. Alter Table_Name
2. Add Constraint Constraint_Name Unique(Column_Name)
Example
1. Alter Table My_Tab
2. Add Constraint Unique_Cons_ Unique(Name)
Without SQL Command
First go to Table definition and select a column and right-click on that column. Now select the option Index/Keys. Now a window will be shown. Add a constrint and mark its “Is Unique” option
as True.
Figure 5: Indexes & Keys
Figure 6: Select Indexes
Primary Key Constraint
A Primary key uniquly identifies each row in a table. It cannot accept null and duplicate data. One or more of the columns of a table can contain a Primary key.
Column Level
Syntax
1. Create Table Table_Name
2. (
3. Column_Name Datatype Constraint Constraint_Name Primary Key,
New Section 1 Page 4
3. Column_Name Datatype Constraint Constraint_Name Primary Key,
4. )
Example
1. Create Table Employee
2. (
3. IId int constraint Const_primary_IId primary key,
4. Name nvarchar(50)
5. )
Table Level
Syntax
1. Alter Table Table_Name
2. Add constraint Constraint_Name Primary Key(Column_Name)
Example
1. Alter Table Employee
2. Add constraint Constraint_Name Primary Key(IId)
Without SqlQuery
First go to table design and right-click on Column and select the “Set Primary Key” Option.
Figure 7: Set Primary Key
Foreign Key Constraint
A Foreign Key is a field in a database table that is a Primary key in another table. A Foreign key creates a relation between two tables. The first table contains a primary key and the second table
contains a foreign key.
Column Level
Syntax
1. Create Table Table_Name
2. (
3. Column_Name Datatype Constraint Constraint_Name References Reference_Table_Name(Reference_Column_Name)
4. )
Example
1. Create Table Employee_
2. (
3. IId int constraint Cons_Reference References My_Constraint(IId),
4. Age int,
5. Salary int
6. )
Table Level
Syntax
1. ALTER TABLE Table_Name
2. ADD CONSTRAINT Constraint_Name FOREIGN KEY(Column_Name)
3. REFERENCES Reference_Table (Column_Name)
Example
1. ALTER TABLE Employee_
2. ADD CONSTRAINT Cons_Emp_Foreign FOREIGN KEY(IId)
3. REFERENCES My_Constraint(IId)
Without SQL Command
First go to table design than right-click on the column and select the “Relationship” option. Now a window will be shown. In this window click on the “Table and Column Specificat” option and
select Primary Key table, Column name and Column name for foreign key.
New Section 1 Page 5
Figure 8: Column Relationships
Figure 9: Foreign Key Relationships
Summary
In this article, we learned about contraints in SQL Server and how to create using SQL.
From <https://www.c-sharpcorner.com/UploadFile/f0b2ed/constraints-in-sql-server/>
New Section 1 Page 6