I’m trying to create mysql db table as below, but it got a syntax error at line 4.
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
current_role VARCHAR(100) NOT NULL, #here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
)
ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci ;
the error description:
#1064 – 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 ‘current_role VARCHAR(100) NOT NULL,
lat DOUBLE NOT NULL,
lng DOUBLE NOT N…’ at line 4
>Solution :
CURRENT_ROLE() is actually a MySQL/MariaDB system function which returns active roles for the current session. So in your CREATE TABLE statement, the database thinks you are trying to call this function. You should call the current_role column something else. If you must stick with this name, then will have to forever refer to it using backticks:
CREATE TABLE findable_drivers (
id BIGINT NOT NULL AUTO_INCREMENT,
driver_id BIGINT NOT NULL,
`current_role` VARCHAR(100) NOT NULL, -- must use backticks here
lat DOUBLE NOT NULL,
lng DOUBLE NOT NULL,
findable BOOLEAN DEFAULT 0 NOT NULL,
CONSTRAINT findable_drivers_PK PRIMARY KEY (id),
CONSTRAINT findable_drivers_UN UNIQUE KEY (driver_id),
CONSTRAINT findable_drivers_driver_FK FOREIGN KEY (driver_id) REFERENCES driver(id)
) ENGINE=InnoDB
DEFAULT CHARSET=latin1
COLLATE=latin1_swedish_ci;