A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Primary Keys



 
 
Thread Tools Display Modes
  #91  
Old July 18th, 2006, 01:34 PM posted to microsoft.public.access,comp.databases.ms-access
David W. Fenton
external usenet poster
 
Posts: 3,373
Default Primary Keys

"Jamie Collins" wrote in
oups.com:

[]

PLONK

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
  #92  
Old July 18th, 2006, 01:37 PM posted to microsoft.public.access,comp.databases.ms-access
David W. Fenton
external usenet poster
 
Posts: 3,373
Default Primary Keys

"Terry Kreft" wrote in
:

There are times when resorting to troll-like behaviour seems to be
the only way to get through to some people.


Or not.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
  #93  
Old July 18th, 2006, 01:59 PM posted to microsoft.public.access,comp.databases.ms-access
Jamie Collins
external usenet poster
 
Posts: 1,705
Default Primary Keys


Jamie Collins wrote:
Consider these tables:

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
PRIMARY KEY (col1, col2)
);

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
UNIQUE (col1, col2)
);

AFAIK in Jet terms the only difference differences between the tables
is that the one with the PRIMARY KEY constraint will cluster on (col1,
col2) whereas the one with the UNIQUE constraint will not (i.e. retain
date/time inserted order).

There is a further point:

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER,
PRIMARY KEY (col1, col2)
);

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER,
UNIQUE (col1, col2)
);

The difference here is that although the one with the PRIMARY KEY
constraint can be created, no row where col2 is NULL can be inserted.

I do not think there are any further differences. If anyone thinks
there are, could they please post them here.


I supposed I should state another obvious difference:

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
PRIMARY KEY (col1, col2),
PRIMARY KEY (col1, col2)
);

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
UNIQUE (col1, col2),
UNIQUE (col1, col2)
);

The one that attempts two PRIMARY KEY designations is illegal
(reflecting the fact a table can only have one clustered index) whereas
the one with two UNIQUE constraints is legal.

Have I omitted anything.

Jamie.

--

  #94  
Old July 18th, 2006, 05:41 PM posted to microsoft.public.access,comp.databases.ms-access
Lyle Fairfield
external usenet poster
 
Posts: 36
Default Primary Keys

Jamie Collins wrote:
Jamie Collins wrote:
Consider these tables:

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
PRIMARY KEY (col1, col2)
);

CREATE TABLE Test (
col1 INTEGER NOT NULL,
col2 INTEGER NOT NULL,
UNIQUE (col1, col2)
);

AFAIK in Jet terms the only difference differences between the tables
is that the one with the PRIMARY KEY constraint will cluster on (col1,
col2) whereas the one with the UNIQUE constraint will not (i.e. retain
date/time inserted order).


This is not my experience. I have found that when a Primary Key is not
created, but a Unique Not Null is, JET will treat the first Unique Non
Null in exactly the same way as a Primary Key, that is, on compact, it
will physically sort the records according to the first Unique Not Null
Index. I checked this a couple of days ago with a hex editor.

Of course, later specifying an index as "Primary Key" will shunt the
previous primary key to a position of lesse importance.

IMO "Primary Key" is a redundant term; it is simply a short way of
referring to the first created Unique Not Null Index. When I create
Tables with DDL I seldom bother to create a "Primary Key", but I do
create a primary key, that is I create a Unique Non Null Index.

  #95  
Old July 18th, 2006, 07:22 PM posted to microsoft.public.access,comp.databases.ms-access
Bri
external usenet poster
 
Posts: 8
Default Primary Keys

Jamie Collins wrote:

...
start_date DATETIME DEFAULT NOW() NOT NULL,
end_date DATETIME,
...



My start_date column has no time elements i.e. all start dates commence
at midnight, making it suitable for clustering.


I'm not getting into this argument, but thought I should point out that
these two parts of your post are inconsistant. The Now() function
returns the Date and Time of the instant it is executed so these dates
will NOT commence at midnight. To do that you should use the Date()
function rather than the Now() function.

--
Bri

  #96  
Old July 19th, 2006, 09:54 AM posted to microsoft.public.access,comp.databases.ms-access
Jamie Collins
external usenet poster
 
Posts: 1,705
Default Primary Keys


Bri wrote:
thought I should point out that
these two parts of your post are inconsistant. The Now() function
returns the Date and Time of the instant it is executed so these dates
will NOT commence at midnight. To do that you should use the Date()
function rather than the Now() function.


Yes, you are indeed correct.

I tried to point this out myself in the post you are replying to. I'll
selective quote myself in an attempt to make the point clearer:

I think I may have mislead you by this:
start_date DATETIME DEFAULT NOW() NOT NULL,

I intended for start_date values to have no time element i.e.
start_date DATETIME DEFAULT DATE() NOT NULL,

I was coding off the top of my head and messed up! Apologies.

For a more considered version (but similarly coded on the fly), see
this:

http://groups.google.com/group/micro...c3f233ba3336cc

Jamie.

--

  #97  
Old July 19th, 2006, 10:06 AM posted to microsoft.public.access,comp.databases.ms-access
Jamie Collins
external usenet poster
 
Posts: 1,705
Default Primary Keys


Lyle Fairfield wrote:
I have found that when a Primary Key is not
created, but a Unique Not Null is, JET will treat the first Unique Non
Null in exactly the same way as a Primary Key, that is, on compact, it
will physically sort the records according to the first Unique Not Null
Index. I checked this a couple of days ago with a hex editor.

Of course, later specifying an index as "Primary Key" will shunt the
previous primary key to a position of lesse importance.


Very interesting. I did not know that.

IMO "Primary Key" is a redundant term; it is simply a short way of
referring to the first created Unique Not Null Index. When I create
Tables with DDL I seldom bother to create a "Primary Key", but I do
create a primary key, that is I create a Unique Non Null Index.


I prefer an explicit syntax to one dependent on which NOT NULL UNIQUE
was created first. Ideally PRIMARY KEY would be a redundant term (as it
has effectively become IMO in SQL Server, for example) and we would
have an explicit CLUSTERED syntax (as for SQL Server, for example) .
However, because for Jet all we have is PRIMARY KEY then I will use it
(or misuse it, if you prefer g) to leverage the clustering
functionality.

Jamie.

--

  #98  
Old July 19th, 2006, 10:59 AM posted to microsoft.public.access,comp.databases.ms-access
Lyle Fairfield
external usenet poster
 
Posts: 36
Default Primary Keys

Jamie Collins wrote:
I prefer an explicit syntax to one dependent on which NOT NULL UNIQUE
was created first.


Upon further review I believe the NOT NULL UNIQUE index with name first
in alphabetical order will substitute as primary key when there is not
Primary Key named.

  #99  
Old July 19th, 2006, 11:12 AM posted to microsoft.public.access,comp.databases.ms-access
Jamie Collins
external usenet poster
 
Posts: 1,705
Default Primary Keys


Lyle Fairfield wrote:
Upon further review I believe the NOT NULL UNIQUE index with name first
in alphabetical order will substitute as primary key when there is not
Primary Key named.


Alpha order of name sounds a worse approach than date created order!

Out of interest, have you tested whether a compound index will
substitute as primary key? What about when one of the columns is
nullable?

TIA,
Jamie.

--

  #100  
Old July 19th, 2006, 02:13 PM posted to microsoft.public.access,comp.databases.ms-access
Lyle Fairfield
external usenet poster
 
Posts: 36
Default Primary Keys


Jamie Collins wrote:
Lyle Fairfield wrote:
Upon further review I believe the NOT NULL UNIQUE index with name first
in alphabetical order will substitute as primary key when there is not
Primary Key named.


Alpha order of name sounds a worse approach than date created order!

Out of interest, have you tested whether a compound index will
substitute as primary key? What about when one of the columns is
nullable?


not yet ...

 




Thread Tools
Display Modes

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

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


All times are GMT +1. The time now is 11:18 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.