Database design question
Author
Discussion

The Griffalo

Original Poster:

72,863 posts

270 months

Friday 24th August 2007
quotequote all
I need to get the foundations for this right so I thought I'd ask the guru's before I go too far.

I need to design a database. It will work on six sites and produce numbered permits. There are six different types of permit. Each permit in each "book" has a serial number.

As I see it I'm going to need 36 auto numbered tables to produce the permits.

IE:
TblSiteAPermit1
TblSiteAPermit2
TblSiteAPermit3
TblSiteAPermit4
TblSiteAPermit5
TblSiteAPermit6

TblSiteBPermit1

etc....

The problem with this is that there can be 36 permits each with the number 1 but they are each of a different type issued from a different location.

I can't quite get my head around the best way to deal with this.

I'm floating the idea of having an autonumber master permit table. This stores the SiteID, PermitTypeID and the number from TblSiteAPermit1.

The bit that is confusing me is when I create a new record I'll need to store the number from TblSiteAPermit1 in the master permit table but it doesn't exist at that moment in time....


How would you go about solving this?

Platform is MySQL, not that it's particularly relevant at this stage as I'm just interested in a good normalised foundation.


Plotloss

67,280 posts

301 months

Friday 24th August 2007
quotequote all
Site owns Site Permit

Site Known By Site ID

Site Permit Owned By Site (Site ID Comes in as a Primary Superordinate Key)

Site Permit Known By Permit ID Number

The Griffalo

Original Poster:

72,863 posts

270 months

Wednesday 10th October 2007
quotequote all
Bump.... Is there any way I can do this without having 36 tables?

Ultimately there could be 100 sites which would mean 600 tables yikes

grumbledoak

32,571 posts

264 months

Wednesday 10th October 2007
quotequote all
Normalise it!

Best I can tell from your post:

Table of sites (side_id, name)
Table of permit types (type_id, name)
Table of permits (site_id, type_id, permit_id)

HTH

GHW

1,294 posts

252 months

Wednesday 10th October 2007
quotequote all
One table for all the permits.

Another table that contains a rolling permit ID for each site/permit type combo?

Adding a new permit increments the permit ID in the second table, then does the insert using the new ID into the first?

Fetchez la vache

5,918 posts

245 months

Wednesday 10th October 2007
quotequote all
To me, and with the limited info, it looks like you'll need a site, permit and permit types table.

Permit types will have 6 rows, on for each type of permit.
Sites will could be auto numbered
Permits will be auto number, and have a relationship to which site this belongs to, and which type of permit it is.

This assumes that the permits require similar fields, but there's not much to go on wink
Can't really offer more without knowing more of the data & relationships... Could provide some SQL for SQL Server but again since I don't know what the columns may be it'd be a bit pointless.

what you do not (repeat, NOT) want to do is to create tables just becuase you're creating more permits or sites

hth

The Griffalo

Original Poster:

72,863 posts

270 months

Wednesday 10th October 2007
quotequote all
Thanks guys, now we're cooking biggrin

The problem is trying to run (for example) 36 auto-numbered sequential "books" for want of a better description.

My master permit table should contain four prefix digits two for the permit type and two for the location followed by six auto-numbered digits.

In an ideal world I would like to achieve the following.

Site1:
London
LO
Site2:
Aberdeen
AB
Site3:
NewYork
NY
Site4:
Glasgow
GL
Site5:
Frankfurt
FF
Site6:
Edinburgh
ED

PermitType1:
RepairCar
RC
PermitType2:
SailABoat
SB
PermitType3:
WashUp
WU
PermitType4:
RepairPipe
RP
PermitType5:
RepairFridge
RF
PermitType6:
ShagGirlfriend
SG


So I initially create a permit for London to repair a car.
Master table entry should be
LORC000001

Then I repair another car in London
LORC000002

Now I ceate the same type of permit for Aberdeen. I want the following entry in my master table.
ABRC000001

The next permit is to shag the girlfriend in Aberdeen. (Well it's been a long day!)
ABSG000001


etc.

Does that explain what I'm trying to achieve? I could end up in table hell if I don't get this bit right!!!




GnuBee

1,340 posts

246 months

Wednesday 10th October 2007
quotequote all
You need to look at a normalised structure as per grumbleoak's post. In the permit table which will have 2 foriegn keys; 1 -> to permit type and 1 -> site you will have a sequence number which you will manually increment.

To find the next sequence number for a given site and permit combination your select statement would look similar to ->

select Max(sequenceNumber)
from tbl_permitInstance
where (siteId = '<siteid>') and (permitTypeId = '<permitTypeId>')

where <siteid> is the id of the site in the site table
and <permittypeid is the id of the permit type in permit type table

Now this will work if your expected transaction and/or contention rate is low otherwise you will need to add logic to ensure that you don't attempt to add 2 sequence numbers that are the same.

There is no way you want to go down the route of n many tables for each permit type and site combination!

pdV6

16,442 posts

292 months

Wednesday 10th October 2007
quotequote all
Don't know about MySQL, but this kind of thing works ok in MS SQL:


CREATE TABLE [dbo].[Site](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Code] [varchar](2) NOT NULL,
[Name] [varchar](20) NULL,
CONSTRAINT [PK_Site] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[PermitType](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Code] [varchar](2) NOT NULL,
[Description] [varchar](50) NULL,
CONSTRAINT [PK_PermitType] PRIMARY KEY CLUSTERED ([ID] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Permit](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ptSite] [int] NOT NULL,
[ptPermitType] [int] NOT NULL,
[SiteTypeIndex] [int] NOT NULL,
[Code] [varchar](10) NOT NULL,
[Notes] [varchar](256) NULL,
CONSTRAINT [PK_Permit] PRIMARY KEY CLUSTERED ([Code] ASC) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[Permit] WITH CHECK ADD CONSTRAINT [FK_Permit_PermitType] FOREIGN KEY([ptPermitType])
REFERENCES [dbo].[PermitType] ([ID])
ALTER TABLE [dbo].[Permit] WITH CHECK ADD CONSTRAINT [FK_Permit_Site] FOREIGN KEY([ptSite])
REFERENCES [dbo].[Site] ([ID])
GO

CREATE PROCEDURE [dbo].[sp_AddPermit]
@SiteCode varchar(2),
@PermitType varchar(2),
@Notes varchar(256)

AS
BEGIN
SET NOCOUNT ON;
declare @SiteTypeIndex int
declare @PermitCode varchar(10)
declare @ptSite int
declare @ptPermitType int

select @ptSite = id from [site] where [Code]=@SiteCode
select @ptPermitType = id from [permittype] where [code]=@PermitType

select @SiteTypeIndex = max(SiteTypeIndex)+1
from Permit
where ptSite=@ptSite
and ptPermitType=@ptPermitType
if @SiteTypeIndex is null set @SiteTypeIndex=1

set @PermitCode = @SiteCode + @PermitType + right('000000' + cast(@SiteTypeIndex as varchar(6)),6)

insert into Permit(ptSite,ptPermitType,SiteTypeIndex,[Code],Notes)
values (@ptSite,@ptPermitType,@SiteTypeIndex,@PermitCode,@Notes)
END
GO

exec sp_AddPermit 'LO','RC','repair car 1'
exec sp_AddPermit 'LO','RC','repair car 2'
exec sp_AddPermit 'LO','SB','sail boat 1'
exec sp_AddPermit 'AB','RC','repair car 3'
exec sp_AddPermit 'NY','SB','sail boat 2'

select * from permit


ID ptSite ptPermitType SiteTypeIndex Code Notes
-----------------------------------------------------------------
1 6 2 1 1 ABRC000001 repair car 3
2 3 1 1 1 LORC000001 repair car 1
3 4 1 1 2 LORC000002 repair car 2
4 5 1 2 1 LOSB000001 sail boat 1
5 7 3 2 1 NYSB000001 sail boat 2


Forgot to script the adding in of the site and permit type data, but you get the picture!

Edited by pdV6 on Wednesday 10th October 13:35

grumbledoak

32,571 posts

264 months

Wednesday 10th October 2007
quotequote all
"Wot ^ they said", basically. Difficult to be more specific from here.

Best guesses:

I've never used MySQL, but you can probably lose the constraints on the identity columns. If they work as in Sybase the ids will be unique.

And if you are prepared to wrapper all table access with stored procs, dropping the foreign key constraints can make your life easier - databases enforce constraints in a pretty brutal manner, e.g. index scanning before inserting/deleting (slow), or just not allowing you to drop and re-create the tables as you develop (just plain annoying). You have to enforce consistency in your own code, but this is often easier than dealing with SQL exceptions correctly...

HTH

Plotloss

67,280 posts

301 months

Wednesday 10th October 2007
quotequote all
The Griffalo said:
Bump.... Is there any way I can do this without having 36 tables?

Ultimately there could be 100 sites which would mean 600 tables yikes
You cheeky get, the design above is right, its just not in SQL code that you can cut and paste you lazy bastard!

mystomachehurts

11,669 posts

281 months

Wednesday 10th October 2007
quotequote all
Plotloss said:
its just not in SQL code that you can cut and paste you lazy bastard!
s'alright, I just had him on the phone for 30 mins, I think he knows what needs to be done now smile

Plotloss

67,280 posts

301 months

Wednesday 10th October 2007
quotequote all
mystomachehurts said:
Plotloss said:
its just not in SQL code that you can cut and paste you lazy bastard!
s'alright, I just had him on the phone for 30 mins, I think he knows what needs to be done now smile
Tsk, young 'uns.

Codd would not be proud.

The Griffalo

Original Poster:

72,863 posts

270 months

Wednesday 10th October 2007
quotequote all
mystomachehurts said:
Plotloss said:
its just not in SQL code that you can cut and paste you lazy bastard!
s'alright, I just had him on the phone for 30 mins, I think he knows what needs to be done now smile
Services above and beyond the call of duty clapbeer

(Turns out it would have been 114 tables in the end!)

mystomachehurts

11,669 posts

281 months

Wednesday 10th October 2007
quotequote all
The Griffalo said:
Services above and beyond the call of duty clapbeer
Not at all, I'm beginning to realise that I need the help of other people on this forum to progress my projects and the success of my Company, it all boils down to Beer Token Opportunities (forever let them be known as BTOs)

It's just give and take, and the levels at which the give and take operates.

Rob, today's stuff was pretty easy, I really think it was a case of simple solution, complex rhetoric on PH trying to get you pointed in the right direction. Much easier just to pick up the phone, talk it through and get the problem sorted.

My Dad (old school - i.e. fax is a major new technology for him) did this with me the other week when he pointed out that to get anything fixed, feck email, feck faxes... just call the person and talk.

It's a direction I intend to move on. It's way easier than dicking about with emails and posts on forums.

Let's consider the options:
1. Sat face to face across a table - utterly immediate - you can watch their face, draw stuff on paper
2. Telephone: immediate, you can ask questions, cut each other off, clarify points.
3. MSN/chat: requires the other party to deeply understand the problem space
4. forums: if your question is succint: you get an answer that is probably corect, but yiu are likely to have a heap of other questions.
5. email: requires the other party to deeply understand the problem space and takes an age to iron out the details.


Hmmmm. I wonder, consider myspace, consider facebook. consider linked-in, consider PH.

There is a problem with all of this which is how to we account for our time?
I see a new social networking site that is related to people's experience but you pay to get info and the suppliers of the info get paid a bit too.

But, anyway, for now, let us all talk, it's good to talk, and I think that given the opportunity to help someone that is what we would do.










cyberface

12,214 posts

288 months

Wednesday 10th October 2007
quotequote all
Plotloss said:
mystomachehurts said:
Plotloss said:
its just not in SQL code that you can cut and paste you lazy bastard!
s'alright, I just had him on the phone for 30 mins, I think he knows what needs to be done now smile
Tsk, young 'uns.

Codd would not be proud.
Heh. smile

The Griffalo

Original Poster:

72,863 posts

270 months

Wednesday 10th October 2007
quotequote all
Ade drew me pictures and everything biggrin

thumbup