当前位置: 动力学知识库 > 问答 > 编程问答 >

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. But I have decided to make my Title value as so.

How can I manually auto increment alarmID so that every time I insert values, the alarmID value gets incremented by exactly 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.

$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.

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.

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: