programing

MariaDB 계산된 필드에 고유 제약 조건 추가

skycolor 2023. 8. 5. 10:01
반응형

MariaDB 계산된 필드에 고유 제약 조건 추가

동일한 ID에서 2분마다 두 개 이상의 트랜잭션이 발생하지 않도록 테이블에 고유한 제약 조건을 추가하려고 합니다. 하지만 MariaDB는 이를 좋아하지 않는 것 같습니다.

ALTER TABLE expenses ADD CONSTRAINT UNIQUE (id, FLOOR(UNIX_TIMESTAMP(date_inserted)/120));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UNIX_TIMESTAMP(date_inserted)/120))' at line 1

이것을 달성할 수 있는 방법이 있습니까?

감사해요.

생성된 열을 만들고 이 열을 인덱스에 사용합니다.

예를 들어

CREATE TABLE `t` (
  `salesperson` varchar(10) DEFAULT NULL,
  `id` int(11) DEFAULT NULL,
  `date_inserted` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
alter table t
    add column uts int as (floor(UNIX_TIMESTAMP(date_inserted)/120));
ALTER TABLE t 
 ADD CONSTRAINT UNIQUE iddt (id, uts);

언급URL : https://stackoverflow.com/questions/71193161/mariadb-add-unique-constraint-on-calculated-field

반응형