Archive for the ‘Beginning SQL Server’ Category

From Queries to Stored Procedures

Monday, April 19th, 2010

Basic Stored Procedures are incredibly simple.

If you write the following query:

SELECT Column1, Column2 FROM Table1

The equivilent stored procedure would be:

CREATE PROCEDURE StoredProcedure
AS
SELECT Column1, Column2 FROM Table1
GO

To call it simply type:

EXEC dbo.StoredProcedure

And execute.

What is the difference between a Primary Key and a Unique Index?

Thursday, April 15th, 2010

Unique Indexes and Primary Keys have similar properties. They can both be used to enforce foreign keys, both be used as bases for computed columns they can both be used as clustered and non clustered indexes and they can both be declared on multiple columns.

However they are not the same thing and should not be treated or even thought of as such. There are numerous differences. Primary Key Columns cannot contain nulls whereas the columns that make up a unique index can*. You are only allowed a single primary key but as many unique indexes as you need. A unique index is thus an alternate key.

* SQL Server allows a single null in a unique index. Oracle however allows several as nulls by thier very nature are unknown its up for debate if the Oracle method breaks unique rules or not.

Why use views?

Thursday, April 15th, 2010

Views have three major advantages when it comes to developing:

Security – They can provide a security mechanism that restricts users to a certain subset of data in one or more base tables.

Maintenance – They can provide a mechanism that allows developers to customize how users can logically view the data stored in base tables.

Perfomance – It is possible to create a unique clustered index on a view.

From Queries to Stored Procedures

Thursday, April 15th, 2010

Basic Stored Procedures are incredibly simple.

If you write the following query:

SELECT Column1, Column2 FROM Table1

The equivilent stored procedure would be:

CREATE PROCEDURE StoredProcedure
AS
SELECT Column1, Column2 FROM Table1

To run the stored procedure

Exec StoredProcedure