English 中文(简体)
处理日期/时间的复合主要钥匙或代用品
原标题:Composite primary keys or surrogates when dealing with date/time

我就此进行了很多讨论。 我只想征求你对此的建议。 基本上,我使用的是PHP和MySQL。 页: 1 表格:

         users
------------------------------
uid(pk) | username | password
------------------------------
   12   |  user1   | hashedpw
------------------------------

供用户更新的表格

                updates
--------------------------------------------
uid |        date         |     content
--------------------------------------------
 12 | 2011-11-17 08:21:01 | updated profile
 12 | 2011-11-17 11:42:01 | created group
--------------------------------------------

用户简介页面将显示用户最新5次。 问题是:

  1. For the updates table, would it be possible to set both uid and date as composite primary keys with uid referencing uid from users
  2. OR would it be better to just create another column in updates which auto-increments and will be used as the primary key (while uid will be FK to uid in users)?
最佳回答

您的想法(第1款)所依据的假设是,用户可在第二版内never<>上填写两个“最新情况”。 这设计很差。 你们从来没有知道你今后将履行什么职能,但机会是,大约一天1点击会导致2项行动,因此本表有2条线。

我说的是“最新消息”,因为我认为这是一张伐木表。 谁知道你今后可能想在某个地方做些什么。

至于非同寻常的初级钥匙:不这样做,几乎总是在你面前回头,你不得不做大量工作,在之后添加一个适当的自动加固的钥匙。

问题回答

这取决于这一要求,但第三种可能性是,你可以确定关键(设计、日期、内容)。 您还可以增加一个代议钥匙,但在这种情况下,你大概希望执行。 关键因素——复合和代谢——不仅仅是一个。 不要犯你必须作选择的错误。

添加替代物是否有用取决于其使用方式——不增加替代物,除非或直到您需要。 无论如何,我假定是用户表上的一个外国关键参考资料。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...