Showing posts with label master. Show all posts
Showing posts with label master. Show all posts

Monday, March 19, 2012

composite primary key

i have a master table with around 15 columns and i am trying to find
the appropriate primary keys and indexes for the table.

To make the records unique, i need to include two datetime columns (
start and end dates ) and two integer columns ( attributes of the
record ) to make up a composite primary key. Both of these four
columns are found in the WHERE clause of my queries.

Is it acceptable in the view of performance and how should i create
the indexes?Hi

I think this is probably a candidate for a surrogate key.

John
"onder" <okeen1@.hotmail.com> wrote in message
news:9c3d3756.0309022355.10656428@.posting.google.c om...
> i have a master table with around 15 columns and i am trying to find
> the appropriate primary keys and indexes for the table.
> To make the records unique, i need to include two datetime columns (
> start and end dates ) and two integer columns ( attributes of the
> record ) to make up a composite primary key. Both of these four
> columns are found in the WHERE clause of my queries.
> Is it acceptable in the view of performance and how should i create
> the indexes?|||Hi John,

sorry for this but can you tell a little more on that?|||Hi

There is usually quite a debate in the newsgroups when surrogate keys are
mentioned. Basically it is when you add a column that you populate with
unique values, so you can reference a row by that instead of the 4 columns
you mentioned. This is a way to reduce the width of any table that refers to
your table with a foreign key (you would need all 4 columns replicated in
that table if you did not use a surrogate key). If you searched google for
"Surrogate SQLServer" it will turn up a few debates.

Quite often a surrogate key is given the IDENTITY property, so SQL Server
will populate the values for you,
http://msdn.microsoft.com/library/d...asp?frame=true
but it is possible to maintain the values your self.

HTH

John

"onder" <okeen1@.hotmail.com> wrote in message
news:9c3d3756.0309030711.5f4f582c@.posting.google.c om...
> Hi John,
> sorry for this but can you tell a little more on that?|||Ok about the surrogate keys. But i have an issue to ask:

In my 'select' queries, i always have the three columns which defines
a unique combination. So, i think the optimization of the table design
should bear this in mind.
If i add a surrogate key to the table to be a primary key, then i will
need to get that column to my select queries and then do the updates
based on that column.

What would you recommend for the index of the table?|||Hi

You would need to have the surrogate key in the JOIN or WHERE clause. This
could be totally transparent to the user. As a Primary Key there will
already be an index on the surrogate key column,

It is possible a covering index on the surrogate and candidate key columns
will be useful, but like any index you should investigate if it is being
used and if the speed improvements are worth while.

John

"onder" <okeen1@.hotmail.com> wrote in message
news:9c3d3756.0309080039.65cccc67@.posting.google.c om...
> Ok about the surrogate keys. But i have an issue to ask:
> In my 'select' queries, i always have the three columns which defines
> a unique combination. So, i think the optimization of the table design
> should bear this in mind.
> If i add a surrogate key to the table to be a primary key, then i will
> need to get that column to my select queries and then do the updates
> based on that column.
> What would you recommend for the index of the table?

Sunday, March 11, 2012

complicated sql condition query

Hi,

hope someone can help or suggest something to help me with the issue.

I have a list of projects. this list contains all master (parent) and all subprojects(child). when I click on a project I want to be able to retreive information about that project and it's subprojects. here is my delema. I want my sql query to check if project is master or sub. if master then get all data for this project and its subprojects but if it's sub then get data only for that sub. below is a sample data that I hope it clear things up

parent_ID child_id Type projname

--

100 100 P parent_X_proj

100 25 C child_X_proj

100 29 C child_X_proj2

200 200 P parent_Z

300 300 P parent_Y

etc................

this is how my table is constructed. my application passes child_id and what I want is if someone clicks on parent_X_proj I want to be able to retreive the three projects (100,25,29) but if someone clicks on child project (29) then I want only that project.

so I want my query to look for the type and if my type is P (parent) then get all project where parent_id = 100(for example) but if type= C then get child_id = 29.

I know it could be done in stored procedure buy my application cannon executre SP but only sql statements.

Thank you for any help

which version of SS are you using?

declare @.child_id int

set @.child_id = 100

;with proj

as

(

select

child_id, projname, type, 1 as lvl

from

dbo.t1

where

child_id = @.child_id

union all

select

c.child_id, c.projname, c.type, p.lvl + 1 as lvl

from

dbo.t1 as c

inner join

proj as p

on p.child_id = c.parent_id

and ((p.lvl = 1 and p.type = 'P') or (p.lvl > 1 and p.type = 'C'))

)

select

*

from

proj;

AMB

P.S. not tested

|||

thank you. I'm using SS 2005. as I mentioned in my post that I can't use Stored Procedure since my application can only execute plain sql statement.

thanks

|||

AMB has given you the answer. Your application just needs to form the query and execute it.

Code Snippet

;with proj

as

(

select

child_id, projname, type, 1 as lvl

from

dbo.t1

where

child_id = <selectedValue_goes_here>

union all

select

c.child_id, c.projname, c.type, p.lvl + 1 as lvl

from

dbo.t1 as c

inner join

proj as p

on p.child_id = c.parent_id

and ((p.lvl = 1 and p.type = 'P') or (p.lvl > 1 and p.type = 'C'))

)

select * from proj;

Thursday, March 8, 2012

Complex View creation question

I need to create a cross-database view (same server) in a master database.
The databases I'm joining in the view are listed in a table in that master.
I'd like for the view to automatically include new databases whose names are
added to that table in the master. How would I go about doing this? I'm no
t
familiar with dynamic sql... TIA.Views aren't going to have dynamic SQL in them, and I wouldn't really
recommend a multi-line table user-defined function even though you could
probably accomplish what you want by using dynamic SQL in it.
I would recommend a hook in your application/middle-tier (optimally) or a
trigger on that master table (less optimal) that would ALTER the view to
include all the existing databases in your master table whenever the list of
databases changes. Just create the ALTER VIEW statement based on the list
from the master table. If done from the app, you have many ways to do this;
if from a trigger, you'd create the SQL string via a cursor or a funky
SELECT statement and then execute it via EXEC (dynamic SQL).
The things to consider in this scenario would be: what impact does changing
the view have on the live system? how frequently does this change? does
the user adding/deleting records in the master table have permissions to
alter the view?
Mike
"William Sullivan" <WilliamSullivan@.discussions.microsoft.com> wrote in
message news:AD0E4C8D-1983-4003-B8FE-09D0222548F4@.microsoft.com...
>I need to create a cross-database view (same server) in a master database.
> The databases I'm joining in the view are listed in a table in that
> master.
> I'd like for the view to automatically include new databases whose names
> are
> added to that table in the master. How would I go about doing this? I'm
> not
> familiar with dynamic sql... TIA.

Saturday, February 25, 2012

Complex Query (atleast I think so)

We have a couple of Bill of Material tables in our SQL 2000 database and I
need to extract information.
The first table is the master table and the only relevant field is fpartno.
Table two is the child table, and it contains all of the parts that make up
the fpartno in the master table. The only two relevant fields are
fcomponent and fcparent.
In the child table a component (Part No) may be in the table several times
because we may use it in different parent parts. And to complicate things,
the child table usually includes multiple levels of material.
How can I query the table so that it will pass over the table as many times
as needed to get the full multiple level bill of material.
Here would be a simple example.
PartA
PartB
PartC
PartD
PartE
PartF
As you can see the BOM for PartA goes out multiple levels. Any ideas on how
I can query this?
Thanks.Oracle has a CONNECT BY clause that makes this easy. Unfortunitly for
SQL Server you have to go thru this mess:
http://vyaskn.tripod.com/hierarchie...r_databases.htm|||SQL 2000 or SQL 2005?
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<Preacher Man> wrote in message
news:u3iA4ULvGHA.4384@.TK2MSFTNGP04.phx.gbl...
> We have a couple of Bill of Material tables in our SQL 2000 database and I
> need to extract information.
> The first table is the master table and the only relevant field is
> fpartno.
> Table two is the child table, and it contains all of the parts that make
> up the fpartno in the master table. The only two relevant fields are
> fcomponent and fcparent.
> In the child table a component (Part No) may be in the table several times
> because we may use it in different parent parts. And to complicate
> things, the child table usually includes multiple levels of material.
> How can I query the table so that it will pass over the table as many
> times as needed to get the full multiple level bill of material.
> Here would be a simple example.
> PartA
> PartB
> PartC
> PartD
> PartE
> PartF
> As you can see the BOM for PartA goes out multiple levels. Any ideas on
> how I can query this?
> Thanks.
>
>|||SQL 2000.
"Arnie Rowland" <arnie@.1568.com> wrote in message
news:%2319ACkLvGHA.4472@.TK2MSFTNGP02.phx.gbl...
> SQL 2000 or SQL 2005?
> --
> Arnie Rowland, Ph.D.
> Westwood Consulting, Inc
> Most good judgment comes from experience.
> Most experience comes from bad judgment.
> - Anonymous
>
> <Preacher Man> wrote in message
> news:u3iA4ULvGHA.4384@.TK2MSFTNGP04.phx.gbl...
>|||Perhaps these articles on recursive queries will get you moving in a
direction that works to solve your problem.
http://www.paragoncorporation.com/A...spx?ArticleID=9
http://www.yafla.com/papers/sqlhier...hierarchies.htm
http://www.wwwcoder.com/main/parent...68/default.aspx
http://msdn.microsoft.com/library/d...r />
p03i8.asp
http://www.sqlservercentral.com/col...lserver2005.asp
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<Preacher Man> wrote in message
news:eLd2X4LvGHA.4160@.TK2MSFTNGP06.phx.gbl...
> SQL 2000.
> "Arnie Rowland" <arnie@.1568.com> wrote in message
> news:%2319ACkLvGHA.4472@.TK2MSFTNGP02.phx.gbl...
>

Complex Query (atleast I think so)

We have a couple of Bill of Material tables in our SQL 2000 database and I
need to extract information.
The first table is the master table and the only relevant field is fpartno.
Table two is the child table, and it contains all of the parts that make up
the fpartno in the master table. The only two relevant fields are
fcomponent and fcparent.
In the child table a component (Part No) may be in the table several times
because we may use it in different parent parts. And to complicate things,
the child table usually includes multiple levels of material.
How can I query the table so that it will pass over the table as many times
as needed to get the full multiple level bill of material.
Here would be a simple example.
PartA
PartB
PartC
PartD
PartE
PartF
As you can see the BOM for PartA goes out multiple levels. Any ideas on how
I can query this?
Thanks.Oracle has a CONNECT BY clause that makes this easy. Unfortunitly for
SQL Server you have to go thru this mess:
http://vyaskn.tripod.com/hierarchies_in_sql_server_databases.htm|||SQL 2000 or SQL 2005?
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<Preacher Man> wrote in message
news:u3iA4ULvGHA.4384@.TK2MSFTNGP04.phx.gbl...
> We have a couple of Bill of Material tables in our SQL 2000 database and I
> need to extract information.
> The first table is the master table and the only relevant field is
> fpartno.
> Table two is the child table, and it contains all of the parts that make
> up the fpartno in the master table. The only two relevant fields are
> fcomponent and fcparent.
> In the child table a component (Part No) may be in the table several times
> because we may use it in different parent parts. And to complicate
> things, the child table usually includes multiple levels of material.
> How can I query the table so that it will pass over the table as many
> times as needed to get the full multiple level bill of material.
> Here would be a simple example.
> PartA
> PartB
> PartC
> PartD
> PartE
> PartF
> As you can see the BOM for PartA goes out multiple levels. Any ideas on
> how I can query this?
> Thanks.
>
>|||SQL 2000.
"Arnie Rowland" <arnie@.1568.com> wrote in message
news:%2319ACkLvGHA.4472@.TK2MSFTNGP02.phx.gbl...
> SQL 2000 or SQL 2005?
> --
> Arnie Rowland, Ph.D.
> Westwood Consulting, Inc
> Most good judgment comes from experience.
> Most experience comes from bad judgment.
> - Anonymous
>
> <Preacher Man> wrote in message
> news:u3iA4ULvGHA.4384@.TK2MSFTNGP04.phx.gbl...
>> We have a couple of Bill of Material tables in our SQL 2000 database and
>> I need to extract information.
>> The first table is the master table and the only relevant field is
>> fpartno.
>> Table two is the child table, and it contains all of the parts that make
>> up the fpartno in the master table. The only two relevant fields are
>> fcomponent and fcparent.
>> In the child table a component (Part No) may be in the table several
>> times because we may use it in different parent parts. And to complicate
>> things, the child table usually includes multiple levels of material.
>> How can I query the table so that it will pass over the table as many
>> times as needed to get the full multiple level bill of material.
>> Here would be a simple example.
>> PartA
>> PartB
>> PartC
>> PartD
>> PartE
>> PartF
>> As you can see the BOM for PartA goes out multiple levels. Any ideas on
>> how I can query this?
>> Thanks.
>>
>|||Perhaps these articles on recursive queries will get you moving in a
direction that works to solve your problem.
http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=9
http://www.yafla.com/papers/sqlhierarchies/sqlhierarchies.htm
http://www.wwwcoder.com/main/parentid/191/site/1857/68/default.aspx
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlpro03/html/sp03i8.asp
http://www.sqlservercentral.com/columnists/fBROUARD/recursivequeriesinsql1999andsqlserver2005.asp
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
<Preacher Man> wrote in message
news:eLd2X4LvGHA.4160@.TK2MSFTNGP06.phx.gbl...
> SQL 2000.
> "Arnie Rowland" <arnie@.1568.com> wrote in message
> news:%2319ACkLvGHA.4472@.TK2MSFTNGP02.phx.gbl...
>> SQL 2000 or SQL 2005?
>> --
>> Arnie Rowland, Ph.D.
>> Westwood Consulting, Inc
>> Most good judgment comes from experience.
>> Most experience comes from bad judgment.
>> - Anonymous
>>
>> <Preacher Man> wrote in message
>> news:u3iA4ULvGHA.4384@.TK2MSFTNGP04.phx.gbl...
>> We have a couple of Bill of Material tables in our SQL 2000 database and
>> I need to extract information.
>> The first table is the master table and the only relevant field is
>> fpartno.
>> Table two is the child table, and it contains all of the parts that make
>> up the fpartno in the master table. The only two relevant fields are
>> fcomponent and fcparent.
>> In the child table a component (Part No) may be in the table several
>> times because we may use it in different parent parts. And to
>> complicate things, the child table usually includes multiple levels of
>> material.
>> How can I query the table so that it will pass over the table as many
>> times as needed to get the full multiple level bill of material.
>> Here would be a simple example.
>> PartA
>> PartB
>> PartC
>> PartD
>> PartE
>> PartF
>> As you can see the BOM for PartA goes out multiple levels. Any ideas on
>> how I can query this?
>> Thanks.
>>
>>
>