Syntax error 1064 in CREATE TABLE statement with TINYTEXT columns?

青春壹個敷衍的年華 提交于 2019-12-02 09:22:37

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.

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