About Me

My photo
Mumbai, Maharastra, India
He has more than 7.6 years of experience in the software development. He has spent most of the times in web/desktop application development. He has sound knowledge in various database concepts. You can reach him at viki.keshari@gmail.com https://www.linkedin.com/in/vikrammahapatra/ https://twitter.com/VikramMahapatra http://www.facebook.com/viki.keshari

Search This Blog

Tuesday, June 3, 2014

A new way to create Non Clustered composite index in SQL Server 2014



In Earlier version of SQL Serve i.e SQL Server 2008, SQL Server 2012, we were unable to create Composite NONCLUSTERED index on table level, i.e while creating table.

Creating NONCLUSTERED composite Index on SQL Server 2008R2

create table test1
(col1 int primary key clustered,
col2 int index idx_nc(col2, col2),
col3 int
)
Msg 1018, Level 15, State 1, Line 3
Incorrect syntax near 'index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.

Earlier version of SQL Server does not allow us to create Composite NONCLUSTERED index on table level. So to create Composite index, we were writing separate statement of Create INDEX, see below

First create table

create table test1
(col1 int primary key clustered,
col2 int,
col3 int
)
Command(s) completed successfully.

Then once the table is ready, create composite index on columns.

create nonclustered index idx_test1_col2_col3
on test1(col2,col3)
Command(s) completed successfully.

But with the advancement of SQL Server 2014, it allow us to create Composite NonClustered Index on table level, lets see how we can create Composite index in SQL Server 2014 at table level.

Creating NONCLUSTERED Composite Index at Table Level in SQL Server 2014

create table test1
(col1 int primary key clustered,
col2 int index idx_nc(col2, col3),
col3 int
)
Command(s) completed successfully.

Querying Sys.Indexes to check the indexes created.

select name, type_desc from sys.indexes
where object_id = OBJECT_ID('test1')

name                         type_desc
---------------------------- --------------
PK__test1__357D0D3EAC78055D  CLUSTERED
idx_nc                       NONCLUSTERED

Your code is so flexible, u can twist it, blend it and fit it to ur client wants, it has the potential of likeliness to patrons needs.


Post Reference: Vikram Aristocratic Elfin Share

Monday, June 2, 2014

Reusability, create a global default value and bind it to various tables



Here in this article we will see, how we can create default value and bind it to various table fields.

So First we will be creating a User defined datatype if int basic type.

sp_addtype 'pin_code_type', int, null
Command(s) completed successfully.

Now lets create a Default and assign a default value.

create default pin_default as 326595
Command(s) completed successfully.

Now lets bind the Default value to the User defined Data type

sp_bindefault 'pin_default', 'pin_code_type'
Command(s) completed successfully.
Default bound to data type.
The new default has been bound to columns(s) of the specified user data type.

Lets create table and bound the  column to user defined data type ‘pin_code_type’

create table temp
(id int,
zip pin_code_type)
Command(s) completed successfully.

insert into temp(id) values(1)
select * from temp
id          zip
----------- -----------
1           326595
1           326595

Lets create on more Table an assign the default variable to  the table column.

create table temp2
(id int identity(1,1),
c_address varchar(10),
zip pin_code_type)

insert into temp2(c_address) values('delhi')
select * from temp2

id          c_address  zip
----------- ---------- -----------
1           delhi      326595

Conclusion: Here we can see, we created only one Default Value and bind it to more than one table. This is an example of Re-Usability.

Code makes the coder dance in the Rain; Let the life get easier, let’s welcome the coder to your life


Post Reference: Vikram Aristocratic Elfin Share

Sunday, June 1, 2014

RULE III: How RULE are different from Constraint


Read Prev Article: A backward compatibility feature ‘RULE’ in SQL Server

These are few difference which I found..
1. CHECK are applied only to column, whereas RULE are applied to Column as well as User Defined Datatype.
2. CHECK Constraint are bound to Create Table statement, you cannot reuse CHECK Constraint, whereas RULE are created as separated objects and bound to any number of column, so RULE offer reusability.
3. RULE are backward-compatibility feature. So anytime in new version it can be removed.  
4. RULE are not ANSI SQL, so you may see its implementation in SQL Server and probably Sybase. 

                                  
We already covered first point in previous article, here we will check, “CHECK Constraint are bound to Create Table statement, you cannot reuse CHECK Constraint, whereas RULE are created as separated objects and bound to any number of column, so RULE offer reusability.

We will create a Rule then we will try to bind the rule to many column and datatype. Lets start the demonstration.

Creating User Defined Data Type

create type phoone_number_type from varchar(13) not null
go

Creating Table using User Defined Data Type

create table PersonContacts
(pid int identity(1,1),
name varchar(10),
phone_number phoone_number_type)

Creating RULE

create rule phone_no_constraint
as
len(@phone) = 13 and
substring(@phone,3,1) = '-' and
isnumeric( left(@phone,2)) =1 and
isnumeric(right(@phone,10)) =1
Command(s) completed successfully.

Binding Rule to the column

exec sp_bindrule 'phone_no_constraint', 'phoone_number_type'
Rule bound to table column.

Now since Rules are bind to the column, lets try to insert value to the table. Here we have rule for Phone number is ’XX-XXXXXXXXX’ and X is integer type.

Lets insert a proper record in the table.
insert into PersonContacts values('Lopa','91-9099956156')
(1 row(s) affected)

Let’s query the table to conform insertion.

select * from PersonContacts
pid         name       phone_number
----------- ---------- -------------
4           Lopa       91-9099956156

Lets Apply the same rule to a column (Re-Usability)

create table Contacts_Employee
(eid int identity(1,1),
ename varchar(10),
phone_number varchar(13))
Command(s) completed successfully.

Binding Rule to the column

exec sp_bindrule 'phone_no_constraint', 'PersonContacts.phone_number'
Rule bound to table column.

Insert record

insert into Contacts_Employee values(Pragyan,'91-9099956156')
(1 row(s) affected)

Unbind RULE

exec sp_unbindrule 'phoone_number_type'
Rule unbound from table column.

exec sp_unbindrule 'Contacts_Employee.phone_number'
Rule unbound from table column.

Drop Rule

drop rule phone_no_constraint
Command(s) completed successfully.

So here we can see the same rule has been applied to two table. 

Deeply involvement in code gives strength and courage, we named it LOVE between coder and code!!!
 

Post Reference: Vikram Aristocratic Elfin Share