Syntax error 1064 in CREATE TABLE statement with TINYTEXT columns?

前端 未结 1 1519
旧时难觅i
旧时难觅i 2021-01-28 18:40

This is the MySQL code I have so far:

CREATE DATABASE bankbase;

USE bankbase;

CREATE TABLE clienttable(
ClientID SMALLINT(15) NOT NULL DEFAULT 0,
ClientFirstNa         


        
1条回答
  •  孤独总比滥情好
    2021-01-28 19:11

    Here are the problems I see with your original script:

    1. BLOB and TEXT columns cannot have DEFAULT values.

    2. TINYTEXT is the same as VARCHAR(255), so you can't declare a maximum length for a TINYTEXT field because one is already implied.

    3. You need a space between the words PRIMARYKEY. It should be PRIMARY KEY.

    4. Finally, it isn't a problem per se, but in your first CREATE TABLE statement, you have odd spacing. Changing the ClientEmail line to the following makes it a lot more readable:

    Much better:

    ClientPhone CHAR(10) NOT NULL,
    ClientEmail TINYTEXT NOT NULL,
    

    See the MySQL documentation for more information. After all of those corrections, these are the working MySQL queries:

    CREATE DATABASE bankbase;
    USE bankbase;
        
    CREATE TABLE clienttable(
    ClientID SMALLINT(15) NOT NULL DEFAULT 0,
    ClientFirstName VARCHAR(30) NOT NULL DEFAULT "first name",
    ClientLastName VARCHAR(30) NOT NULL DEFAULT "last name",
    ClientPhone CHAR(10) NOT NULL,
    ClientEmail TINYTEXT NULL,
    ClientAddress TINYTEXT NOT NULL,
    PRIMARY KEY(ClientID)
    );
    
    CREATE TABLE branchtable(
    BranchID SMALLINT(15) NOT NULL DEFAULT 0,
    BranchCity TINYTEXT NOT NULL,
    BranchManagerFName VARCHAR(30) NULL DEFAULT "Branch Manager's First Name", 
    BranchManagerLName VARCHAR(30) NULL DEFAULT "Branch Manager's LAst Name",
    BranchPhone CHAR(10) NOT NULL,
    BranchEmail TINYTEXT NULL,
    PRIMARY KEY(BranchID)
    );
        
    CREATE TABLE transactiontable(
    TypeID SMALLINT(15) NOT NULL DEFAULT 0,
    Type ENUM('CHEQUING','SAVINGS') NOT NULL,
    TransAmount DECIMAL NOT NULL,
    TransDate TIMESTAMP NOT NULL,
    Balance DOUBLE NOT NULL,
    PRIMARY KEY(TypeID)
    );
    

    If you click Build Schema in this SQL fiddle, you'll see that it works!

    A bit more information on how to methodically fix errors like this

    If you're methodical, these problems are easy to solve, especially with CREATE TABLE statements. For example, when you're debugging the first CREATE TABLE statement, move through the column declarations one at at time.

    Try making a table with just the first column:

    CREATE TABLE clienttable(
    ClientID SMALLINT(15) NOT NULL DEFAULT 0);
    

    That code works so delete the table and add columns one by one until you add one that throws an error:

    DROP TABLE clienttable;
    CREATE TABLE clienttable(
    ClientID SMALLINT(15) NOT NULL DEFAULT 0,
    ClientFirstName VARCHAR(30) NOT NULL DEFAULT "first name",
    ClientLastName VARCHAR(30) NOT NULL DEFAULT "last name",
    ClientPhone CHAR(10) NOT NULL, ClientEmail 
    
    TINYTEXT(30) NULL);
    

    We get the error you asked about:

    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 '(30) NULL)' at line 7

    Now you know exactly which line has the error. I've even seen code written like this occasionally (this is the same code as in the previous example):

    CREATE TABLE clienttable(
    ClientID
    SMALLINT(15) 
    NOT NULL 
    DEFAULT 0,
    ClientFirstName 
    VARCHAR(30) 
    NOT NULL 
    DEFAULT "first name",
    ClientLastName 
    VARCHAR(30) 
    NOT NULL 
    DEFAULT "last name",
    ClientPhone 
    CHAR(10) 
    NOT NULL,
    ClientEmail 
    TINYTEXT(30) //Line 18 <- This is where the error occurs 
    NULL);
    

    Yes, it's not highly readable, but if we run it, we get a syntax error at line 18, i.e. the TINYTEXT(30) line. Reading the documentation, asking online, etc. would show you wy this is wrong. Once all the errors are fixed, make the code readable again and you're set.

    0 讨论(0)
提交回复
热议问题