Unix Technical Forum

Integrity Constraints?

This is a discussion on Integrity Constraints? within the MySQL forums, part of the Database Server Software category; --> Hi I have two tables, User and Book, in Book is a foreign key to UserID in the User ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 09:24 AM
Lawrence
 
Posts: n/a
Default Integrity Constraints?

Hi

I have two tables, User and Book, in Book is a foreign key to UserID
in the User table. (Think of it as an extremely simple library
system).

How would I integrate Integrity Constraints so that 1 user can only
loan/belong to 6 books? I was looking into Stored Procedures/
Functions but was not sure how to go about incorporating this into the
database...

I would be extremely grateful for any help


Thanks

Lawrence

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 09:25 AM
strawberry
 
Posts: n/a
Default Re: Integrity Constraints?

On Mar 15, 9:59 am, "Lawrence" <lawrence.h...@gmail.com> wrote:
> Hi
>
> I have two tables, User and Book, in Book is a foreign key to UserID
> in the User table. (Think of it as an extremely simple library
> system).
>
> How would I integrate Integrity Constraints so that 1 user can only
> loan/belong to 6 books? I was looking into Stored Procedures/
> Functions but was not sure how to go about incorporating this into the
> database...
>
> I would be extremely grateful for any help
>
> Thanks
>
> Lawrence


I posted the following response previously - but I can't see it here
so I'll post it again. Apologies if it now appears twice...

Just thinking out loud I think it's a mistake to have the FK in Book.
Instead I'd structure it something like this:

book(book_id*,title,etc)
user(user_id*,name,allowance,no_on_loan)
loan(user_id,book_id,out_date,due_date)

Maybe a statement like this could then be used to maintain an FK
constraint. (It's a redundancy but I'm not sure if there's any way of
tying an FK to derived data):

UPDATE user SET no_on_loan = (SELECT count(*) loans FROM loan WHERE
user_id = myuser GROUP BY user_id) WHERE user_id = myuser;

FWIW, personally, I'd manage the constraint in my interface (I'm
imagining some php front-end to this db) - but that's more to do with
my general ignorance of mysql constraints than anything else.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:40 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com