SQL syntax error when creating table

2019-06-14 19:48发布

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

2条回答
我只想做你的唯一
2楼-- · 2019-06-14 20:19

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

查看更多
我想做一个坏孩纸
3楼-- · 2019-06-14 20:34

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)
 );
查看更多
登录 后发表回答