SQL syntax error when creating table

自闭症网瘾萝莉.ら 提交于 2020-01-03 06:40:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!