SQ L Server Final Soft
SQ L Server Final Soft
SQ L Server Final Soft
System databases are an integral part of the SQL Server product as it depends on the System
Databases to function. Having a good knowledge of System Databases will help the Database
Administrator to perform day-to-day tasks effectively.
System Database in SQL Server 2005 & 2008 Versions
Master Database
The Master database basically consists of two physical files, namely master.mdf (data file) and
mastlog.ldf (log file). By default when you are installing SQL Server 2008 the master database
related data and log file are installed in the following folder location Drive:\Program
Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\. If master database is
corrupted or if it is not available then the SQL Server Service will not start.
Model Database
The Model database basically consists of two physical files namely Model.mdf (data file) and
ModelLog.ldf (log file).
Physical Path---- Drive:\Program Files\Microsoft SQL
Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\. If the Model database is damaged or
corrupted then SQL Server Service will not start up as it will not be able to create the tempdb
database.
MSDB Database
The MSDB database basically consists of two physical files namely MSDBData.mdf (data file)
and MSDBLog.ldf (log file). By default when you are installing SQL Server 2008 the MSDB
database related data and log file are created in the following folder location Drive:\Program
Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\. If the MSDB
database is corrupted or damaged then scheduling information used by SQL Server Agent will be
lost.
TempDB Database
The TempDB database basically consists of two physical files namely tempdb.mdf (data file)
and templog.ldf (log file). By default when you are installing SQL Server 2008 the TempDB
database related data and log file are created in the following folder location Drive:\Program
Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\.
Note The master database should be backed up when a user database is created.
Fractions cannot be specified in the SIZE, MAXSIZE, and FILEGROWTH parameters. To
specify a fraction of a megabyte in SIZE parameters, convert to kilobytes by multiplying the
number by 1,024. For example, specify 1,536 KB instead of 1.5 MB (1.5 multiplied by 1,024
equals 1,536).
When a simple CREATE DATABASE database_name statement is specified with no additional
parameters, the database is made the same size as the model database.
A. Create a database that specifies the data and transaction log files
This example creates a database called Sales. Because the keyword PRIMARY is not used, the
first file (Sales_dat) becomes the primary file. Because neither MB or KB is specified in the
SIZE parameter for the Sales_dat file, it defaults to MB and is allocated in megabytes. The
Sales_log file is allocated in megabytes because the MB suffix is explicitly stated in the SIZE
parameter.
USE master
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
( NAME = Arch2,
( NAME = Arch3,
LOG ON
( NAME = Archlog1,
( NAME = Archlog2,
GO
USE master
GO
CREATE DATABASE Products
ON
( NAME = prods_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\prods.mdf',
SIZE = 4,
MAXSIZE = 10,
FILEGROWTH = 1 )
GO
USE master
GO
CREATE DATABASE Products2
ON
( NAME = prods2_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\prods2.mdf' )
GO
All databases have at least a primary filegroup. All system tables are allocated in
the primary filegroup. A database can also have user-defined filegroups. If an
object is created with an ON filegroup clause specifying a user-defined filegroup,
then all the pages for the object are allocated from the specified filegroup.
F. Create a database with filegroups
This example creates a database named sales with three filegroups:
The primary filegroup with the files Spri1_dat and Spri2_dat. The FILEGROWTH
increments for these files is specified as 15 percent.
A filegroup named SalesGroup1 with the files SGrp1Fi1 and SGrp1Fi2.
A filegroup named SalesGroup2 with the files SGrp2Fi1 and SGrp2Fi2.
CREATE DATABASE Sales
ON PRIMARY
( NAME = SPri1_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SPri1dat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 15% ),
( NAME = SPri2_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SPri2dt.ndf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 15% ),
FILEGROUP SalesGroup1
( NAME = SGrp1Fi1_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SG1Fi1dt.ndf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 ),
( NAME = SGrp1Fi2_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SG1Fi2dt.ndf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 ),
FILEGROUP SalesGroup2
( NAME = SGrp2Fi1_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SG2Fi1dt.ndf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 ),
( NAME = SGrp2Fi2_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\SG2Fi2dt.ndf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = 'Sales_log',
FILENAME = 'c:\program files\microsoft sql server\mssql\data\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB )
GO
Approximate numerics
Binary strings
Character strings
In SQL Server, based on their storage characteristics, some data types are
designated as belonging to the following groups:
Large value data types: varchar(max), nvarchar(max), and varbinary(max)
Large object data types: text, ntext, image, varchar(max), nvarchar(max),
varbinary(max), and xml
Exact Numerics
Integers
bigint
decimal
Fixed precision and scale numeric data from -10^38 +1 through 10^38 1.
numeric
Floating precision number data with the following valid values: -1.79E + 308
through -2.23E - 308, 0 and 2.23E + 308 through 1.79E + 308.
real
Floating precision number data with the following valid values: -3.40E + 38
through -1.18E - 38, 0 and 1.18E - 38 through 3.40E + 38.
datetime and smalldatetime
datetime
Date and time data from January 1, 1753, through December 31, 9999, with an
accuracy of three-hundredths of a second, or 3.33 milliseconds.
smalldatetime
Date and time data from January 1, 1900, through June 6, 2079, with an accuracy
of one minute.
Character Strings
char
Binary Strings
binary
A reference to a cursor.
sql_variant
A data type that stores values of various SQL Server-supported data types, except
text, ntext, timestamp, and sql_variant.
table
A special data type used to store a result set for later processing .
timestamp
A database-wide unique number that gets updated every time a row gets updated.
uniqueidentifier
4. datetimeoffset
5. datetime2
6. datetime
7. smalldatetime
8. date
9. time
10.
float
11.real
12.
decimal
13.
money
14.
smallmoney
15.
bigint
16.
int
17.
smallint
18.
tinyint
19.
bit
20.
ntext
21.
text
22.
image
23.
timestamp
24.
uniqueidentifier
25.
26.
nchar
27.
28.
char
29.
30.
binary (lowest)
Type
bigint
Int
Smallint
Tinyint
Bit
Decimal and numeric precision 1-9
Decimal and numeric precision 10-19
Decimal and numeric precision 20-28
Decimal and numeric precision 29-38
Money
Smallmoney
Float 7 digit precision
Float 15 digit precision
Real
Datetime
Smalldatetime
Char
Nchar
Storage in bytes
8
4
2
1
1 bit
5
9
13
17
8
4
4
8
4
Two 4 byte integers
Two 2 byte integers
Each char occupies 1 byte
Each nchar occupies 2 bytes
(235,'Disney Studios','(800)243-0000'),
(986,'Universal Studios','(800)565-0000'),
(457,'Paramount Pictures','(800)322-5555'),
(381,'Tri-Star Productions','(800)665-8998')
go
INTEGRITY CONSTRAINTS
Nullability Rules Within a Table Definition
The nullability of a column determines whether or not that column can allow a null value
(NULL) as the data in that column. NULL is not zero or blank: it means no entry was made or an
explicit NULL was supplied, and it usually implies that the value is either unknown or not
applicable.
UNIQUE Constraints
If CLUSTERED or NONCLUSTERED is not specified for a UNIQUE constraint,
NONCLUSTERED is used by default.
Each UNIQUE constraint generates an index. The number of UNIQUE constraints cannot
cause the number of indexes on the table to exceed 249 nonclustered indexes and 1
clustered index.
are specified.
FOREIGN KEY constraints can reference only tables within the same database on the
same server. Cross-database referential integrity must be implemented through triggers.
FOREIGN KEY constraints can reference another column in the same table (a selfreference).
The REFERENCES clause of a column-level FOREIGN KEY constraint can list only one
reference column, which must have the same data type as the column on which the
constraint is defined.
The REFERENCES clause of a table-level FOREIGN KEY constraint must have the
same number of reference columns as the number of columns in the constraint column
list. The data type of each reference column must also be the same as the corresponding
column in the column list.
use videorentalsystem
go
CREATE TABLE Dept(
deptno int primary key,
dname nvarchar(20) not null,
loc nvarchar(20) not null)
go
CREATE TABLE Emp(
empid int primary key,
ename nvarchar(20) not null,
job nvarchar(20) not null,
salary int not null,
deptno int references Dept(deptno))
go
-------------------------------------*
CREATE TABLE Emp2(
empid int primary key,
ename nvarchar(20) not null,
job nvarchar(20) not null,
salary int not null,
deptno int,
foreign key(deptno)references Dept(deptno))
go
-------------------------------------*
Create table TX
(col1x int,
col2x int,
col3x nvarchar(10),
primary key(col1x,col2x))
go
Create table TY
(
col1y int,
col2y int,
col3y int,
Constraint TY_fk Foreign Key (col1y,col2y)
references TX(col1x,col2x))
go
-------------------------------------*
Create table AA(
col1 int primary key,
col2 nvarchar(10),
col3 int unique)
go
DEFAULT Definitions
A column can have only one DEFAULT definition.
A DEFAULT definition can contain constant values, functions, SQL-92 niladic functions,
or NULL. The table shows the niladic functions and the values they return for the default
during an INSERT statement.
SQL-92 niladic function
Value returned
CURRENT_TIMESTAMP
Current date and time.
CURRENT_USER
Name of user performing insert.
SESSION_USER
Name of user performing insert.
SYSTEM_USER
Name of user performing insert.
USER
Name of user performing insert.
Defaults supply a value (with the INSERT and UPDATE statements) when no
value is supplied. For example, the AdventureWorks2008R2 database could
include a lookup table listing the different jobs employees can fill in the company.
Under a column that describes each job, a character string default could supply a
description when an actual description is not entered explicitly.
DEFAULT 'New Position - title not formalized yet'
In addition to constants, DEFAULT definitions can include functions. Use the following example
to get the current date for an entry.
DEFAULT (getdate())
A niladic-function scan can also improve data integrity. To keep track of the user that inserted a
row, use the niladic-function for USER. Do not enclose the niladic-functions with parentheses.
DEFAULT USER
CHECK Constraints
A column can have any number of CHECK constraints, and the condition can include
multiple logical expressions combined with AND and OR. Multiple CHECK constraints
for a column are validated in the order created.
The search condition must evaluate to a Boolean expression and cannot reference another
table.
A column-level CHECK constraint can reference only the constrained column, and a
table-level CHECK constraint can reference only columns in the same table.
CHECK CONSTRAINTS and rules serve the same function of validating the data during
INSERT and DELETE statements.
When a rule and one or more CHECK constraints exist for a column or columns, all
restrictions are evaluated.
use videorentalsystem
go
create table T7(
col1 int primary key ,
col2 nvarchar(10),
This example specifies that the values must be within a specific list or follow a specified pattern.
CHECK (emp_id IN ('1389', '0736', '0877', '1622', '1756')
OR emp_id LIKE '99[0-9][0-9]')
Before creating clustered indexes, understand how your data will be accessed.
Consider using a clustered index for:
Columns that contain a large number of distinct values.
Queries that return a range of values using operators such as BETWEEN, >, >=, <, and
<=.
Columns that are accessed sequentially.
Queries that return large result sets.
Columns that are frequently accessed by queries involving join or GROUP BY clauses;
typically these are foreign key columns. An index on the column(s) specified in the
ORDER BY or GROUP BY clause eliminates the need for SQL Server to sort the data
because the rows are already sorted. This improves query performance.
use videorentalsystem
go
create table T1(
col1 int primary key clustered,
col2 nvarchar(10),
col3 int unique)
go
------------------------------------------*
use videorentalsystem
go
create table T2(
col1 int primary key clustered,
col2 nvarchar(10),
col3 int unique clustered)
go
Msg 8112, Level 16, State 0, Line 2
Cannot add more than one clustered index for constraints on table 'T2'.
------------------------------------------------*
use videorentalsystem
go
create table T3(
col1 int primary key ,
col2 nvarchar(10),
col3 int unique clustered)
go
--------------------------------------------*
use videorentalsystem
go
create table T4(
col1 int ,
col2 nvarchar(10),
col3 int unique ,
primary key clustered(col1,col2) )
go
-------------------------------------------*
use videorentalsystem
go
create table T5(
col1 int primary key,
col2 nvarchar(10),
col3 int ,
unique clustered(col2,col3) )
go
--------------------------------------------*
use videorentalsystem
go
create table T6(
col1 int primary key nonclustered,
col2 nvarchar(10),
col3 int ,
)
go
---------------------------------------*
CREATE TABLE Stars
(StarID int PRIMARY KEY NONCLUSTERED,
StarName varchar(50) Unique,
SolarMass decimal(10,2) CHECK(SolarMass > 0),
StarType varchar(50) DEFAULT 'Orange Giant');
GO
CREATE CLUSTERED INDEX Ix_Star_Name
ON Stars(StarName)
GO
CREATE NONCLUSTERED INDEX Ix_Star_Type
ON Stars (StarType)
GO
-----------------------------------------------------------------*
use videorentalsystem
go
CREATE TABLE doc_exa (column_a INT) ;
GO
ALTER TABLE doc_exa ADD column_b VARCHAR(20) NULL ;
GO
EXEC sp_help doc_exa ;
GO
DROP TABLE doc_exa ;
GO
B. Dropping a column
The following example modifies a table to remove a column.
use videorentalsystem
go
CREATE TABLE doc_exb (column_a INT, column_b VARCHAR(20) NULL) ;
GO
ALTER TABLE doc_exb DROP COLUMN column_b ;
GO
EXEC sp_help doc_exb ;
GO
use videorentalsystem
go
CREATE TABLE doc_exy (column_a INT ) ;
GO
INSERT INTO doc_exy (column_a) VALUES (10) ;
GO
Select * from doc_exy
go
exec sp_help doc_exy
go
ALTER TABLE doc_exy ALTER COLUMN column_a DECIMAL (5, 2) ;
GO
DROP TABLE doc_exy ;
GO
Use VideoRentalSystem
go
CREATE TABLE doc_exc (column_a INT) ;
GO
ALTER TABLE doc_exc ADD column_b VARCHAR(20) NULL
CONSTRAINT exb_unique UNIQUE ;
GO
EXEC sp_help doc_exc ;
GO
DROP TABLE doc_exc ;
GO
Use VideoRentalSystem
go
CREATE TABLE doc_exd ( column_a INT) ;
GO
INSERT INTO doc_exd VALUES (-1) ;
GO
ALTER TABLE doc_exd WITH NOCHECK
ADD CONSTRAINT exd_check CHECK (column_a > 1) ;
GO
Select * from doc_exd
go
EXEC sp_help doc_exd ;
GO
DROP TABLE doc_exd ;
GO
Use VideoRentalSystem
go
CREATE TABLE doc_exe ( column_a INT CONSTRAINT column_a_un
UNIQUE) ;
GO
ALTER TABLE doc_exe ADD
-- Add a PRIMARY KEY identity column.
column_b INT IDENTITY
CONSTRAINT column_b_pk PRIMARY KEY,
-- Add a column that references another column in the same table.
column_c INT NULL
CONSTRAINT column_c_fk
REFERENCES doc_exe(column_a),
-- Add a column with a constraint to enforce that
-- nonnull data is in a valid telephone number format.
column_d VARCHAR(16) NULL
CONSTRAINT column_d_chk
CHECK
(column_d LIKE '[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]' OR
column_d LIKE
'([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'),
-- Add a nonnull column with a default.
column_e DECIMAL(3,3)
CONSTRAINT column_e_default
DEFAULT .081 ;
GO
EXEC sp_help doc_exe ;
GO
DROP TABLE doc_exe ;
GO
Use VideoRentalSystem
go
CREATE TABLE doc_exf ( column_a INT) ;
GO
INSERT INTO doc_exf VALUES (1) ;
GO
ALTER TABLE doc_exf
ADD AddDate smalldatetime NULL
CONSTRAINT AddDateDflt
DEFAULT GETDATE() WITH VALUES ;
GO
SELECT * FROM doc_exf
go
DROP TABLE doc_exf ;
GO
use VideoRentalSystem
go
CREATE TABLE cnst_example
(id INT NOT NULL,
name VARCHAR(10) NOT NULL,
salary MONEY NOT NULL
CONSTRAINT salary_cap CHECK (salary < 100000)
);
-- Valid inserts
INSERT INTO cnst_example VALUES (1,'Joe Brown',65000);
INSERT INTO cnst_example VALUES (2,'Mary Smith',75000);
-- This insert violates the constraint.
INSERT INTO cnst_example VALUES (3,'Pat Jones',105000);
-- Disable the constraint and try again.
ALTER TABLE cnst_example NOCHECK CONSTRAINT salary_cap;
INSERT INTO cnst_example VALUES (3,'Pat Jones',105000);
-- Re-enable the constraint and try another insert; this will fail.
ALTER TABLE cnst_example CHECK CONSTRAINT salary_cap;
INSERT INTO cnst_example VALUES (4,'Eric James',110000) ;
J. Dropping a constraint
The following example removes a UNIQUE constraint from a table.
Use VideoRentalSystem
go
(ContactID int) ;
GO
ALTER TABLE ContactBackup
ADD CONSTRAINT FK_ContactBacup_Contact FOREIGN KEY (ContactID)
REFERENCES Person (BusinessEntityID) ;
ALTER TABLE ContactBackup
DROP CONSTRAINT FK_ContactBacup_Contact ;
GO
DROP TABLE ContactBackup ;
Insert Command
Use VideoRentalSystem
go
exec sp_help t1
go
INSERT INTO T1(col1, col2,col3)
SELECT 1,'First' ,1
UNION ALL
SELECT 2,'Second' ,2
UNION ALL
SELECT 3,'Third' ,3
UNION ALL
SELECT 4, 'Fourth' ,4
UNION ALL
SELECT 5,'Fifth' ,5
GO
Select * from T1
go
------------------------------------*
INSERT INTO T1(col1, col2,col3)
VALUES( 1,'First' ,1),
(2,'Second' ,2),
( 3,'Third' ,3),
( 4, 'Fourth' ,4),
(5,'Fifth' ,5)
GO
Renaming column and table:
The script for renaming any column :
Exec sp_RENAME 'TableName.[OldColumnName]' ,
'[NewColumnName]', 'COLUMN'
Renaming a database:
ROLLBACK:
ROLLBACK TRANSACTION erases all data modifications made from the start of
the transaction or to a savepoint. It also frees resources held by the transaction.
ROLLBACK TRANSACTION without a savepoint_name or transaction_name rolls back to the
beginning of the transaction. When nesting transactions, this same statement rolls back all inner
transactions to the outermost BEGIN TRANSACTION statement. In both cases, ROLLBACK
and Inner2 transactions for other developers, but SQL Server does not use them.
Also, the COMMIT TRANSACTION statement does not use the transaction name.
Only a ROLLBACK uses the transaction name, and only the outermost transaction
name. For example, trying to do ROLLBACK TRANSACTION Inner1 where it is
commented out in the code snippet above would not work.
Committing inner transactions is ignored by Microsoft SQL Server. The
transaction is either committed or rolled back based on the action taken at the end
of the outermost transaction. If the outer transaction is committed, the inner nested
transactions are also committed. If the outer transaction is rolled back, then all
inner transactions are also rolled back, regardless of whether or not the inner
transactions were individually committed.
EXEC sp_databases
To list fields in a table :
sp_help tablename
Eg:
sp_help studentTable
Or
SELECT tablename
FROM DBName.sys.tables;
To the get the Tables count in the DB
SELECT Count(*)
FROM DBName.sys.tables;
SELECT name,Create_Date
FROM DBName.sys.tables;
(
EmpID int PRIMARY KEY,
EmpName varchar(30),
MgrID int FOREIGN KEY REFERENCES Emp(EmpID)
)
GO
INSERT dbo.Emp VALUES( 1, 'President', NULL)
go
INSERT dbo.Emp Values(2, 'Vice President', 1)
go
INSERT dbo.Emp values( 3, 'CEO', 2)
go
INSERT dbo.Emp values( 4, 'CTO', 2)
go
INSERT dbo.Emp values( 5, 'Group Project Manager', 4)
go
Queries:
Coulmn-aliasing:
Show all the details of all the distributors. In the report, change first and second
Select VideoNum,Vtitle, DistNum, Price from Video where Price >= 1.0 and Price
<= 3.0
go
Logical Operators
Assignment Operator
Bitwise Operators
Set Operators
Comparison Operators
Compound Operators
Unary Operators
Operators have the precedence levels shown in the following table. An operator on
higher levels is evaluated before an operator on a lower level.
Level
1
Operators
~ (Bitwise NOT)
=, >, <, >=, <=, <>, !=, !>, !< (Comparison operators)
NOT
(Logical)
AND
(Logical)
= (Assignment)
(Logical)
Note:
The plus (+) and minus (-) operators can also be used to perform arithmetic
operations on datetime and smalldatetime values.
SQL Server provides the following set operators. Set operators combine results
from two or more queries into a single result set.
EXCEPT INTERSECT
UNION
Compound operators execute some operation and set an original value to the result
of the operation. For example, if a variable @x equals 35, then @x += 2 takes the
original value of @x, add 2 and sets @x to that new value (37).
select * from employee where Sal >= 2000 AND Sal <= 4500
select * from employee where Sal BETWEEN 2000 AND 4500
select * from employee where Sal not between 2000 and 4500
select * from employee where hiredate between '1980-12-17' and '1981-12-03'
select * from employee where hiredate between '17-DEC-1980' and '3-DEC-1981'
select * from employee where deptno = 10 or deptno = 20;
select * from employee where deptno in(10,20);
select * from employee where deptno NOT in(10,20);
select * from employee where comm is null
use master
go
create database BIN collate Latin1_General_BIN
go
create database CI_AI_KS collate Latin1_General_CI_AI_KS
go
create database CS_AS_KS_WS collate Latin1_General_CS_AS_KS_WS
go
width-sensitive.
French_CI_AI - French, case-insensitive, accent-insensitive, kanatype-insensitive, widthinsensitive.
Korean_Wansung_BIN - Korean-Wansung, binary sort.
SQL_Latin1_General_CP1250_CI_AS - Latin1-General, case-insensitive, accentsensitive, kanatype-insensitive, width-insensitive.
Print Convert(int,125.66);
String based functions:
To get the length of a string, you can use the LEN() function.
Its syntax is:
int LEN(String)
If you have a string, to get the ASCII code of its leftmost character,
you can use the ASCII() function. Its syntax is:
int ASCII(String)
print Ascii('allen')
If you have the ASCII code of a character and want to find its actual character, you
can use the CHAR() function. Its syntax is:
char CHAR(int value)
print Char(65)
When you receive a string, if you want to convert all of its characters to lowercase,
you can use the LOWER() function. Its syntax is:
varchar LOWER(String)
This function takes as argument a string. Any lowercase letter that is part of the string would not
change. Any letter that is part of the string would be converted to lowercase. Any other character
or symbol would be kept "as is". After conversion, the LOWER() function returns a new string.
print left('system',3)
varchar RIGHT(String, NumberOfCharacters)
print right('system',3)
To replace one character or a sub-string from a string, you can use the
REPLACE() function. Its syntax is:
varchar REPLACE(String, FindString, ReplaceWith)
print replace('system','sys','xxxx')
Arithmetic Functions:
SIGN(Expression)
print sign(125)
print sign(-125)
print sign(null)
To get the absolute value of a number, you can use the ABS() function. Its syntax
is:
ABS(Expression)
print abs(-1567.77789)
print abs('-12.5')
In algebra, the ceiling of a number is the closest integer that is greater than or
higher than the number considered. In the first case, the ceiling of 12.155 is 13
because 13 is the closest integer greater than or equal to 12.155. The ceiling of
24.06 is 24.
To get the ceiling of a number, Transact-SQL provides the CEILING() function. Its syntax is:
CEILING(Expression)
This function takes as argument a number or an expression that can evaluate to a number. After
the conversion, if the function succeeds, it returns a double-precision number that is greater than
or equal to Expression.
print ceiling(12.456)
print ceiling(-24.06)
Consider two decimal numbers such as 128.44 and -36.72. The number 128.44 is
between 128 and 129 with 128 being the lower. The number 36.72 is between 37
and 36 with 37 being the lower. The lowest but closest integer value of a number
is referred to as its floor. Based on this, the floor of 128.44 is 128. The floor of
36.72 is 37.
To support finding the floor of a number, Transact-SQL provides the FLOOR() function. Its
syntax is:
FLOOR(Expression)
The FLOOR() function takes as argument a numeric value or an expression that can be
evaluated to a number.
print floor(128.44)
print floor(-36.72)
To calculate the exponential value of a number, Transact-SQL provides the EXP()
function. Its syntax is:
EXP(Expression)
This function takes one argument as a number or an expression that can be evaluated to a
number.
print exp(6.48)
The power of a number is the value of that number when raised to another number.
This is done using the following formula:
ReturnValue = xy
To support finding the power of a number, Transact-SQL provides the POWER() function. Its
syntax is:
POWER(x, y)
This function takes two required arguments. The first argument, x, is used as the base number to
be evaluated. The second argument, y, also called the exponent, will raise x to this value.
print power(5,2)
print power(5,2.1)
print power(2.667,8)
To assist with finding the natural logarithm of a number, Transact-SQL provides
the LOG() function. Its syntax is:
LOG(Expression)
This function takes one argument as a number or an expression that can evaluate to a number.
After the calculation, it returns the natural logarithm of the argument.
print log(10)
print log(48.16)
To calculate the base 10 logarithm of a number, Transact-SQL provides the
LOG10() function. Its syntax is:
LOG10(Expression)
The number to be evaluated is passed as the argument X. The function returns the logarithm on
base 10 using the formula:
y = log10x
which is equivalent to
x = 10y
print log10(10)
print log10(48.16)
To support the calculation of a square root, Transact-SQL provides the SQRT()
function. Its syntax is:
SQRT(Expression)
This function takes one argument as a positive decimal number. If the number is positive, after
the calculation, the function returns the square root of x.
print sqrt(25)
print sqrt(27.999)
Measure based function:
The letter , also written as PI, is a number used in various mathematical
calculations. Its approximate value is 3.1415926535897932. The calculator of
Microsoft Windows represents it as 3.1415926535897932384626433832795. To
get the value of PI, Transact-SQL provides the PI() function. Its syntax is simply:
PI()
print PI()
If you know the value of an angle in degrees and you want to get the radians,
Transact-SQL provides the RADIANS() function. Its syntax is:
RADIANS(Expression)
This function takes as argument a value in degrees. If it succeeds in its calculation, it returns the
radians value.
print radians(180)
If you know the radians but want to get the degrees of an angle, you can use the
DEGREES() function. Its syntax is:
DEGREES(Expression)
This function takes as argument a value in radians. If it succeeds, it returns the equivalent value
in degrees.
print degrees(4)
To get the cosine of an angle, you can call the COS() function. Its syntax is:
COS(Expression)
The angle to be considered is passed as the argument to this function. The function then
calculates and returns its cosine.
print cos(45)
To get the sine of an angle, you can use the SIN() function whose syntax is:
SIN(Expression)
The angle to be considered is passed as the argument. After its calculation, the function returns
the sine of the angle between 1 and 1.
print sin(60)
To get the tangent of an angle, you can use the TAN() function of Transact-SQL.
Its syntax is:
TAN(Expression)
This function simply returns the current date and time of the operating system.
print GetDate()
One of the primary operations you may want to perform on a date or a time value
would consist of adding a value to it. To support this operation, Transact-SQL
provides the DATEADD() function. Its syntax is:
DATEADD(TypeOfValue, ValueToAdd, DateOrTimeReferenced)
The third argument to this function is the value of a date or a time on which the operation will be
performed. It can be a constant value in the form of 'year/month/day' for a date or 'hour:minutes
AM/PM' for a time.
The second argument is the value that will be added. It should be a constant integer, such as 8, or
a floating point value, such as 4.06.
When calling this function, you must first specify the type of value that you want to add. This
type is passed as the first argument. It is used as follows:
If you want to add a number of years to a date, specify the TypeOfValue as Year or yy, or
yyyy (remember that SQL is case-insensitive).
print DATEADD(yy,4,GetDate())
print DATEADD(mm,4,GetDate())
print DATEADD(dd,4,GetDate())
print DATEADD(yy,8,'2010-04-27')
select DATEADD(yy,8,'2010-04-27') as [resultant date]
If you want to add a number of quarters of a year to a date, specify the
TypeOfValue as Quarter or d, or qq.
In the same way, you can add values as follows:
Type of
Value
Year
quarter
Month
dayofyear
Day
Week
Abbreviation
yy
yyyy
q
qq
m
mm
y
dy
d
dd
wk
ww
As a result
A number of years will be added to the date value
A number of quarters of a year will be added to the date value
A number of months will be added to the date value
A number of days of a year will be added to the date value
A number of days will be added to the date value
A number of weeks will be added to the date value
Hour
minute
second
millisecond
hh
n
mi
s
ss
ms
This function takes three arguments. The second argument is the starting date or the starting time
of the range to be considered. The third argument is the end or last date or time of the considered
range. You use the first argument to specify the type of value you want the function to produce.
This argument uses the same value as those of the DATEADD() function:
Type of
Value
Year
quarter
Month
dayofyear
Day
Week
Hour
minute
second
millisecond
Abbreviation
yy
yyyy
q
qq
m
mm
y
dy
d
dd
wk
ww
hh
n
mi
s
ss
ms
As a result
The function will return the number of years that have elapsed
between the start and the end dates
The function will return the number of quarters of a year that have
elapsed between the start and the end dates
The function will return the number of months that have elapsed
between the start and the end dates
The function will return the number of days of a year that have
elapsed between the start and the end dates
The function will return the number of days that have elapsed
between the start and the end dates
The function will return the number of weeks that have elapsed
between the start and the end dates
The function will return the number of hours that have elapsed
between the start and the end times or dates
The function will return the number of minutes that have elapsed
between the start and the end times or dates
The function will return the number of seconds that have elapsed
between the start and the end times or dates
The function will return the number of milliseconds that have elapsed
between the start and the end times or dates
print DATEDIFF(yy,'2000-11-10',GetDate())
Aggregate Functions:
Count: The database engine uses the Count() function to count the number of
occurrences of the category in the column and produces the total. This function
also counts NULL values. The syntax of the Count() function is:
int COUNT ( { [ [ ALL | DISTINCT ] expression ] | * } )
This function takes one argument. To get the count of occurrences of a value, in the Criteria
section, select COUNT(*).
The Count() function returns an int value. If you are working on a large number of
records, you can call the Count_Big() function. Its syntax is:
bigint COUNT_BIG ( { [ ALL | DISTINCT ] expression } | * )
Min: The lowest value of the category would be produced from the Min()
function. The syntax of this function is:
Max: The highest value of the category would be produced using the Max()
function. The syntax of this function is:
go
Select SUM(Price) from Video
go
Select MIN(Price) from Video
go
Select MAX(Price) from Video
go
Select AVG(Price) from Video
go
Select COUNT(Phone) from Customer
go
Select COUNT(*) from Customer
go
Select COUNT(VTitle) from Video
go
Select COUNT(distinct VTitle) from Video
go
Select COUNT(VType) from Video
go
Select COUNT(Distinct VType) from Video
go
use videorentalsystem
go
Select * from Video
go
SElect City, COUNT(CustomerId) from customer
Group By City
go
SElect City, COUNT(CustomerId) As [Customer Id] from customer
Group By City
go
Select VType, COUNT(*) As [Num Of Videos] from Video
group by VType
go
Select DistNum, COUNT(*) As [Num of Videos] from Video
group by DistNum
go
Select VType,DistNum, COUNT(*) As [Num of Videos] from Video
group by VType,DistNum order by VType
go
Select DistNum, SUM(Price) as [Total Investment] from Video
Group By DistNum
go
Select DistNum, Avg(Price) as [Average Price] from Video
Group By DistNum
go
RollUp:
It is used to calculate cumulative totals at the end of grouping
created based on the first column in group.
Select VType, Sum(Price) as [Total Invested] from Video
Group By VType with RollUp
go
Select isnull(VType,'Total') VType, Sum(Price) as [Total Invested] from Video
Group By VType with RollUp
go
Cube is used for calculating sub-totals at the end of each group
Select DistNum,isnull(VType,'Total'), Sum(Price) As [Total Cost] from Video
group by VType,DistNum with cube
go
while "compute by" will display the result of aggregate only once at end of the
group where as over(partition by...) displays the results of aggregates with every
row in the group and not at the end of the group.
Find total cost of videos their type wise while displaying complete details of
video.
use videorentalsystem
go
ranking. The rank of a row is one plus the number of distinct ranks that come
before the row in question.
If two or more rows tie for a rank in the same partition, each tied rows receives the
same rank. For example, if salaries of two employees are the highest and are
also equal then, they are both ranked one. The employee with the next highest
salary is ranked number two. This is one more than the number of distinct rows
that come before this row. Therefore, the numbers returned by the DENSE_RANK
function do not have gaps and always have consecutive ranks. The sort order used
for the whole query determines the order in which the rows appear in a result. This
implies that a row ranked number one does not have to be the first row in the
partition.
use VideoRentalSystem
go
select row_number() over (order by price desc) as [Row number],
VTitle,Price,RANK() over (order by price desc) as [Rank],
DENSE_RANK() over (order by price desc) as [Dense Rank]
from Video
---------------------*
select ROW_NUMBER() over(order by sal desc), ename,Sal,RANK()
over (order by sal desc), DENSE_RANK() over (order by sal desc) from employee
show emplyoee name,department number and salary along with rank fro the
employee based on salary dept wise by giving rank1 for the highest paid employee.
select ROW_NUMBER() over(partition by deptno order by sal desc)
as Sno, ename,deptno,sal,RANK() over (partition by deptno order by sal desc)
as [rank],DENSE_RANK() over (partition by deptno order by sal desc)
as [dense rank] from EMPLOYEE
---------------------------------*
Top 'n':
This is used to retrieve top 'n' rows from the list of rows retrieved by the selected
statements. It supports a keyword percent to retrieve top n percent of rows
instead of top n rows. It also supports the keyword 'with ties to retrieve every row
from the table that has same value as last row retrieved by the top n clause withing
the column that is specified by in order by clasuse.
Use Videorentalsystem
go
SELECT VideoNum, VTitle, Vtype,Price, Price =
CASE
select
case when GROUPING(deptno)=1 then 'All Departments'
JOINS
A Join allows us to retrieve data from multiple tables using
a single select statement.
They are classified as:
Cross Join
Inner Join
Outer Join
ANSI syntax:
SELECT <columns-list> FROM <table1>
inner join / outer join/ cross join <table2>
[ on <join condition>]......
Non-ANSI
SELECT <columns-list> FROM <table1>,<table2>....,<tableN>
[where <join codition>]
Inner JOIN
A JOIN that displays only rows that have a match in both the joined
tables is known as INNER JOIN.
Equi Join: This is the inner join or outer join that uses '=' operator in the join condition
Natural Join: Either inner join or outer join is a natural join if:
it uses '=' operator
all common columns in the tables are in the join condition
only one set of common columns is displayed in the operation
Non-Equi join: The inner join or outer join that uses an operator other than '=' in join condition
Self-Join: A join that joins a table with itself
Outer JOIN
A JOIN that includes rows even if they do not have related rows in the joined table is an
Outer JOIN. You can create three different outer JOINs to specify the unmatched rows to be
included:
Left Outer JOIN: In Left Outer JOIN, all rows in the first-named table, i.e. left table,
which appears leftmost in the JOIN clause, are included.
Unmatched rows in the right table do not appear.
Right Outer JOIN: In Right Outer JOIN, all rows in the second-named table, i.e. right table,
which appears rightmost in the JOIN clause, are included. Unmatched rows in the left table are
not included.
Full Outer JOIN: In Full Outer JOIN, all rows in all the joined tables are included, whether they
are matched or not.
Examples:
Use Videorentalsystem
go
Create Table VideoGrades(LowPrice decimal(5,2),HighPrice decimal(5,2),Grade
char(1))
go
Insert into VideoGrades values(0.5,1.0,'C')
go
Insert into VideoGrades values(1.1,3.0,'B')
go
Insert into VideoGrades values(3.1,5.0,'A')
go
Use Videorentalsystem
go
select Rental.*,customer.FName,customer.LName
from Rental Cross Join Customer
go
Use Videorentalsystem
go
select Rental.CustomerId,Rental.VideoNumber,
Rental.DateRented,Customer.FName,Customer.LName,Customer.City
from Rental Inner Join Customer
ON Rental.CustomerId=Customer.CustomerId
go
--OR
Use Videorentalsystem
go
select Rental.CustomerId,Rental.VideoNumber,
Rental.DateRented,Customer.FName,Customer.LName,Customer.City
from Rental Join Customer
ON Rental.CustomerId=Customer.CustomerId
go
Joining 3 Tables:
Use Videorentalsystem
go
select Rental.CustomerId,Rental.VideoNumber,Video.VTitle,
Video.VType,Rental.DateRented,Customer.FName,Customer.LName,
Customer.City from Rental Inner Join Video
ON Rental.VideoNumber = Video.VideoNum Inner Join customer
ON Rental.CustomerId=Customer.CustomerId
Go
USE ADWorks
GO
SELECT TOP 100 P.ProductID,
P.Name,
P.ListPrice,
P.Size,
P.ModifiedDate,
SOD.UnitPrice,
SOD.UnitPriceDiscount,
SOD.OrderQty,
SOD.LineTotal
FROM Sales.SalesOrderDetail SOD
INNER JOIN Production.Product P
ON SOD.ProductID = P.ProductID
WHERE SOD.UnitPrice > 1000
ORDER BY SOD.UnitPrice DESC
GO
Joining 3 Tables
USE AdWorks;
GO
SELECT C.ContactID,
C.FirstName,
C.LastName,
SP.SalesPersonID,
SP.CommissionPct,
SP.SalesYTD,
SP.SalesLastYear,
SP.Bonus,
ST.TerritoryID,
ST.Name,
ST.[Group],
ST.SalesYTD
FROM Person.Contact C
INNER JOIN Sales.SalesPerson SP
ON C.ContactID = SP.SalesPersonID
Inner JOIN Sales.SalesTerritory ST
ON ST.TerritoryID = SP.TerritoryID
ORDER BY ST.TerritoryID, C.LastName
GO
In this example we are combining two concepts to show that more than two tables can be
JOINed in one SELECT statement and more than one JOIN type can be used in a single
SELECT statement. In the sample code below, we are retrieving the matching data between the
Person.Contact and Sales.SalesPerson tables in conjunction with all of the data from the
Sales.SalesPerson table and matching data in the Sales.SalesTerritory table. For records that
exist Sales.SalesPerson table and not in the Sales.SalesTerritory table, NULL values are returned
for the columns in the Sales.SalesTerritory. In addition, this code uses two columns to order the
data i.e. ST.TerritoryID and C.LastName.
USE AdWorks;
GO
SELECT C.ContactID,
C.FirstName,
C.LastName,
SP.SalesPersonID,
SP.CommissionPct,
SP.SalesYTD,
SP.SalesLastYear,
SP.Bonus,
ST.TerritoryID,
ST.Name,
ST.[Group],
ST.SalesYTD
FROM Person.Contact C
INNER JOIN Sales.SalesPerson SP
ON C.ContactID = SP.SalesPersonID
LEFT OUTER JOIN Sales.SalesTerritory ST
ON ST.TerritoryID = SP.TerritoryID
ORDER BY ST.TerritoryID, C.LastName
GO
RIGHT OUTER JOIN Example
In an effort to explain how the RIGHT OUTER JOIN and LEFT OUTER JOIN is logically a
reciprocal on one another, the code below is re-written version of the LEFT OUTER JOIN
above. As you can see the JOIN order and tables are different, but the final result set matches the
LEFT OUTER JOIN logic. In the sample code below, we are retrieving the matching data
between the Person.Contact and Sales.SalesPerson tables in conjunction with all of the data from
the Sales.SalesPerson table and matching data in the Sales.SalesTerritory table. For records that
exist Sales.SalesPerson table and not in the Sales.SalesTerritory table, NULL values are returned
for the columns in the Sales.SalesTerritory.
USE AdWorks;
GO
SELECT C.ContactID,
C.FirstName,
C.LastName,
SP.SalesPersonID,
SP.CommissionPct,
SP.SalesYTD,
SP.SalesLastYear,
SP.Bonus,
ST.TerritoryID,
ST.Name, ST.[Group],
ST.SalesYTD
FROM Sales.SalesTerritory ST
RIGHT OUTER JOIN Sales.SalesPerson SP
ON ST.TerritoryID = SP.TerritoryID
INNER JOIN Person.Contact C
ON C.ContactID = SP.SalesPersonID
ORDER BY ST.TerritoryID, C.LastName
GO
Self Join Example
In this example, we are actually self joining to the HumanResources.Employee table. We are
doing this to obtain the information about the Employee and Manager relationship in the
HumanResources.Employee table. In conjunction with that JOIN logic we are also joining to the
Person.Contact twice in order to capture the name and title data based on the original Employee
and Manager relationships. In addition, another new concept introduced in this query is aliasing
each of the columns. Although we could have done so in the previous examples, we made point
of doing so in this query to differentiate between the Employee and Manager related data.
USE AdWorks;
GO
SELECT M.ManagerID AS 'ManagerID',
M1.ContactID AS 'ManagerContactID',
M1.FirstName AS 'ManagerFirstName',
M1.LastName AS 'ManagerLastName',
M.Title AS 'ManagerTitle',
E.EmployeeID AS 'EmployeeID',
E1.ContactID AS 'EmployeeContactID',
E1.FirstName AS 'EmployeeFirstName',
E1.LastName AS 'EmployeeLastName',
E.Title AS 'EmployeeTitle'
FROM HumanResources.Employee E
INNER JOIN HumanResources.Employee M
ON E.ManagerID = M.ManagerID
INNER JOIN Person.Contact E1
ON E1.ContactID = E.ContactID
INNER JOIN Person.Contact M1
ON M1.ContactID = M.ContactID
ORDER BY M1.LastName
GO
USE AdWorks;
GO
SELECT TOP 100 P.ProductID,
P.Name,
P.ListPrice,
P.Size,
P.ModifiedDate,
SOD.UnitPrice,
SOD.UnitPriceDiscount,
SOD.OrderQty,
SOD.LineTotal
FROM Sales.SalesOrderDetail SOD
CROSS JOIN Production.Product P
WHERE SOD.UnitPrice > 3500
ORDER BY SOD.UnitPrice DESC
GO
FULL OUTER JOIN Example
In our last example, we have modified the logic from the LEFT OUTER JOIN example above
and converted the LEFT OUTER JOIN syntax to a FULL OUTER JOIN. In this circumstance,
the result set is the same as the LEFT OUTER JOIN where we are returning all of the data
between both tables and data not available in the Sales.SalesTerritory is returned as NULL.
USE AdWorks;
GO
SELECT C.ContactID,
C.FirstName,
C.LastName,
SP.SalesPersonID,
SP.CommissionPct,
SP.SalesYTD,
SP.SalesLastYear,
SP.Bonus,
ST.TerritoryID,
ST.Name,
ST.[Group],
ST.SalesYTD
FROM Person.Contact C
INNER JOIN Sales.SalesPerson SP
ON C.ContactID = SP.SalesPersonID
FULL OUTER JOIN Sales.SalesTerritory ST
ON ST.TerritoryID = SP.TerritoryID
ORDER BY ST.TerritoryID, C.LastName
GO
SET OPERATIONS
Set Operations are used to combine multiple result sets
into one single result set.
There is BIG difference between join and Combine.
Join is Horizontal operation and Combine is vertical operation.
select * into cheapvideos from video where price < 3.0
select * into Employee_Hist from employee where hiredate < '1981-12-31'
Lets go with simple example so that it will give you clear picture.
Basically there are 3 set operators available in SQL Server.
1) Union: This is to combine two or more result sets into single
with or without (by using ALL) duplicates.
2) Except: Takes the data from one result set
where there is no matching in another.
Here also we are combining the results from Employee and Employee_HIST table.
When you use UNION ALL operator it will not eliminate the duplicate records
meaning if you have the same record in both tables then in the final output you will
see both the records.
UNION is always creates the performance issue. So when ever you are using
UNION use it very judiciously. If you are not sure of what kind of data you have
then you can use UNION. If you know you dont have any duplicate records for
which you want to combine the results then use UNION ALL.
SELECT Empno,eName,Sal,hiredate from Employee
UNION ALL
SELECT Empno,eName,Sal,hiredate from Employee_HIST
Intersection:
As you know this is basically to combine multiple result sets into single to fetch
the common records in multiple result sets. Inner join finds common rows
horizontally, while an INTERSECT finds common rows vertically.
SELECT VideoNum,Vtitle,Price from Video
Intersect
SELECT VideoNum,Vtitle,Price from CheapVideos
go
SELECT Empno,eName,Sal,hiredate from Employee
INTERSECT
SELECT Empno,eName,Sal,hiredate from Employee_HIST
Except:
This is basically to return all the records from one result set where there is no
matching in another table. This looks very similar to Outer join but join does
horizontally and EXCEPT does vertically.
SELECT VideoNum,Vtitle,Price from Video
Except
SELECT VideoNum,Vtitle,Price from CheapVideos
go
SELECT Empno,eName,Sal,hiredate from Employee
EXCEPT
SELECT Empno,eName,Sal,hiredate from Employee_HIST
With the above query we are fetching only the records which are in Employee but
not in Employee_HIST.
SELECT VideoNum,Vtitle,Price,Price*0.5 AS sf1 from Video
Union ALL
SELECT VideoNum,Vtitle,Price,Price*0.5 As sf2 from CheapVideos
go
SELECT VideoNum,Vtitle,Price from Video
Union ALL
SELECT VideoNum,Vtitle,Price from CheapVideos Order by Price
go
Summary:
Normally set operations are costly operations so use them very judiciously.
SUBQUERIES
A subquery is a query that is nested inside a
SELECT, INSERT, UPDATE, or DELETE statement,
or inside another subquery.
A subquery is also called an inner query or inner select,
while the statement containing a subquery is also called
an outer query or outer select.
Many Transact-SQL statements that include subqueries
can be alternatively formulated as joins. Other questions
independent query. Conceptually, the subquery results are substituted into the outer
query (although this is not necessarily how Microsoft SQL Server actually
processes Transact-SQL statements with subqueries).
There are three basic types of subqueries:
Subqueries that operate on lists introduced with IN,
or those that a comparison operator modified by ANY or ALL.
Subqueries tht are introduced with an unmodified
comparison operator and must return a single value.
A subquery that only selects one column or expression
and returns just one row is also called as a Scalar subquery.
Subquries that are used with EXISTS.
Scalar sunqueries:
scalar subqueries allow us to treat the output of a subquery as a column or even an
expression within a SELECT statement. It is a query that only selects one column
or expression and returns just one row. If the scalar subquery fails to return any
rows, Sql Server will use a NULL value for the output of the scalar subquery.
Use VideoRentalSystem
go
select d.distributornum,d.DistributorName ,
(select COUNT(*) from video v
where v.distnum=d.distributornum) As [Num Videos]
from Distributor d;
----------------------------------------*
Show number,title,type and distribuotr number of all
those videos supplied by the distrbutor Disney Studios
Use VideoRentalSystem
go
select VideoNum,VTitle,VType,DistNum from Video where
Distnum = (select DistributorNum from Distributor
where DistributorName = 'Disney Studios' );
Correlated Subquery:
Write a query to determine how many number of times every video is ever issued
to any customer:
select v.VideoNum,v.VTitle,(select COUNT(*) from rental r
where r.VideoNumber=v.VideoNum) As [Num Issues] from Video V;
go
----------------------------*
---------------------------*
Write a query to show all the details of all those videos
that were supplied by the same distributor who supplied the
video 4777-1
select * from Video where DistNum =
(select DistNum from Video where
VideoNum='4777-1')
go
--------------------------------*
select * from Employee where job = (select job from employee where
empno=7698);
-----------------------------------*
List those videos whose price is less than the price of any video
belonging to the category 'Drama'
Select * from Video where Price < Any(
Select e1.* from employee e1 where e1.deptno <> (select e2.deptno from
employee e2 where e2.ename='SMITH');
-----------------------*
Nested Subquery:
Write a query to list all the details of all those videos rented by customers living in
the city LA
Select Video.* from Video where VideoNum In
(Select Rental.VideoNumber from Rental where Rental.CustomerId In
(Select Customer.CustomerId from customer where Customer.City='LA'))
go
-------------------------------*
List all the employees who are working in Chicago.
select empno,ename,job,sal,deptno from empLOYEE where
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Correlated subquery
A correlated subquery is evaluated once FOR EACH ROW as opposed to a normal
subquery which is evaluated only once for each table.
You can reference the outer query inside the correlated subquery using an alias
which makes it so handy to use.
Let's select all employees whose salary is less than the average of all the
employees' salaries in the same department.
Correlated Subquery:
Write a query to determine how many number of times every video is ever issued
to any customer:
select v.VideoNum,v.VTitle,(select COUNT(*) from rental r
where r.VideoNumber=v.VideoNum) As [Num Issues] from Video V;
----------------------------*
Write a query to determine how many rows in the DEPARTMENTS table contain
an employee corresponding to each row in the EMPLOYEE table.
Use VideoRentalSystem
go
select d.distributornum,d.DistributorName ,
(select COUNT(*) from video v
where v.distnum=d.distributornum) As [Num Videos]
from Distributor d;
------------------------------------*
select x.ename ,x.sal ,x.deptno from employee x where
x.sal < (select avg(sal) from employee y
where x.deptno = y.deptno)
order by x.deptno;
---------------------------------------------*
Using a subquery in an update:
Raise the prices of all cheap videos
whose present prices are less than the
present avg price to the present average price itself.
Select * into TempVideo from Video
go
UPDATE TempVideo
set price = (select avg(price)
from Tempvideo)
where price <
(select avg(price) from TempVideo );
Select * from TempVideo
go
------------------------*
Let's give these people
(whose salary is less than their department's average) a raise.
max_price numeric,
min_price numeric,
avg_price numeric
);
insert into summary (
sum_price,
max_price,
min_price,
avg_price)
values (
(select sum(price) from video),
(select max(price) from video),
(select min(price) from video),
(select avg(price) from video)
);
select * from summary;
_________________________________*
create table summary (
sum_sal numeric,
max_sal numeric,
min_sal numeric,
avg_sal numeric
);
insert into summary (
sum_sal,
max_sal,
min_sal,
avg_sal)
values (
(select sum(sal) from employee),
(select max(sal) from employee),
(select min(sal) from employee),
(select avg(sal) from employee)
);
Using
Exists:
where exists (select ename from employee x where e.mgr = x.empno and
ename = 'KING');
WHEN YOU USE 'IN', WHILE CHECKING FOR WHERE CONDITION SQL
SERVER ENGINE DOES WHOLE TABLE SCAN.
IF YOU USE 'EXISTS' AS SOON AS ENGINE FINDS THE REQUIRED
ROW IT WILL STOP EXECUTING QUERY AND GOING FURTHER SCANNING
TABLE.
SO BASICALLY EXISTS IS FASTER AS COMPARED TO IN.
Find the names of all those employees who are working In Mr.Smith's department.
In the output show department name also against each employee name.
SELECT e1.ename, d.dname
FROM employee e1 inner join departments d
on e1.deptno = d.deptno
AND EXISTS
(SELECT * FROM employee e2
WHERE e2.ename = 'Smith' AND e2.deptno = e1.deptno)
--------------------------------------------*
Nested sub query:
Display all the details of those videos whose price is more than the least price of all
videos supplied by 'Disney Studios'.
Select * from Video where Price > (
Select MIN(price) from Video
where DistNum = (
Select DistributorNum from Distributor
where DistributorName='Disney Studios'))
go
-----------------------*
Display the records of those employees who are earning more than the least salary
earned by any employee working at NEW YORK.
SELECT eName
FROM Employee
WHERE sal >(SELECT min(sal)
FROM Employee
WHERE deptno = (SELECT deptno
FROM departments
WHERE loc = 'NEW
YORK'));
Show the records of those employees who are earning less than any salesman along
with records of those employees who are working the same department in which
any salesman works;
SELECT *
FROM Employee
WHERE sal <(SELECT min(sal) FROM Employee
WHERE job = 'SALESMAN')
or
deptno = (SELECT distinct deptno
FROM Employee
WHERE job = 'SALESMAN');
VIEWS
View
A view is a virtual table that consists of columns from one or more tables.
These tables are referred to as base or underlying tables.
Once we define a view, we can reference it like any other table in a database.
A view serves as a security mechanism. It ensures that users are able to
retrieve and modify only the data seen by them.
A view also serves as a mechanism to simplify query execution.
Complex queries can be stored in the form of a view,
and data from the view can be extracted using simple queries.
Creating Views
A view can be created by using the CREATE VIEW statement.
Syntax
CREATE VIEW view_name
[(column_name[,column_name].)]
[WITH ENCRYPTION]
AS select_statement [WITH CHECK OPTION]
Where:
view_name specifies the name of the view and must follow the rules for identifiers.
column_name specifies the name of the column to be used in view. If the
column_name option is not specified, then the view is created with the same
columns as specified in the select_statement.
WITH ENCRYPTION encrypts the text for the view in the syscomments table.
AS specifies the actions that will be performed by the view.
select_statement specifies the SELECT Statement that defines a view. The view
may use the data contained in other views and tables.
WITH CHECK OPTION forces the data modification statements to fulfill the
criteria given in the SELECT statement defining the view. It also ensures that the
data is visible after the modifications are made permanent.
The restrictions imposed on views are as follows:
A view can be created only in the current database.
The name of a view must follow the rules for identifiers and must not be the
same as that of the base table.
A view can be created only if there is a SELECT permission on its base
table.
A SELECT INTO statement cannot be used in view declaration statement.
A for trigger or an index cannot be defined on a view.
The CREATE VIEW statement cannot be combined with other SQL
statements in a single batch.
Example
CREATE VIEW vwCustomer
AS
SELECT CustomerId, Company Name, Phone
FROM Customers
Creates a view called vwCustomer. Note that the view is a query stored as an
object. The data is derived from the columns of the base table Customers.
You use the view by querying the view like a table.
SELECT *FROM vwCUSTOMER
Updatable Views
You can modify the data of an underlying base table through a view,
as long as the following conditions are true:
Any modifications, including UPDATE, INSERT, and DELETE statements,
must reference columns from only one base table.
The columns being modified in the view must directly reference the
underlying data in the table columns. The columns cannot be derived in
any other way, such as through the following:
An aggregate function: AVG, COUNT, SUM, MIN, MAX,
GROUPING, STDEV, STDEVP, VAR, and VARP.
A computation. The column cannot be computed from an expression
that uses other columns. Columns that are formed by using the set
operators UNION, UNION ALL, CROSSJOIN, EXCEPT,
and INTERSECT amount to a computation and are also not updatable.
The columns being modified are not affected by GROUP BY,
HAVING, or DISTINCT clauses.
TOP is not used anywhere in the select_statement of the view together with
the WITH CHECK OPTION clause.
Syntax
sp_helptext
sp_helptext
Displaying
@objname
[
@objname
the
sp_helptext 'dbo.nameofsp'
definition
'name'
=
[
of
@columnname
trigger
'name'
=
or
computed_column_name
stored
procedure
STORED PROCEDURES
Creating a Stored Procedure that returns a value:
use videorentalsystem
go
CREATE PROC TestReturn (@InValue int)
AS
Return @Invalue
GO
DECLARE @ReturnValue INT
EXEC @ReturnValue = TestReturn 3
SELECT ReturnValue=@ReturnValue
GO
use videorentalsystem
go
CREATE PROCEDURE op_test
@ip1 int,
@op1 int OUTPUT,
@op2 int OUTPUT
AS
set @op1 = 10 + @ip1
set @op2 = 20 + @ip1
go
--Call it like this:
declare @p int = 10
declare @x int
declare @y int
exec op_test @p ,@x OUTPUT, @y OUTPUT
select @X
select @y
Creating a Stored Procedure with OUTPUT Parameters
use videorentalsystem
go
CREATE PROCEDURE GetNumOfVideosByVType (
@VType NVARCHAR(50),
AS
SELECT @VideoCount = COUNT(*)
FROM Video
WHERE VType = @VType
Return
Go
--call
DECLARE @TheCount INT
EXEC GetNumOfVideosByVType
@VType = 'Comedy', @VideoCount = @TheCount OUTPUT
SELECT TheCount = @TheCount
GO
------------------*
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'MyProcedure2' AND type = 'P')
DROP PROCEDURE MyProcedure2
GO
Create procedure MyProcedure2(
@para1 int ,
@para2 int=100)
As
Print @para1 + @para2
Return
Go
Exec MyProcedure2 150
go
Exec MyProcedure2 @para1 = 200
go
-------------------------------*
use VideoRentalSystem
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'MyProcedure3' AND type = 'P')
DROP PROCEDURE MyProcedure3
GO
Create procedure MyProcedure3(
@para1 int=50 ,
@para2 int=100)
As
Print @para1 + @para2
Return
Go
Exec MyProcedure3 10,20
go
Exec MyProcedure3 @para1 = 200
go
Exec MyProcedure3 @para2 = 5
go
Exec MyProcedure3 @para1 = 45, @para2 = 60
go
-------------------------*
EXECUTE VideoInfo
-- Or
EXECUTE VideoInfo 'G%'
-- Or
EXECUTE VideoInfo @VTitle = 'T%'
-- Or
EXECUTE VideoInfo 'Alien Surfer'
-------------------------------*
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'MyProcedure4' AND type = 'P')
DROP PROCEDURE MyProcedure4
GO
Create procedure MyProcedure4
AS
Select * from UnKnownTable
Go
ON
e.DEPTNO = d.DEPTNO INNER JOIN SALGRADE ON e.SAL BETWEEN
SALGRADE.LOSAL AND
SALGRADE.HISAL LEFT OUTER JOIN EMPLOYEE m ON e.MGR =
m.EMPNO ORDER BY e.deptno
GO
The WITH ENCRYPTION clause obfuscates the text of a stored procedure. This example
creates an obfuscated procedure, uses the sp_helptext system stored procedure to get
information on that obfuscated procedure, and then attempts to get information on that procedure
directly from the syscomments table.
use VideoRentalSystem
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'MyProcedure5' AND type = 'P')
DROP PROCEDURE MyProcedure5
GO
Create procedure MyProcedure5
AS
Select * from Video
Go
exec sp_helptext MyProcedure5
go
-------------------------*
use VideoRentalSystem
go
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'MyProcedure5' AND type = 'P')
DROP PROCEDURE MyProcedure5
GO
Create procedure MyProcedure5 WITH ENCRYPTION
AS
Select * from Video
Go
exec sp_helptext MyProcedure5
go
---------------------*
BEGIN
Declare @a int = 10
Print @a
End
Triggers
Introduction to Triggers
A trigger is an action that is performed behind-the-scenes when an event occurs.
Unlike a stored procedure, we never have to execute a trigger. The operating system (through
the object(s) event(s)) and the database engine take care of this. A trigger acts behind the scenes
when the object to which it is associated fires the appropriate event. In fact, the event fires
whether the object received a change or not (whether a record was created, edited, or deleted, or
not).
There are
DML Triggers
A DML trigger is a procedure that acts as a result of a data manipulation language (DML) event
occurring on a table. This means that the trigger must be created in connection to a table of a
non-system database.
AFTER/FOR INSERT Triggers :
An insert trigger is a DML trigger that acts when a new record is added to its intended table.
Such a trigger uses the INSERT keywork. The primary formula to create an INSERT DML
trigger on a table is:
CREATE TRIGGER TriggerName
ON TableName
AFTER/FOR INSERT/UPDATE/DELETE
AS
TriggerCode
In our formula, we assume that the trigger will apply when a record has been added to the table.
Therefore, you use either the AFTER INSERT or the FOR INSERT expression.
To start the SQL code that constitutes the trigger, write AS, and then write your code.
After creating an INSERT trigger, at the right time (when its intended event fires), it will be
executed. When this happens, the database engine automatically and internally creates a
temporary table named inserted. This table holds a copy of the records that were created. You
can access those records if necessary.
use northwind;
go
Create table MyBooks(Bookid int primary key,
BookTitle nvarchar(100),
BookAuthor nvarchar(100),
BookPrice decimal);
go
CREATE TABLE DatabaseOperations (
DBOperationID int identity(1,1) NOT NULL,
ObjectType nchar(20),
ObjectName nvarchar(40),
PerformedBy nvarchar(50),
ActionPerformed nvarchar(50),
TimePerformed datetime,
CONSTRAINT PK_DBOperations PRIMARY KEY(DBOperationID)
);
GO
CREATE TRIGGER RecordInsertion
ON MyBooks
AFTER INSERT
AS
BEGIN
INSERT INTO DatabaseOperations
VALUES(N'Table', N'MyBooks', SUSER_SNAME(),
N'Created a new record', GETDATE())
END
GO
select * from databaseoperations;
go
insert into MyBooks values(1,'Programming through C',
'Venugopal',450);
go
(1 row(s) affected)
(1 row(s) affected)
The new keyword in this formula is UPDATE. This indicates that the DML trigger will act when
the record has been updated. Everything else is as described for the INSERT operator.
Remember to use either AFTER UPDATE or FOR UPDATE.
go
select * from databaseoperations;
go
AFTER/FOR DELETE Triggers
When a record has been removed from a table, you can apply a DML trigger in
response. To make it possible, you can use the following formula:
CREATE TRIGGER TriggerName
ON TableName
AFTER/FOR DELETE
AS
TriggerCode
This time, the formula uses the DELETE operator as in AFTER DELETE or FOR DELETE.
The other factors follow the same description we saw for the INSERT operator.
When a DELETE trigger has acted on a table, the database engine creates a special temporary
table named deleted. This table holds a copy of the records that were deleted. Eventually, if
necessary, you can access this table to find out about those records.
Trigger Management
A trigger is a database object. As such, it has a name. It can be modified. It can also
be deleted.
Modifying a Trigger
If the behavior of a trigger is not appropriate, you can change it. The formula to
Deleting a Trigger
If you do not need a trigger anymore, you can remove it from a table. The formula
to do this is:
DROP TRIGGER TriggerName
After the DROP TRIGGER expression, enter the name (of the trigger).
We saw that, using a DML trigger, you can make a notification. For example you
can fill out a log to keep track of the changes. By default, when a record is
submitted, it gets saved. In some cases, when a user has opened a table and tried to
make a change, such as adding a new record, editing an existing record, or deleting
a record, instead of accepting the change, you can dismiss it. You can then use a
DML trigger to make a note. This is the basis of another category of DML triggers:
an "instead of" trigger.
While an AFTER/FOR trigger acts on a table after the action has occurred, you may want to do
something before the event fires. For example, you may want to prevent the user from adding a
new record on a tale, or from changing an existing record, or from deleting a record. Of course, it
is better to take care of this before the action is performed. One way you can do this is by
creating an "instead of" trigger.
AS
TriggerCode
You start with the CREATE TRIGGER expression followed by a name for the trigger. After the
name of the trigger, type ON followed by the name of either a table or a view on which the
trigger will act.
From our review of the AFTER trigger, the new expression here is INSTEAD OF. This
expression is followed by the type of operation to perform:
If you want to catch the creation of a new record, use the INSERT operator
If you want to catch the editing operation of an existing record, use the UPDATE
operator
If you want to catch the removal of a record, use the DELETE operator
To start the triggering code, type AS and write the desired code.
If you use the INSTEAD OF expression, the trigger starts when the table or view is opened but
before a change has taken place. The difference with the AFTER trigger is that, this time, you
can perform some action(s) before the change is made on the table or view. This also implies
that, if the code of the trigger is to create a new record, at this time, the record doest not yet exist,
which means you cannot catch that record. At this time also, you can prevent the record from
being created (since it has not yet been created anyway). For example, the following code will
not accept that a new record be added to the table:
USE SmallBusiness;
GO
CREATE TRIGGER CreateCustomer
ON Customers
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO DatabaseOperations
VALUES(SUSER_SNAME(),
N'Attempt to create new record', GETDATE())
END
GO
If you want to get a copy of the record that was affected by the event, you can
access it from the inserted (for an INSERT or UPDATE trigger) or from the
deleted (for a DELETE) trigger. Here is an example:
USE SmallBusiness;
GO
DDL Triggers
You start a DDL trigger with the CREATE TRIGGER expression followed by a name for the
new trigger. The name follows the same rules we have applied to objects so far. After the name
of the trigger, type the ON keyword:
If you want the trigger to act on the current database, type DATABASE. When the
intended event occurs on the current database, the trigger will execute
If you want the trigger to act on the server, follow the ON operator with ALL SERVER.
In this case, when the intended event occurs on any part of the server, the trigger executes
After specifying the object (the whole server or only the current database) on which the trigger
will act, type either FOR or AFTER. This is followed by the event against which the trigger will
act. As mentioned already, the events are DDL commands. To specify the event, use the formula
of the command with the words separated by an underscore. For example, if you want the trigger
to act when a CREATE TABLE command is executed, specify the event as CREATE_TABLE.
After specifying the event that will fire, type AS followed by the normal code of the trigger.
Here is an example that makes a note and adds it to a table when a new table has been created:
USE SmallBusiness;
GO
CREATE TRIGGER LogNewTableCreation
ON DATABASE
FOR CREATE_TABLE
AS
BEGIN
INSERT INTO DatabaseOperations
VALUES(SUSER_SNAME(),
N'A new table was created', GETDATE())
END
GO
Whenever a new table is created in the current database, the trigger runs, gets the
name of the user who created the table, the date and time the table was created, and
a small message. These pieces of information are then stored in a log table.
As mentioned for DML triggers, you manage DDL triggers by modifying or deleting them.
These are done using the same description we saw for DML triggers.
DDL Trigger:
Returns information about server or database events. EVENTDATA is called when
an event notification fires, and the results are returned to the specified service
broker. EVENTDATA can also be used inside the body of a DDL or logon trigger.
USE Exercise;
GO
CREATE TRIGGER safety
ON DATABASE
FOR CREATE_TABLE
AS
PRINT 'CREATE TABLE Issued.'
SELECT EVENTDATA().value
('(/EVENT_INSTANCE/TSQLCommand/CommandText)
[1]','nvarchar(max)')
RAISERROR ('New tables cannot be created in this database.', 16, 1)
ROLLBACK
;
GO
--Test the trigger.
CREATE TABLE NewTable (Column1 int);
GO
--Drop the trigger.
Triggers use two logical tables called the INSERTED and the DELETED table.
These tables are accessed only inside triggers and we cannot directly modify the
data in these tables. These tables hold the new and old rows of the data being
inserted or deleted.
If we DELETE a row , a row will be created in the DELETED table.
Similarly if we INSERT a row, a row is created in the INSERTED table.
If we update a record with an UPDATE statement, the INSERTED table will
contain the updated row and a previous state of the row will be added to the
DELETED table.
Note: we cannot use a CREATE INDEX command on these tables.
CREATE TRIGGER TrigIns
ON Person
AFTER INSERT
AS
insert into InsAudit(SSN,DateOfIns,InsByWhom)
values((Select SSN From INSERTED),GetDate(),SYSTEM_USER)
Go
insert into Person(SSN,Name,Address,BirthDate)
values('111-11','John','5th Avenue LA','1980-05-25')
go
Select * From Person
go
Go
Delete From Person
go
Select * from Person
go
ON Sales.SalesPersonQuotaHistory
AFTER INSERT
AS RAISERROR ('Notify Compensation', 16, 10);
GO
The Transact-SQL statements create two base tables, a view, a table to record
errors, and the INSTEAD OF trigger on the view. The following tables separate
personal and business data and are the base tables for the view.
CREATE TABLE Person
(
SSN
char(11) PRIMARY KEY,
Name
nvarchar(100),
Address nvarchar(100),
Birthdate datetime
)
CREATE TABLE EmployeeTable
(
EmployeeID
int PRIMARY KEY,
SSN
char(11) UNIQUE,
Department
nvarchar(10),
Salary
money,
CONSTRAINT FKEmpPer FOREIGN KEY (SSN)
REFERENCES Person (SSN)
)
The following view reports all relevant data from the two tables for a person.
CREATE VIEW Employee AS
SELECT P.SSN as SSN, Name, Address,
Birthdate, EmployeeID, Department, Salary
FROM Person P, EmployeeTable E
WHERE P.SSN = E.SSN
We can record attempts to insert rows with duplicate social security numbers. The
PersonDuplicates table logs the inserted values, the name of the user who tried the
insert, and the time of the insert.
CREATE TABLE PersonDuplicates
(
SSN
char(11),
Name
nvarchar(100),
Address
nvarchar(100),
Birthdate datetime,
InsertSNAME nchar(100),
WhenInserted datetime
)
The INSTEAD OF trigger inserts rows into multiple base tables from a single
view. Attempts to insert rows with duplicate SSN are recorded in the
PersonDuplicates table. Duplicate rows in the EmployeeTable are changed to
update statements.
------------------------------------------------------------------------------------------CREATE TRIGGER IO_Trig_INS_Employee ON Employee
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
-- Check for duplicate Person. If there is no duplicate, do an insert.
IF (NOT EXISTS (SELECT P.SSN
FROM Person P, inserted I
WHERE P.SSN = I.SSN))
INSERT INTO Person
SELECT SSN,Name,Address,Birthdate
FROM inserted
ELSE
-- Log an attempt to insert duplicate Person row in PersonDuplicates table.
INSERT INTO PersonDuplicates
SELECT SSN,Name,Address,Birthdate,SUSER_SNAME(),GETDATE()
FROM inserted
-- Check for duplicate Employee. If there is no duplicate, do an INSERT.
IF (NOT EXISTS (SELECT E.SSN
FROM EmployeeTable E, inserted
WHERE E.SSN = inserted.SSN))
INSERT INTO EmployeeTable
SELECT EmployeeID,SSN, Department, Salary
FROM inserted
ELSE
--If there is a duplicate, change to UPDATE so that there will not
--be a duplicate key violation error.
UPDATE EmployeeTable
SET EmployeeID = I.EmployeeID,
Department = I.Department,
Salary = I.Salary
FROM EmployeeTable E, inserted I
WHERE E.SSN = I.SSN
END
----------------------------------------------------
Because this function returns a scalar value of a varchar(30) this function could be
used anywhere a varchar(30) expression is allowed such as a computed column in
a table, view, a T-SQL select list item.
print dbo.whichContinent('Denmark')
create table test
(Country varchar(15),
Continent as (dbo.WhichContinent(Country)))
insert into test (country)
values ('USA')
select * from test
insert into test (country)
values ('Argentina')
select * from test
Use IndianEnterprise
go
CREATE FUNCTION CustomersByContinent
(@Continent varchar(30))
RETURNS TABLE
AS
RETURN
SELECT dbo.WhichContinent(Customers.Country) as continent,
customers.*
FROM customers
WHERE dbo.WhichContinent(Customers.Country) = @Continent
GO
Use IndianEnterprise
go
CREATE FUNCTION dbo.CustomersbyCountry ( @Country varchar(15) )
RETURNS
@CustomersbyCountryTab table (
CustID int ,
CustName nvarchar (30),
Country nvarchar(30)
)
AS
BEGIN
INSERT INTO @CustomersByCountryTab
SELECT CustID,
CustName,
Country
FROM [Customers]
WHERE country = @Country
IF((SELECT COUNT(*) FROM @customersbyCountryTab)=0)
Begin
INSERT INTO @CustomersByCountryTab (
CustId,
CustName,
Country
)
VALUES (null,'No Customer Found','Unknown')
End
RETURN
END
GO
Stored Procedure
3
4
5
6
7
FOR AdventureWorks.Production.ProductCategory
Having done this, you can now proceed to use the synonym in SQL statements. For example:
SELECT * FROM ProdCat
If you think about it, that second list shouldn't be too surprising: it encompasses the most
common statements where you'd use the objects from the first list. Note that you cannot
reference a synonym in a DDL statement. If you want to use ALTER TABLE to change
something about the ProductCategory table, for example, you need to work with the base
table, not with the ProdCat synonym.
To create a synonym, you need CREATE SYNONYM permission.
After a synonym has been created, it has its own associated GRANT, DENY,
and REVOKE permissions.
that you could use a synonym. But sometimes it's nice to have these little amenities in the
product, just because they make for less typing and an easier development experience. Perhaps
synonyms will only save you ten minutes over the course of an entire application, but still, those
little ten-minute savings add up. Let's hope Microsoft keeps delivering nice little improvements
like this along with the big headline features.
IF EXISTS (
SELECT name
FROM sys.databases
WHERE name = N'Exercise'
)
DROP DATABASE Exercise
GO
CREATE DATABASE Exercise
GO
USE Exercise;
GO
CREATE TABLE Employees
(
EmployeeNumber int NOT NULL,
LastName nvarchar(20) NOT NULL,
FirstName nvarchar(20),
Username nchar(8) NOT NULL,
DateHired date NULL,
HourlySalary money
);
GO
ON Employees(LastName, Username);
GO
Deleting an Index
The basic syntax to delete an index in Transact-SQL is:
DROP INDEX IndexName ON TableName;
USE Exercise;
GO
DROP INDEX IX_Employees ON Employees;
GO
Checking the Existence of an Indexes
USE Exercise;
GO
IF EXISTS (SELECT name FROM sys.indexes
WHERE name = N'IX_Employees')
DROP INDEX IX_Employees
ON Employees
GO
CREATE INDEX IX_Employees
ON Employees(EmployeeNumber);
GO
The Types of Indexes
Clustered Indexes
In our introduction, we saw that an index is primarily created using one or more
columns from a designated table. This means that, when it comes to using the
index, we would use the values stored in the column(s) that was (were) selected for
the index. Such an index is referred to as clustered. The columns that were made
part of an index are referred to as keys.
USE Exercise;
GO
CREATE CLUSTERED INDEX IX_Employees
ON Employees(LastName);
GO
Non-clustered Index
While a clustered index uses a sorted list of records of a table or view, another type
of index can use a mechanism not based on the sorted records but on a bookmark.
This is called a non-clustered index. As opposed to a clustered table that can
contain only one clustered index, you can create not only one, but as many as 249
non-clustered indexes.
To create a non-clustered index in SQL, use the following formula:
CREATE NONCLUSTERED INDEX IndexName ON Table/View(Column(s))
USE Exercise;
GO
CREATE NONCLUSTERED INDEX NIX_Employees
ON Employees(LastName, FirstName);
GO
Introduction to Index Uniqueness
When creating a table, we can create index for it and let the index apply a rule that
states that each record would be unique. To take care of this, we can apply a
uniqueness rule on the index.
To create a uniqueness index in SQL, apply the UNIQUE keyword in the formula:
CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED] INDEX index_name ON
Table/View(Column(s))
USE Exercise;
GO
CREATE UNIQUE CLUSTERED INDEX UIX_Employees
ON Employees(EmployeeNumber);
GO
Cannot create more than one clustered index on table 'Employees'. Drop the existing clustered index 'IX_Employees' before creating another.
USE Exercise;
GO
DROP INDEX IX_Employees ON Employees
Go
CREATE UNIQUE CLUSTERED INDEX UIX_Employees
ON Employees(EmployeeNumber);
GO
Table and Index Partitioning
Data in your database may involve many records, in thousands or millions, so
much that at one time, it may become difficult to manage. One way you can deal
with this is to store the records of a table in different file groups. This makes it
possible to store one section of records in one file group, another section in another
file group, possibly another section in another file group, and so on. As a result,
when it comes time to look for one or a few records among thousands or millions
of records, it would be easier to locate it or to locate them. Of course, the data still
belongs to one database and to the same table.
USE master;
GO
( NAME = N'RealEstate3Log',
FILENAME = N'C:\Real Estate Main Repository\RealEstateLogger.ldf',
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB);
GO
Partitioning a Table
Before partitioning a table, you must create the necessary file groups. This can be
done when creating the database since it is at that time that you specify how the
database will be stored; that is, what files will hold the information of the database.
After creating the database and creating its file groups. Before partitioning a table, you must
create a partition function and a partition scheme.
A Partition Function
A partition function is used to define the ranges of records that will be stored in
what file group. The SQL formula to create a partition function is:
CREATE PARTITION FUNCTION PartitionFunctionName ( ParameterType )
AS RANGE [ LEFT | RIGHT ]
FOR VALUES (StartRange1, StartRange2, StartRange_n)
After closing the parenthesis, type AS RANGE, which indicates that you are going
to specify the ranges of values. This is followed by either LEFT or RIGHT. When
the partition function will have been created and when the table itself will have
been created, when the database engine is asked to look for a record or a range of
records, it may have to sort the records. If you want it to sort the records from left
to right, use the LEFT keyword. If you want the records sorted from right to left,
use the RIGHT keyword.
The AS RANGE LEFT or AS RANGE RIGHT expression is followed by FOR VALUES that
is followed by parentheses.
When creating a partition function, you must provide a way for the database engine to get a
range of records. For example, you can use records from number 1 to number 1000, then another
range from 1001 to 5000, and so on. Or you can specify that a range of records would go from
February 11th, 2000 to June 26th, 2005. Then another range would go from June 26th 2005 to
December 14th, 2006, and so on.
You specify the range in the parentheses that follow the FOR VALUES expression. Type the
first value of the first range, followed by a comma, followed by the first value of the second
range, and so on.
USE RealEstate3;
GO
CREATE PARTITION FUNCTION RealEstateSegmentation(int)
AS RANGE LEFT FOR VALUES(1, 10);
GO
A Partition Scheme
A partition scheme specifies the names of the file groups, in their order that will
store the ranges of records that were created in the partition function. The formula
to create a partition scheme is:
CREATE PARTITION SCHEME PartitionSchemeName
AS PARTITION PartitionFunctionName
[ ALL ] TO ( { file_group_name | [ PRIMARY ] } [ ,...n ] )
You start with the CREATION PARTITION SCHEME expression do indication your
intention. This is followed by a name. The name follows the rules of objects.
After the name of the partition scheme, type AS PARTITION followed by the name of the
partition function you should have previously created.
If you are planning to use only one file group, after the name of the partition function, enter
ALL, followed by parentheses, in which you will type PRIMARY.
If you are planning to use different file groups, after the name of the partition function, enter TO,
followed by parentheses. We saw that, in the parentheses of the FOR VALUES of the partition
function, you entered the starting value of the first range. In the parentheses of the TO keyword,
type the name of the file group that will hold the records of the first range of the partition
function. We also saw how to specify the second range in the partition function. In the
parentheses of the TO clause, after the name of the first file group, type a comma followed by the
name of the file group that will hold the records of the second range.
USE RealEstate3;
GO
Partitioning a Table
After creating the partition scheme, you can create the table. The formula to
specify a partition scheme when creating a table is:
CREATE TABLE What We Have Learned So Far
(
What We Have Learned So Far
) ON PartitionSchemeName(ColumnName)
USE RealEstate3;
GO
CURSORS
Processing Sequentially Through a Set of Records
Operations in a relational database act on a complete set of rows. The set of rows returned
by a SELECT statement consists of all the rows that satisfy the conditions in the WHERE
clause of the statement. This complete set of rows returned by the statement is known as
the result set. Applications, especially interactive online applications, cannot always work
effectively with the entire result set as a unit.
If applications need a mechanism to work with one row or a small block of rows at a time,
then Cursors are an extension to result sets that provide that mechanism.
Using a Cursor
To define a cursor the DECLARE CURSOR statement is used. Here is the basic format for the
simple cursor .
DECLARE cursor_name CURSOR
FOR select_statement
The cursor_name is the name we want to associate with the cursor. The select_statement is
the query that will determine the rows that make up the cursor.
Complete syntax:
ISO Syntax
DECLARE cursor_name [ INSENSITIVE ] [ SCROLL ] CURSOR
FOR select_statement
[ FOR { READ ONLY | UPDATE [ OF column_name [ ,...n ] ] } ]
[;]
Transact-SQL Extended Syntax
DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ]
[ FORWARD_ONLY | SCROLL ]
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]
[ TYPE_WARNING ]
FOR select_statement
[ FOR UPDATE [ OF column_name [ ,...n ] ] ]
[;]
cursor_name - the name of the server side cursor, must contain from 1 to 128 characters.
LOCAL - specifies that cursor can be available only in the batch, stored procedure,
or trigger in which the cursor was created. The LOCAL cursor will be implicitly
deallocated when the batch, stored procedure, or trigger terminates.
Cursor Process
Transact-SQL cursors and API cursors have different syntax, but the following general process is
used with all SQL Server cursors:
1. Associate a cursor with the result set of a Transact-SQL statement, and define
characteristics of the cursor, such as whether the rows in the cursor can be updated.
2. Execute the Transact-SQL statement to populate the cursor.
3. Retrieve the rows in the cursor we want to see. The operation to retrieve one row or one
block of rows from a cursor is called a FETCH. Performing a series of fetches to retrieve
rows in either a forward or backward direction is called scrolling.
4. Optionally, perform modification operations (update or delete) on the row at the current
position in the cursor.
5. Close the cursor.
Cursor Functions:
Return value
Description
0
-1
-2
@@CURSOR_ROWS
Returns the number of qualifying rows currently in the last cursor opened on the connection.
@@CURSOR_ROWS can be called to determine that the number of the rows that qualify for
a cursor are retrieved at the time @@CURSOR_ROWS is called.
CURSOR_STATUS:
A scalar function that allows the caller of a stored procedure to determine whether or not
the procedure has returned a cursor and result set for a given parameter.
1 7369
2 7499
3 7521
4 7566
5 7654
Let's look at the above code in a little more detail.
We first declared a cursor called "EmpList".
The "EmpList" cursor is populated using a SELECT statement that uses the
TOP clause to return only the top 5 employee no's.
Inside the WHILE loop the @RowNum variable is incremented by 1 for each record
processed. The calculated Row Number and @Empno are then printed out.
Lastly, a "FETCH NEXT" statement is used to retrieve the next row before the next cycle of
the WHILE loop. This process continues one record at a time until all records in cursor
"EmpList" have been processed.
Another Example:
FAST_FORWARD
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @VTitle,@presentPrice
WHILE @@FETCH_STATUS = 0
BEGIN
if @presentPrice < 3
set @newPrice = @presentPrice + @presentPrice * 0.10
else
set @newPrice = @presentPrice + @presentPrice * 0.05
PRINT @VTitle + ' - present price is ' + cast(@presentPrice as nvarchar) +
' - new price is ' + cast(@newPrice as nvarchar)
FETCH NEXT FROM @MyCursor
INTO @VTitle,@presentPrice
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
GO
--------------------------------------*
OPEN @MyCursor
FETCH NEXT FROM @MyCursor
INTO @ename,@presentSal
WHILE @@FETCH_STATUS = 0
BEGIN
if @presentSal < 3000
set @newSal = @presentSal + @presentSal * 0.10
else
set @newSal = @presentSal + @presentSal * 0.05
PRINT @ename + ' ' + cast(@presentSal as nvarchar) + ' ' + cast(@newSal as
nvarchar)
FETCH NEXT FROM @MyCursor
INTO @ename,@presentSal
END
CLOSE @MyCursor
DEALLOCATE @MyCursor
GO
Another example:
END
CLOSE EmpList
DEALLOCATE EmpList
Another Example:
SELECT @@CURSOR_ROWS
DECLARE employees_cursor CURSOR FOR
SELECT ename FROM Employee
OPEN employees_cursor
FETCH NEXT FROM employees_cursor
SELECT @@CURSOR_ROWS
CLOSE employees_cursor
DEALLOCATE employees_cursor
Using a Select Statement
We can also use a SELECT statement to process through a set of records one record at a
time. To do this we will issue an initial SELECT statement that will return the first row, then
a series of follow on SELECT statements where each SELECT statement retrieves the next
row. This is done by using the "TOP 1" clause of the SELECT statement, and a WHERE
statement.
We will use the same example as above and only return the top 5 employee no's from the
Northwind database Employee table. In this code we will use two different "SELECT TOP 1"
statements and a WHILE loop to return all 5 records. Each record will be processed one at a
time.
7369
7499
7521
7566
7654
Here we can see the first SELECT statement selects only the first Empno.
This ID is placed in the local variable @Empno.
The WHILE loop is controled by the local variable @RowNum.
Each time through the WHILE loop, the Row Number and Empno are printed out.
Prior to returning to the top of the WHILE loop we used another "SELECT TOP 1" statement
to select the next Empno.
This SELECT statement uses a WHERE clause on the SELECT statement to select the first
Empno that is greater than the Empno that was just printed.
The WHILE loop is processed 5 times, allowing the SELECT TOP 1 method to retrieve the top
5 Empno's one records at a time.
WHILE @@FETCH_STATUS = 0
BEGIN
SET @RowNum = @RowNum + 1
PRINT cast(@RowNum as char(1)) + ' ' + @Empno
FETCH NEXT FROM EmpList
INTO @Empno
END
CLOSE EmpList
DEALLOCATE EmpList
Do not forget to close SQL Server cursor when its result set is not needed.
Do not forget to deallocate SQL Server cursor when the data structures
comprising the cursor are not needed.
Try avoid using insensitive, static and keyset cursors, whenever possible.
These types of cursor produce the largest amount of overhead on SQL Server,
because they cause a temporary table to be created in TEMPDB, which results in
some performance degradation.
Use FORWARD_ONLY cursors, if you need updatable cursor and the FETCH
NEXT will be the only used fetch option.
If you need read-only cursor and the FETCH NEXT will be the only used fetch option,
try to use FAST_FORWARD cursor instead of FORWARD_ONLY cursor. By the way, if
one of the FAST_FORWARD or FORWARD_ONLY is specified the other cannot be
specified.
Cursor Components
Based on the example above, cursors include these components:
DECLARE statements - Declare variables used in the code block
SET\SELECT statements - Initialize the variables to a specific value
DECLARE CURSOR statement - Populate the cursor with values that will be evaluated
NOTE - There are an equal number of variables in the DECLARE <cursor_name>
CURSOR FOR statement as there are in the SELECT statement. This could be 1
or many variables and associated columns.
OPEN statement - Open the cursor to begin data processing
FETCH NEXT statements - Assign the specific values from the cursor to the variables
NOTE - This logic is used for the initial population before the WHILE statement
and then again during each loop in the process as a portion of the WHILE
statement
WHILE statement - Condition to begin and continue data processing
BEGIN...END statement - Start and end of the code block
NOTE - Based on the data processing multiple BEGIN...END statements can be
used
Data processing - In this example, this logic is to backup a database to a specific path and
file name, but this could be just about any DML or administrative logic
CLOSE statement - Releases the current data and associated locks, but permits the cursor
to be re-opened
DEALLOCATE statement - Destroys the cursor
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
An insensitive cursor is not affected by changes to the underlying data. When the cursor
is opened, a temporary table is created in the tempdb database. This table is populated
with the results of the query immediately. Once populated, each fetch retrieves information
from the temporary table, rather than the live data.
To specify that a cursor is insensitive, add the INSENSITIVE clause immediately after the cursor
name in the declaration. For example:
DECLARE BillingCursor INSENSITIVE CURSOR
FOR SELECT JobId, ContractNumber, Duration, EngineerCost, PartsCost
FROM BillingSystemOutputData
If a DECLARE CURSOR using Transact-SQL syntax does not specify READ_ONLY,
OPTIMISTIC, or SCROLL_LOCKS, the default is as follows:
If the SELECT statement does not support updates (insufficient permissions, accessing
remote tables that do not support updates, and so on), the cursor is READ_ONLY.
STATIC and FAST_FORWARD cursors default to READ_ONLY.
DYNAMIC and KEYSET cursors default to OPTIMISTIC.
UPDATE) occurs. Triggers are stored in and managed by the DBMS. Triggers are used to
maintain the referential integrity of data by changing the data in a systematic fashion. A trigger
cannot be called or executed; DBMS automatically fires the trigger as a result of a data
modification to the associated table. Triggers can be viewed as similar to stored procedures in
that both consist of procedural logic that is stored at the database level. Stored procedures,
however, are not event-drive and are not attached to a specific table as triggers are. Stored
procedures are explicitly executed by invoking a CALL to the procedure while triggers are
implicitly executed. In addition, triggers can also execute stored procedures.
Nested Trigger: A trigger can also contain INSERT, UPDATE and DELETE l
ogic within itself, so when the trigger is fired because of data modification
it can also cause another data modification, thereby firing another trigger.
A trigger that contains data modification logic
within itself is called a nested trigger.
What is View?
A simple view can be thought of as a subset of a table. It can be used for retrieving data, as well
as updating or deleting rows. Rows updated or deleted in the view are updated or deleted in the
table the view was created with. It should also be noted that as data in the original table changes,
so does data in the view, as views are the way to look at part of the original table. The results of
using a view are not permanently stored in the database. The data accessed through a view is
actually constructed using standard T-SQL select command and can come from one to many
different base tables or even other views.
What is Index?
An index is a physical structure containing pointers to the data.
Indices are created in an existing table to locate rows more quickly and efficiently. It is possible
to create an index on one or more columns of a table, and each index is given a name. The users
cannot see the indexes; they are just used to speed up queries. Effective indexes are one of the
best ways to improve performance in a database application. A table scan happens when there is
no index available to help a query. In a table scan SQL Server examines every row in the table to
satisfy the query results. Table scans are sometimes unavoidable, but on large tables, scans have
a terrific impact on performance.
What is a Linked Server?
Linked Servers is a concept in SQL Server by which we can add
other SQL Server to a Group and query both the SQL Server databases using
T-SQL Statements.
With a linked server, you can create very clean, easy to follow, SQL statements that allow remote
data to be retrieved, joined and combined with local data. Stored Procedure sp_addlinkedserver,
sp_addlinkedsrvlogin will be used add new Linked Server-level
1) General Questions of SQL SERVER
What is Cursor?
Inner Join
A join that displays only the rows that have a match in both joined tables is known as inner Join.
This is the default type of join in the Query and View Designer.
Outer Join
A join that includes rows even if they do not have related rows in the joined table is an Outer
Join. You can create three different outer join to specify the unmatched rows to be included:
Left Outer Join: In Left Outer Join all rows in the first-named table i.e. left table,
which appears leftmost in the JOIN clause are included. Unmatched rows in the right
table do not appear.
Right Outer Join: In Right Outer Join all rows in the second-named table i.e. right
table, which appears rightmost in the JOIN clause are included. Unmatched rows in the
left table are not included.
Full Outer Join: In Full Outer Join all rows in all joined tables are included, whether
they are matched or not.
Self Join
This is a particular case when one table joins to itself, with one or two aliases to avoid confusion.
A self join can be of any type, as long as the joined tables are the same. A self join is rather
unique in that it involves a relationship with only one table. The common example is when
company has a hierarchal reporting structure whereby one member of staff reports to another.
Self Join can be Outer Join or Inner Join.
What are primary keys and foreign keys?
Primary keys are the unique identifiers for each row. They must contain unique values and
cannot be null. Due to their importance in relational databases, Primary keys are the most
fundamental of all keys and constraints. A table can have only one Primary key.
Foreign keys are both a method of ensuring data integrity and a manifestation of the relationship
between tables.
What is a User Defined Function? What kind of User-Defined Functions
can be created?
User-Defined Functions allow defining its own T-SQL functions that can accept 0 or more
parameters and return a single scalar data value or a table data type.
Different Kinds of User-Defined Functions created are:
Scalar User-Defined Function
A Scalar user-defined function returns one of the scalar data types. Text, ntext, image and
timestamp data types are not supported. These are the type of user-defined functions that most
developers are used to in other programming languages. You pass in 0 to many parameters and
you get a return value.
Inline Table-Value User-Defined Function
An Inline Table-Value user-defined function returns a table data type and is an exceptional
alternative to a view as the user-defined function can pass parameters into a T-SQL select
command and in essence provide us with a parameterized, non-updateable view of the
underlying tables.
Multi-statement Table-Value User-Defined Function
A Multi-Statement Table-Value user-defined function returns a table and is also an exceptional
alternative to a view as the function can support multiple T-SQL statements to build the final
result where the view is limited to a single SELECT statement. Also, the ability to pass
parameters into a TSQL select command or a group of them gives us the capability to in essence
create a parameterized, non-updateable view of the data in the underlying tables. Within the
create function command you must define the table structure that is being returned. After
creating this type of user-defined function, It can be used in the FROM clause of a T-SQL
command unlike the behavior found when using a stored procedure which can also return record
sets.
What is Identity?
Identity (or AutoNumber) is a column that automatically
generates numeric values. A start and increment value can be set,
but most DBA leave these at 1.
A GUID column also generates numbers; the value of this cannot be controlled. Identity/GUID
columns do not need to be indexed.
What is DataWarehousing?
Subject-oriented, meaning that the data in the database is organized
so that all the data elements relating to the same real-world event or
object are linked together;
Time-variant, meaning that the changes to the data in the
database are tracked and recorded so that reports can be produced
showing changes over time;
Non-volatile, meaning that data in the database is never
over-written or deleted, once committed, the data is static,
read-only, but retained for future reporting.
Integrated, meaning that the database contains data from
most or all of an organizations operational applications,
and that this data is made consistent.
1) General Questions of SQL SERVER
2) Common Questions Asked
Which TCP/IP port does SQL Server run on? How can it be changed?
SQL Server runs on port 1433. It can be changed from the
Network Utility TCP/IP properties -> Port number, both on client and the server.
applied to each row before they are part of the GROUP BY function in a query.
What are the properties and different Types of Sub-Queries?
Properties of Sub-Query
A sub-query must be enclosed in the parenthesis.
A sub-query must be put in the right hand of the comparison operator, and
A sub-query cannot contain an ORDER-BY clause.
A query can contain more than one sub-query.
Types of Sub-query
Single-row sub-query, where the sub-query returns only one row.
Multiple-row sub-query, where the sub-query returns multiple rows,. and
Multiple column sub-query, where the sub-query returns multiple columns
What is SQL Profiler?
SQL Profiler is a graphical tool that allows system administrators to monitor events in an
instance of Microsoft SQL Server. You can capture and save data about each event to a file or
SQL Server table to analyze later. For example, you can monitor a production environment to see
which stored procedures are hampering performances by executing too slowly.
Use SQL Profiler to monitor only the events in which you are interested. If traces are becoming
too large, you can filter them based on the information you want, so that only a subset of the
event data is collected. Monitoring too many events adds overhead to the server and the
monitoring process and can cause the trace file or trace table to grow very large, especially when
the monitoring process takes place over a long period of time.
What are the authentication modes in SQL Server? How can it be changed?
Windows mode and Mixed Mode SQL & Windows.
To change authentication mode in SQL Server click Start, Programs, Microsoft SQL Server and
click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server
program group. Select the server then from the Tools menu select SQL Server Configuration
Properties, and choose the Security page.
1) General Questions of SQL SERVER
Which command using Query Analyzer will give you the version of SQL server and
operating system?
SELECT SERVERPROPERTY ('productversion'), SERVERPROPERTY ('productlevel'),
SERVERPROPERTY ('edition')
nesting is possible?
Yes. Because Transact-SQL supports recursion, you can write stored procedures that call
themselves. Recursion can be defined as a method of problem solving wherein the solution is
arrived at by repetitively applying it to subsets of the problem. A common application of
recursive logic is to perform numeric computations that lend themselves to repetitive evaluation
by the same processing steps. Stored procedures are nested when one stored procedure calls
another or executes managed code by referencing a CLR routine, type, or aggregate. You can
nest stored procedures and managed code references up to 32 levels.
What is Log Shipping?
Log shipping is the process of automating the backup of database and transaction log files on a
production SQL server, and then restoring them onto a standby server. Enterprise Editions only
supports log shipping. In log shipping the transactional log file from one server is automatically
updated into the backup database on the other server. If one server fails, the other server will
have the same db and can be used this as the Disaster Recovery plan. The key feature of log
shipping is that it will automatically backup transaction logs throughout the day and
automatically restore them on the standby server at defined interval.
Name 3 ways to get an accurate count of the number of records in a table?
SELECT * FROM table1
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
What does it mean to have QUOTED_IDENTIFIER ON? What are the implications of
having it OFF?
When SET QUOTED_IDENTIFIER is ON, identifiers can be delimited by double quotation
marks, and literals must be delimited by single quotation marks. When SET
QUOTED_IDENTIFIER is OFF, identifiers cannot be quoted and must follow all Transact-SQL
rules for identifiers.
What is the difference between a Local and a Global temporary table?
A local temporary table exists only for the duration of a connection or, if defined inside a
compound statement, for the duration of the compound statement.
A global temporary table remains in the database permanently, but the rows exist only within a
given connection. When connection is closed, the data in the global temporary table disappears.
However, the table definition remains with the database for access when database is opened next
time.
What is the STUFF function and how does it differ from the REPLACE function?
STUFF function is used to overwrite existing characters. Using this syntax, STUFF
(string_expression, start, length, replacement_characters), string_expression is the string that will
have characters substituted, start is the starting position, length is the number of characters in the
string that are substituted, and replacement_characters are the new characters interjected into the
string. REPLACE function to replace existing characters of all occurrences. Using the syntax
REPLACE (string_expression, search_string, replacement_string), where every incidence of
search_string found in the string_expression will be replaced with replacement_string.
If someone is using db it will not accept sp_renmaedb. In that case first bring db to single user
using sp_dboptions. Use sp_renamedb to rename database. Use sp_dboptions to bring database
to multi user mode.
E.g.
USE master;
GO
EXEC sp_dboption AdventureWorks, 'Single User', True
GO
EXEC sp_renamedb 'AdventureWorks', 'AdventureWorks_New'
GO
EXEC sp_dboption AdventureWorks, 'Single User', False
GO
To rename Table
We can change the table name using sp_rename as follows,
sp_rename 'oldTableName' 'newTableName'
E.g.
To rename Column
The script for renaming any column :
sp_rename 'TableName.[OldcolumnName]', 'NewColumnName', 'Column'
E.g.
sp_RENAME 'Table_First.Name', 'NameChange' , 'COLUMN'
GO
You can run following command and check advance global configuration settings.
sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE
GO
What are the basic functions for master, msdb, model, tempdb and resource databases?
The master database holds information for all databases located on the SQL Server instance and
is theglue that holds the engine together. Because SQL Server cannot start without a functioning
masterdatabase, you must administer this database with care.
The msdb database stores information regarding database backups, SQL Agent information,
DTS packages, SQL Server jobs, and some replication information such as for log shipping.
The tempdb holds temporary objects such as global and local temporary tables and stored
procedures.
The model is essentially a template database used in the creation of any new user database
created in the instance.
The resoure Database is a read-only database that contains all the system objects that are
included with SQL Server. SQL Server system objects, such as sys.objects, are physically
persisted in the Resource database, but they logically appear in the sys schema of every database.
The Resource database does not contain user data or user metadata.
What is Service Broker?
Service Broker is a message-queuing technology in SQL Server that allows developers to
integrate SQL Server fully into distributed applications. Service Broker is feature which provides
facility to SQL Server to send an asynchronous, transactional message. it allows a database to
send a message to another database without waiting for the response, so the application will
continue to function if the remote database is temporarily unavailable.
Where SQL server user names and passwords are stored in SQL server?
They get stored in System Catalog Views sys.server_principals and sys.sql_logins.
What is Policy Management?
Policy Management in SQL SERVER 2008 allows you to define and enforce policies for
configuring and managing SQL Server across the enterprise. Policy-Based Management is
configured in SQL Server Management Studio (SSMS). Navigate to the Object Explorer and
expand the Management node and the Policy Management node; you will see the Policies,
Conditions, and Facets nodes. (Read More Here)
What is Replication and Database Mirroring?
Database mirroring can be used with replication to provide availability for the publication
database. Database mirroring involves two copies of a single database that typically reside on
different computers. At any given time, only one copy of the database is currently available to
clients which are known as the principal database. Updates made by clients to the principal
database are applied on the other copy of the database, known as the mirror database. Mirroring
involves applying the transaction log from every insertion, update, or deletion made on the
principal database onto the mirror database.
What are Sparse Columns?
A sparse column is another tool used to reduce the amount of physical storage used in a database.
They are the ordinary columns that have an optimized storage for null values. Sparse columns
reduce the space requirements for null values at the cost of more overhead to retrieve nonnull
values. (Read More Here)
What does TOP Operator Do?
The TOP operator is used to specify the number of rows to be returned by a query. The TOP
operator has new addition in SQL SERVER 2008 that it accepts variables as well as literal values
and can be used with INSERT, UPDATE, and DELETES statements.
What is CTE?
CTE is an abbreviation Common Table Expression. A Common Table Expression (CTE) is an
expression that can be thought of as a temporary result set which is defined within the execution
of a single SQL statement. A CTE is similar to a derived table in that it is not stored as an object
and lasts only for the duration of the query. (Read More Here)
What is MERGE Statement?
MERGE is a new feature that provides an efficient way to perform multiple DML operations. In
previous versions of SQL Server, we had to write separate statements to INSERT, UPDATE, or
DELETE data based on certain conditions, but now, using MERGE statement we can include the
logic of such data modifications in one statement that even checks when the data is matched then
just update it and when unmatched then insert it. One of the most important advantages of
MERGE statement is all the data is read and processed only once.
What is Filtered Index?
Filtered Index is used to index a portion of rows in a table that means it applies filter on INDEX
which improves query performance, reduce index maintenance costs, and reduce index storage
costs compared with full-table indexes. When we see an Index created with some where clause
then that is actually a FILTERED INDEX.
Which are new data types introduced in SQL SERVER 2008?
The GEOMETRY Type: The GEOMETRY data type is a system .NET common language runtime
(CLR) data type in SQL Server. This type represents data in a two-dimensional Euclidean
coordinate system.
The GEOGRAPHY Type: The GEOGRAPHY datatypes functions are the same as with
GEOMETRY. The difference between the two is that when you specify GEOGRAPHY, you are
usually specifying points in terms of latitude and longitude.
New Date and Time Datatypes: SQL Server 2008 introduces four new datatypes related to date
and time: DATE, TIME, DATETIMEOFFSET, and DATETIME2.
DATE: The new DATE type just stores the date itself. It is based on the Gregorian
calendar and handles years from 1 to 9999.
TIME: The new TIME (n) type stores time with a range of 00:00:00.0000000 through
23:59:59.9999999. The precision is allowed with this type. TIME supports seconds down
to 100 nanoseconds. The n in TIME (n) defines this level of fractional second precision,
from 0 to 7 digits of precision.
The DATETIMEOFFSET Type: DATETIMEOFFSET (n) is the time-zone-aware version
of a datetime datatype. The name will appear less odd when you consider what it really is:
a date + a time + a time-zone offset. The offset is based on how far behind or ahead you
are from Coordinated Universal Time (UTC) time.
The DATETIME2 Type: It is an extension of the datetime type in earlier versions of SQL
Server. This new datatype has a date range covering dates from January 1 of year 1
through December 31 of year 9999. This is a definite improvement over the 1753 lower
boundary of the datetime datatype. DATETIME2 not only includes the larger date range,
but also has a timestamp and the same fractional precision that TIME type provides
What are the Advantages of using CTE?
Using CTE improves the readability and makes maintenance of complex queries easy.
The query can be divided into separate, simple, logical building blocks which can be then
used to build more complex CTEs until final result set is generated.
CTE can be defined in functions, stored procedures, triggers or even views.
After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT,
UPDATE or DELETE Data.
How can we rewrite sub-queries into simple select statements or with joins?
Yes we can write using Common Table Expression (CTE). A Common Table Expression (CTE)
is an expression that can be thought of as a temporary result set which is defined within the
execution of a single SQL statement. A CTE is similar to a derived table in that it is not stored as
an object and lasts only for the duration of the query.
E.g.
USE AdventureWorks
GO
WITH EmployeeDepartment_CTE AS (
SELECT EmployeeID,DepartmentID,ShiftID
FROM HumanResources.EmployeeDepartmentHistory
)
SELECT ecte.EmployeeId,ed.DepartmentID, ed.Name,ecte.ShiftID
FROM HumanResources.Department ed
INNER JOIN EmployeeDepartment_CTE ecte ON ecte.DepartmentID = ed.DepartmentID
GO
What is CLR?
In SQL Server 2008, SQL Server objects such as user-defined functions can be created using
such CLR languages. This CLR language support extends not only to user-defined functions, but
also to stored procedures and triggers. You can develop such CLR add-ons to SQL Server using
Visual Studio 2008. (
What are synonyms?
Synonyms give you the ability to provide alternate names for database objects. You can alias
object names; for example, using the Employee table as Emp. You can also shorten names. This
is especially useful when dealing with three and four part names; for example, shortening
server.database.owner.object to object.
What is LINQ?
Language Integrated Query (LINQ) adds the ability to query objects using .NET languages. The
LINQ to SQL object/relational mapping (O/RM) framework provides the following basic
features:
Tools to create classes (usually called entities) mapped to database tables
Compatibility with LINQs standard query operations
The DataContext class, with features such as entity record monitoring, automatic SQL
statement generation, record concurrency detection, and much more
What is Isolation Levels?
Transactions specify an isolation level that defines the degree to which one transaction must be
isolated from resource or data modifications made by other transactions. Isolation levels are
described in terms of which concurrency side-effects, such as dirty reads or phantom reads, are
allowed.
Transaction isolation levels control:
Whether locks are taken when data is read, and what type of locks are requested.
How long the read locks are held.
Whether a read operation referencing rows modified by another transaction:
Blocks until the exclusive lock on the row is freed.
Retrieves the committed version of the row that existed at the time the statement or
transaction started.
Reads the uncommitted data modification.
What is use of EXCEPT Clause?
EXCEPT clause is similar to MINUS operation in Oracle. The EXCEPT query and MINUS
query returns all rows in the first query that are not returned in the second query. Each SQL
statement within the EXCEPT query and MINUS query must have the same number of fields in
the result sets with similar data types.
What is XPath?
XPath uses a set of expressions to select nodes to be processed. The most common expression
that youll use is the location path expression, which returns back a set of nodes called a node
set. XPath can use both an unabbreviated and an abbreviated syntax. The following is the
unabbreviated syntax for a location path:
/axisName::nodeTest[predicate]/axisName::nodeTest[predicate]
What is NOLOCK?
Using the NOLOCK query optimizer hint is generally considered good practice in order to
improve concurrency on a busy system. When the NOLOCK hint is included in a SELECT
statement, no locks are taken when data is read. The result is a Dirty Read, which means that
another process could be updating the data at the exact time you are reading it. There are no
guarantees that your query will retrieve the most recent data. The advantage to performance is
that your reading of data will not block updates from taking place, and updates will not block
your reading of data. SELECT statements take Shared (Read) locks. This means that multiple
SELECT statements are allowed simultaneous access, but other processes are blocked from
modifying the data. The updates will queue until all the reads have completed, and reads
requested after the update will wait for the updates to complete. The result to your system is
delay (blocking).
How would you handle error in SQL SERVER 2008?
SQL Server now supports the use of TRYCATCH constructs for providing rich error handling.
TRYCATCH lets us build error handling at the level we need, in the way we need to, by
setting a region where if any error occurs, it will break out of the region and head to an error
handler. The basic structure is as follows:
BEGIN TRY
<code>
END TRY
BEGIN CATCH
<code>
END CATCH
So if any error occurs in the TRY block, execution is diverted to the CATCH block, and the error
can be dealt.
What is RAISEERROR?
RaiseError generates an error message and initiates error processing for the session.
RAISERROR can either reference a user-defined message stored in the sys.messages catalog
view or build a message dynamically. The message is returned as a server error message to the
calling application or to an associated CATCH block of a TRYCATCH construct.
How to rebuild Master Databse?
Master database is system database and it contains information about running servers
configuration. When SQL Server 2005 is installed it usually creates master, model, msdb,
tempdb resource and distribution system database by default. Only Master database is the one
which is absolutely must have database. Without Master database SQL Server cannot be started.
This is the reason it is extremely important to backup Master database.
To rebuild the Master database, Run Setup.exe, verify, and repair a SQL Server instance, and
rebuild the system databases. This procedure is most often used to rebuild the master database
for a corrupted installation of SQL Server.
What is XML Datatype?
The xml data type lets you store XML documents and fragments in a SQL Server database. An
XML fragment is an XML instance that is missing a single top-level element. You can create
columns and variables of the xml type and store XML instances in them. The xml data type and
associated methods help integrate XML into the relational framework of SQL Server.
What is Data Compression?
How to copy the tables, schema and views from one SQL Server to another?
What is SQLCMD?
sqlcmd is enhanced version of the isql and osql and it provides way more functionality than other
two options. In other words sqlcmd is better replacement of isql (which will be deprecated
eventually) and osql (not included in SQL Server 2005 RTM). sqlcmd can work two modes i)
BATCH and ii) interactive modes.
What is Aggregate Functions?
Aggregate functions perform a calculation on a set of values and return a single value. Aggregate
functions ignore NULL values except COUNT function. HAVING clause is used, along with
GROUP BY, for filtering query using aggregate values.
Following functions are aggregate functions.
AVG, MIN, CHECKSUM_AGG, SUM, COUNT, STDEV, COUNT_BIG, STDEVP,
GROUPING, VAR, MAX, VARP
What do you mean by Table Sample?
TABLESAMPLE allows you to extract a sampling of rows from a table in the FROM clause.
The rows retrieved are random and they are not in any order. This sampling can be based on a
percentage of number of rows. You can use TABLESAMPLE when only a sampling of rows is
necessary for the application instead of a full result set.
What is Row_Number()?
ROW_NUMBER() returns a column as an expression that contains the rows number within the
result set. This is only a number used in the context of the result set, if the result changes, the
ROW_NUMBER() will change.
What are Ranking Functions?
Ranking functions return a ranking value for each row in a partition. All the ranking functions
are non-deterministic. Different Ranking functions are:
ROW_NUMBER () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first
row in each partition.
RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of each row within the partition of a result set.
DENSE_RANK () OVER ([<partition_by_clause>] <order_by_clause>)
Returns the rank of rows within the partition of a result set, without any gaps in the ranking.
What is the difference between UNION and UNION ALL?
UNION
The UNION command is used to select related information from two tables, much like the JOIN
command. However, when using the UNION command all selected columns need to be of the
same data type. With UNION, only distinct values are selected.
UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects
all values.
The difference between Union and Union all is that Union all will not eliminate duplicate rows,
instead it just pulls all rows from all tables fitting your query specifics and combines them into a
table.
What is B-Tree?
The database server uses a B-tree structure to organize index information. B-Tree generally has
following types of index pages or nodes:
root node: A root node contains node pointers to branch nodes which can be only one.
branch nodes: A branch node contains pointers to leaf nodes or other branch nodes which
can be two or more.
leaf nodes: A leaf node contains index items and horizontal pointers to other leaf nodes
which can be many.