Tuesday, 29 January 2013

How to deal with null values in joins?

NULLs and JOINs
 Related Resources
• SQL Fundamentals
• Starting a Career in Databases
• Certification Resources
In our last article, we introduced the concept of the NULL (or “unknown”) value and described how SQL treats this value when encountered in logical operations such as AND and OR.  In this article, we take a look at how NULL values impact a more complicated operation – the JOINing of two tables.
Let's begin by taking a look at two sample tables that comprise the course database for a school.  We might first have a list of courses and instructors, as shown below.  (Note that, for simplicity's sake, we are making the assumption that there is only one section of each course and, therefore, that we can use it as the primary key.)  In this case, the instructor Adams has not yet been assigned to a course and the Computers course has no instructor assigned yet.
CourseInstructor
MathSmith
ScienceJones
NULLAdams
ComputersNULL
Now, let's assume that we have a table containing course registrations for students.  Again, for simplicity's sake, we'll assume that each student must take one and only one course.  Every student must take a class, so we will include a NULL record for any student whose course registration information is unknown.  In this case, we're not sure what course Alan is taking.
StudentCourse
RyanMath
BettyScience
AlanNULL
If we perform an INNER JOIN, NULL values do not match each other.  For example, if we perform the following query:
SELECT *
FROM Instructors INNER JOIN Students
ON Instructors.Course = Students.Course
We'd get the following result:
CourseInstructorStudent
MathSmithRyan
ScienceJonesBetty
The OUTER JOIN operations includes data from one or both tables that doesn't match data in the other table.  There are three types of OUTER JOINs:
  • The LEFT OUTER JOIN includes rows from the table specified on the left side of the JOIN statement that don't match with rows from the table on the right side of the JOIN statement.
  • The RIGHT OUTER JOIN includes rows from the table specified on the right side of the JOIN statement that don't match with rows from the table on the left side of the JOIN statement.
  • The FULL OUTER JOIN includes rows from both tables that don't match data in the other table.
If we performed the following query:
SELECT *
FROM Instructors LEFT OUTER JOIN Students
ON Instructors.Course = Students.Course
We'd get the following result set:
CourseInstructorStudent
MathSmithRyan
ScienceJonesBetty
NULLAdamsNULL
ComputersNULLNULL
Similarly, the query:
SELECT *
FROM Instructors RIGHT OUTER JOIN Students
ON Instructors.Course = Students.Course
Would give us:
CourseInstructorStudent
MathSmithRyan
ScienceJonesBetty
NULLNULLAlan
And, finally, the query:
SELECT *
FROM Instructors FULL OUTER JOIN Students
ON Instructors.Course = Students.Course
Would yield:
CourseInstructorStudent
MathSmithRyan
ScienceJonesBetty
ComputersNULLNULL
NULLNULLAlan
NULLAdamsNULL
And that's a look at how NULL values are treated by the JOIN operation!

Join ,union and union all with null values in tables?


--demo of join,union and union all
NOTE: Tables having null values
create table join1(id int,name varchar(50))

insert into join1 values(1,'jana'),(2,'sundu'),(null,null),(4,'null')

create table join2(id int,name varchar(50))

insert into join2 values(1,'jana'),(2,null),(3,null),(4,'null')

select * from join1 j1
inner join
join2 j2 on
j1.id=j2.id

Result
id            name    id            name
1              jana       1              jana
3              sundu   3              NULL
4              null         4              null
--union
select * from join1 j1
union
select * from join2 j2
--Note:union will give both matching rows and nonmatching rows only once
--i.e distinct values

Result
id    name
NULL  adi
1     jana
2     NULL
3     NULL
3     sundu
4     null
--union all

select * from join1 j1
union all
select * from join2 j2
--it gives all values from both tables
Result
id            name
1              jana
NULL     adi
3              sundu
4              null
1              jana
2              NULL
3              NULL
4              null

Sunday, 27 January 2013

views in sql server?

Views are virtual tables that represent the result set of a select statement from one or more tables or other views. In most cases, that is unless a view is indexed, a view is basically a predefined query that is stored and executed whenever the view is referenced in a query.
You can use a view to limit user access to sensitive information in the underlying table. For example, you can create a view that only includes a subset of columns in a table and give the user access to the view instead of the underlying table. You can also use a WHERE clause to limit the rows of data that are returned, ensuring the user can see only specific rows.
There are a few limitations you need to be aware of when creating a view. A SELECT statement in a view cannot include any of the following:
  • A COMPUTE or COMPUTE BY clause.
  • An ORDER BY clause without also including the TOP clause. The workaround for this is to select the top 100 percent, as in the following query: SELECT TOP 100 PERCENT FROM <table> ORDER BY <columns>.
  • The INTO keyword used to create a new table.
  • The OPTION clause.
  • A reference to a temporary table or table variable.
The example in Listing 1 creates a view that shows the name and department description of all the employees in a single department. As you can see in the example, we are able to use the view to hide the employee's social security number. We are also able to provide more user-friendly column names by using an alias. The WITH ENCRYPTION option prevents the view definition from being displayed. You should be careful when using the encryption option because you will not be able to retrieve the definition from the database if you need it.
Example 1. Sample Code Used to Create an Encrypted View
USE AdventureWorks2008
GO

CREATE TABLE Employee
(EmpID int NOT NULL CONSTRAINT PK_EMP PRIMARY KEY CLUSTERED,
EmpFirstName Varchar(50),
EmpLastName Varchar(50),
EmpSSN Varchar(9),
DepartmentID int)
GO

CREATE TABLE Department
(DepartmentID int NOT NULL CONSTRAINT PK_DEPT PRIMARY KEY CLUSTERED,
DepartmentDscr Varchar(50))
GO

CREATE VIEW vMarketingEmployees
WITH ENCRYPTION
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
       dbo.Employee.EmpLastName AS LastName,
       dbo.Department.DepartmentDscr AS Department
FROM  dbo.Department INNER JOIN
      dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 1

       

Let's say you found out that the Marketing department is actually DepartmentID 2 instead of 1. You can use the ALTER VIEW statement shown in Listing 2 to make the changes. While making the changes, you also decide to remove the encryption option. All you have to do to remove the encryption is not to specify the option when running the ALTER VIEW statement.
Example 2. Syntax Used to Alter an Existing View
USE AdventureWorks2008
GO

ALTER VIEW vMarketingEmployees
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
       dbo.Employee.EmpLastName AS LastName,
       dbo.Department.DepartmentDscr AS Department
FROM  dbo.Department INNER JOIN
      dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 2

       

To remove a view from the database, all you have to do is issue the DROP VIEW statement followed by the view name, as shown in Listing 3.
Example 3. Syntax to Remove a View from the Database
DROP VIEW vMarketingEmployees

1. Partitioned Views

Distributed partitioned views are those that reference data across multiple servers and combine the data to the user as a single result set. You can use distributed partitioned views to form a federation of database servers, which are separately managed servers used to spread the processing load for a single application across multiple servers. Listing 4 shows an example of a distributed partitioned view.
NOTE

You can create a local partitioned view by referencing only tables on the same server; however, creating partitioned tables is the preferred method for locally partitioning data.
Example 4. Common Syntax Used in a Distributed Partitioned View
CREATE VIEW vDistributedSample
AS
SELECT col1, col2, col3 FROM Server1.DBName.dbo.TableName
UNION ALL
SELECT col1, col2, col3 FROM Server2. DBName.dbo.TableName
UNION ALL
SELECT col1, col2, col3 FROM Server3.DBName.dbo.TableName

2. Updateable Views

You can use a view to insert, update, and delete data in the underlying tables as long as certain conditions are met:
  • All the columns being modified must be in the same base table.
  • The columns must also directly reference the base table; you cannot modify computed columns or columns that are derived from or affected by aggregate functions.
  • If the WITH CHECK option is specified, as shown in Listing 5, the view cannot be updated in any way that would cause the updated record to disappear from the result set.
For example, given the view created in Listing 5, you could not run an UPDATE statement to set the DepartmentID = 2.
Example 5. Syntax to Create an Updatable View Using the WITH CHECK Option
USE AdventureWorks2008
GO

--Drop the view if it currently exists
IF OBJECT_ID('dbo.vMarketingEmployees', 'V') IS NOT NULL
  DROP VIEW dbo.vMarketingEmployees;

GO

--Create a view using the WITH CHECK option
CREATE VIEW vMarketingEmployees
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
       dbo.Employee.EmpLastName AS LastName,
       dbo.Department.DepartmentID,
       dbo.Department.DepartmentDscr AS Department
FROM  dbo.Department INNER JOIN
      dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
WHERE dbo.Department.DepartmentID = 1
WITH CHECK OPTION

       

If you look at Figure 1, you can see that trying to change DepartmentID fails, since it will violate the CHECK OPTION constraint, but changing Department is successful.
Figure 1. Results of updating a view that uses the WITH CHECK option

3. Indexed Views

If you have a process-intensive view that is run often, you can improve performance and reduce the processing time needed to execute the view by creating an index on the view. The first index you create on a view must be a unique clustered index, which causes the result set of the view to be stored in the database. Thereafter, you can create nonclustered indexes.
Listing 6 shows an example of creating an indexed view. Notice the WITH SCHEMABINDING clause in the CREATE VIEW statement. If you plan to create an index on a view, then you must first create the view using that clause, which prevents any changes to the underlying tables referenced by the view that would affect the view definition. An indexed view is essentially a stored result set returned by the view, so SQL Server does not have to process the query each time the view is referenced. By creating the view WITH SCHEMABINDING, SQL Server can ensure that no underlying changes to the data will invalidate the stored results.
There are several other requirements that must be met in order to create an indexed view. For the specific requirements, refer to the topic "Creating Indexed Views" in SQL Server Books Online.
Example 6. Syntax to Create an Indexed View
USE AdventureWorks2008
GO

CREATE VIEW vEmployees
WITH SCHEMABINDING
AS
SELECT dbo.Employee.EmpFirstName AS FirstName,
       dbo.Employee.EmpLastName AS LastName,
       dbo.Department.DepartmentID,
       dbo.Department.DepartmentDscr AS Department
FROM  dbo.Department INNER JOIN
      dbo.Employee ON dbo.Department.DepartmentID = dbo.Employee.DepartmentID
GO
--Create an index on the view
CREATE UNIQUE CLUSTERED INDEX IDX_vEmployee_Dept
 ON vEmployees (DepartmentID);
GO

       

Indexed views are best suited for situations where the underlying data is rarely updated; because as the data is updated in the base tables, the data must also be updated in the indexed view to reflect the changes. If the data is frequently updated, maintaining the index could actually lead to performance issues instead of performance gains. There are many caveats to using indexed views that could lead to an administrative headache, so you shouldn't go around creating indexes on all your views. However, there are certain situations, especially in a data warehouse environment, where indexed views can provide an enormous performance benefit.
You can create an indexed view in any edition of SQL Server 2008, but you receive some extra benefits when you are running the Enterprise Edition. In the Enterprise Edition, the query optimizer can automatically take advantage of an index created on a view, even if the view is not specifically referenced in a query.

self join in sql server?





»

SQL SERVER – 2005 Explanation and Example – SELF JOIN

A self-join is simply a normal SQL join that joins one table to itself. This is accomplished by using table name aliases to give each instance of the table a separate name. Joining a table to itself can be useful when you want to compare values in a column to other values in the same column. A join in which records from a table are combined with other records from the same table when there are matching values in the joined fields. A self-join can be an inner join or an outer join. A table is joined to itself based upon a field or combination of fields that have duplicate data in different records. The data-type of the inter-related columns must be of the same type or needs to cast them in same type.
When all of the data you require is contained within a single table, but data needed to extract is related to each other in the table itself. Examples of this type of data relate to Employee information, where the table may have both an Employee’s ID number for each record and also a field that displays the ID number of an Employee’s supervisor or manager. To retrieve the data tables are required to relate/join to itself.
Another example which can be tried on SQL SERVER 2005 sample database AdventureWorks is to find products that are supplied by more than one vendor. Please refer the sample database for table structure.




--SELF JOIN

CREATE TABLE Employee246(
EmployeeID INT PRIMARY KEY,
Name NVARCHAR(50),
ManagerID INT
)

INSERT INTO Employee246
SELECT 1, 'Mike', 3
UNION ALL
SELECT 2, 'David', 3
UNION ALL
SELECT 3, 'Roger', NULL
UNION ALL
SELECT 4, 'Marry',2
UNION ALL
SELECT 5, 'Joseph',2
UNION ALL
SELECT 7, 'Ben',2

select * from Employee246

select e2.Name,e1.Name as manager from Employee246 e1
inner join
Employee246 e2
on
e2.EmployeeID=e1.ManagerID

Thursday, 24 January 2013

Primary key and unique ikey and difference between them?


A UNIQUE constraint is similar to PRIMARY key, but you can have more than one UNIQUE constraint per table.
When you declare a UNIQUE constraint, SQL Server creates a UNIQUE index to speed up the process of searching for duplicates. In this case the index defaults to NONCLUSTERED index, because you can have only one CLUSTERED index per table.
* The number of UNIQUE constraints per table is limited by the number of indexes on the table i.e 249 NONCLUSTERED index and one possible CLUSTERED index.
Contrary to PRIMARY key UNIQUE constraints can accept NULL but just once. If the constraint is defined in a combination of fields, then every field can accept NULL and can have some values on them, as long as the combination values is unique.

Monday, 31 December 2012

Difference between Delete and Truncate





ifference between TRUNCATE, DELETE and DROP commands

 

DELETE

The DELETE command is used to remove rows from a table. A WHERE clause can be used to only remove some rows. If no WHERE condition is specified, all rows will be removed. After performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to fire.
SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)
----------
        14

SQL> DELETE FROM emp WHERE job = 'CLERK';

4 rows deleted.

SQL> COMMIT;

Commit complete.

SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)
----------
        10

TRUNCATE

TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE.
SQL> TRUNCATE TABLE emp;

Table truncated.

SQL> SELECT COUNT(*) FROM emp;

  COUNT(*)
----------
         0

DROP

The DROP command removes a table from the database. All the tables' rows, indexes and privileges will also be removed. No DML triggers will be fired. The operation cannot be rolled back.
SQL> DROP TABLE emp;

Table dropped.

SQL> SELECT * FROM emp;
SELECT * FROM emp
              *
ERROR at line 1:
ORA-00942: table or view does not exist

DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. Therefore DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.
From Oracle 10g a table can be "undropped". Example:
SQL> FLASHBACK TABLE emp TO BEFORE DROP;

Flashback complete.
PS: DROP and TRUNCATE are DDL commands, whereas DELETE is a DML command. As such, DELETE operations can be rolled back (undone), while DROP and TRUNCATE operations cannot be rolled back.

Difference between TRUNCATE and DELETE commands

1>TRUNCATE is a DDL command whereas DELETE is a DML command.
2>TRUNCATE is much faster than DELETE.
Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.Thatswhy TRUNCATE is faster.Once you Truncate you cann't get back the data.
3>You cann't rollback in TRUNCATE but in DELETE you can rollback.TRUNCATE removes the record permanently.
4>In case of TRUNCATE ,Trigger doesn't get fired.But in DML commands like DELETE .Trigger get fired.
5>You cann't use conditions(WHERE clause) in TRUNCATE.But in DELETE you can write conditions using WHERE clause

One more difference.

Thanks for this information.
There is one more difference that TRUNCATE command resets the High Water Mark for the table but DELETE does not. So after TRUNCATE the operations on table are much faster.

about truncate and drop

Drop command will delete the entire row also the structure.But truncate will delete the contenets only not the strucure, so no need to give specifications for another table creation.

DELETE,DROP,TRUNCATE

DELETE
Delete is the command that only remove the data from the table. It is DML statement. Deleted data can be rollback. By using this we can delete whole data from the table(if use without where clause).If ew want to remove only selected data then we should specify condition in the where clause
SQL>delete from employee;(this command will remove all the data from table)
SQL>delete from employee where employee_name='JOHN';(This command will remove only that row from employee table where employee_name is JOHN');
DROP:
Drop command remove the table from data dictionary. This is the DDL statement. We can not recover the table before Oracle 10g. But Oracle 10g provide the command to recover it by using the command (FLASHBACK)
TRUNCATE:
This is the DML command. This command delete the data from table. But there is one difference from ordinary delete command. Truncate command drop the storage held by this table. Drop storage can be use by this table again or some other table. This is the faster command because it directly drop the storage

Truncate is DDL command

DML commands have a roll back option.But DDL commands do not have.So truncate is a ddl statement

Diff between DML & DDL command

Yes, DML command can be roll back but DDL can not.

DELETE,DROP,TRUNCATE

DROP command is a DDL command. It removes the information along with structure. It also removes all information about the table from data dictionary.
TRUNCATE command is DDL command. It removes all the information of the table. Regarding performance if you have to delete all the rows of a table you should perform TRUNCATE command with DROP STORAGE option.
DELETE is a DML command. It provides the facility of conditional-based deletion. It also generates REDO information.

Undo tablespace + Truncate and Constraints + Grants with drops

If you're removing a large quantity of data (e.g. debug log table) then it makes sense to use truncate to clear the table if you're not worried about needing to retrieve it as part of a transaction - remember that you are stretching your undo tablespace to cover the whole of the data you are looking to delete if you using the DELETE FROM... command, only to immediately wipe that out again when you commit - for what purpose? A lot more processing effort when all you want to do is clear a table of its contents.
On the minus side, if you have a foreign key constraint referring to the table you are trying to truncate, this won't work - even if the referring table has no data in it! This is because the foreign key checking is done with DDL rather than DML. This can be got around by temporarily disabling the foreign key constraint(s) to the table.

With dropping a table, also bear in mind that you lose any associated grants / constraints etc. and if you want to recreate the table, you would need to recreate these - and it may be that the user dropping the table may not have the rights to re-grant etc. - so dropping is always an extreme measure! If you're constantly dropping / recreating you should probably be using temporary tables for one-off jobs or global temporary tables for frequently running processes..

TRUNCATE V/s DELETE

TruncateDelete
TRUNCATE is a DDL commandDELETE is a DML command
TRUNCATE TABLE always locks the table and page but not each rowDELETE statement is executed using a row lock,                                             each row in the table is locked for deletion
Cannot use Where ConditionWe can specify filters in where clause
It Removes all the dataIt deletes specified data if where condition exists.
TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.Delete activates a trigger because the operation                                are logged individually.
Faster in performance wise, because it is minimally logged in transaction log.Slower than truncate because, it maintain logs for every record
 Drop all object’s statistics and marks like High Water Mark free extents and leave the object really empty with the first extent. zero pages are left in the tablekeeps object’s statistics and all allocated space. After a                       DELETE statement is executed,the table can still contain empty pages.
TRUNCATE TABLE removes the data by deallocating the data pages used to store the table data and records only the page deallocations in the transaction loThe DELETE statement removes rows one at a time                       and records an entry in the transaction log for each deleted row
If the table contains an identity column, the counter for that column is reset to the seed value that is defined for the columnDELETE retain the identity
Restrictions on using Truncate Statement
1. Are referenced by a FOREIGN KEY constraint.
2. Participate in an indexed view.
3. Are published by using transactional replication or merge replication.

Friday, 28 December 2012

Try catch block in sql


SQL SERVER – 2005 – Explanation of TRY…CATCH and ERROR Handling

SQL Server 2005 offers a more robust set of tools for handling errors than in previous versions of SQL Server. Deadlocks, which are virtually impossible to handle at the database level in SQL Server 2000, can now be handled with ease. By taking advantage of these new features, you can focus more on IT business strategy development and less on what needs to happen when errors occur. In SQL Server 2005, @@ERROR variable is no longer needed after every statement executed, as was the case in SQL Server 2000. SQL Server 2005 provides the TRY…CATCH construct, which is already present in many modern programming languages. TRY/CATCH helps to write logic separate the action and error handling code. The code meant for the action is enclosed in the TRY block and the code for error handling is enclosed in the CATCH block. In case the code within the TRY block fails, the control automatically jumps to the CATCH block, letting the transaction roll back and resume execution. In addition to this, the CATCH block captures and provides error information that shows you the ID, message text, state, severity and transaction state of an error.
Functions to be used in CATCH block are :
  • ERROR_NUMBER: returns the error number, and is the same value of @@ERROR.
  • ERROR_SEVERITY: returns the severity level of the error that invoked the CATCH block.
  • ERROR_STATE: returns the state number of the error.
  • ERROR_LINE: returns the line number where the error occurred.
  • ERROR_PROCEDURE: returns the name of the stored procedure or trigger for which the error occurred.
  • ERROR_MESSAGE: returns the full message text of the error. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.
You can use these functions anywhere inside a CATCH block, and they will return information regarding the error that has occurred. These functions will return the value null outside of the CATCH block.
Syntax:
BEGIN TRY
{ sql_statement 
|statement_block }END TRYBEGIN CATCH
{ sql_statement 
|statement_block }END CATCH
The TRY or CATCH block can contain a single T-SQL statement or a series of statements. The CATCH block must follow immediately after the TRY block. The TRY/CATCH block cannot span more than a single batch. In addition, TRY/CATCH block cannot span an IF/ELSE statement.
Example of TRY…CATCH:
BEGIN TRYDECLARE @X INT---- Divide by zero to generate ErrorSET @X = 1/0PRINT 'Command after error in TRY block'END TRYBEGIN CATCHPRINT 'Error Detected'END CATCHPRINT 'Command after TRY/CATCH blocks'
Above code will return following result:
Error Detected
Command after TRY/CATCH blocks

If all the statements within the TRY block are executed successfully, then processing does not enter the CATCH block, but instead skips over the CATCH block and executes the first statement following the END CATCH statement. Removing SET statement in above code PRINT ‘Error Detected’ statement is not executed, but the PRINT statement within the TRY block is executed, as well as the PRINT statement after the TRY/CATCH block. TRY/CATCH blocks can be nested.
Limitation of TRY…CATCH:
  • Compiled errors are not caught.
  • Deferred name resolution errors created by statement level recompilations. (If process is terminated by Kill commands or broken client connections TRY…CATCH will be not effective)
  • Errors with a severity greater than 10 that do not terminate their database connection are caught in the TRY/CATCH block.
For errors that are not trapped, SQL Server 2005 passes control back to the application immediately, without executing any CATCH block code.
Similar example of TRY…CATCH which includes all the ERROR functions:
USE AdventureWorks;GOBEGIN TRY-- Generate a divide-by-zero error.SELECT 1/0;END TRYBEGIN CATCHSELECTERROR_NUMBER() AS ErrorNumber,ERROR_SEVERITY() AS ErrorSeverity,ERROR_STATE() AS ErrorState,ERROR_PROCEDURE() AS ErrorProcedure,ERROR_LINE() AS ErrorLine,ERROR_MESSAGE() AS ErrorMessage;END CATCH;GO