简体   繁体   English

如何通过按时间顺序在php中跟踪mysql数据库表条目

[英]How to keep track of mysql database table entries by chronological insertion in php

I had originally wanted my alarmID value to be the primary key and to be Auto incremented. 我本来希望将alarmID值用作主键并自动递增。 But I have decided to make my Title value as so. 但我决定照此设定Title值。

How can I manually auto increment alarmID so that every time I insert values, the alarmID value gets incremented by exactly 1. 如何手动自动递增alarmID,以便每次插入值时,alarmID值都精确地增加1。

I want a way to keep track of entries by when they were inserted to be display chronologically later on. 我想要一种跟踪条目何时插入的方法,以便以后按时间顺序显示。

Here is how I have my php code. 这就是我的PHP代码的方式。

$sql = "CREATE TABLE IF NOT EXISTS alarms (
    alarmID INT NOT NULL,
    PRIMARY KEY (Title),
    Title CHAR(30) NOT NULL,
    Description TEXT,
    DT DATETIME
    )";

Something like this should work, you still get a unique indexed title and the auto increment alarmID, it's much more subtle than using a mysql function / proc. 像这样的事情应该可以工作,您仍然可以获得唯一的索引标题和自动递增的alarmID,这比使用mysql函数/ proc更加微妙。

CREATE TABLE IF NOT EXISTS alarms (
    alarmID MEDIUMINT NOT NULL AUTO_INCREMENT,
    Title CHAR(30) NOT NULL,
    Description TEXT,
    DT DATETIME,
    PRIMARY KEY (alarmID),
    UNIQUE KEY title (Title)
);

Your best bet here would be to make alarmID an auto incrementing primary key and, if Title has to be unique, place a unique constraint on it. 最好的选择是将alarmID设置为自动递增的主键,如果Title必须是唯一的,则对其施加唯一的约束。

Manually computing the new incremented ID could in fact lead to issues if multiple users use your system, so it is better to leave the job to the DBMS itself. 如果有多个用户使用您的系统,则手动计算新的递增ID实际上可能会导致问题,因此最好将工作留给DBMS本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM