Showing posts with label model. Show all posts
Showing posts with label model. Show all posts

Tuesday, March 27, 2012

Data Source View and Report Model

Guys,

I created a Data Source View that uses two different Data Source connections to two different databases. I was able to drop in the tables in the data source view designer from the two databases, but when I try to create the Report Model based on that data source view, it's complaining that it can't find table in the non-primary data source from the data source view.

Here is the error I'm getting:

An error occurred while executing a command.

Message: Invalid object name 'dbo.[tablename]'

Command:

SELECT COUNT(*) FROM [dbo].[tablename] t

Why can't it find the database to connect to since the data source view is already creating using two different databases. No problem in the data source view, but when it actually becomes a model, it complains. How come it can't find one of the tables in a different database?

Please help and any help is appreciated.

Enkh.

semantic queries currently do not support multiple data sources. you can create named query that would reference table from another database using db.schema.tablename notation.

Data Source For Report Model Project

When I tried to generate ad-hoc reports with Microsoft reporting services
2005 (april CTP), it generally ask for the tables and views. Is there a way
that I can just put in a stored procedure and use the results as a data
feeder for the report model? Thanks.I don't think this is supported out of the box
You can however create a named query as part of the data source view
"Geek" wrote:
> When I tried to generate ad-hoc reports with Microsoft reporting services
> 2005 (april CTP), it generally ask for the tables and views. Is there a way
> that I can just put in a stored procedure and use the results as a data
> feeder for the report model? Thanks.sql

Tuesday, March 20, 2012

Data Purging

Hi

We have a requirement for purging the historical data based on the specified criteria.We have a complex data model with Parent Child Hirearchy goes to multiple levels.

Does any body knows information on how data purging is done in production environments with characteristics such as

Data Volume is huge (500 GB)

Multiple level of realtionships (It can go up to 6 levels)

Physical relationship exists between tables

thanks in advance :)


Regards
Samsudeen B

My 20 cents worth:

One of the reasons data purging may be a pain is the dilemma of associated indexes. You need them to locate the records to delete and they can make the whole process slow because they may have to be rebuilt (not forgetting any locking issues).

The other issue would be whether you have cascading referential integrity set up.

I would not embark on an all-at-once deletion. I would do small/medium range deletions and I might also disable any indexes on the underlying tables. I you have cascading referential integrity, all you have to do is just delete the relevant parent records.

DELETE FROM parent_table

WHERE date_column BETWEEN a AND b

GO

DELETE FROM parent_table

WHERE date_column BETWEEN b AND c

GO

.......................

........................

DELETE FROM parent_table

WHERE date_column BETWEEN y AND z

|||

Hi Nathan,

Thanks for the response.

I don't have the cascading referential integrity setup in my model.As I told earlier my model is complex with child tables been refered with multiple foreign keys..

Do you have any idea on how these kind of situations handled in pruging data in production environment.

Thanks,

Samsudeen B

|||

Without cascading referential integrity, you will have to delete data in the correct order to avoid constraint violations. Another headache.

Anyway, in a production environment, you will have to be much more cautious as it is very easy to slow down the server to a crawl due to table locking and excessive transaction log growth.

Here is what I have done before:

1. Script out the DROP and CREATE index statements for the tables concerned.

2. Script out the DROP and CREATE foreign key constraints for all child tables.

3. Make a script containing piece-meal delete statements (deleting a few thousand rows per table per batch starting with the child tables first). You need to understand the dependencies between your tables

During my "off-peak" hours

4. Change the database recovery model to "simple"

5. Disable all Triggers on the tables.

6. Drop the indexes and foreign keys (all in a script)

7. Run the "delete" script. Delete as many rows as possible within my "free" time window.

8. Enable the triggers

9. Run the script to recreate the indexes for the tables

10. Shrink the log file (DBCC SHRINKFILE)

11. Revert to "Full" Recovery model

12. Run a script to update statistics on the tables.

You don't have to delete all the data all at once. You can do this as many times as necessary if you have a very busy server. And you can put all that work (4-12) into a script and make some coffee. My server is not used for at least 4 hours in the night.

If your server never "chills out" then a carefully scripted delete operation and a simple recovery model may be of help. You may also save yourself a few headaches by adding cascading referential integrity to the tables. That way you script only the delete statements for the parent tables.

Sunday, March 11, 2012

Data Modelling Question with Cascade Deletes

Hi, I have this scenario that keeps coming up on different models and
I'm yet to find a solution.
In this model I have Customers, Customer Recognitions, Sectors and
Divisions.
Sectors and Divisions are related and reference data.
Customers become recognized in sectors, and then within divisions in
that sector.
The problems I have are these:
1) I need to be able to delete data at all levels & can create all but
1 of the cascade rules - because of circular references. In this case
when I delete a Sector I would want to Cascade and delete all related
CustomerSectors
2) Its feels to me like an unnecessary relationships exists in the
database because of the circular design - but I cant decide how and
exactly to remove a relationship.
Can anyone advise?.
Thanks.
CREATE TABLE [dbo].[CustomerSectorDivision] (
[CSD_ID] [smallint] NOT NULL ,
[D_ID] [smallint] NOT NULL ,
[CS_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Customer] (
[C_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[CustomerRecognition] (
[CR_ID] [smallint] NOT NULL ,
[C_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[CustomerSector] (
[CS_ID] [smallint] NOT NULL ,
[CR_ID] [smallint] NOT NULL ,
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Division] (
[D_ID] [smallint] NOT NULL ,
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Sector] (
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSectorDivision] ADD
CONSTRAINT [PK_CustomerSectorDivision] PRIMARY KEY CLUSTERED
(
[CSD_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Customer] ADD
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[C_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerRecognition] ADD
CONSTRAINT [PK_CustomerRecognition] PRIMARY KEY CLUSTERED
(
[CR_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSector] ADD
CONSTRAINT [PK_CustomerSector] PRIMARY KEY CLUSTERED
(
[CS_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Division] ADD
CONSTRAINT [PK_Division] PRIMARY KEY CLUSTERED
(
[D_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Sector] ADD
CONSTRAINT [PK_Sector] PRIMARY KEY CLUSTERED
(
[S_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSectorDivision] ADD
CONSTRAINT [FK_CustomerSectorDivision_CustomerSecto
r] FOREIGN KEY
(
[CS_ID]
) REFERENCES [dbo].[CustomerSector] (
[CS_ID]
) ON DELETE CASCADE ,
CONSTRAINT [FK_CustomerSectorDivision_Division] FOREIGN KEY
(
[D_ID]
) REFERENCES [dbo].[Division] (
[D_ID]
) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerRecognition] ADD
CONSTRAINT [FK_CustomerRecognition_Customer] FOREIGN KEY
(
[C_ID]
) REFERENCES [dbo].[Customer] (
[C_ID]
) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerSector] ADD
CONSTRAINT [FK_CustomerSector_CustomerRecognition] FOREIGN KEY
(
[CR_ID]
) REFERENCES [dbo].[CustomerRecognition] (
[CR_ID]
) ON DELETE CASCADE ,
CONSTRAINT [FK_CustomerSector_Sector] FOREIGN KEY
(
[S_ID]
) REFERENCES [dbo].[Sector] (
[S_ID]
)
GO
ALTER TABLE [dbo].[Division] ADD
CONSTRAINT [FK_Division_Sector] FOREIGN KEY
(
[S_ID]
) REFERENCES [dbo].[Sector] (
[S_ID]
) ON DELETE CASCADE
GOIf customers are in sectors and sectors are in divisions, then the
CustomerSectorDivision table breaks normal form.
Please elaborate on the actual (real-life, not current database model)
relationships between the entities.
ML
http://milambda.blogspot.com/|||Remove the FK from CustomerSector to Sector.
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
.
"hals_left" <cc900630@.ntu.ac.uk> wrote in message
news:1138707797.465718.285110@.g47g2000cwa.googlegroups.com...
Hi, I have this scenario that keeps coming up on different models and
I'm yet to find a solution.
In this model I have Customers, Customer Recognitions, Sectors and
Divisions.
Sectors and Divisions are related and reference data.
Customers become recognized in sectors, and then within divisions in
that sector.
The problems I have are these:
1) I need to be able to delete data at all levels & can create all but
1 of the cascade rules - because of circular references. In this case
when I delete a Sector I would want to Cascade and delete all related
CustomerSectors
2) Its feels to me like an unnecessary relationships exists in the
database because of the circular design - but I cant decide how and
exactly to remove a relationship.
Can anyone advise?.
Thanks.
CREATE TABLE [dbo].[CustomerSectorDivision] (
[CSD_ID] [smallint] NOT NULL ,
[D_ID] [smallint] NOT NULL ,
[CS_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Customer] (
[C_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[CustomerRecognition] (
[CR_ID] [smallint] NOT NULL ,
[C_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[CustomerSector] (
[CS_ID] [smallint] NOT NULL ,
[CR_ID] [smallint] NOT NULL ,
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Division] (
[D_ID] [smallint] NOT NULL ,
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Sector] (
[S_ID] [smallint] NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSectorDivision] ADD
CONSTRAINT [PK_CustomerSectorDivision] PRIMARY KEY CLUSTERED
(
[CSD_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Customer] ADD
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[C_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerRecognition] ADD
CONSTRAINT [PK_CustomerRecognition] PRIMARY KEY CLUSTERED
(
[CR_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSector] ADD
CONSTRAINT [PK_CustomerSector] PRIMARY KEY CLUSTERED
(
[CS_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Division] ADD
CONSTRAINT [PK_Division] PRIMARY KEY CLUSTERED
(
[D_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Sector] ADD
CONSTRAINT [PK_Sector] PRIMARY KEY CLUSTERED
(
[S_ID]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[CustomerSectorDivision] ADD
CONSTRAINT [FK_CustomerSectorDivision_CustomerSecto
r] FOREIGN KEY
(
[CS_ID]
) REFERENCES [dbo].[CustomerSector] (
[CS_ID]
) ON DELETE CASCADE ,
CONSTRAINT [FK_CustomerSectorDivision_Division] FOREIGN KEY
(
[D_ID]
) REFERENCES [dbo].[Division] (
[D_ID]
) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerRecognition] ADD
CONSTRAINT [FK_CustomerRecognition_Customer] FOREIGN KEY
(
[C_ID]
) REFERENCES [dbo].[Customer] (
[C_ID]
) ON DELETE CASCADE
GO
ALTER TABLE [dbo].[CustomerSector] ADD
CONSTRAINT [FK_CustomerSector_CustomerRecognition] FOREIGN KEY
(
[CR_ID]
) REFERENCES [dbo].[CustomerRecognition] (
[CR_ID]
) ON DELETE CASCADE ,
CONSTRAINT [FK_CustomerSector_Sector] FOREIGN KEY
(
[S_ID]
) REFERENCES [dbo].[Sector] (
[S_ID]
)
GO
ALTER TABLE [dbo].[Division] ADD
CONSTRAINT [FK_Division_Sector] FOREIGN KEY
(
[S_ID]
) REFERENCES [dbo].[Sector] (
[S_ID]
) ON DELETE CASCADE
GO|||Because there are two paths to CustomerSectorDivision, this cascading delete
scenario isn't supported. I prefer to avoid all cascading referential
actions because they cede control over the order in which locks are
obtained, which can make it more difficult to minimize deadlocks.
As far as I can tell, there aren't any unnecessary relationships in this
model. Cascading referential actions in SQL Server require that the
relationships form a tree, but networks can and do occur in perfectly valid
models.
"hals_left" <cc900630@.ntu.ac.uk> wrote in message
news:1138707797.465718.285110@.g47g2000cwa.googlegroups.com...
> Hi, I have this scenario that keeps coming up on different models and
> I'm yet to find a solution.
> In this model I have Customers, Customer Recognitions, Sectors and
> Divisions.
> Sectors and Divisions are related and reference data.
> Customers become recognized in sectors, and then within divisions in
> that sector.
> The problems I have are these:
> 1) I need to be able to delete data at all levels & can create all but
> 1 of the cascade rules - because of circular references. In this case
> when I delete a Sector I would want to Cascade and delete all related
> CustomerSectors
> 2) Its feels to me like an unnecessary relationships exists in the
> database because of the circular design - but I cant decide how and
> exactly to remove a relationship.
> Can anyone advise?.
> Thanks.
> CREATE TABLE [dbo].[CustomerSectorDivision] (
> [CSD_ID] [smallint] NOT NULL ,
> [D_ID] [smallint] NOT NULL ,
> [CS_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Customer] (
> [C_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[CustomerRecognition] (
> [CR_ID] [smallint] NOT NULL ,
> [C_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[CustomerSector] (
> [CS_ID] [smallint] NOT NULL ,
> [CR_ID] [smallint] NOT NULL ,
> [S_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Division] (
> [D_ID] [smallint] NOT NULL ,
> [S_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[Sector] (
> [S_ID] [smallint] NOT NULL
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[CustomerSectorDivision] ADD
> CONSTRAINT [PK_CustomerSectorDivision] PRIMARY KEY CLUSTERED
> (
> [CSD_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[Customer] ADD
> CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
> (
> [C_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[CustomerRecognition] ADD
> CONSTRAINT [PK_CustomerRecognition] PRIMARY KEY CLUSTERED
> (
> [CR_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[CustomerSector] ADD
> CONSTRAINT [PK_CustomerSector] PRIMARY KEY CLUSTERED
> (
> [CS_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[Division] ADD
> CONSTRAINT [PK_Division] PRIMARY KEY CLUSTERED
> (
> [D_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[Sector] ADD
> CONSTRAINT [PK_Sector] PRIMARY KEY CLUSTERED
> (
> [S_ID]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[CustomerSectorDivision] ADD
> CONSTRAINT [FK_CustomerSectorDivision_CustomerSecto
r] FOREIGN KEY
> (
> [CS_ID]
> ) REFERENCES [dbo].[CustomerSector] (
> [CS_ID]
> ) ON DELETE CASCADE ,
> CONSTRAINT [FK_CustomerSectorDivision_Division] FOREIGN KEY
> (
> [D_ID]
> ) REFERENCES [dbo].[Division] (
> [D_ID]
> ) ON DELETE CASCADE
> GO
> ALTER TABLE [dbo].[CustomerRecognition] ADD
> CONSTRAINT [FK_CustomerRecognition_Customer] FOREIGN KEY
> (
> [C_ID]
> ) REFERENCES [dbo].[Customer] (
> [C_ID]
> ) ON DELETE CASCADE
> GO
> ALTER TABLE [dbo].[CustomerSector] ADD
> CONSTRAINT [FK_CustomerSector_CustomerRecognition] FOREIGN KEY
> (
> [CR_ID]
> ) REFERENCES [dbo].[CustomerRecognition] (
> [CR_ID]
> ) ON DELETE CASCADE ,
> CONSTRAINT [FK_CustomerSector_Sector] FOREIGN KEY
> (
> [S_ID]
> ) REFERENCES [dbo].[Sector] (
> [S_ID]
> )
> GO
> ALTER TABLE [dbo].[Division] ADD
> CONSTRAINT [FK_Division_Sector] FOREIGN KEY
> (
> [S_ID]
> ) REFERENCES [dbo].[Sector] (
> [S_ID]
> ) ON DELETE CASCADE
> GO
>|||It appears to me that divisions are in sectors. I see nothing wrong with
the model.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:C08CF6D6-993B-4C78-9AD0-C6EC4CE9BCBA@.microsoft.com...
> If customers are in sectors and sectors are in divisions, then the
> CustomerSectorDivision table breaks normal form.
> Please elaborate on the actual (real-life, not current database model)
> relationships between the entities.
>
> ML
> --
> http://milambda.blogspot.com/|||I don't see what the CustomerSectorDivision is there for. The relationship i
s
already defined thorugh the CustomerSector and Division tables, isn't it?
ML
http://milambda.blogspot.com/|||Sectors are areas of industry, Divisions are just sub-sectors within
e.g Finance has several divisions Basic Accounts, Taxation, Assets &
Liabilities. Customers s recognition first in a sector and then for
divisions within the sector.
A customer cannot have recognition for a division without the
corresponsing sector.|||Not necessarily. Since there's a 1:0..n relationship between Sector and
Division, it follows that there could also be a 1:0..n relationship between
CustomerSector and Division. CustomerSectorDivision provides a place to
store information that is related only to the juxtaposition of a
CustomerSector and a Division.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:DEB5E66A-CE4D-43A3-AA63-7A2CB1A15CF1@.microsoft.com...
>I don't see what the CustomerSectorDivision is there for. The relationship
>is
> already defined thorugh the CustomerSector and Division tables, isn't it?
>
> ML
> --
> http://milambda.blogspot.com/|||I see. Now I understand. However, I'd try to further isolate individual
relationships.
Entities:
Sectors
Divisions
Customers
Relationships:
Sector <-- Customer (actual relationship, but I wouldn't implement it
directly in the physical model)
Sector <-- Division
Sector <-- Division <-- Customer (actual relationship, but I wouldn't
implement it directly)
Tables:
Sectors (SectorId : ...)
Divisions (DivisionId : ...)
Customers (CustomerId : ...)
SectorDivisions (SectorDivisionId : SectorId : DivisionId)
DivisionCustomers (SectorDivisionId : CustomerId)
Views:
SectorDivisionCustomers (SectorId : DivisionId : CustomerId)
SectorCustomers (SectorId : CustomerId)
I believe this model supports cascading foreign keys better. But one
question remains - can a Customer belong to a Sector without belonging to a
Division?
ML
http://milambda.blogspot.com/|||The relationship between a Customer and a Sector is implied by the foreign
key constraint between CustomerSector and CustomerRecognition and the
foreign key constraint between CustomerRecognition and Customer.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:002BDCD5-0F8E-4BBE-A4AC-1F996289717F@.microsoft.com...
> That is if the direct relationship between a Customer and a Sector is
> valid.
>
> ML
> --
> http://milambda.blogspot.com/

Data Modeling question

We are wanting to model standards for the equipment. But for each
equipment, there may be a completely different set of characteristics
that make up its standard. There's more to it than just this, but
let's keep it simple here. What would be clear and obvious to anyone
who's done even a basic Access database would be to have all of these
characteristics (and I mean all of them across all the different
equipment) as columns in one huge standards table. But wait - we
could have hundreds of characteristics, and most importantly, the users
will (really, just accept this fact PLEASE) want to change or at least
add more characteristics over time. This option is a nightmare and we
(me and the project manager overseeing this) agree it as such. So
what's flexible?
I came up with one and know it will work and is normalized and very
relational, BUT it's a slight bear getting the data out and once
it's out it cannot be easily joined with anything (you will see why
in a bit).
A three table structure - Standards, StandardCharacteristics and
StandardValues. Visual picture available here -->
http://www.flickr.com/photos/ccjjharmon/85629981/
"Standards" holds the standards records that change over time, but
these records don't hold ANY of the values - they are just the
placeholder.
"StandardCharacteristics" holds the names and units for the
characteristics and can be associated to multiple pieces of equipment
(separate table to encapsulate this - not shown though).
"StandardValues" holds the meat (and by far the most records) by
containing the characteristic and value pair per record. A set of these
records makes up the standards when joined with the Standards record
associated.
So this does work for data storage - but getting the data out is a
little challenging. A crosstab(based on this article:
http://www.windowsitpro.com/SQLServ...5608/15608.html)
was used to flip StandardValues rows into columns in addition to the
Standards columns.
OK - so that's my design. What other options are out there? Any
thoughts on my design? Is it good? Bad?Look up all the problems with EAV (Entity-Attribute-Value) designs. I
worked for a company that did this all the time; it falls apart in a
year of production work, eats up storage and the simplest queries
become nightmares.
Also, you might want to learn the diffrences between rows and records.
It will help you to think in a proper data model.|||Is this SQL 2005?
If so, it sounds like a great candidate for storing part of your data in XML
.
If it is not 2005, I wouldn't recommend using XML. But, in 2005 the support
is solid, and the use case of when to use it is for cases when you have a
variable set of attributes for each row. It seems that storing your
standards as XML would be worth exploring.
HTH
--
Ryan Powers
Clarity Consulting
http://www.claritycon.com
" ccjjharmonDOTREMOVE@.dotRemovedotgmaildot
" wrote:

> We are wanting to model standards for the equipment. But for each
> equipment, there may be a completely different set of characteristics
> that make up its standard. There's more to it than just this, but
> let's keep it simple here. What would be clear and obvious to anyone
> who's done even a basic Access database would be to have all of these
> characteristics (and I mean all of them across all the different
> equipment) as columns in one huge standards table. But wait - we
> could have hundreds of characteristics, and most importantly, the users
> will (really, just accept this fact PLEASE) want to change or at least
> add more characteristics over time. This option is a nightmare and we
> (me and the project manager overseeing this) agree it as such. So
> what's flexible?
> I came up with one and know it will work and is normalized and very
> relational, BUT it's a slight bear getting the data out and once
> it's out it cannot be easily joined with anything (you will see why
> in a bit).
> A three table structure - Standards, StandardCharacteristics and
> StandardValues. Visual picture available here -->
> http://www.flickr.com/photos/ccjjharmon/85629981/
> "Standards" holds the standards records that change over time, but
> these records don't hold ANY of the values - they are just the
> placeholder.
> "StandardCharacteristics" holds the names and units for the
> characteristics and can be associated to multiple pieces of equipment
> (separate table to encapsulate this - not shown though).
> "StandardValues" holds the meat (and by far the most records) by
> containing the characteristic and value pair per record. A set of these
> records makes up the standards when joined with the Standards record
> associated.
> So this does work for data storage - but getting the data out is a
> little challenging. A crosstab(based on this article:
> http://www.windowsitpro.com/SQLServ...5608/15608.html)
> was used to flip StandardValues rows into columns in addition to the
> Standards columns.
> OK - so that's my design. What other options are out there? Any
> thoughts on my design? Is it good? Bad?
>|||It's a well know approach. I've seen it being implemened many times. To
my best knowledge, for reasonable amounts of data it's always a total
and miserable failure - the performance is horrible. You can get a
great explanation why in one of Tom Kyte's books.
I would set up a regular table with regular columns for the attributes
all the models have.
For the flexible part, I would first of all consider storing them as
XML.|||* Thanks for the responses.
I will look into EAV --> any particular references you could point out
to refer to?
What is the difference between a row and a record? I would imagine for
my scenario that a complete "Standard" row consists of one Standards
record along with multiple StandardValues records (one for every
recorded StandardCharacteristic). Is there more?
It *could* be in SQL Server 2005 possibly. We did discuss using XML, as
we attended the SQL Server 2005 Launch locally and even talked about
this with a MS expert. We didn't like it however because we do
reference the StandardCharacteristics with other data tables.
What I haven't mentioned (because I hadn't thought of it, but my
product manager mentioned a few minutes ago) was that reporting on this
data is the most important piece to the database model design.
The standards are only one piece of the reporting puzzle - we also are
recording the actual values occurring on the floor, and we need to
report on the differences between these values and the associated
standards. Wouldn't the XML data storage idea be bad for this?
Lastly, using one big regular table with all the attributes as columns
I said was NOT what we wanted to do and is explained in my initial
post.
I will look into Tom Kyte and his books.
So it appears I came up with this EAV design, but it looks like *no
one* recommends it. When if at all should this design be used?
And if never, what is better other than one big table?|||ccjjharmonDOTREMOVE@.dotRemovedotgmaildot
com wrote:
> * Thanks for the responses.
> I will look into EAV --> any particular references you could point out
> to refer to?
You may find the following articles useful:
Generic Design of Web-Based Clinical Databases
http://www.jmir.org/2003/4/e27/
The EAV/CR Model of Data Representation
http://ycmi.med.yale.edu/nadkarni/eav_CR_contents.htm
An Introduction to Entity-Attribute-Value Design for Generic Clinical Study
Data Management Systems
http://ycmi.med.yale.edu/nadkarni/I...V%20systems.htm
Data Extraction and Ad Hoc Query of an Entity Attribute Value Database
[url]http://www.pubmedcentral.nih.gov/articlerender.fcgi?tool=pubmed&pubmedid=9824799[/
url]
Exploring Performance Issues for a Clinical Database Organized Using an Enti
ty-Attribute-Value Representation
[url]http://www.pubmedcentral.nih.gov/articlerender.fcgi?tool=pubmed&pubmedid=10984467[
/url]|||Excellent links. I believe you (and the medical industry in general)
are the only one so far who says EAV has a place in real-world database
modeling. Does anyone (or industry) else use this model?|||ccjjharmonDOTREMOVE@.dotRemovedotgmaildot
com wrote:
> Excellent links. I believe you (and the medical industry in general)
> are the only one so far who says EAV has a place in real-world database
> modeling. Does anyone (or industry) else use this model?
I asked this question about a year ago in this group. If I remember
correctly, everybody here who participated in the discussion was
against the approach, but still didn't give a better solution for
this particular requirement: unknown number of attributes at design
time. Looks like SQL Server (or any relational database) is not
the best instrument to handle this kind of data, but still can be
used to store the data if you design your model carefully.|||On 17 Jan 2006 11:20:48 -0800,
ccjjharmonDOTREMOVE@.dotRemovedotgmaildot
com wrote:
(snip)
>What is the difference between a row and a record?
Hi ccjjharmon,
Better ignore that part of CELKO's message. He is stuck in the 70s when
people used the terms "field" and "record" in sequential files and
non-relational databases, while relational databases used the words
"column" and "row" for the corresponding parts of the relational model.
Joe (Celko, that is) seems to have missed that lots have changed over
the past 30 years, and that many books on relational databases,
including some good books, use these terms interchangeably.
Think of it as an old man shouting "it's called an automobile, young
lad, not a car!"

>What I haven't mentioned (because I hadn't thought of it, but my
>product manager mentioned a few minutes ago) was that reporting on this
>data is the most important piece to the database model design.
All the more reason to not touch EAV with a 10-foot stick. Include 10
columns in your report, and you have a ten-fold self-join. Not fun.

>The standards are only one piece of the reporting puzzle - we also are
>recording the actual values occurring on the floor, and we need to
>report on the differences between these values and the associated
>standards. Wouldn't the XML data storage idea be bad for this?
I haven't had a chance to play with SQL Server 2005's XML options yet.
My guess, though, is that the overhead of XML parsing and data type
conversion will hurt your performance.

>So it appears I came up with this EAV design, but it looks like *no
>one* recommends it. When if at all should this design be used?
I'd only consider it for attributes that are typed in by the user,
stored in the DB (without ANY checking!!) and displayed on screen when
the user displays the same row again. And even in those cases, I'd have
a strong desire to use a better design.

>Lastly, using one big regular table with all the attributes as columns
>I said was NOT what we wanted to do and is explained in my initial
>post.
(...)
>And if never, what is better other than one big table?
Two slightly smaller tables? :-))
Serious though. I agree with you that one big table is probably a bad
design. Instead, you should sit down with a subject matter expert, find
out the functional dependencies between the attributes, then normalize
to at least third normal form. If, after that, you still have one big
table, yoou can safely conclude that one big table IS the best option.
I know it's not the answer you want to hear, but I don't think there are
any better answers.
Hugo Kornelis, SQL Server MVP|||Sericinus - you said you were looking at using this EAV model a year
ago - what did you end up using for a model?
Hugo - thanks for the detailed response. We had worked around the
self-joining through a crosstab stored procedure - and found the
performance to appear to be OK. With all of these comments however, I
honestly am seriously questioning using this model even after my
testing found it should be able to work. If I didn't point it out
before though, the only to get to the data in what would appear to be a
complete Standards table (with the values and characteristics
cross-tabbed) is via this crosstab stored procedure - and then we can't
reference in a SELECT statement (e.g. SELECT * FROM uspStandards), can
we? No. This is a hindrance.
I still want to push back further before making the decision... and
towards that - what I don't specifically understand is the drawbacks
that are listed for EAV and how they might apply to our usage of this
model. We are asking experts in the area for their feedback on this
specific need and usage of this model. At that point, we would probably
try to "find out the functional dependencies between the attributes,
then normalize to at least third normal form" -- whatever that means.
Hugo - can you put this into more understandable language? I know SQL
fairly well, but am not up on that side of the theory and syntax (Nth
normal form).
We were discussing these comments and what seems possible as another
data model is to use a single large [flattened] table with a set of
characteristic columns (A, B, C, D, etc.) and have within some table a
"decoder" that maps these columns to what they actually are. Does
anyone know well the XML capabilities in SQL 05?
Chris Harmon

Thursday, March 8, 2012

Data Model: TypeCode Reference Tables

Hello World,
I have two two typcode reference tables with the following DDL:
CREATE TABLE [dbo].[LanguageType](
[Description] [varchar](255) NULL,
[LanguageTypeCode][char](6)NOT NULL
)
GO
ALTER TABLE dbo.LanguageType ADD CONSTRAINT
PK_LanguageType PRIMARY KEY CLUSTERED
(
LanguageTypeCode
)
GO
CREATE TABLE [dbo].[BookType](
[Description] [varchar](255) NULL,
[BookTypeCode] [char](6)NOT NULL
)
GO
ALTER TABLE dbo.BookType ADD CONSTRAINT
PK_BookType PRIMARY KEY CLUSTERED
(
BookTypeCode
)
GO
I need to create a template that has a referential constraints check against
the primary key of EITHER table above. For example,
ALTER TABLE dbo.Template ADD CONSTRAINT
PK_Template PRIMARY KEY CLUSTERED
(
TemplateID,
TypeCode -- Either LanguageTypeCode or BookTypeCode
)
What are the options that I have to implement this? I thought about
combining the Language and Book TypeCodes into one table, but we think that
would only complicate our data model. Please comment.Add a "parent" Table that has both TypeCoeds in it
Create Table TemplateTypeCode -- You can think of better name for this
abstraction
(TypeCode Char(6( Primary Key Not Null)
And alter both pf your other tables
Alter Table dbo.LanguageType
ADD CONSTRAINT FKTypeCode Foreign Key (LanguageTypeCode)
references TemplateTypeCode(TypeCode)
Alter Table dbo.BookType
ADD CONSTRAINT FKTypeCode Foreign Key (BookTypeCode)
references TemplateTypeCode(TypeCode)
Then create constraint on Template table agaist TemplateTypeCode Table
ALTER TABLE dbo.Template ADD CONSTRAINT
FK_TypeCodes (TypeCode)
references TemplateTypeCode(TypeCode)
"C TO" wrote:

> Hello World,
> I have two two typcode reference tables with the following DDL:
> CREATE TABLE [dbo].[LanguageType](
> [Description] [varchar](255) NULL,
> [LanguageTypeCode][char](6)NOT NULL
> )
> GO
> ALTER TABLE dbo.LanguageType ADD CONSTRAINT
> PK_LanguageType PRIMARY KEY CLUSTERED
> (
> LanguageTypeCode
> )
> GO
> CREATE TABLE [dbo].[BookType](
> [Description] [varchar](255) NULL,
> [BookTypeCode] [char](6)NOT NULL
> )
> GO
> ALTER TABLE dbo.BookType ADD CONSTRAINT
> PK_BookType PRIMARY KEY CLUSTERED
> (
> BookTypeCode
> )
> GO
> I need to create a template that has a referential constraints check again
st
> the primary key of EITHER table above. For example,
> ALTER TABLE dbo.Template ADD CONSTRAINT
> PK_Template PRIMARY KEY CLUSTERED
> (
> TemplateID,
> TypeCode -- Either LanguageTypeCode or BookTypeCode
> )
>
> What are the options that I have to implement this? I thought about
> combining the Language and Book TypeCodes into one table, but we think tha
t
> would only complicate our data model. Please comment.|||Thanks. Wouldn't this create more repeated data? How would you compare
creating a parent table with creating trigger to enforce referential
integrity?
"CBretana" wrote:
> Add a "parent" Table that has both TypeCoeds in it
> Create Table TemplateTypeCode -- You can think of better name for this
> abstraction
> (TypeCode Char(6( Primary Key Not Null)
> And alter both pf your other tables
> Alter Table dbo.LanguageType
> ADD CONSTRAINT FKTypeCode Foreign Key (LanguageTypeCode)
> references TemplateTypeCode(TypeCode)
> Alter Table dbo.BookType
> ADD CONSTRAINT FKTypeCode Foreign Key (BookTypeCode)
> references TemplateTypeCode(TypeCode)
> Then create constraint on Template table agaist TemplateTypeCode Table
> ALTER TABLE dbo.Template ADD CONSTRAINT
> FK_TypeCodes (TypeCode)
> references TemplateTypeCode(TypeCode)
>
> "C TO" wrote:
>|||A thing is either a type or a code, but not both. You also might want
to consider not blindly assigning columns a VARCHAR(255).
CREATE TABLE Languages
(language_code CHAR(3) NOT NULL PRIMARY KEY, -- ISO Standared 3166
language_name CHAR(15) NOT NULL);
CREATE TABLE BookTypes
( book_type CHAR(6) NOT NULL PRIMARY KEY, -- ABA codes? shop
categories?
book_description VARCHAR(25) NOT NULL);
against the primary key of EITHER table above. For example, ...
TypeCode -- Either LanguageTypeCode or BookTypeCode <<
No, you need a real relational design with well-defined attributes. It
is a total violation of just about every rule to have a column that
holds different attributes. That is less than First Normal Form.
table, but we think that would only complicate our data model. Please
comment. <<
That design flaw is called the "One True Lookup Table" and you can
Goolge for the painful details.
Here are some standards that might help:
1) If you need to know the language of a book, you can get it from the
fist two digits of the ISBN.
2) ISO 3166 standard reference with 2 letter, 3 letter and number
codes:
The standard ISO 639 provides two sets of language codes, one as a
two-letter code set (ISO 639-1:2002 Codes for the representation of
names of languages -- Part 1: Alpha-2 code) and another as a
three-letter code set (ISO 639-2:1998 Codes for the representation of
names of languages -- Part 2: Alpha-3 code). The ISO 639 code lists are
also made available via the Web site of the ISO 639 Joint Advisory
Committee.|||CTO,
Creating a parent table is the way I'd go... You ARE creating repeated
data (copies) of the Keys, but that happens anytime you have a Foreign Key
to Primary Key Constraint. This technique is called "subclassing". It's
advantedge is that the FK On the Template table can use built in DRI
COnstraint which is better performance and cleaner than using Trigger...
"C TO" wrote:
> Thanks. Wouldn't this create more repeated data? How would you compare
> creating a parent table with creating trigger to enforce referential
> integrity?
>
> "CBretana" wrote:
>|||Hi CELKO,
Your input is really helpful and make me think harder. First of all, the
examples I provided related to Book and Language types were bad in this case
because language is an attribute of the book. But I really meant to provide
examples that would answer my question about "One True Lookup Table" and the
referential integrity enforment.
Secondly, I have heard you mentioned ISO before and felt very helpless
soemtimes because I did not how to get access to the codes. I've tried, many
of them required "placing an order in the shopping cart". BTW, in general,
how do I know which are accessible to the Public?
There were really book or language in this model, but I am interested in
using the universal, standard reference codes. Thanks.
"--CELKO--" wrote:

> A thing is either a type or a code, but not both. You also might want
> to consider not blindly assigning columns a VARCHAR(255).
> CREATE TABLE Languages
> (language_code CHAR(3) NOT NULL PRIMARY KEY, -- ISO Standared 3166
> language_name CHAR(15) NOT NULL);
> CREATE TABLE BookTypes
> ( book_type CHAR(6) NOT NULL PRIMARY KEY, -- ABA codes? shop
> categories?
> book_description VARCHAR(25) NOT NULL);
>
> against the primary key of EITHER table above. For example, ...
> TypeCode -- Either LanguageTypeCode or BookTypeCode <<
> No, you need a real relational design with well-defined attributes. It
> is a total violation of just about every rule to have a column that
> holds different attributes. That is less than First Normal Form.
>
> table, but we think that would only complicate our data model. Please
> comment. <<
> That design flaw is called the "One True Lookup Table" and you can
> Goolge for the painful details.
> Here are some standards that might help:
> 1) If you need to know the language of a book, you can get it from the
> fist two digits of the ISBN.
> 2) ISO 3166 standard reference with 2 letter, 3 letter and number
> codes:
> The standard ISO 639 provides two sets of language codes, one as a
> two-letter code set (ISO 639-1:2002 Codes for the representation of
> names of languages -- Part 1: Alpha-2 code) and another as a
> three-letter code set (ISO 639-2:1998 Codes for the representation of
> names of languages -- Part 2: Alpha-3 code). The ISO 639 code lists are
> also made available via the Web site of the ISO 639 Joint Advisory
> Committee.
>|||Thanks. If that is the only common practice, then I would go that route too.
Talking about DRI, is that still available in SQL 2005?
"CBretana" wrote:
> CTO,
> Creating a parent table is the way I'd go... You ARE creating repeated
> data (copies) of the Keys, but that happens anytime you have a Foreign Ke
y
> to Primary Key Constraint. This technique is called "subclassing". It's
> advantedge is that the FK On the Template table can use built in DRI
> COnstraint which is better performance and cleaner than using Trigger...
> "C TO" wrote:
>|||Correction: I meant "there was not any book or language..."
"C TO" wrote:
> Hi CELKO,
> Your input is really helpful and make me think harder. First of all, the
> examples I provided related to Book and Language types were bad in this ca
se
> because language is an attribute of the book. But I really meant to provid
e
> examples that would answer my question about "One True Lookup Table" and t
he
> referential integrity enforment.
> Secondly, I have heard you mentioned ISO before and felt very helpless
> soemtimes because I did not how to get access to the codes. I've tried, ma
ny
> of them required "placing an order in the shopping cart". BTW, in general,
> how do I know which are accessible to the Public?
> There were really book or language in this model, but I am interested in
> using the universal, standard reference codes. Thanks.
>
>
> "--CELKO--" wrote:
>|||Hi CELKO,
I overlooked a point you pointed out about "A thing is either a type or a
code". Do you mean in general a type is usually a popular thing which can be
understood easily in a spoken/written language (e.g. Type of Language:
English, Spanish...) and the code is usually used for application only (e.g
.
EN, SP) ? So you were questioning why the column was named that way?
You further said, "You also might want to consider not blindly assigning
columns a VARCHAR(255)." What do you mean by that?
Say, I have a type of a thing that is not so common or not to have easily
standardized type, having a description column would allow further
explaination about that type. Why not?
"--CELKO--" wrote:

> A thing is either a type or a code, but not both. You also might want
> to consider not blindly assigning columns a VARCHAR(255).
> CREATE TABLE Languages
> (language_code CHAR(3) NOT NULL PRIMARY KEY, -- ISO Standared 3166
> language_name CHAR(15) NOT NULL);
> CREATE TABLE BookTypes
> ( book_type CHAR(6) NOT NULL PRIMARY KEY, -- ABA codes? shop
> categories?
> book_description VARCHAR(25) NOT NULL);
>
> against the primary key of EITHER table above. For example, ...
> TypeCode -- Either LanguageTypeCode or BookTypeCode <<
> No, you need a real relational design with well-defined attributes. It
> is a total violation of just about every rule to have a column that
> holds different attributes. That is less than First Normal Form.
>
> table, but we think that would only complicate our data model. Please
> comment. <<
> That design flaw is called the "One True Lookup Table" and you can
> Goolge for the painful details.
> Here are some standards that might help:
> 1) If you need to know the language of a book, you can get it from the
> fist two digits of the ISBN.
> 2) ISO 3166 standard reference with 2 letter, 3 letter and number
> codes:
> The standard ISO 639 provides two sets of language codes, one as a
> two-letter code set (ISO 639-1:2002 Codes for the representation of
> names of languages -- Part 1: Alpha-2 code) and another as a
> three-letter code set (ISO 639-2:1998 Codes for the representation of
> names of languages -- Part 2: Alpha-3 code). The ISO 639 code lists are
> also made available via the Web site of the ISO 639 Joint Advisory
> Committee.
>|||>> I did not how to get access to the codes. I've tried, many of them
required "placing an order in the shopping cart". BTW, in general, how
do I know which are accessible to the Public? <<
ISO makes its money from sales, while ANSI makes its money from
membership fees. ISOP is therefore not as open about things as ANSI.
You have to Google around like crazy sometimes to find out about a
Standard if you do not know the ISO number or a good search phrase for
the title. I think that thre might be market for a pocket guidebook to
various standard codes -- the name, source, purpose and brief
history/description of each one.

Data Model Relationships

I am trying to create a data source view that includes a Fact table and
several related dimension tables. One of my dimension tables is quite small
and uses a tinyint PK. For some reason when I bring this table into the
model it thinks the PK is a System.Int32 datatype and won't allow a
relationship to the fact table FK which it sees as a System.Byte datatype.
In the database both fields are tinyint datatypes and there is a
relationship between the two.
My question is: Do I have to increase the field to a smallint in order to
create a relationship in the model or is there a workaround. This is my
first attempt at creating a data model for the Report Builder and any tips
would be appreciated. Thanks.I've noticed that if a key is an IDENTITY field then, regardless, it is seen
as an Int32. The behavior seems consistent in reporting services and in
analysis services. What we've decided to do is to make all primary and
foreign keys standard Ints (Int32) instead of tiny and small ints - even when
the smaller values were appropriate. The other thing you could do is to make
the table a named query and cast the key as a smaller int so the relationship
works.
"Elmer Miller" wrote:
> I am trying to create a data source view that includes a Fact table and
> several related dimension tables. One of my dimension tables is quite small
> and uses a tinyint PK. For some reason when I bring this table into the
> model it thinks the PK is a System.Int32 datatype and won't allow a
> relationship to the fact table FK which it sees as a System.Byte datatype.
> In the database both fields are tinyint datatypes and there is a
> relationship between the two.
> My question is: Do I have to increase the field to a smallint in order to
> create a relationship in the model or is there a workaround. This is my
> first attempt at creating a data model for the Report Builder and any tips
> would be appreciated. Thanks.
>
>|||sounds correct to me.
I had an identity on a TinyInt and I think that it just blatantly
friggin crashed SSAS or something ridiculous
-Aaron
Aaron wrote:
> I've noticed that if a key is an IDENTITY field then, regardless, it is seen
> as an Int32. The behavior seems consistent in reporting services and in
> analysis services. What we've decided to do is to make all primary and
> foreign keys standard Ints (Int32) instead of tiny and small ints - even when
> the smaller values were appropriate. The other thing you could do is to make
> the table a named query and cast the key as a smaller int so the relationship
> works.
> "Elmer Miller" wrote:
> > I am trying to create a data source view that includes a Fact table and
> > several related dimension tables. One of my dimension tables is quite small
> > and uses a tinyint PK. For some reason when I bring this table into the
> > model it thinks the PK is a System.Int32 datatype and won't allow a
> > relationship to the fact table FK which it sees as a System.Byte datatype.
> > In the database both fields are tinyint datatypes and there is a
> > relationship between the two.
> > My question is: Do I have to increase the field to a smallint in order to
> > create a relationship in the model or is there a workaround. This is my
> > first attempt at creating a data model for the Report Builder and any tips
> > would be appreciated. Thanks.
> >
> >
> >

Data Model Question

I posted to programming but thought maybe this is a btter group:
There may be some tools out there already developed that can handle this but
I haven't had success in finding them.
Essentially what I am trying to do is create a data model that can
accomodate the creation of a variety of forms to be filled out.
The end user should be able to create a question and select what type of
answer it will bne, multi select, checkbox, radio box, free text. Provide
the possible answers if needed. And then allow the form to be filled out
and the responses saved.
That part shouldn't be too tough. Forms may need to be able to be related
to other forms or multiple forms could make up 1 form to fill out. Like
form A and D.
If there is something like this already I don't want to reinvent the wheel.
Also if someone has experience with this type of model has it been good or
bad.
Thanks.You had one response. Was that not helpful? There are also some posts from
Janmar on a similar topic. Did you read those posts?|||A response in this group or the programming one? I didn't see any.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:ucBrV62lFHA.3300@.TK2MSFTNGP15.phx.gbl...
> You had one response. Was that not helpful? There are also some posts
> from
> Janmar on a similar topic. Did you read those posts?
>

Data Model Question

I posted to programming but thought maybe this is a btter group:
There may be some tools out there already developed that can handle this but
I haven't had success in finding them.
Essentially what I am trying to do is create a data model that can
accomodate the creation of a variety of forms to be filled out.
The end user should be able to create a question and select what type of
answer it will bne, multi select, checkbox, radio box, free text. Provide
the possible answers if needed. And then allow the form to be filled out
and the responses saved.
That part shouldn't be too tough. Forms may need to be able to be related
to other forms or multiple forms could make up 1 form to fill out. Like
form A and D.
If there is something like this already I don't want to reinvent the wheel.
Also if someone has experience with this type of model has it been good or
bad.
Thanks.
You had one response. Was that not helpful? There are also some posts from
Janmar on a similar topic. Did you read those posts?
|||A response in this group or the programming one? I didn't see any.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:ucBrV62lFHA.3300@.TK2MSFTNGP15.phx.gbl...
> You had one response. Was that not helpful? There are also some posts
> from
> Janmar on a similar topic. Did you read those posts?
>

Data Model Question

I posted to programming but thought maybe this is a btter group:
There may be some tools out there already developed that can handle this but
I haven't had success in finding them.
Essentially what I am trying to do is create a data model that can
accomodate the creation of a variety of forms to be filled out.
The end user should be able to create a question and select what type of
answer it will bne, multi select, checkbox, radio box, free text. Provide
the possible answers if needed. And then allow the form to be filled out
and the responses saved.
That part shouldn't be too tough. Forms may need to be able to be related
to other forms or multiple forms could make up 1 form to fill out. Like
form A and D.
If there is something like this already I don't want to reinvent the wheel.
Also if someone has experience with this type of model has it been good or
bad.
Thanks.You had one response. Was that not helpful? There are also some posts from
Janmar on a similar topic. Did you read those posts?|||A response in this group or the programming one? I didn't see any.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:ucBrV62lFHA.3300@.TK2MSFTNGP15.phx.gbl...
> You had one response. Was that not helpful? There are also some posts
> from
> Janmar on a similar topic. Did you read those posts?
>

Data Model off a view without a primary key

I am trying to bind the data view off of a view that is a
monster, I dont want to make it an indexed view, is there
anyway I can bind to this view without a primary key?
When I try to currently it gives a primary key required
error.
Can I schema bind my view or something? It is a SQL
Server 2005 database.In the model designer you can open data source view editor and assign
primary key to the view.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Greg" <gfricke@.gmail.com> wrote in message
news:1124216552.895088.277620@.g47g2000cwa.googlegroups.com...
>I am trying to bind the data view off of a view that is a
> monster, I dont want to make it an indexed view, is there
> anyway I can bind to this view without a primary key?
> When I try to currently it gives a primary key required
> error.
> Can I schema bind my view or something? It is a SQL
> Server 2005 database.
>

Data Model Help

There may be some tools out there already developed that can handle this but
I haven't had success in finding them.
Essentially what I am trying to do is create a data model that can
accomodate the creation of a variety of forms to be filled out.
The end user should be able to create a question and select what type of
answer it will bne, multi select, checkbox, radio box, free text. Provide
the possible answers if needed. And then allow the form to be filled out
and the responses saved.
That part shouldn't be too tough. Forms may need to be able to be related
to other forms or multiple forms could make up 1 form to fill out. Like
form A and D.
If there is something like this already I don't want to reinvent the wheel.
Also if someone has experience with this type of model has it been good or
bad.
Thanks.Hi
http://www.databaseanswers.org/data_models/ may be of help
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Brian" <brian@.nospam.com> wrote in message
news:%23Kbg6XflFHA.2180@.TK2MSFTNGP15.phx.gbl...
> There may be some tools out there already developed that can handle this
> but
> I haven't had success in finding them.
> Essentially what I am trying to do is create a data model that can
> accomodate the creation of a variety of forms to be filled out.
> The end user should be able to create a question and select what type of
> answer it will bne, multi select, checkbox, radio box, free text. Provide
> the possible answers if needed. And then allow the form to be filled out
> and the responses saved.
> That part shouldn't be too tough. Forms may need to be able to be related
> to other forms or multiple forms could make up 1 form to fill out. Like
> form A and D.
> If there is something like this already I don't want to reinvent the
> wheel.
> Also if someone has experience with this type of model has it been good or
> bad.
> Thanks.
>

Data Model for Application Inventory

Can someone help me provide a logical E-R model of the different entities
with relationship for collecting Application Inventory ?
The goal is to collect all the Applications along with dependencies.
On the top of my head, I can think of a Server/Hardware Table, an
ApplicationName Table that would appear to be many to many i.e One Server
could host multiple apps and one App could be hosted on multiple servers.
Anyways, I am sure you know what Im talking about. But can someone help me
with all the different tables,etc. that could also include location of
servers, type of servers, type of apps, application dependencies, database
dependencies,etc.
ThanksPublic forums are not the right avenue to get this information since your
conceptual model & business rules are mostly transparent to others here.
If you are ill-equipped to develop and E-R model, the right approach is to
hire a qualified professional who can have direct access to your business
model.
Anith|||Anith,
This is just for my own collection of application inventory and not to be
consumed by the entire company. I was just looking for a very conceptual
model of the different entities with some common attributes. I dont think I
would need to hire an external consultant for this. Just want to get a start
on where to begin..
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:%23wKTNzIVHHA.2256@.TK2MSFTNGP02.phx.gbl...
> Public forums are not the right avenue to get this information since your
> conceptual model & business rules are mostly transparent to others here.
> If you are ill-equipped to develop and E-R model, the right approach is to
> hire a qualified professional who can have direct access to your business
> model.
> --
> Anith
>|||Not sure if the point is missed, but hardly anyone here is familiar with
your conceptual model other than you. A general advice that you receive here
may be based on what others perceive as the problem and may not be the
actual design
problem, not to mention the chances of misunderstanding and
misinterpretation being very high. That is why newsgroup responses cannot
substantively contribute to your requirement.
Different organizations categorize and document their applications
differently. Some consider cataloging all aspects of the existing
applications while some just documents their names with perhaps say, usable
acronyms. Some companies introduce a temporal element to document the
current or historical status of applications used by them.
Even if you are doing it for personal use, I have no idea of your
expectations are. A google search returned a few shareware downloads that do
this sort of thing. I don't know what your application inventory would be
like. Perhaps someone who is familiar with developing it might be able to
help you with this.
Anith

Data Model for Application Inventory

Can someone help me provide a logical E-R model of the different entities
with relationship for collecting Application Inventory ?
The goal is to collect all the Applications along with dependencies.
On the top of my head, I can think of a Server/Hardware Table, an
ApplicationName Table that would appear to be many to many i.e One Server
could host multiple apps and one App could be hosted on multiple servers.
Anyways, I am sure you know what Im talking about. But can someone help me
with all the different tables,etc. that could also include location of
servers, type of servers, type of apps, application dependencies, database
dependencies,etc.
ThanksPublic forums are not the right avenue to get this information since your
conceptual model & business rules are mostly transparent to others here.
If you are ill-equipped to develop and E-R model, the right approach is to
hire a qualified professional who can have direct access to your business
model.
--
Anith|||Anith,
This is just for my own collection of application inventory and not to be
consumed by the entire company. I was just looking for a very conceptual
model of the different entities with some common attributes. I dont think I
would need to hire an external consultant for this. Just want to get a start
on where to begin..
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:%23wKTNzIVHHA.2256@.TK2MSFTNGP02.phx.gbl...
> Public forums are not the right avenue to get this information since your
> conceptual model & business rules are mostly transparent to others here.
> If you are ill-equipped to develop and E-R model, the right approach is to
> hire a qualified professional who can have direct access to your business
> model.
> --
> Anith
>|||Not sure if the point is missed, but hardly anyone here is familiar with
your conceptual model other than you. A general advice that you receive here
may be based on what others perceive as the problem and may not be the
actual design
problem, not to mention the chances of misunderstanding and
misinterpretation being very high. That is why newsgroup responses cannot
substantively contribute to your requirement.
Different organizations categorize and document their applications
differently. Some consider cataloging all aspects of the existing
applications while some just documents their names with perhaps say, usable
acronyms. Some companies introduce a temporal element to document the
current or historical status of applications used by them.
Even if you are doing it for personal use, I have no idea of your
expectations are. A google search returned a few shareware downloads that do
this sort of thing. I don't know what your application inventory would be
like. Perhaps someone who is familiar with developing it might be able to
help you with this.
--
Anith

Data model for a web messaging application.

I need to develop an internal messaging sub-system that is similar to
a web mail application but without SMTP support (e.g message routes
are confined to the webapp domain). The requirements are rather
simple: Each user (e.g mailbox) can view incoming messages and his
outgoing messages. Message quota is defined as the sum of all incoming
and outgoing messages per user
and tracked in the users' row (Users table log_TotalMessages). The
quota is enforced by the business logic layer and not by the DB.

I am considering the following data model for the storage component,
and would appreciate community feedback:

Table layout for incoming and outgoing messages
************************************************

CREATE TABLE [dbo].[Messages] (
[MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
[RecipientID] [int] NOT NULL , // The userid ('Users'
Table)
[SenderID] [int] NOT NULL , // The userid ('Users'
Table)
[GroupID] [uniqueidentifier] NULL , // Only assigned if the
user "replyed" to an incoming message

[SubmitDate] [smalldatetime] NOT NULL , // the date of the
message

[DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
one copy of each message I mark a message "to be deleted" and delete
only if both are true.

[DeleteByRecipient] [bit] NOT NULL ,

[SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
messages

[Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
list

[MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL
) ON [PRIMARY]

CREATE INDEX [Messages_RecipientID_IDX] ON
[dbo].[Messages]([RecipientID]) ON [PRIMARY]
CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
ON [PRIMARY]

/* Send Message */

CREATE PROCEDURE SendMessage (

@.IN_RecipientID int,
@.IN_SenderID int,
@.IN_GroupID uniqueidentifier,
@.IN_Subject tinyint,
@.IN_MessageText varchar(2000),

@.OUT_ERRCODE tinyint OUTPUT
)

AS
BEGIN TRANSACTION SendMessageTrans

INSERT INTO Messages

(RecipientID,
SenderID,
GroupID,
SubmitDate,
Subject,
MessageText)

VALUES (@.IN_RecipientID,
@.IN_SenderID,
@.IN_GroupID,
GETDate(),
@.IN_Subject,
@.IN_MessageText)

UPDATE Users

SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
WHERE usr_AccountNo = @.IN_RecipientID

UPDATE Users

SET log_TotalMessages = log_TotalMessages + 1
WHERE usr_AccountNo = @.IN_SenderID

SAVE TRANSACTION SendMessageTrans

SET @.OUT_ERRCODE = @.@.error

IF (@.@.error <> 0)

BEGIN
ROLLBACK TRANSACTION SendMessageTrans
END

ELSE

BEGIN
COMMIT TRANSACTION SendMessageTrans
END

/* ReadMessage */

CREATE PROCEDURE ReadMessage (

@.IN_MessageID int,
@.IN_RecipientID int,

@.OUT_ERRCODE tinyint OUTPUT
)

AS

BEGIN TRANSACTION ReadMessageTrans

SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID

UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID =
@.IN_MessageID

UPDATE Users SET log_NumberOfNewMessages =
log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID

SAVE TRANSACTION ReadMessageTrans

SET @.OUT_ERRCODE = @.@.error

IF (@.@.error <> 0)
BEGIN
ROLLBACK TRANSACTION ReadMessageTrans
END

ELSE

BEGIN
COMMIT TRANSACTION ReadMessageTrans
END

/* Delete Message */

CREATE PROCEDURE DeleteMessage (

@.IN_MessageID int,
@.IN_DeleteIncomingMessage bit,
@.IN_DeleteOutgoingMessage bit,

@.OUT_ERRCODE tinyint OUTPUT
)

AS
BEGIN TRANSACTION DeleteMessageTrans

DECLARE @.Recipient int
DECLARE @.Sender int

SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID =
@.IN_MessageID)
SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID =
@.IN_MessageID)

IF (@.IN_DeleteIncomingMessage = 1)

BEGIN

IF((SELECT DeleteBySender FROM Messages WHERE MessageID =
@.IN_MessageID) = 1)

BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Recipient
END

ELSE

BEGIN

UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID =
@.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Recipient
END
END

IF (@.IN_DeleteOutgoingMessage = 1)

BEGIN

IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID =
@.IN_MessageID) = 1)

BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Sender
END

ELSE

BEGIN
UPDATE Messages SET DeleteBySender = 1 WHERE MessageID =
@.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Sender
END
END

SAVE TRANSACTION DeleteMessageTrans

SET @.OUT_ERRCODE = @.@.error
IF (@.@.error <> 0)
BEGIN
ROLLBACK TRANSACTION DeleteMessageTrans
END
ELSE
BEGIN
COMMIT TRANSACTION DeleteMessageTrans
END

/* ListIncomingMessages */

CREATE PROCEDURE ListIncomingMessages (

@.IN_RecipientID int

)

AS

SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
= @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC

/* ListOutgoingMessages */

CREATE PROCEDURE ListOutgoingMessages (

@.IN_SenderID int

)

AS

SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
= @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC

Thanks in advance!

-ItaiItai,shalom
What is a primary key of the table? So what are you actually asking?
Are you concerned about a perfomance of the stored procedures? Do they give
you a wrong output?

"Itai" <itaitai2003@.yahoo.com> wrote in message
news:429f6e7d.0410030033.7573337c@.posting.google.c om...
> I need to develop an internal messaging sub-system that is similar to
> a web mail application but without SMTP support (e.g message routes
> are confined to the webapp domain). The requirements are rather
> simple: Each user (e.g mailbox) can view incoming messages and his
> outgoing messages. Message quota is defined as the sum of all incoming
> and outgoing messages per user
> and tracked in the users' row (Users table - log_TotalMessages). The
> quota is enforced by the business logic layer and not by the DB.
> I am considering the following data model for the storage component,
> and would appreciate community feedback:
>
> Table layout for incoming and outgoing messages
> ************************************************
> CREATE TABLE [dbo].[Messages] (
> [MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
> [RecipientID] [int] NOT NULL , // The userid ('Users'
> Table)
> [SenderID] [int] NOT NULL , // The userid ('Users'
> Table)
> [GroupID] [uniqueidentifier] NULL , // Only assigned if the
> user "replyed" to an incoming message
> [SubmitDate] [smalldatetime] NOT NULL , // the date of the
> message
> [DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
> one copy of each message I mark a message "to be deleted" and delete
> only if both are true.
> [DeleteByRecipient] [bit] NOT NULL ,
> [SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
> messages
> [Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
> list
> [MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
> NOT NULL
> ) ON [PRIMARY]
>
> CREATE INDEX [Messages_RecipientID_IDX] ON
> [dbo].[Messages]([RecipientID]) ON [PRIMARY]
> CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
> ON [PRIMARY]
>
>
> /* Send Message */
>
> CREATE PROCEDURE SendMessage (
> @.IN_RecipientID int,
> @.IN_SenderID int,
> @.IN_GroupID uniqueidentifier,
> @.IN_Subject tinyint,
> @.IN_MessageText varchar(2000),
>
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION SendMessageTrans
> INSERT INTO Messages
> (RecipientID,
> SenderID,
> GroupID,
> SubmitDate,
> Subject,
> MessageText)
> VALUES (@.IN_RecipientID,
> @.IN_SenderID,
> @.IN_GroupID,
> GETDate(),
> @.IN_Subject,
> @.IN_MessageText)
>
> UPDATE Users
> SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
> WHERE usr_AccountNo = @.IN_RecipientID
> UPDATE Users
> SET log_TotalMessages = log_TotalMessages + 1
> WHERE usr_AccountNo = @.IN_SenderID
>
> SAVE TRANSACTION SendMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION SendMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION SendMessageTrans
> END
>
>
> /* ReadMessage */
> CREATE PROCEDURE ReadMessage (
> @.IN_MessageID int,
> @.IN_RecipientID int,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION ReadMessageTrans
> SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_NumberOfNewMessages =
> log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID
> SAVE TRANSACTION ReadMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION ReadMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION ReadMessageTrans
> END
>
>
> /* Delete Message */
>
> CREATE PROCEDURE DeleteMessage (
> @.IN_MessageID int,
> @.IN_DeleteIncomingMessage bit,
> @.IN_DeleteOutgoingMessage bit,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION DeleteMessageTrans
> DECLARE @.Recipient int
> DECLARE @.Sender int
> SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID =
> @.IN_MessageID)
> SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID =
> @.IN_MessageID)
>
> IF (@.IN_DeleteIncomingMessage = 1)
> BEGIN
> IF((SELECT DeleteBySender FROM Messages WHERE MessageID =
> @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> END
>
> IF (@.IN_DeleteOutgoingMessage = 1)
> BEGIN
> IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID =
> @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteBySender = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> END
>
> SAVE TRANSACTION DeleteMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION DeleteMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION DeleteMessageTrans
> END
>
>
> /* ListIncomingMessages */
>
> CREATE PROCEDURE ListIncomingMessages (
> @.IN_RecipientID int
> )
> AS
> SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
> = @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
>
> /* ListOutgoingMessages */
>
> CREATE PROCEDURE ListOutgoingMessages (
> @.IN_SenderID int
> )
> AS
> SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
> = @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC
>
> Thanks in advance!
> -Itai|||Hi

I am not sure what you are requiring people to do regarding your post, you
are the only person that can do the analysis of what is required, if you
have captured them correctly then you will know what to store in the
database. That said, there does not seem to be any referential integrity
built into the DDL, FKs and PKs should be defined. There is probably a
natural key of RecipientID, SenderID, and SubmitDate so a covering unique
index may be useful, and a INT may not be sufficient for you messageid.
Whether just storing bits to indicated the actions or whether a date would
be better would depend on whatever auditing requirements you require.

In your stored procedures you should implement better error handling, any
statement may fail and you are only checking the result from a few. From
Books online "Because @.@.ERROR is cleared and reset on each statement
executed, check it immediately following the statement validated, or save it
to a local variable that can be checked later." you may not even be checking
what you think!.

John

"Itai" <itaitai2003@.yahoo.com> wrote in message
news:429f6e7d.0410030033.7573337c@.posting.google.c om...
> I need to develop an internal messaging sub-system that is similar to
> a web mail application but without SMTP support (e.g message routes
> are confined to the webapp domain). The requirements are rather
> simple: Each user (e.g mailbox) can view incoming messages and his
> outgoing messages. Message quota is defined as the sum of all incoming
> and outgoing messages per user
> and tracked in the users' row (Users table - log_TotalMessages). The
> quota is enforced by the business logic layer and not by the DB.
> I am considering the following data model for the storage component,
> and would appreciate community feedback:
>
> Table layout for incoming and outgoing messages
> ************************************************
> CREATE TABLE [dbo].[Messages] (
> [MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
> [RecipientID] [int] NOT NULL , // The userid ('Users'
> Table)
> [SenderID] [int] NOT NULL , // The userid ('Users'
> Table)
> [GroupID] [uniqueidentifier] NULL , // Only assigned if the
> user "replyed" to an incoming message
> [SubmitDate] [smalldatetime] NOT NULL , // the date of the
> message
> [DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
> one copy of each message I mark a message "to be deleted" and delete
> only if both are true.
> [DeleteByRecipient] [bit] NOT NULL ,
> [SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
> messages
> [Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
> list
> [MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
> NOT NULL
> ) ON [PRIMARY]
>
> CREATE INDEX [Messages_RecipientID_IDX] ON
> [dbo].[Messages]([RecipientID]) ON [PRIMARY]
> CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
> ON [PRIMARY]
>
>
> /* Send Message */
>
> CREATE PROCEDURE SendMessage (
> @.IN_RecipientID int,
> @.IN_SenderID int,
> @.IN_GroupID uniqueidentifier,
> @.IN_Subject tinyint,
> @.IN_MessageText varchar(2000),
>
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION SendMessageTrans
> INSERT INTO Messages
> (RecipientID,
> SenderID,
> GroupID,
> SubmitDate,
> Subject,
> MessageText)
> VALUES (@.IN_RecipientID,
> @.IN_SenderID,
> @.IN_GroupID,
> GETDate(),
> @.IN_Subject,
> @.IN_MessageText)
>
> UPDATE Users
> SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
> WHERE usr_AccountNo = @.IN_RecipientID
> UPDATE Users
> SET log_TotalMessages = log_TotalMessages + 1
> WHERE usr_AccountNo = @.IN_SenderID
>
> SAVE TRANSACTION SendMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION SendMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION SendMessageTrans
> END
>
>
> /* ReadMessage */
> CREATE PROCEDURE ReadMessage (
> @.IN_MessageID int,
> @.IN_RecipientID int,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION ReadMessageTrans
> SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_NumberOfNewMessages =
> log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID
> SAVE TRANSACTION ReadMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION ReadMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION ReadMessageTrans
> END
>
>
> /* Delete Message */
>
> CREATE PROCEDURE DeleteMessage (
> @.IN_MessageID int,
> @.IN_DeleteIncomingMessage bit,
> @.IN_DeleteOutgoingMessage bit,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION DeleteMessageTrans
> DECLARE @.Recipient int
> DECLARE @.Sender int
> SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID =
> @.IN_MessageID)
> SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID =
> @.IN_MessageID)
>
> IF (@.IN_DeleteIncomingMessage = 1)
> BEGIN
> IF((SELECT DeleteBySender FROM Messages WHERE MessageID =
> @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> END
>
> IF (@.IN_DeleteOutgoingMessage = 1)
> BEGIN
> IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID =
> @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteBySender = 1 WHERE MessageID =
> @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> END
>
> SAVE TRANSACTION DeleteMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION DeleteMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION DeleteMessageTrans
> END
>
>
> /* ListIncomingMessages */
>
> CREATE PROCEDURE ListIncomingMessages (
> @.IN_RecipientID int
> )
> AS
> SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
> = @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
>
> /* ListOutgoingMessages */
>
> CREATE PROCEDURE ListOutgoingMessages (
> @.IN_SenderID int
> )
> AS
> SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
> = @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC
>
> Thanks in advance!
> -Itai|||Uri, John and especially David! Thanks for the code review; I am now
one step further :)

"John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<41605341$0$21630$afc38c87@.news.easynet.co.uk>...

> There is probably a
> natural key of RecipientID, SenderID, and SubmitDate so a covering unique
> index may be useful, and a INT may not be sufficient for you messageid.

"Covering index" is something I lack to understand ... How is it
stored in a
b-tree structure, does the whole string composed of the diffrent
columns get saved as one key in each node? How does SQL server
(depending on the query's' where clause of course) 'extracts' the
right column and scan for its appropriate value within the index? What
are the questions to ask when considering a covering index as a design
requirement.

Regarding the INT data type for messageID, what would you suggest?
Messages will often be deleted but the counter value will always
progress...

I thought about using a uid, but they are not suitable for a Clustered
Index since they are not guaranteed to be chosen in incremental
order...

Thanks again

-Itai

BTW does anyone know how to dump a table to a text file using command
line with arguments?|||Hi

I have not seen Davids reply!!
itaitai2003@.yahoo.com (Itai) wrote in message news:<429f6e7d.0410050519.28073d82@.posting.google.com>...
> Uri, John and especially David! Thanks for the code review; I am now
> one step further :)
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<41605341$0$21630$afc38c87@.news.easynet.co.uk>...
> > There is probably a
> > natural key of RecipientID, SenderID, and SubmitDate so a covering unique
> > index may be useful, and a INT may not be sufficient for you messageid.
> "Covering index" is something I lack to understand ... How is it
> stored in a
> b-tree structure, does the whole string composed of the diffrent
> columns get saved as one key in each node? How does SQL server
> (depending on the query's' where clause of course) 'extracts' the
> right column and scan for its appropriate value within the index? What
> are the questions to ask when considering a covering index as a design
> requirement.
The uniqueness of the index would make sure no duplicates values of
the three combined columns are inserted into your database, this may
be important for maintaining integrity. I would expect that when you
are looking for a message you will be mainly searching on a
combination of these three columns. The query processor will decide on
whether an index is useful using various algorithms.

> Regarding the INT data type for messageID, what would you suggest?
> Messages will often be deleted but the counter value will always
> progress...
You would have to determine the number of records and what growth you
are expecting, but when you could be mailing a significant number of
recipients then the maximum number offered by an INT datatype will
probably get used up quickly, therefore BIGINT may be better.
> I thought about using a uid, but they are not suitable for a Clustered
> Index since they are not guaranteed to be chosen in incremental
> order...
> Thanks again
> -Itai
>
> BTW does anyone know how to dump a table to a text file using command
> line with arguments?

BCP, DTS or even osql will do this, look at books online for more
information on these.

John

Data model for a web messaging application.

I need to develop an internal messaging sub-system that is similar to
a web mail application but without SMTP support (e.g message routes
are confined to the webapp domain). The requirements are rather
simple: Each user (e.g mailbox) can view incoming messages and his
outgoing messages. Message quota is defined as the sum of all incoming
and outgoing messages per user
and tracked in the users' row (Users table ? log_TotalMessages). The
quota is enforced by the business logic layer and not by the DB.
I am considering the following data model for the storage component,
and would appreciate community feedback:
Table layout for incoming and outgoing messages
************************************************
CREATE TABLE [dbo].[Messages] (
[MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
[RecipientID] [int] NOT NULL , // The userid ('Users'
Table)
[SenderID] [int] NOT NULL , // The userid ('Users'
Table)
[GroupID] [uniqueidentifier] NULL , // Only assigned if the
user "replyed" to an incoming message
[SubmitDate] [smalldatetime] NOT NULL , // the date of the
message
[DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
one copy of each message I mark a message "to be deleted" and delete
only if both are true.
[DeleteByRecipient] [bit] NOT NULL ,
[SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
messages
[Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
list
[MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL
) ON [PRIMARY]
CREATE INDEX [Messages_RecipientID_IDX] ON
[dbo].[Messages]([RecipientID]) ON [PRIMARY]
CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
ON [PRIMARY]
/* Send Message */
CREATE PROCEDURE SendMessage (
@.IN_RecipientID int,
@.IN_SenderID int,
@.IN_GroupID uniqueidentifier,
@.IN_Subject tinyint,
@.IN_MessageText varchar(2000),
@.OUT_ERRCODE tinyint OUTPUT
)
AS
BEGIN TRANSACTION SendMessageTrans
INSERT INTO Messages
(RecipientID,
SenderID,
GroupID,
SubmitDate,
Subject,
MessageText)
VALUES (@.IN_RecipientID,
@.IN_SenderID,
@.IN_GroupID,
GETDate(),
@.IN_Subject,
@.IN_MessageText)
UPDATE Users
SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
WHERE usr_AccountNo = @.IN_RecipientID
UPDATE Users
SET log_TotalMessages = log_TotalMessages + 1
WHERE usr_AccountNo = @.IN_SenderID
SAVE TRANSACTION SendMessageTrans
SET @.OUT_ERRCODE = @.@.error
IF (@.@.error <> 0)
BEGIN
ROLLBACK TRANSACTION SendMessageTrans
END
ELSE
BEGIN
COMMIT TRANSACTION SendMessageTrans
END
/* ReadMessage */
CREATE PROCEDURE ReadMessage (
@.IN_MessageID int,
@.IN_RecipientID int,
@.OUT_ERRCODE tinyint OUTPUT
)
AS
BEGIN TRANSACTION ReadMessageTrans
SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID
UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_NumberOfNewMessages = log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID
SAVE TRANSACTION ReadMessageTrans
SET @.OUT_ERRCODE = @.@.error
IF (@.@.error <> 0)
BEGIN
ROLLBACK TRANSACTION ReadMessageTrans
END
ELSE
BEGIN
COMMIT TRANSACTION ReadMessageTrans
END
/* Delete Message */
CREATE PROCEDURE DeleteMessage (
@.IN_MessageID int,
@.IN_DeleteIncomingMessage bit,
@.IN_DeleteOutgoingMessage bit,
@.OUT_ERRCODE tinyint OUTPUT
)
AS
BEGIN TRANSACTION DeleteMessageTrans
DECLARE @.Recipient int
DECLARE @.Sender int
SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID = @.IN_MessageID)
SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID = @.IN_MessageID)
IF (@.IN_DeleteIncomingMessage = 1)
BEGIN
IF((SELECT DeleteBySender FROM Messages WHERE MessageID = @.IN_MessageID) = 1)
BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Recipient
END
ELSE
BEGIN
UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Recipient
END
END
IF (@.IN_DeleteOutgoingMessage = 1)
BEGIN
IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID = @.IN_MessageID) = 1)
BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Sender
END
ELSE
BEGIN
UPDATE Messages SET DeleteBySender = 1 WHERE MessageID = @.IN_MessageID
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Sender
END
END
SAVE TRANSACTION DeleteMessageTrans
SET @.OUT_ERRCODE = @.@.error
IF (@.@.error <> 0)
BEGIN
ROLLBACK TRANSACTION DeleteMessageTrans
END
ELSE
BEGIN
COMMIT TRANSACTION DeleteMessageTrans
END
/* ListIncomingMessages */
CREATE PROCEDURE ListIncomingMessages (
@.IN_RecipientID int
)
AS
SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
= @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
/* ListOutgoingMessages */
CREATE PROCEDURE ListOutgoingMessages (
@.IN_SenderID int
)
AS
SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
= @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC
Thanks in advance!
-ItaiItai,shalom
What is a primary key of the table? So what are you actually asking?
Are you concerned about a perfomance of the stored procedures? Do they give
you a wrong output?
"Itai" <itaitai2003@.yahoo.com> wrote in message
news:429f6e7d.0410030033.7573337c@.posting.google.com...
> I need to develop an internal messaging sub-system that is similar to
> a web mail application but without SMTP support (e.g message routes
> are confined to the webapp domain). The requirements are rather
> simple: Each user (e.g mailbox) can view incoming messages and his
> outgoing messages. Message quota is defined as the sum of all incoming
> and outgoing messages per user
> and tracked in the users' row (Users table - log_TotalMessages). The
> quota is enforced by the business logic layer and not by the DB.
> I am considering the following data model for the storage component,
> and would appreciate community feedback:
>
> Table layout for incoming and outgoing messages
> ************************************************
> CREATE TABLE [dbo].[Messages] (
> [MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
> [RecipientID] [int] NOT NULL , // The userid ('Users'
> Table)
> [SenderID] [int] NOT NULL , // The userid ('Users'
> Table)
> [GroupID] [uniqueidentifier] NULL , // Only assigned if the
> user "replyed" to an incoming message
> [SubmitDate] [smalldatetime] NOT NULL , // the date of the
> message
> [DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
> one copy of each message I mark a message "to be deleted" and delete
> only if both are true.
> [DeleteByRecipient] [bit] NOT NULL ,
> [SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
> messages
> [Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
> list
> [MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
> NOT NULL
> ) ON [PRIMARY]
>
> CREATE INDEX [Messages_RecipientID_IDX] ON
> [dbo].[Messages]([RecipientID]) ON [PRIMARY]
> CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
> ON [PRIMARY]
>
>
> /* Send Message */
>
> CREATE PROCEDURE SendMessage (
> @.IN_RecipientID int,
> @.IN_SenderID int,
> @.IN_GroupID uniqueidentifier,
> @.IN_Subject tinyint,
> @.IN_MessageText varchar(2000),
>
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION SendMessageTrans
> INSERT INTO Messages
> (RecipientID,
> SenderID,
> GroupID,
> SubmitDate,
> Subject,
> MessageText)
> VALUES (@.IN_RecipientID,
> @.IN_SenderID,
> @.IN_GroupID,
> GETDate(),
> @.IN_Subject,
> @.IN_MessageText)
>
> UPDATE Users
> SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
> WHERE usr_AccountNo = @.IN_RecipientID
> UPDATE Users
> SET log_TotalMessages = log_TotalMessages + 1
> WHERE usr_AccountNo = @.IN_SenderID
>
> SAVE TRANSACTION SendMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION SendMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION SendMessageTrans
> END
>
>
> /* ReadMessage */
> CREATE PROCEDURE ReadMessage (
> @.IN_MessageID int,
> @.IN_RecipientID int,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION ReadMessageTrans
> SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_NumberOfNewMessages => log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID
> SAVE TRANSACTION ReadMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION ReadMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION ReadMessageTrans
> END
>
>
> /* Delete Message */
>
> CREATE PROCEDURE DeleteMessage (
> @.IN_MessageID int,
> @.IN_DeleteIncomingMessage bit,
> @.IN_DeleteOutgoingMessage bit,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION DeleteMessageTrans
> DECLARE @.Recipient int
> DECLARE @.Sender int
> SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID => @.IN_MessageID)
> SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID => @.IN_MessageID)
>
> IF (@.IN_DeleteIncomingMessage = 1)
> BEGIN
> IF((SELECT DeleteBySender FROM Messages WHERE MessageID => @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> END
>
> IF (@.IN_DeleteOutgoingMessage = 1)
> BEGIN
> IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID => @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteBySender = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> END
>
> SAVE TRANSACTION DeleteMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION DeleteMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION DeleteMessageTrans
> END
>
>
> /* ListIncomingMessages */
>
> CREATE PROCEDURE ListIncomingMessages (
> @.IN_RecipientID int
> )
> AS
> SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
> = @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
>
> /* ListOutgoingMessages */
>
> CREATE PROCEDURE ListOutgoingMessages (
> @.IN_SenderID int
> )
> AS
> SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
> = @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC
>
> Thanks in advance!
> -Itai|||"Itai" <itaitai2003@.yahoo.com> wrote in message
news:429f6e7d.0410030033.7573337c@.posting.google.com...
>I need to develop an internal messaging sub-system that is similar to
> a web mail application but without SMTP support (e.g message routes
> are confined to the webapp domain). The requirements are rather
> simple: Each user (e.g mailbox) can view incoming messages and his
> outgoing messages. Message quota is defined as the sum of all incoming
> and outgoing messages per user
> and tracked in the users' row (Users table - log_TotalMessages). The
> quota is enforced by the business logic layer and not by the DB.
> I am considering the following data model for the storage component,
> and would appreciate community feedback:
>
See changes inline. . .
David
--Use Declarative referential integrity and cascading deletes
CREATE TABLE [dbo].[Messages] (
[MessageID] [int] IDENTITY (1, 1) NOT NULL PRIMARY KEY,
[RecipientID] [int] NOT NULL REFERENCES USERS ON DELETE CASCADE,
[SenderID] [int] NOT NULL REFERENCES USERS ON DELETE CASCADE,
[GroupID] [uniqueidentifier] NULL ,
[SubmitDate] [smalldatetime] NOT NULL ,
[DeleteBySender] [bit] NOT NULL ,
[DeleteByRecipient] [bit] NOT NULL ,
[SeenByRecipient] [bit] NOT NULL ,
[Subject] [tinyint] NOT NULL ,
[MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL
) ON [PRIMARY]
CREATE INDEX [Messages_RecipientID_IDX] ON
[dbo].[Messages]([RecipientID]) ON [PRIMARY]
CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
ON [PRIMARY]
/* Send Message
change the error handling.
there was a mix of "nested transactions" and savepoints
which wouldn't really work.
Slso get rid of the @.out_errorcode parameter.
Any error will go to the client in a message anyway.
If you really need the error value to be returned to another
stored procedure, just return it as the return value of the
stored procedure.
*/
CREATE PROCEDURE SendMessage (
@.IN_RecipientID int,
@.IN_SenderID int,
@.IN_GroupID uniqueidentifier,
@.IN_Subject tinyint,
@.IN_MessageText varchar(2000)
)
AS
BEGIN TRANSACTION
SAVE TRANSACTION SendMessageTrans
INSERT INTO Messages
(RecipientID,
SenderID,
GroupID,
SubmitDate,
Subject,
MessageText)
VALUES (@.IN_RecipientID,
@.IN_SenderID,
@.IN_GroupID,
GETDate(),
@.IN_Subject,
@.IN_MessageText)
IF (@.@.error <> 0) goto eh
UPDATE Users
SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
WHERE usr_AccountNo = @.IN_RecipientID
IF (@.@.error <> 0) goto eh
UPDATE Users
SET log_TotalMessages = log_TotalMessages + 1
WHERE usr_AccountNo = @.IN_SenderID
IF (@.@.error <> 0) goto eh
COMMIT TRANSACTION
RETURN 0
eh:
ROLLBACK TRANSACTION SendMessageTrans
COMMIT TRANSACTION
RETURN 1
/* ReadMessage
Single value results should be returned in
output parameters instead of resultsets.
And only decrement log_NumberOfNewMessages the first time
*/
CREATE PROCEDURE ReadMessage (
@.IN_MessageID int,
@.IN_RecipientID int,
@.OUT_MessageText out varchar(2000)
)
AS
BEGIN TRANSACTION
SAVE TRANSACTION ReadMessageTrans
declare @.AlreadySeen bit
SELECT
@.OUT_MessageText = MessageText
@.AlreadySeen = SeenByRecipient
FROM Messages WHERE MessageID = @.IN_MessageID
IF (@.AlreadySeen = 0)
BEGIN
UPDATE Messages SET
SeenByRecipient = 1
WHERE MessageID = @.IN_MessageID
IF (@.@.error <> 0) GOTO EH
UPDATE Users
SET log_NumberOfNewMessages = log_NumberOfNewMessages - 1
WHERE usr_AccountNo = @.IN_RecipientID
IF (@.@.error <> 0) GOTO EH
END
COMMIT TRANSACTION
RETURN 0
EH:
ROLLBACK TRANSACTION ReadMessageTrans
COMMIT TRANSACTION
RETURN 1
/* Delete Message */
CREATE PROCEDURE DeleteMessage (
@.IN_MessageID int,
@.IN_DeleteIncomingMessage bit,
@.IN_DeleteOutgoingMessage bit
)
AS
BEGIN TRANSACTION
SAVE TRANSACTION DeleteMessageTrans
DECLARE @.Recipient int
DECLARE @.Sender int
DECLARE @.DeleteBySender bit
DECLARE @.DeleteByRecipient bit
--do this in one query
SET
@.Recipient = RecipientID,
@.Sender = SenderID,
@.DeleteBySender = DeleteBySender
@.DeleteByRecipient = DeleteByRecipient
FROM Messages (updlock, holdlock)
WHERE MessageID = @.IN_MessageID
IF @.IN_DeleteOutgoingMessage = 1
BEGIN
if (@.DeleteByRecipient = 1)
BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
IF (@.@.error <> 0) GOTO EH
END
ELSE
BEGIN
UPDATE Messages SET DeleteBySender = 1
WHERE MessageID = @.IN_MessageID
IF (@.@.error <> 0) GOTO EH
END
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Sender
IF (@.@.error <> 0) GOTO EH
END
ELSE --@.IN_IncomingMessage
BEGIN
if (@.DeleteBySender = 1)
BEGIN
DELETE FROM Messages WHERE MessageID = @.IN_MessageID
IF (@.@.error <> 0) GOTO EH
END
ELSE
BEGIN
UPDATE Messages SET DeleteByRecipient = 1
WHERE MessageID = @.IN_MessageID
IF (@.@.error <> 0) GOTO EH
END
UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
WHERE usr_AccountNo = @.Recipient
IF (@.@.error <> 0) GOTO EH
END
COMMIT TRANSACTION
RETURN 0
EH:
ROLLBACK TRANSACTION DeleteMessageTrans
COMMIT TRANSACTION
RETURN 1
/* ListIncomingMessages */
CREATE PROCEDURE ListIncomingMessages (
@.IN_RecipientID int
)
AS
SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
= @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
/* ListOutgoingMessages */
CREATE PROCEDURE ListOutgoingMessages (
@.IN_SenderID int
)
AS
SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
= @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC|||Hi
I am not sure what you are requiring people to do regarding your post, you
are the only person that can do the analysis of what is required, if you
have captured them correctly then you will know what to store in the
database. That said, there does not seem to be any referential integrity
built into the DDL, FKs and PKs should be defined. There is probably a
natural key of RecipientID, SenderID, and SubmitDate so a covering unique
index may be useful, and a INT may not be sufficient for you messageid.
Whether just storing bits to indicated the actions or whether a date would
be better would depend on whatever auditing requirements you require.
In your stored procedures you should implement better error handling, any
statement may fail and you are only checking the result from a few. From
Books online "Because @.@.ERROR is cleared and reset on each statement
executed, check it immediately following the statement validated, or save it
to a local variable that can be checked later." you may not even be checking
what you think!.
John
"Itai" <itaitai2003@.yahoo.com> wrote in message
news:429f6e7d.0410030033.7573337c@.posting.google.com...
> I need to develop an internal messaging sub-system that is similar to
> a web mail application but without SMTP support (e.g message routes
> are confined to the webapp domain). The requirements are rather
> simple: Each user (e.g mailbox) can view incoming messages and his
> outgoing messages. Message quota is defined as the sum of all incoming
> and outgoing messages per user
> and tracked in the users' row (Users table - log_TotalMessages). The
> quota is enforced by the business logic layer and not by the DB.
> I am considering the following data model for the storage component,
> and would appreciate community feedback:
>
> Table layout for incoming and outgoing messages
> ************************************************
> CREATE TABLE [dbo].[Messages] (
> [MessageID] [int] IDENTITY (1, 1) NOT NULL , // The messageID
> [RecipientID] [int] NOT NULL , // The userid ('Users'
> Table)
> [SenderID] [int] NOT NULL , // The userid ('Users'
> Table)
> [GroupID] [uniqueidentifier] NULL , // Only assigned if the
> user "replyed" to an incoming message
> [SubmitDate] [smalldatetime] NOT NULL , // the date of the
> message
> [DeleteBySender] [bit] NOT NULL , // Since I want to maintain only
> one copy of each message I mark a message "to be deleted" and delete
> only if both are true.
> [DeleteByRecipient] [bit] NOT NULL ,
> [SeenByRecipient] [bit] NOT NULL , // Used to "highlight" unread
> messages
> [Subject] [tinyint] NOT NULL , // Subject is derived from a fixed
> list
> [MessageText] [varchar] (2000) COLLATE SQL_Latin1_General_CP1_CI_AS
> NOT NULL
> ) ON [PRIMARY]
>
> CREATE INDEX [Messages_RecipientID_IDX] ON
> [dbo].[Messages]([RecipientID]) ON [PRIMARY]
> CREATE INDEX [Messages_SenderID_IDX] ON [dbo].[Messages]([SenderID])
> ON [PRIMARY]
>
>
> /* Send Message */
>
> CREATE PROCEDURE SendMessage (
> @.IN_RecipientID int,
> @.IN_SenderID int,
> @.IN_GroupID uniqueidentifier,
> @.IN_Subject tinyint,
> @.IN_MessageText varchar(2000),
>
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION SendMessageTrans
> INSERT INTO Messages
> (RecipientID,
> SenderID,
> GroupID,
> SubmitDate,
> Subject,
> MessageText)
> VALUES (@.IN_RecipientID,
> @.IN_SenderID,
> @.IN_GroupID,
> GETDate(),
> @.IN_Subject,
> @.IN_MessageText)
>
> UPDATE Users
> SET log_NumberOfNewMessages = log_NumberOfNewMessages + 1
> WHERE usr_AccountNo = @.IN_RecipientID
> UPDATE Users
> SET log_TotalMessages = log_TotalMessages + 1
> WHERE usr_AccountNo = @.IN_SenderID
>
> SAVE TRANSACTION SendMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION SendMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION SendMessageTrans
> END
>
>
> /* ReadMessage */
> CREATE PROCEDURE ReadMessage (
> @.IN_MessageID int,
> @.IN_RecipientID int,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION ReadMessageTrans
> SELECT MessageText FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Messages SET SeenByRecipient = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_NumberOfNewMessages => log_NumberOfNewMessages - 1 WHERE usr_AccountNo = @.IN_RecipientID
> SAVE TRANSACTION ReadMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION ReadMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION ReadMessageTrans
> END
>
>
> /* Delete Message */
>
> CREATE PROCEDURE DeleteMessage (
> @.IN_MessageID int,
> @.IN_DeleteIncomingMessage bit,
> @.IN_DeleteOutgoingMessage bit,
> @.OUT_ERRCODE tinyint OUTPUT
> )
> AS
> BEGIN TRANSACTION DeleteMessageTrans
> DECLARE @.Recipient int
> DECLARE @.Sender int
> SET @.Recipient = (SELECT RecipientID FROM Messages WHERE MessageID => @.IN_MessageID)
> SET @.Sender = (SELECT SenderID FROM Messages WHERE MessageID => @.IN_MessageID)
>
> IF (@.IN_DeleteIncomingMessage = 1)
> BEGIN
> IF((SELECT DeleteBySender FROM Messages WHERE MessageID => @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteByRecipient = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Recipient
> END
> END
>
> IF (@.IN_DeleteOutgoingMessage = 1)
> BEGIN
> IF((SELECT DeleteByRecipient FROM Messages WHERE MessageID => @.IN_MessageID) = 1)
> BEGIN
> DELETE FROM Messages WHERE MessageID = @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> ELSE
> BEGIN
> UPDATE Messages SET DeleteBySender = 1 WHERE MessageID => @.IN_MessageID
> UPDATE Users SET log_TotalMessages = log_TotalMessages - 1
> WHERE usr_AccountNo = @.Sender
> END
> END
>
> SAVE TRANSACTION DeleteMessageTrans
> SET @.OUT_ERRCODE = @.@.error
> IF (@.@.error <> 0)
> BEGIN
> ROLLBACK TRANSACTION DeleteMessageTrans
> END
> ELSE
> BEGIN
> COMMIT TRANSACTION DeleteMessageTrans
> END
>
>
> /* ListIncomingMessages */
>
> CREATE PROCEDURE ListIncomingMessages (
> @.IN_RecipientID int
> )
> AS
> SELECT SenderID, MessageID, SubmitDate FROM Messages WHERE RecipientID
> = @.IN_RecipientID AND DeleteByRecipient = 0 ORDER BY SubmitDate DESC
>
> /* ListOutgoingMessages */
>
> CREATE PROCEDURE ListOutgoingMessages (
> @.IN_SenderID int
> )
> AS
> SELECT RecipientID, MessageID, SubmitDate FROM Messages WHERE SenderID
> = @.IN_SenderID AND DeleteBySender = 0 ORDER BY SubmitDate DESC
>
> Thanks in advance!
> -Itai|||Uri, John and especially David! Thanks for the code review; I am now
one step further :)
"John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<41605341$0$21630$afc38c87@.news.easynet.co.uk>...
> There is probably a
> natural key of RecipientID, SenderID, and SubmitDate so a covering unique
> index may be useful, and a INT may not be sufficient for you messageid.
"Covering index" is something I lack to understand ... How is it
stored in a
b-tree structure, does the whole string composed of the diffrent
columns get saved as one key in each node? How does SQL server
(depending on the query's' where clause of course) 'extracts' the
right column and scan for its appropriate value within the index? What
are the questions to ask when considering a covering index as a design
requirement.
Regarding the INT data type for messageID, what would you suggest?
Messages will often be deleted but the counter value will always
progress...
I thought about using a uid, but they are not suitable for a Clustered
Index since they are not guaranteed to be chosen in incremental
order...
Thanks again
-Itai
BTW does anyone know how to dump a table to a text file using command
line with arguments?|||Hi
I have not seen Davids reply!!
itaitai2003@.yahoo.com (Itai) wrote in message news:<429f6e7d.0410050519.28073d82@.posting.google.com>...
> Uri, John and especially David! Thanks for the code review; I am now
> one step further :)
> "John Bell" <jbellnewsposts@.hotmail.com> wrote in message news:<41605341$0$21630$afc38c87@.news.easynet.co.uk>...
> > There is probably a
> > natural key of RecipientID, SenderID, and SubmitDate so a covering unique
> > index may be useful, and a INT may not be sufficient for you messageid.
> "Covering index" is something I lack to understand ... How is it
> stored in a
> b-tree structure, does the whole string composed of the diffrent
> columns get saved as one key in each node? How does SQL server
> (depending on the query's' where clause of course) 'extracts' the
> right column and scan for its appropriate value within the index? What
> are the questions to ask when considering a covering index as a design
> requirement.
The uniqueness of the index would make sure no duplicates values of
the three combined columns are inserted into your database, this may
be important for maintaining integrity. I would expect that when you
are looking for a message you will be mainly searching on a
combination of these three columns. The query processor will decide on
whether an index is useful using various algorithms.
> Regarding the INT data type for messageID, what would you suggest?
> Messages will often be deleted but the counter value will always
> progress...
You would have to determine the number of records and what growth you
are expecting, but when you could be mailing a significant number of
recipients then the maximum number offered by an INT datatype will
probably get used up quickly, therefore BIGINT may be better.
> I thought about using a uid, but they are not suitable for a Clustered
> Index since they are not guaranteed to be chosen in incremental
> order...
> Thanks again
> -Itai
>
> BTW does anyone know how to dump a table to a text file using command
> line with arguments?
BCP, DTS or even osql will do this, look at books online for more
information on these.
John