23.10.10

Composite primary key in SQL Server

Primary Key:

A column which is used to identify the records uniquely is referred to as Primary Key.

1. Primary key won't allow Null values.
2. If a Primary key is created, It will automatically create the clustered index on the table. In turn, the data will be sorted based on this column.
3. Only one Primary key can be created for a table.

Composite Primary Key:

1. Multiple columns can participate in the Primary Key which is referred to as composite primary key.

Am creating the table with Composite Primary key on the column ID and Name

CREATE TABLE VENKAT_SAMPLE_TABLE ( ID INT, NAME VARCHAR(100),BIRTHDATE DATETIME
,PRIMARY KEY(ID,NAME))


To get the information about your Primary key on the columns.You can use the below system view.

SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE


In one of the popular forum, I have seen a query. How to achieve it your designer window. Right click on the table and you will get the columns. Press control or Shift key and select the columns -> Right click and you can set the primary key.


Cheers,
Venkatesan Prabu .J

17.10.10

Specifying the schema Name in the Table designer

I have seen a query in a popular forum and I would like to answer the query,

When I create a table, it creates under dbo.
I would like to create it under a different schema, but when I use the 'New Table' dialog, I can never find the field where to specify this.

The answer is,
You need to specify the schema name in the Properties window of the table.


Cheers,
Venkatesan Prabu .J

Grant vs With Grant option in SQL Server

What's the difference between grant and with grant option in SQL Server?

Grant will allow the current user to access the object or provide access to the specified user.

CREATE TABLE venkat_Table (ID INT)
GRANT SELECT ON venkat_Table TOVenkatFriend

With grant option is nothing but a higher level of access provider. This will inform the database that, this user will have access to the particular object and he is having necessary previlege to provide access to other users on the object.

GRANT SELECT ON venkat_Table TO VenkatFriend WITH GRANT OPTION



You can see this option in the schema properties window.
Cheers,
Venkatesan Prabu .J

9.10.10

Sparse Columns in SQL Server 2008

Memory handling on NULL values using sparse columns:

It's a new feature in SQL Server 2008. It enables us to store the null values effectively. Ideally, if the column is NULL. Then, the data won't be stored in the database and it's cost effective solution to save the space. If your table has more Null values, we can opt this solution.

Sparse column is associated with two specialised sub-features:

1. Column Sets - This will provide you the consolidated report of all the sparse columns. We will see this one in our example below.

2. Filtered index - Index will be created on the not null data in the sparse columns.

Limitations and conditions to use Sparse column:

1. We can create index on the sparse column.
2. Sparse column cannot be added on certain column types like text, filestream datatype, geography, image, ntext etc..,
3. Sparse column could not be bounded with default value or rule cannot be applied on it.

Let's see an example,

drop table Venkat_Sparse_ColumnCheck
go
-- created a table to check the sparse column
CREATE TABLE dbo.Venkat_Sparse_ColumnCheck
(
ID INT IDENTITY(1,1) NOT NULL,
FIRST_NAME NVARCHAR(50) NOT NULL,
LAST_NAME NVARCHAR(50) NOT NULL,
ADDRESS1 NVARCHAR(20) SPARSE NULL, -- Sparse columns
ADDRESS2 NVARCHAR(20) SPARSE NULL, -- Sparse columns
CITY NVARCHAR(20) SPARSE NULL, -- Sparse columns
STATE NVARCHAR(2) SPARSE NULL, -- Sparse columns
COUNTRY NVARCHAR(10) SPARSE NULL, -- Sparse columns
ZIP_CODE NVARCHAR(20) SPARSE NULL, -- Sparse columns
CONSTRAINT PK_Venkat_Sparse_ColumnCheck PRIMARY KEY (ID)
)

GO
set nocount on
go

insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Venkatesan', 'Prabu', '1, first raod', null, 'Dharmapuri', 'TN', 'India', NULL);
insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Subs', 'subs', '2 Second road', null, 'trichy', 'tr', 'india', '636701');
insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Janu', 'C', '3 Third road', null, 'trichy', 'tr', 'india', '636701');

-- Here, we are getting a very ordinary output.
Select * from dbo.Venkat_Sparse_ColumnCheck


To see the exact data storage, we need to use the column sets functionality.
Column Set should be defined with datatype as XML and it should be given the condition as ALL_SPARSE_COLUMNS

drop table Venkat_Sparse_ColumnCheck
go

-- created a table to check the sparse column
CREATE TABLE dbo.Venkat_Sparse_ColumnCheck
(
ID INT IDENTITY(1,1) NOT NULL,
FIRST_NAME NVARCHAR(50) NOT NULL,
LAST_NAME NVARCHAR(50) NOT NULL,
ADDRESS1 NVARCHAR(20) SPARSE NULL, -- Sparse columns
ADDRESS2 NVARCHAR(20) SPARSE NULL, -- Sparse columns
CITY NVARCHAR(20) SPARSE NULL, -- Sparse columns
STATE NVARCHAR(2) SPARSE NULL, -- Sparse columns
COUNTRY NVARCHAR(10) SPARSE NULL, -- Sparse columns
ZIP_CODE NVARCHAR(20) SPARSE NULL, -- Sparse columns
ADDRESS_SET XML COLUMN_SET FOR ALL_SPARSE_COLUMNS, -- Column Set to get all the details of the sparse columns
CONSTRAINT PK_Venkat_Sparse_ColumnCheck PRIMARY KEY (ID)
)
GO

set nocount on
go

insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Venkatesan', 'Prabu', '1, first raod', null, 'Dharmapuri', 'TN', 'India', NULL);
insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Subs', 'subs', '2 Second road', null, 'trichy', 'tr', 'india', '636701');
insert into dbo.Venkat_Sparse_ColumnCheck (first_name, last_name, ADDRESS1, ADDRESS2, city, STATE, country, zip_code)
values ('Janu', 'C', '3 Third road', null, 'trichy', 'tr', 'india', '636701');

-- Here, we are getting a very ordinary output.
Select * from dbo.Venkat_Sparse_ColumnCheck


Output for the first record,

1, first raod
Dharmapuri
TN
India

Output for the Second record,

2 Second road
trichy
tr
india
636701

Output for the Third record,

3 Third road
trichy
tr
india
636701

Cheers,

Venkatesan Prabu .J

3.10.10

Replication and autonumber generation issue

Sent: 30 September 2010 22:22
To: jvprabhusanthi@gmail.com
Subject: Invoice Number Generation in offline/online database

Hi Venkatesan Prabu


I would like to get your suggestions in regards to invoice number generation in an application that work offline and online

We have a inventory application developed in .net and sql server ,that can work in a LAN by connecting to the server database

Now we need to provide the offline feature for the application, a counter can work independenly without connecting to the server database.i.e, by connecting
to a local copy of the server database

Before the enter any sales data in the counter ,the sales person has to synchronize the data with the server database .to get the stock details and all
Once synchronistation is done,sales bills can be entered from the counter.and in the end of the day or before starting next days work the sales person has to
synchronise the data with the server again.so the Sales details will be available in the server.

The problem here is that the invcoice number generation.Currently the Automatic invoice number is generated using the query

select isNull(Max(BillNumber),0)+1 from PurchaseHeader

If there is more one sales counters that work on local copy of the server database ,the sales billnumber will be duplicated.
because the billNumber is generated from the local database only. when this is uploaded to server there will be duplicate billnumbers

Could you please suggest me some possible solutions to this problem

--
Thanks,
Bijith

Check my Reply for the above problem. Share your thoughts and post some more solutions
Hi Bijith,

Very good practical problem. We can provide multiple solution for your problem.

Below is the best solution for your problem.

1. You need to use Replication for updating your data from local database to master database.
2. In the local database, generate a unique id (Your Invoice number) and amend another unique number (Local counter number). (Your invoice number is 120 and counter id is 20, then the actual invoice number is 12020. The last two number will be unique for your counter).
3. While pushing this data to the master database, generate unique id in the master database and another column will hold the invoice number generated in the local database.

Let me know for any other details needed.
Cheers,
Venkatesan Prabu .J
MVP / MVM / MCITP / MCTS / MCAD / CCNA / QAI
Microsoft SQL Server MVP / Mind Cracker MVP
http://venkattechnicalblog.blogspot.com/