Friday, March 23, 2012
Need expert's opinion on table schema...
I have seen different types of table schemas employed in
applications and I would like to get the opinions of the
SQL experts...
First some definitions:
LU = Lookup
IP = Index Person
IA = Index Account
Background sample info:
Fred has two accounts number 9999 and 8888
Joe has one account 7777
I can represent these accounts in the following
table examples:
(probably lots more ways to do this, but I am interested
in these two architectures, but would be willing to
entertain other schemas)
Example 1:
Person LU Account
IP Name IP IA IA Num
1 Fred 1 1 1 9999
2 Joe 1 2 2 8888
2 3 3 7777
Example 2:
Person Account
IP Name IA IP Num
1 Fred 1 1 9999
2 Joe 2 1 8888
3 2 7777
My questions to the people who live and breath SQL,
What are the pros and cons to the above table schemas?
Please be specific and if necessary write me in a
separate email : Mike@.e-liss.org
Thanks
Mike"mike" <Mike@.e-liss.org> wrote in message
news:3ff801c3b129$dd3c06e0$a601280a@.phx.gbl...
> Hello,
> I have seen different types of table schemas employed in
> applications and I would like to get the opinions of the
> SQL experts...
> First some definitions:
> LU = Lookup
> IP = Index Person
> IA = Index Account
> Background sample info:
> Fred has two accounts number 9999 and 8888
> Joe has one account 7777
>
> I can represent these accounts in the following
> table examples:
> (probably lots more ways to do this, but I am interested
> in these two architectures, but would be willing to
> entertain other schemas)
>
> Example 1:
> Person LU Account
> IP Name IP IA IA Num
> 1 Fred 1 1 1 9999
> 2 Joe 1 2 2 8888
> 2 3 3 7777
>
> Example 2:
> Person Account
> IP Name IA IP Num
> 1 Fred 1 1 9999
> 2 Joe 2 1 8888
> 3 2 7777
>
Example 1 uses a linking table, and is a more general structure than Example
2. Using a linking table it is possible to model relationships 1-1 1-many
or many-many. Using a foregn key you can only model 1-1 or 1-many. From
just that, you should prefer Example 2. One of the guiding principles of
data modeling is to use the most specific model that meets your needs.
From a performance point of view, Example will be superior as well. Notice
that you can transform Example 1 into Example 2.
CREATE VIEW v_Account
as
select Account.IA, LU.IP, Account.Num
from Account join LU
on Account.IA = LU.IA
But to add or delete anaccount will require 2 operations instead of one.
Also queries will have to perform an additional and unnecessary join.
Or, think of it this way:
LU has a 1-1 relationship with Account. Whenever you see a datamodel with a
1-1 relationship, the related should be merged. There are exceptions, but
all rules have execptions.
David|||>--Original Message--
>"mike" <Mike@.e-liss.org> wrote in message
>news:3ff801c3b129$dd3c06e0$a601280a@.phx.gbl...
>> Hello,
>> I have seen different types of table schemas employed
in
>> applications and I would like to get the opinions of
the
>> SQL experts...
>> First some definitions:
>> LU = Lookup
>> IP = Index Person
>> IA = Index Account
>> Background sample info:
>> Fred has two accounts number 9999 and 8888
>> Joe has one account 7777
>>
>> I can represent these accounts in the following
>> table examples:
>> (probably lots more ways to do this, but I am
interested
>> in these two architectures, but would be willing to
>> entertain other schemas)
>>
>> Example 1:
>> Person LU Account
>> IP Name IP IA IA Num
>> 1 Fred 1 1 1 9999
>> 2 Joe 1 2 2 8888
>> 2 3 3 7777
>>
>> Example 2:
>> Person Account
>> IP Name IA IP Num
>> 1 Fred 1 1 9999
>> 2 Joe 2 1 8888
>> 3 2 7777
>>
>Example 1 uses a linking table, and is a more general
structure than Example
>2. Using a linking table it is possible to model
relationships 1-1 1-many
>or many-many. Using a foregn key you can only model 1-1
or 1-many. From
>just that, you should prefer Example 2. One of the
guiding principles of
>data modeling is to use the most specific model that
meets your needs.
>From a performance point of view, Example will be
superior as well. Notice
>that you can transform Example 1 into Example 2.
>CREATE VIEW v_Account
>as
>select Account.IA, LU.IP, Account.Num
>from Account join LU
> on Account.IA = LU.IA
>But to add or delete anaccount will require 2 operations
instead of one.
>Also queries will have to perform an additional and
unnecessary join.
>Or, think of it this way:
>LU has a 1-1 relationship with Account. Whenever you
see a datamodel with a
>1-1 relationship, the related should be merged. There
are exceptions, but
>all rules have execptions.
>David
>
>.
>
David,
Are there any significant draw backs to not using Ex 1 ?
If not, the what is the purpose of the LU table?
So Fred and Joe can share the same account ?
ie:
IP IA
1 1
2 1
Where in example 2 this is not possible ?
Thanks
Mike|||"Mike" <anonymous@.discussions.microsoft.com> wrote in message
news:043e01c3b134$23540330$a001280a@.phx.gbl...
> >--Original Message--
> >
> >"mike" <Mike@.e-liss.org> wrote in message
> >news:3ff801c3b129$dd3c06e0$a601280a@.phx.gbl...
> >> Hello,
> >>
. . .
> David,
> Are there any significant draw backs to not using Ex 1 ?
> If not, the what is the purpose of the LU table?
No.
> So Fred and Joe can share the same account ?
> ie:
> IP IA
> 1 1
> 2 1
> Where in example 2 this is not possible ?
Exactly right. It just allows modeling different kinds of relationships.
David
Need Experts on Vb.net CLR intergration problem
Hi all I have the following CLR stored procedure :
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub sssGetActiveRepositoryByTitle( _
ByVal title As String)
' Add your code here
Using conn As New SqlConnection("context connection=true")
Dim objCommand As New SqlCommand()
Dim TitleParam As New SqlParameter("@.Title", SqlDbType.VarChar, 100)
TitleParam.Value = title
objCommand.Connection = conn
conn.Open()
'build the delete command
objCommand.CommandText = _
"select * from sstRepository where IsActive = 1 and Title =" & TitleParam.Value.ToString
SqlContext.Pipe.ExecuteAndSend(objCommand)
conn.Close()
End Using
End Sub
Now I have a windows service in my data layer that needs to access this stored procedure and convert it into a dataaset to pass to the client application :
Imports System.Data.SqlClient
Imports NBS.SURVEYSDATABASEservice.DBMS
Public Class clsClient
' it inherits the stored procedures from the DBMS class which is the name
'of the CLR dll
Inherits StoredProcedures
Public Function GetClientByVirtualPath(ByVal pstrVirtualPath As String) As DataSet
Try
Dim i As SqlDataReader
'parameters are stored in an array (zero based) for use in the base class
Dim parmArrSqlParms(0) As SqlClient.SqlParameter
' Dim fff As Int32
i = sdsGetClientByVirtualPath(pstrVirtualPath)
' Return MyBase.RunProcedure("dbo.sdsGetClientByVirtualPath", parmArrSqlParms)
Catch ex As Exception
'log the error
'cLogger.LogMessage("ACME", "SampleApplication", Logger.EntryTypes.RunError, System.Environment.MachineName, "clsDemoClass.SelectAllCompanies", ex.Message)
'raise the error to the caller for handling
Throw ex
End Try
End Function
I've tried a bunch of different things to no avail the error I keep getting trying to access the sqlpipe resulsts is " this expressions does not return any values"
any ideas ? I am basically converting around TSQL 50 stored procs into managed CLR code and the CLR funtions are created but I am really having problems accessing the resuluts on the client end .
Help please !
Before moving 50 TSQL stored proc into managed code, have you considered our TSQL vs CLR guidelines located here?
Need Experts on Vb.net CLR intergration problem
Hi all I have the following CLR stored procedure :
Partial Public Class StoredProcedures
<Microsoft.SqlServer.Server.SqlProcedure()> _
Public Shared Sub sssGetActiveRepositoryByTitle( _
ByVal title As String)
' Add your code here
Using conn As New SqlConnection("context connection=true")
Dim objCommand As New SqlCommand()
Dim TitleParam As New SqlParameter("@.Title", SqlDbType.VarChar, 100)
TitleParam.Value = title
objCommand.Connection = conn
conn.Open()
'build the delete command
objCommand.CommandText = _
"select * from sstRepository where IsActive = 1 and Title =" & TitleParam.Value.ToString
SqlContext.Pipe.ExecuteAndSend(objCommand)
conn.Close()
End Using
End Sub
Now I have a windows service in my data layer that needs to access this stored procedure and convert it into a dataaset to pass to the client application :
Imports System.Data.SqlClient
Imports NBS.SURVEYSDATABASEservice.DBMS
Public Class clsClient
' it inherits the stored procedures from the DBMS class which is the name
'of the CLR dll
Inherits StoredProcedures
Public Function GetClientByVirtualPath(ByVal pstrVirtualPath As String) As DataSet
Try
Dim i As SqlDataReader
'parameters are stored in an array (zero based) for use in the base class
Dim parmArrSqlParms(0) As SqlClient.SqlParameter
' Dim fff As Int32
i = sdsGetClientByVirtualPath(pstrVirtualPath)
' Return MyBase.RunProcedure("dbo.sdsGetClientByVirtualPath", parmArrSqlParms)
Catch ex As Exception
'log the error
'cLogger.LogMessage("ACME", "SampleApplication", Logger.EntryTypes.RunError, System.Environment.MachineName, "clsDemoClass.SelectAllCompanies", ex.Message)
'raise the error to the caller for handling
Throw ex
End Try
End Function
I've tried a bunch of different things to no avail the error I keep getting trying to access the sqlpipe resulsts is " this expressions does not return any values"
any ideas ? I am basically converting around TSQL 50 stored procs into managed CLR code and the CLR funtions are created but I am really having problems accessing the resuluts on the client end .
Help please !
Before moving 50 TSQL stored proc into managed code, have you considered our TSQL vs CLR guidelines located here?
Saturday, February 25, 2012
nee stored procedure please help
I'm vinod, a junior DBA. working in HYD, india. i need one stored procedure.
Using one procedure i want to know the columns that are associated with the primary key and the name of the primary key.
The output should be as follows
Table Name PK_Constraint Name Columns
TABLE017 PK_TABLE009 COLUMN001
TABLE017 FK_TABLE009 COLUMN002
please send me the stored procedure, it will be a great help for me. thanks in advance.
vinod.mallolu@.exensys.com
vinodselect kcu.Table_Name, kcu.Constraint_Name, kcu.Column_Name from information_schema.table_constraints tc join information_schema.key_column_usage kcu
on tc.Constraint_Name = kcu.Constraint_Name and tc.Constraint_Type = 'PRIMARY KEY'
Mahesh|||i'M TRYING WITH THAT FRIEND. THANK YOU VERY MUCH FOR YOU REPLY