I copied this code from here:
CREATE TABLE records(
email TEXT REFERENCES users(email),
lat DECIMAL,
lon DECIMAL,
depth TEXT,
upload_date TIMESTAMP,
comment TEXT,
PRIMARY KEY (upload_date,email)
);
CREATE TABLE samples(
date_taken TIMESTAMP,
temp DECIMAL,
intensity DECIMAL,
upload_date TIMESTAMP,
email TEXT,
PRIMARY KEY(date_taken,upload_date,email),
FOREIGN KEY (upload_date,email) REFERENCES records(upload_date,email)
);
The first thing that caught my eyes was the use of natural composite keys as primary keys for both tables.
3 things I was able to extract from this piece of code:
- The
users
table (not shown here) usesemail
as primary key of typetext
.. - The
records
table uses a composite key oftext
+timestamp
. - The
samples
table uses a composite key of 3 fields of typetext
+timestamp
+timestamp
.
Now in this case wouldn't a surrogate key be better of identification? I mean performance wise indexing an int
should be better than indexing a text
? Is there something that could make a surrogate key a bad choice?
ntext
,text
, andimage
data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Usenvarchar(max)
,varchar(max)
, andvarbinary(max)
instead.. And for SQL Server this wouldn't work - you cannot use aTEXT
column in an index - an index entry can be 900 bytes at most