Monday, 10 March 2014

Stored Procedure Basics

Creating the Sproc: Basic Syntax
Creating a sproc works pretty much the same as creating any other object in a database, except that it
uses the AS keyword we used with views. The basic syntax looks like this:
CREATE PROCEDURE|PROC <sproc name>
[<parameter name> [schema.]<data type> [VARYING] [= <default value>] [OUT
[PUT]][,
<parameter name> [schema.]<data type> [VARYING] [= <default value>]
[OUT[PUT]][,
...
...
]]
[WITH
RECOMPILE| ENCRYPTION | [EXECUTE AS { CALLER|SELF|OWNER|<’user name’>}]
[FOR REPLICATION]
AS
<code> | EXTERNAL NAME <assembly name>.<assembly class>
As you can see, you still have the basic CREATE <Object Type> <Object Name> syntax that is the
backbone of every CREATE statement. The only oddity here is the choice between PROCEDURE and PROC.
Either option works fine, but as always, I recommend that you be consistent regarding which one you
choose. (Personally, I like the saved keystrokes of PROC, and in my experience, that’s the way most people
do it.) The name of your sproc must follow the rules for naming as outlined in Chapter 1.
After the name comes a list of parameters. Parameterization is optional, and I defer that discussion until
a little later in the chapter.
Last, but not least, comes your actual code following the AS keyword.
An Example of a Basic Sproc
Perhaps the best example of basic sproc syntax is found in the most basic of sprocs—a sproc that returns
all the columns in all the rows on a table—in short, everything to do with a table’s data.
I hope that, by now, you have the query that returns all the contents of a table down cold (Hint: SELECT
* FROM....)
USE AdventureWorks
GO
CREATE PROC spEmployee
AS
SELECT * FROM HumanResources.Employee
Not too rough, eh?
Now that you have your sproc created, execute it to see what you get:
EXEC spEmployee
You get exactly what you would have gotten if you had run the SELECT statement that’s embedded in
the sproc.
Changing Stored Procedures with ALTER
ALTER statements for sprocs work almost identically to views from the standpoint of what an ALTER
statement does.
The main thing to remember when you edit sprocs with T-SQL is that you are completely replacing the
existing sproc. The only differences between using the ALTER PROC statement and the CREATE PROC
statement are as follows:
❑ ALTER PROC expects to find an existing sproc, whereas CREATE doesn’t.
❑ ALTER PROC retains any permissions that have been established for the sproc. It keeps the same
object ID within system objects and allows the dependencies to be kept. For example, if procedure
A calls procedure B and you drop and re-create procedure B, you no longer see the dependency
between the two. If you use ALTER, it’s all still there.
❑ ALTER PROC retains any dependency information on other objects that may call the sproc being
altered.
Dropping Sprocs
It doesn’t get much easier than this:
DROP PROC|PROCEDURE <sproc name>
And it’s gone.

Monday, 3 March 2014

Database Authorization,Security and Encryption rules

Database  Authorization,Security and  Encryption rules:

User-Defined Procedures 

In addition to the passwords and usernames, users can define their own procedures for security. For example, a user can add some secret questions in the procedure, in order to secure his account. Now, whenever the user logs in, he has to give procedure name along with his password. The procedure will ask the answers of the given secret questions and on submission of correct answers the user is given access to the database. 

Encryption 

Data encryption is a technique in which highly sensitive data is encoded (converted into unreadable form) before its storage or transmission over a network. The encoding method is kept tightly protected by the DBMS. The encoded data decoded (reverse process of encoding) for authenticated users only. Thus unauthorized users cannot read data and sensitive data is automatically encoded by DBMS. For example, in ATM system the pin code provided by the customer is encoded (encrypted) before its submission to the database server. A DBA can also manually encrypt data when needed.
Authentication Rules 
Authentication rules are restriction on users, applied through DBMS for controlling data access and limiting actions on data. For example, a user can access a database table after giving a valid username and password. Another user can change records in the table but cannot add new records. Senior users are given full authority to take any action with the table. This authorization is applied by DBA according to the sensitivity of data and organizational rules. 

Authentication schemes

Illegal access to computer resources is becoming a serious problem in the modern world. Passwords and usernames cannot identify the actual person who is logging on the computer system. Password can be leaked out or stolen therefore; it is not a secure method to block illegal access. Alternatively, some systems are using other authentication schemes for this purpose. These authentication schemes include signature dynamics, fingerprints and retina prints etc. Smart cards are normally used for the implementation of these schemes. For example, fingerprints are permanently stored on the smart card and whenever user wants to log in, he is required to insert his card into a card reader. He is also required to scan his fingerprints. Now if saved fingerprints on card and his actual fingerprints match with each other he is given access otherwise access is denied.

Development Life Cycle in Database

Phases in Database Development Life Cycle.

There are number of phases in Database Development Life Cycle.Major steps are listed below:

  • Planning
  • Analysis
  • Design
  • DBMS Selection
  • Implementation
  • Maintenance


Database planning
The database-planning phase begins when a customer requests to develop a database project. It is set of tasks or activities, which decide the resources required in the database development and time limits of different activities. During planning phase, four major activities are performed.
  • Review and approve the database project request.
  • Prioritize the database project request.
  • Allocate resources such as money, people and tools.
  • Arrange a development team to develop the database project.
Database planning should also include the development of standards that govern how data will be collected, how the format should be specified, what necessary documentation will be needed.
Requirements Analysis
Requirements analysis is done in order to understand the problem, which is to be solved. It is very important activity for the development of database system. The person responsible for the requirements analysis is often called "Analyst".
In requirements analysis phase, the requirements and expectations of the users are collected and analyzed. The collected requirements help to understand the system that does not yet exist. There are two major activities in requirements analysis.        
  • Problem understanding or analysis
  • Requirement specifications.
Design
The database design is the major phase of information engineering. In this phase, the information models that were developed during analysis are used to design a conceptual schema for the database and to design transaction and application.
  • In conceptual schema design, the data requirements collected in Requirement Analysis phase are examined and a conceptual database schema is produced.
  • In transaction and application design, the database applications analyzed in Requirement Analysis phase are examined and specifications of these applications are produced. There are two major steps in design phase:
  • Database Design
  • Process Design
DBMS
In this phase an appropriate DBMS is selected to support the information system. A number of factors are involved in DBMS selection. They may be technical and economical factors. The technical factors are concerned with the suitability of the DBMS for information system. The following technical factors are considered.
  • Type of DBMS such as relational, object-oriented etc
  • Storage structure and access methods that the DBMS supports.
  • User and programmer interfaces available.
  • Type of query languages.
  • Development tools etc. 
Implementation
After the design phase and selecting a suitable DBMS, the database system is implemented. The purpose of this phase is to construct and install the information system according to the plan and design as described in previous phases. Implementation involves a series of steps leading to operational information system that includes creating database definitions (such as tables, indexes etc), developing applications, testing the system, developing operational procedures and documentation, training the users and populating the database. In the context of information engineering, it involves two steps.
  •  Database definitions.
  • Creating applications.
Operational Maintenance 
Once the database system is implemented, the operational maintenance phase of the database system begins. The operational maintenance is the process of monitoring and maintaining the database system. Maintenance includes activities such as adding new fields, changing the size of existing field, adding new tables, and so on. As the database system requirement change, it becomes necessary to add new tables or remove existing tables and to reorganize some files by changing primary access methods or by dropping old indexes and constructing new ones. Some queries or transactions may be rewritten for better performance. Database tuning or reorganization continues throughout the life of database and while the requirements keep changing.  

Using Query in Sql Server Generate All Database Backup

Steps Generate All Database Backup in Sql Server:

  1. Open Your  Sql Server
  2. Click on New Query window
  3. Copy and paste the query below
  4. Change your database path (where you want to save your database backup)
  5. Execute query


--use this query--
DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
DECLARE @filedate1 varchar(100)
SET @path = 'D:\Santosh\Database backup'
 
SELECT @fileDate = CONVERT(varchar(50),GETDATE(),103)
 
SELECT @filedate1= REPLACE(@fileDate,'/','-')
DECLARE db_cursor CURSOR FOR
SELECT name
FROM master.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')
 
OPEN db_cursor 
FETCH NEXT FROM db_cursor INTO @name 
 
WHILE @@FETCH_STATUS = 0 
BEGIN 
       SET @fileName = @path + @name + '_' + @filedate1 + '.BAK'
       BACKUP DATABASE @name TO DISK = @fileName
 
       FETCH NEXT FROM db_cursor INTO @name 
END 
 
CLOSE db_cursor 
DEALLOCATE db_cursor


--End of query--

as shown in picture:
















that's it