问题
I use MySQL
5.6 command line client to create a simple database and I can create database yet to create my first table I have a syntax error that I cannot identify. Thanks for your help and sorry if there are English mispells it is not my first language.
Please find pasted the SQL
syntax error:
mysql> USE SYLVAINTEST
Database changed
mysql> CREATE TABLE students
-> (
-> studentid INT NOT NULL,
-> firstname VARCHAR,
-> lastname VARCHAR,
-> dob VARCHAR,
-> CONSTRAINT pk_students PRIMARY KEY (studentid)
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' lastname VARCHAR, dob VARCHAR, CONSTRAINT pk_students ' at line 4
mysql> CREATE TABLE students
-> (
-> studentid INT NOT NULL,
-> firstname VARCHAR ,
-> lastname VARCHAR ,
-> dob VARCHAR ,
-> CONSTRAINT pk_students PRIMARY KEY (studentid)
-> );
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' lastname VARCHAR , dob VARCHAR , CONSTRAINT pk_students PRIMARY KEY (' at line 4
回答1:
You should specify lenght for VARCHAR()
in following:
CREATE TABLE students
(
studentid INT NOT NULL,
firstname VARCHAR(40),
lastname VARCHAR(40),
dob VARCHAR(20),
CONSTRAINT pk_students PRIMARY KEY (studentid)
);
回答2:
You are missing length for VARCHAR datatypes
CREATE TABLE students
(
studentid INT NOT NULL, firstname VARCHAR(100),
lastname VARCHAR(100),
dob VARCHAR(20),
CONSTRAINT pk_students PRIMARY KEY (studentid)
);
Also make sure to use proper datetime datatype to store datatetime values
来源:https://stackoverflow.com/questions/31383044/sql-syntax-error-when-creating-table