SQL DATABASE
SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational
Database Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase,
Informix, Postgres and SQL Server use SQL as their standard database language.
Allows users to describe the data.
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-
compilers.
Allows users to create and drop databases and tables.
Allows users to create view, stored procedure, functions in a database.
Allows users to set permissions on tables, procedures and views.
A BRIEF HISTORY OF SQL
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
1974 − Structured Query Language appeared.
1978 − IBM worked to develop Codd's ideas and released a product named
System/R.
1986 − IBM developed the first prototype of relational database and
standardized by ANSI. The first relational database was released by Relational
Software which later came to be known as Oracle.
SQL commands can be classified into the following
groups based on their nature –
DDL - Data Definition Language: CREATE, ALTER, DROP
DML - Data Manipulation Language: SELECT, INSERT, UPDATE, DELETE
DCL - Data Control Language: GRANT, REVOKE
WHAT IS TABLE?
The data in an RDBMS is stored in database objects which are called as tables. This table is
basically a collection of related data entries and it consists of numerous columns and rows.
WHAT IS FEILD?
Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS
table consist of ID, NAME, AGE, ADDRESS and SALARY.
A field is a column in a table that is designed to maintain specific information about every
record in the table.
WHAT IS RECORD OR ROW?
A record is also called as a row of data is each individual entry that exists in a table.
WHAT IS COLUMN?
A column is a vertical entity in a table that contains all information associated with a specific
field in a table.
SQL CONSTRAINTS?
Constraints are the rules enforced on data columns on a table. These are used to limit the
type of data that can go into a table. This ensures the accuracy and reliability of the data in
the database.
Following are some of the most commonly used constraints available in SQL
NOT NULL Constraint − Ensures that a column cannot have a NULL value.
DEFAULT Constraint − Provides a default value for a column when none is specified.
UNIQUE Constraint − Ensures that all the values in a column are different.
PRIMARY Key − Uniquely identifies each row/record in a database table.
FOREIGN Key − Uniquely identifies a row/record in any another database table.
CHECK Constraint − The CHECK constraint ensures that all values in a column satisfy
certain conditions.
INDEX − Used to create and retrieve data from the database very quickly.
Data Integrity
The following categories of data integrity exist with each RDBMS −
Entity Integrity − There are no duplicate rows in a table.
Domain Integrity − Enforces valid entries for a given column by restricting the type,
the format, or the range of values.
Referential integrity − Rows cannot be deleted, which are used by other records.
User-Defined Integrity − Enforces some specific business rules that do not fall into
entity, domain or referential integrity.
Database Normalization
Database normalization is the process of efficiently organizing data in a database. There
are two reasons of this normalization process −
Eliminating redundant data, for example, storing the same data in more than one
table.
Ensuring data dependencies make sense.
MS SQL Server
MS SQL Server is a Relational Database Management System developed by Microsoft Inc.
Its primary query languages are −
T-SQL
ANSI SQL
History
1987 - Sybase releases SQL Server for UNIX.
1988 - Microsoft, Sybase, and Aston-Tate port SQL Server to OS/2.
1989 - Microsoft, Sybase, and Aston-Tate release SQL Server 1.0 for OS/2.
1990 - SQL Server 1.1 is released with support for Windows 3.0 clients.
Aston - Tate drops out of SQL Server development.
2000 - Microsoft releases SQL Server 2000.
2001 - Microsoft releases XML for SQL Server Web Release 1 (download).
2002 - Microsoft releases SQLXML 2.0 (renamed from XML for SQL Server).
2002 - Microsoft releases SQLXML 3.0.
2005 - Microsoft releases SQL Server 2005 on November 7th, 2005.
Features
High Performance
High Availability
Database mirroring
Database snapshots
CLR integration
Service Broker
DDL triggers
Ranking functions
Row version-based isolation levels
XML integration
TRY...CATCH
Database Mail
Exact Numeric Data Types
DATA TYPE FROM TO
bigint -9,223,372,036,854,775,808 9,223,372,036,854,775,807
Int -2,147,483,648 2,147,483,647
smallint -32,768 32,767
tinyint 0 255
Bit 0 1
decimal -10^38 +1 10^38 -1
numeric -10^38 +1 10^38 -1
money 922,337,203,685,477.5807 +922,337,203,685,477.5807
smallmoney -214,748.3648 +214,748.3647
Approximate Numeric Data Types
DATA TYPE FROM TO
float -1.79E + 308 1.79E + 308
Real -3.40E + 38 3.40E + 38
Date and Time Data Types
DATA TYPE FROM TO
datetime Jan 1, 1753 Dec 31, 9999
smalldatetime Jan 1, 1900 Jun 6, 2079
Date Stores a date like June 30, 1991
Time Stores a time of day like 12:30 P.M.
Note − Here, datetime has 3.33 milliseconds accuracy where as smalldatetime has 1
minute accuracy.
Character Strings Data Types
Sr.No. DATA TYPE & Description
char
1
Maximum length of 8,000 characters.( Fixed length non-Unicode
characters)
varchar
2
Maximum of 8,000 characters.(Variable-length non-Unicode data).
3 varchar(max)
Maximum length of 2E + 31 characters, Variable-length non-Unicode
data (SQL Server 2005 only).
text
4
Variable-length non-Unicode data with a maximum length of
2,147,483,647 characters.
Unicode Character Strings Data Types
Sr.No. DATA TYPE & Description
nchar
1
Maximum length of 4,000 characters.( Fixed length Unicode)
nvarchar
2
Maximum length of 4,000 characters.(Variable length Unicode)
nvarchar(max)
3
Maximum length of 2E + 31 characters (SQL Server 2005 only).(
Variable length Unicode)
ntext
4
Maximum length of 1,073,741,823 characters. ( Variable length
Unicode )
Binary Data Types
Sr.No. DATA TYPE & Description
1 binary
Maximum length of 8,000 bytes(Fixed-length binary data )
varbinary
2
Maximum length of 8,000 bytes.(Variable length binary data)
varbinary(max)
3
Maximum length of 2E + 31 bytes (SQL Server 2005 only). ( Variable
length Binary data)
image
4
Maximum length of 2,147,483,647 bytes. ( Variable length Binary
Data)
Misc Data Types
Sr.No. DATA TYPE & Description
sql_variant
1
Stores values of various SQL Server-supported data types, except text,
ntext, and timestamp.
timestamp
2
Stores a database-wide unique number that gets updated every time a
row gets updated
uniqueidentifier
3
Stores a globally unique identifier (GUID)
4 xml
Stores XML data. You can store xml instances in a column or a variable
(SQL Server 2005 only).
cursor
5
Reference to a cursor object
table
6
Stores a result set for later processing