Showing posts with label role. Show all posts
Showing posts with label role. Show all posts

Friday, March 23, 2012

Multipurpose Foreign Key

I've got a field [user.unitID] that can be a foreign key on the table
[Base] *OR* the table [Command] depending on the value of [User.role]
(see "Structure," below).

Question 1: Is this poor DB design?
Question2: If this is okay DB design (or, if I can't change this DB),
how do I perform a join?

I started writing a sproc (see "Beginnings of sproc," below), which
will definitely work, once I get it set up, but when I do these sorts
of things, I later come to find that there was a straight SQL way to
do it. So, is there a straight SQL way to do this, building joins with
CASEs, or something like that?

Thanks,
Jamie

## Structure (simplified) ##

[USER]
userID
unitID
role -- values can be 'B' or 'C' referring to [base] or [command] tbl

[BASE]
ID
NAME

[COMMAND]
ID
NAME

## Beginnings of a sproc ##

GO
USE myDB
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'getSessionInfo' AND type = 'P')
DROP PROCEDURE getSessionInfo
GO
USE myDB
GO
CREATE PROCEDURE getSessionInfo
@.userID varchar(50),
@.password varchar(50)
AS

DECLARE @.myUnitType varchar(2);

SELECT @.myUnitType = unitType
FROM
[user]
WHERE userID = @.userID
AND [password] = @.password

... blah blah blahJamie Jackson (wasteNOSPAMbasket@.bigfoot.com) writes:
> I've got a field [user.unitID] that can be a foreign key on the table
> [Base] *OR* the table [Command] depending on the value of [User.role]
> (see "Structure," below).
> Question 1: Is this poor DB design?

It is certainly not the plain standard design. And the method has the
apparent advantage that you can use FOREIGN-KEY constraint to enforce
the integrity, but you need to rely on triggers.

But since I know very little of your business problem, I am hesitant
to label the design as outright bad, or even poor. What I can say, is
that had I had the problem, I would definitely have looked into a solution
that would have permitted me to use DRI, but that would definitely have
been a case of fitting the solution to the tool.

> Question2: If this is okay DB design (or, if I can't change this DB),
> how do I perform a join?

Depends a little on the output, but say you want user and name of
base or command:

SELECT u.name, u.role, rolename = coalece(b.name, c.name)
FROM users u
LEFT JOIN base b ON u.role = 'B'
AND u.unitid = b.unitid
LEFT JOIN command c ON u.role = 'C'
AND u.unitid = c.unitid

An alternative is to introduce a basecommand table, to gather common
information, but I don't know enough about your business problem to
say whether this is a good idea or not.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Jamie Jackson wrote:
> I've got a field [user.unitID] that can be a foreign key on the table
> [Base] *OR* the table [Command] depending on the value of [User.role]
> (see "Structure," below).
> Question 1: Is this poor DB design?
<snip
Yes. A table represents something. In your case a User, a Base or a
Command. In your design you specify whether a User can have a relation
with a Base or not. If it can have a relation, then this means there is
a foreign key column (or a relation table) for this specific relation.
For Command you make the same decision.

So in your case, User should have one column with a foreign key
constraint to Base, and another column with a foreign key constraint to
Command. You make the relations optional by allowing NULL values in the
foreign key columns.

If this is an Object Oriented design (for example, when Command extends
from Base), then you are in trouble, because OO and RDBMS don't map very
well.

Hope this helps,
Gert-Jan|||Thanks, Erland, that SQL was a nice tutorial on filtered joins and the
coalesce function, neither of which I've ever used.

Thanks,
Jamie

On Wed, 16 Jul 2003 21:54:51 +0000 (UTC), Erland Sommarskog
<sommar@.algonet.se> wrote:

>Jamie Jackson (wasteNOSPAMbasket@.bigfoot.com) writes:
>> I've got a field [user.unitID] that can be a foreign key on the table
>> [Base] *OR* the table [Command] depending on the value of [User.role]
>> (see "Structure," below).
>>
>> Question 1: Is this poor DB design?
>It is certainly not the plain standard design. And the method has the
>apparent advantage that you can use FOREIGN-KEY constraint to enforce
>the integrity, but you need to rely on triggers.
>But since I know very little of your business problem, I am hesitant
>to label the design as outright bad, or even poor. What I can say, is
>that had I had the problem, I would definitely have looked into a solution
>that would have permitted me to use DRI, but that would definitely have
>been a case of fitting the solution to the tool.
>> Question2: If this is okay DB design (or, if I can't change this DB),
>> how do I perform a join?
>Depends a little on the output, but say you want user and name of
>base or command:
>
> SELECT u.name, u.role, rolename = coalece(b.name, c.name)
> FROM users u
> LEFT JOIN base b ON u.role = 'B'
> AND u.unitid = b.unitid
> LEFT JOIN command c ON u.role = 'C'
> AND u.unitid = c.unitid
>An alternative is to introduce a basecommand table, to gather common
>information, but I don't know enough about your business problem to
>say whether this is a good idea or not.

Monday, February 20, 2012

Multiple roles?

Hello. I was wondering if someone could help me understand better about
assigning a user to multiple roles. I created a role for some developers
from another group to be able to to DTS over some data (select, insert,
delete, update) into our local tables from their sql server. I also
assigned them to the db_ddladmin role. They tried to DTS our new data and
looked like they were able to delete the data in our local tables but their
bulk insert command to insert the data wasn't successful. I read and
thought that when assigning a user multiple roles the role with the most
priveleges takes in effect?
Thanks in advance.Yes, rights of several roles accumulate for a user, but db_ddladmin does not
grant BULK INSERT rights.
They need to be members of the bulkadmin server role in order to run BULK
INSERT in SS2000. In SS2005 you need ADMINISTER BULK OPERATIONS, plus
insert rights to the table and you may need ALTER TABLE permissions in some
cases.
See the Permissions subheading on the BULK INSERT command in either version
of Books Online.
RLF
"zz12" <IDontLikeSpam@.Nowhere.com> wrote in message
news:uaHlkYSfHHA.444@.TK2MSFTNGP04.phx.gbl...
> Hello. I was wondering if someone could help me understand better about
> assigning a user to multiple roles. I created a role for some developers
> from another group to be able to to DTS over some data (select, insert,
> delete, update) into our local tables from their sql server. I also
> assigned them to the db_ddladmin role. They tried to DTS our new data and
> looked like they were able to delete the data in our local tables but
> their bulk insert command to insert the data wasn't successful. I read
> and thought that when assigning a user multiple roles the role with the
> most priveleges takes in effect?
> Thanks in advance.
>|||zz12 (IDontLikeSpam@.Nowhere.com) writes:
> Hello. I was wondering if someone could help me understand better about
> assigning a user to multiple roles. I created a role for some
> developers from another group to be able to to DTS over some data
> (select, insert, delete, update) into our local tables from their sql
> server. I also assigned them to the db_ddladmin role. They tried to
> DTS our new data and looked like they were able to delete the data in
> our local tables but their bulk insert command to insert the data wasn't
> successful. I read and thought that when assigning a user multiple
> roles the role with the most priveleges takes in effect?
I would guess that rather all privs are unioned together, and DENY
takes precendence over GRANT.
In any case, BULK INSERT requires membership in the bulkadmin server role,
which you don't seemed to have assign to your users.
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Will look into it further. Thanks a bunch for both of your replies.
Totally appreciate it.
Take care.
"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns99116B91E56CYazorman@.127.0.0.1...
> zz12 (IDontLikeSpam@.Nowhere.com) writes:
> I would guess that rather all privs are unioned together, and DENY
> takes precendence over GRANT.
> In any case, BULK INSERT requires membership in the bulkadmin server role,
> which you don't seemed to have assign to your users.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx