Showing posts with label keys. Show all posts
Showing posts with label keys. Show all posts

Thursday, March 8, 2012

Creating relationships with MSDE

Is it possible to create relationships between tables using foregin keys with MSDE. I have looked for any information and can't seem to find anything.With MSDE you can use Visual Studio and MS Access. With Access you have to create new Data Project. Also you can use SQL Server Enterprise Manager to create relationships. But, you can be always more elite and use oSQL and create relations using SQL.

If you prefer writing SQL for relations then you should read T-SQL documentation. Check out chapters for CREATE TABLE and ALTER TABLE statements.|||Yes. You can create realationships as mentioned. Is there specific information you would like to know.|||Thanks Guys.

Helps to know that I wasn't going to crazy trying to figure it out.

Wednesday, March 7, 2012

creating primary keys

Is there any way to speed up Primary key creation on a
table
I have a 60o million row table and its taking forever
I know that data in table ic clean in data in PK columns i
am ccreating is unique
alter table Profile_table add primary key (as_of_date,
PAN11_ACCOUNT_NUMBER, PRODUCT_CODE)In addition to Brian's replay try to avoid using EM in such cases.
Go to QA and there do a creation.
"Brian Moran" <brian@.solidqualitylearning.com> wrote in message
news:e6q6sGbcDHA.1368@.TK2MSFTNGP11.phx.gbl...
> by default... a Primary Key is a clustered index. So the question is reall
> the same as 'how can I make a clustered index creation go faster'.
> You might find more info if you're searching on index creation...
> * what sort of hardware are you running on? SQL will take good advantage
of
> SMP, lots of memory, and fast disk for creating a large index like this...
> * there's no way to avoid the unique check if that's one thing you're
> wondering about. Doesn't matter if you KNOW that the data is unique... SQL
> will still check it...
> * take a look at the BOL entry for Create Index to make sure you
understand
> some of the basics around what happens if you have other indexes on the
> table already...
> also, I noticed that your key has a column in it that implies it's a
> datetime. Is that true? You should know that a PK has to be unique and
that
> the datetime datatype is only accurate to +-3MS. Therefore... SQL Server
> treats the datetime of '2003-01-01 00:00:00.000' as being identical to
> '2002-12-31 23:59:59.999'. This can create problems if you have a date as
> part of a PK...
>
> declare @.date1 datetime
> ,@.date2 datetime
> set @.date1 = '2003-01-01 00:00:00.000'
> set @.date2 = '2002-12-31 23:59:59.999'
> if @.date1 = @.date2 print 'the dates match'
> --
> Brian Moran
> Principal Mentor
> Solid Quality Learning
> SQL Server MVP
> http://www.solidqualitylearning.com
>
> "sanjay" <sanjayg@.hotmail.com> wrote in message
> news:059e01c371aa$1054d580$a301280a@.phx.gbl...
> > Is there any way to speed up Primary key creation on a
> > table
> > I have a 60o million row table and its taking forever
> >
> > I know that data in table ic clean in data in PK columns i
> > am ccreating is unique
> >
> > alter table Profile_table add primary key (as_of_date,
> > PAN11_ACCOUNT_NUMBER, PRODUCT_CODE)
> >
> >
>

Saturday, February 25, 2012

Creating new tables in aspnetdb.mdf

I have made a new table called 'customer' which i wish to tie into the userId of the db. I have used a db diagram to do this (there are keys on each side of the link and userId is the FK) . When i put the membership/profile view onto the form alongside the new customer table nothing displays in the customer table when i run the app., i dont even see the titles - any ideas (i'm new to this sql malarky btw - so its probably something unbelievably straightforward). Any help appreciated.

Thanks

Hi,

By default, the membership/profile view will not show your additional information.

Since you have added one table into the database, and need to show it on your view, you will need to use your own query to get the user info together with the data in customer table. This can be done either by creating a View or use JOIN statement directly.

Then, you can customize the web UI to show the additional info.

HTH. If this does not answer your question, please feel free to mark the post as Not Answered and reply. Thank you!

|||

Hi, and thanks for replying.

I sort of rather optimistically and naively thought that because my new table was using pre-defined field variables including the key field (i.e. UserId (key), UserName etc.) that the system would know that these were what I required to be displayed (through the diagram), with no further programming, except to set to set the new field variables, that I've added, to new values within the program, and that the new fields would simply display the default value until I did something with them. - in another world that might have happened. After all, what have I created the diagram for?

What I didnt realise was that the aspnet.mdf tables are created, configured, and deleted through the Stored Procedures and that I needed to do the same for my table, even though there is a diagram telling the system that this 'linking in' of the aspnet_userId and my table is what I want to happen .

As I'm using the UserId as my key field I want the table to reflect the same membership as the aspnet.mdf membership tally - so I've sneaked in one or two statements within the createuser and deleteuser Stored Procedures to put in or remove UserId values from my table as new members are created or deleted respectively. I hope that that will solve my problem - it seems to have so far. I just cant help thinking that somewhere along the way my table is not going to tally with aspnet users for some reason, due to something that I'm not aware of (yet), caused by some insertion or deletion of a UserId without the createuser or deleteuser Stored Procedures being involved.

I'm sort of at a loss as to why I've created the diagram. I could now delete it and nothing would change.

Thanks again.

Creating multiple primary keys

Hi!
I have this scenario:
One big table which will be populated with playlists; each playlist is
defined by:
1) tv channel
2) playlist date
Each playlist has many rows; each rows is defined by:
1) tv channel (as said before)
2) playlist date (as said before)
3) playlist onair hour
I must keep in this table these data for example for 2 months with 10
channels and 1000 rows for each playlist.
In my import procedure I have to do some calculations, so I prefer to load
data first in a temporary table, do my calculations and then make an insert
block of all 1000 rows in 1 step with a stored procedure from the temporary
table to the target table when all calculations and editing is ended: insert
into maintable execute('select * from temporarytable'). Then delete the
content of temporary table and import another playlist.
What kind of table structure would you use?
I think the best choice would be a multiple primary key for each row with
the fields 1,2 and 3.
Do you see some better solution? Is a multiple primary key very expensive in
terms of database performance?
I think using an autoincrement primary ID would not help because the insert
block would not work with it.
Any ideas?
Thanks!
Jem777> I think using an autoincrement primary ID would not help because the
> insert
> block would not work with it.
Not sure what you mean, because if you cannot insert data into the table, it
would be valueless for everyone. Either way, you need to have a valid
natural key of some sort defined or you will get logically duplicated data
(the rows may be different, other than the meaningless key)

> Do you see some better solution? Is a multiple primary key very expensive
> in
> terms of database performance?
Your first concern should be the validity of the data. If you only have one
table, and it is not related to any other tables, then the size of the key
will make very little (if any) difference

> I think the best choice would be a multiple primary key for each row with
> the fields 1,2 and 3.

> 1) tv channel (as said before)
> 2) playlist date (as said before)
> 3) playlist onair hour
This is probably a good key, because there is no way you will have two
programs on the same TV channel at the same time. I will go out on a limb
and say that this is not a great table structure, and is going to be wildly
denormalized, but I don't know what you are doing with it, so who knows if
it matters :)
----
Louis Davidson - drsql@.hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design -
http://www.apress.com/book/bookDisplay.html?bID=266
Blog - http://spaces.msn.com/members/drsql/
Note: Please reply to the newsgroups only unless you are interested in
consulting services. All other replies may be ignored :)
"jem777" <camillo@.rockit.it> wrote in message
news:eVBKSj5OFHA.1440@.TK2MSFTNGP10.phx.gbl...
> Hi!
> I have this scenario:
> One big table which will be populated with playlists; each playlist is
> defined by:
> 1) tv channel
> 2) playlist date
> Each playlist has many rows; each rows is defined by:
> 1) tv channel (as said before)
> 2) playlist date (as said before)
> 3) playlist onair hour
> I must keep in this table these data for example for 2 months with 10
> channels and 1000 rows for each playlist.
> In my import procedure I have to do some calculations, so I prefer to load
> data first in a temporary table, do my calculations and then make an
> insert
> block of all 1000 rows in 1 step with a stored procedure from the
> temporary
> table to the target table when all calculations and editing is ended:
> insert
> into maintable execute('select * from temporarytable'). Then delete the
> content of temporary table and import another playlist.
> What kind of table structure would you use?
> I think the best choice would be a multiple primary key for each row with
> the fields 1,2 and 3.
> Do you see some better solution? Is a multiple primary key very expensive
> in
> terms of database performance?
> I think using an autoincrement primary ID would not help because the
> insert
> block would not work with it.
> Any ideas?
> Thanks!
> Jem777
>|||You made me think about it and maybe I found a solution!
MAINTABLE:
3 id_palinsesto int 4 0 (primary key)
0 id_tmp int 4 0
0 data datetime 8 0
0 rete int 4 0
0 ora int 4 -1
TMPTABLE:
3 id_tmp int 4 0 (primary key)
0 data_palinsesto datetime 8 1
0 rete int 4 1
0 ora int 4 1
The copy query:
insert into MAINTABLE(id_tmp,data,rete,ora) execute('select
id_tmp,data,rete,ora from TMPTABLE)
The 2 primary keys are not linked each other!!!!
I did not know it was possible!!!
I dont have to use multiple primary keys!!!
COOL!
Thanks,
jem777
"Louis Davidson" <dr_dontspamme_sql@.hotmail.com> wrote in message
news:uQY20s5OFHA.1396@.TK2MSFTNGP10.phx.gbl...
> Not sure what you mean, because if you cannot insert data into the table,
it
> would be valueless for everyone. Either way, you need to have a valid
> natural key of some sort defined or you will get logically duplicated data
> (the rows may be different, other than the meaningless key)
>
expensive
> Your first concern should be the validity of the data. If you only have
one
> table, and it is not related to any other tables, then the size of the key
> will make very little (if any) difference
>
with
>
> This is probably a good key, because there is no way you will have two
> programs on the same TV channel at the same time. I will go out on a limb
> and say that this is not a great table structure, and is going to be
wildly
> denormalized, but I don't know what you are doing with it, so who knows if
> it matters :)
>
> --
> ----
--
> Louis Davidson - drsql@.hotmail.com
> SQL Server MVP
> Compass Technology Management - www.compass.net
> Pro SQL Server 2000 Database Design -
> http://www.apress.com/book/bookDisplay.html?bID=266
> Blog - http://spaces.msn.com/members/drsql/
> Note: Please reply to the newsgroups only unless you are interested in
> consulting services. All other replies may be ignored :)
> "jem777" <camillo@.rockit.it> wrote in message
> news:eVBKSj5OFHA.1440@.TK2MSFTNGP10.phx.gbl...
load
with
expensive
>|||>> I think the best choice would be a multiple primary key for each row
with
the fields [sic] 1,2 and 3. <<
In SQL Server, the temporal data type has both the date and time.
Generally speaking, you want to show durations of time, not points. You
also never told us what is playing -- TV shows? Here is my guess.
CREATE TABLE PlayLists
(tv_channel CHAR (5) NOT NULL,
time_slot_start DATETIME NOT NULL,
time_slot_end DATETIME NOT NULL,
CHECK (time_slot_start < time_slot_end),
show_name CHAR(15) NOT NULL,
PRIMARY KEY (tv_channel, time_slot_start ));
You can now use BETWEEN predicates with the time slot durations for
your queries.
performance? <<
No. Size, grandularity and the order of the columns in the declaration
are more important.
insert block would not work with it. <<
You do know that an autoincrement extension is not relational,
proprietary and dangerous to data integrity, don't you? By definition
it can never be a key. You already have a natural thanks to the
physics of the situation, so quit trying to mimic a magnetic tape file.|||>>> I think using an autoincrement primary ID would not help
> insert block would not work with it. <<
> You do know that an autoincrement extension is not
> relational,
> proprietary and dangerous to data integrity, don't you?
> By definition
> it can never be a key. You already have a natural thanks
> to the
> physics of the situation, so quit trying to mimic a
> magnetic tape file.
Run for your lives! Data integrity will be lost using
identity columns! Check.
Hate to tell you Joe, but most younger people don't even
know anyone that has even heard of someone using mag tapes.
;->
Suppose, the tv_channel description for a given entity
changes or is entered wrong? Same entity, different
description. That means that all tables that join to
Playlists will also have to be changed along with all
records in the Playlists table. Suppose on entry, you don't
know the tv_channel and do know the timeslot or the other
way around or don't know either for sure? That means that
entity could not be entered into the system until that is
known or a bogus value must be entered. (Other logic can
determine whether a given Playlist item was valid (meaning
it needed a channel and timeslot)).
Using TV_Channel/Timeslot only works if both values are
absolutely know at time of entry and have an extraordinarily
high probability that they will not change over the lifetime
use of the application.
Thomas|||> No. Size, grandularity and the order of the columns in the declaration
> are more important.
Just to clarify, because when I first read this statement it seemed a bit
ambiguous. You are speaking of the declaration of the primary key, not of
the table/columns.|||On Thu, 7 Apr 2005 20:34:40 +0200, jem777 wrote:
(snip)
>The copy query:
>insert into MAINTABLE(id_tmp,data,rete,ora) execute('select
>id_tmp,data,rete,ora from TMPTABLE)
Hi jem777,
Are you aware that you don't need to use dynamic SQL for this?
INSERT INTO MainTable (id_tmp,data,rete,ora)
SELECT id_tmp,data,rete,ora
FROM TmpTable
will work just as well!
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||>> Suppose, the tv_channel description for a given entity changes or is
entered wrong? Same entity, different
description. <<
We could use a reference to another table of televison stations which
is keyed on call letters, and let DRI actions handle this.
know at time of entry and have an extraordinarily
high probability that they will not change over the lifetime use of the
application. <<
I am pretty certain that time will keep going on forever and that I can
break it into timeslots. I can then handle changing the call letters
with DRI actions the few rare times that happens. My one problem is
not knowing the show that is going to run. Having consulted at BRTN in
Belgium, I know that there is no industry standard for identifying
television shows.
I might want to allow time_slot_end to be NULL, so i can have
open-ended time slots.|||
> I am pretty certain that time will keep going on forever and that I can
> break it into timeslots.
Is this really something you want to risk? Stranger things have happened,
you know. Heck, just a few centuries ago they changed our caledar around. An
d
then there's crazy always changings things like daylight savings time.
Besides, what happens if they'll want to run shows in such a small timeslot
that SQL Server can't even differentiate between STart and End?
No, I say make a [DateTime] table: ID IDENTITY INT, DATE DATETIME. That way,
if the definition of "April 07, 2005 09:45PM" ever changes, you'll be set.
-- Alex Papadimoulis
As if it wasn't obvious, it's MonthID 6 (I misspelled February wrong twice
in my calendar and had to delete those entries). I have three more ws of
this!|||<snip>I am pretty certain that time will keep going on forever ...</snip>
I don't know about that, Joe! I just got done with a book on G?del and
Enstein that says There's no such thing as Time...
"--CELKO--" wrote:

> entered wrong? Same entity, different
> description. <<
> We could use a reference to another table of televison stations which
> is keyed on call letters, and let DRI actions handle this.
>
> know at time of entry and have an extraordinarily
> high probability that they will not change over the lifetime use of the
> application. <<
> I am pretty certain that time will keep going on forever and that I can
> break it into timeslots. I can then handle changing the call letters
> with DRI actions the few rare times that happens. My one problem is
> not knowing the show that is going to run. Having consulted at BRTN in
> Belgium, I know that there is no industry standard for identifying
> television shows.
> I might want to allow time_slot_end to be NULL, so i can have
> open-ended time slots.
>

Sunday, February 19, 2012

Creating Foreign Key constraint

I have two tables; each has multi-column primary keys. I need to create a foreign key relationship between the two tables.

Sales:

DKey, int, PK

OKey,int, PK

RKey,int, PK

...

Rep:

Bkey, int, PK, identity

Rkey, int, PK

...

When I try to like Rkey from both tables as a foreign key relationship, I get an error that the columns in one table "do not match an existing primary key or UNIQUE constraint. (I've tried it both ways and get the same error.)

How can I link these two tables?

Thanks.

Make an unique index on Rkey from Rep or Sales depends on the table you can delegate "Primary Key" (use Database Diagram for usability)

|||

Since you did not include rationale for the key selection, this is going to be just a guess.

The purpose of a Primary Key is to uniquely identify a row of data. It looks like each Rep is uniquely identified with the IDENTITY field BKey. I'm not sure why you have RKey also involved in the Primary Key for Rep.

You could recreate the Primary key for Rep using only the Bkey, and then add the Bkey field to Sales to relate Sales to Rep.

OR, if RKey is unique, use it as a Primary Key.

However, if for some silly reason you HAVE to have a combination of Bkey and Rkey as the Primary key in Rep, you will have to add both columns to Sales in order to create the relationship.

As a side note, the use of Bkey, Rkey, etc. for column names is very 'odd', and not only adds confusion, but will be difficult to maintain the pattern for all tables. A 'best practice' naming convention for an IDENTITY field (such as Bkey) is to use the table name and the suffix [ID]. So a preferred name for Bkey would be RepID. The name instantly communicates where the field comes from and what it is, expecially useful when it is added to another table.

|||

I was trying to simplify the question....I probably should have included more details. I'm sorry, I never know how much detail to go into.

What I'm actually trying to do is create a many to many link between:

Sales:

DateKey, int, PK

OrganizationKey, int, PK

CustomerKey, int, PK

RepBridgeKey, int, PK

....

RepBridge:

RepBridgeKey, int, PK, identity

RepKey, int, PK

Rep:

RepKey, int, PK, identity

.....

I can link RepBridge and Rep via RepKey but not RepBridge with Sales via RepBridgeKey.

From your answer, it looks like I would have to include RepKey in my Sales table and do the FK link on both fields?

Thanks.

|||

No, I think you can only make an unique index on RepBridgeKey from RepBridge (is possible because is identity)

You can make the following links vis-a-vis your vision of table schema:

Rep <RepKey-->RepBridge

RepBridge <RepBridgeKey>Sales

|||

John,

In RepBridge, I'm still not sure why you need RepKey as part of the Primary Key. It seems like BridgeRepKey is a unique identifier and would adequately serve as the PK (and the FK in Sales).

RepBridgeKey 'should' be all you need.

However, if, as said earlier, there is a business reason to complicate the Primary key in BridgeRep by adding RepKey -then you will have to add RepKey to Sales -at which point, the RepBridge table seems unnecessary. You would have to duplicate the rows in Sales for each Rep if there are multiple Reps involved with a single Sale.

Your sales table probably needs a better defined primary key. Typically a unique sales identifier is created -something like an INVOICE number, SalesOrder number, etc. In your Sales table above, only one sale would be allowed per day per Rep. That may be reasonable, but is it realistic? I would prefer a [SalesID int IDENTITY Primary Key] field

Typically, a 'bridging' table will have an IDENTITY field as PK, and the PK's from BOTH others tables as FK.

RepBridge

RepBridgeKey, int IDENTITY PK

RepKey FK to Rep

SalesKey Fk to Sales

|||

I see what you're saying....the bridge table does not need the RepKey as part of its primary key. I was thinking that each new sales rep would have to trigger a new bridge record but this can be done with only one PK identity field.

There is a reason behind the key fields in the sales table, but perhaps I shall also revisit that side issue.

Thanks again.

Creating Fireign Keys....How?

Hi!!
How I can create foreign keys using Enterprise Manager without writing any
SQL commands'
Thanks in advance!
TimurHi,
Inside the table creation option from Enterprise manager, click the manage
relationship icon.
Thanks
Hari
MCDBA
"" <tim_@.pochtamt.ru> wrote in message
news:Oa3RUvY8DHA.2560@.TK2MSFTNGP09.phx.gbl...
> Hi!!
> How I can create foreign keys using Enterprise Manager without writing any
> SQL commands'
> Thanks in advance!
> Timur
>

Creating Fireign Keys....How?

Hi!!
How I can create foreign keys using Enterprise Manager without writing any
SQL commands'
Thanks in advance!
TimurHi,
Inside the table creation option from Enterprise manager, click the manage
relationship icon.
Thanks
Hari
MCDBA
"ôÉÍÕÒ" <tim_@.pochtamt.ru> wrote in message
news:Oa3RUvY8DHA.2560@.TK2MSFTNGP09.phx.gbl...
> Hi!!
> How I can create foreign keys using Enterprise Manager without writing any
> SQL commands'
> Thanks in advance!
> Timur
>