Anmol_Sharma Anmol_Sharma - 4 months ago 7
SQL Question

Running SQL script gives syntax error

I have written an sql script with the following contents :

CREATE DATABASE IF NOT EXISTS stock_trading;

USE stock_trading;

CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name,passwrd),
)ENGINE=InnoDB;


and every time I try to run it in the SQL command prompt it keeps giving away the error as:


ERROR 1064 (42000) at line 5 in file: 'db_script.sql': 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 ')ENGINE=InnoDB' at line 5


the script appears to be correct but I don't know why it keeps giving this error.

Additional information:

Operating System :
Arch Linux


Database :
MariaDB

Answer

You have a stray comma at the end of your table definition:

PRIMARY KEY (user_name,passwrd),
                              ^^^ remove this

Your full table definition:

CREATE TABLE IF NOT EXISTS transactions(
    user_name VARCHAR(30) NOT NULL,
    passwrd  BINARY(64) NOT NULL,
    balance_cash BIGINT NOT NULL DEFAULT 100000,
    PRIMARY KEY (user_name, passwrd)
) ENGINE=InnoDB;