I am using below CREATE TABLE statement
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(MAX) NOT NULL,
PRIMARY KEY (`uuid`)
);
However I keep getting this error.
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
'MAX) NOT NULL,
PRIMARY KEY (uuid)
)' at line 3
Makes no sense to me.
MAX is not suported for this use, it is reserved for the MAX function. Use the equivalent number instead, check this out: Equivalent of varchar(max) in MySQL?
This will work for you. MAX is reserved keyword. Specify exact number of varchar instead of max. However, varchar(MAX) will work in SQL SERVER 2005+.
CREATE TABLE IF NOT EXISTS users (
uuid varchar(36) NOT NULL,
json varchar(21808) NOT NULL,
PRIMARY KEY (uuid)
);
FIDDLE
MAX() is a function in MySql,So if you want to declare the size to the max.please refer the below example.
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(65535) NOT NULL,
PRIMARY KEY (`uuid`)
);
and if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.
mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)
Related
This is the code
CREATE TABLE `church` (
`ID` int(10) UNSIGNED NOT NULL,
`StudentID` int(11) NOT NULL,
`semesterID` int(11) NOT NULL,
`attendedWed` int(11) NOT NULL,
`attendedFri` int(11) NOT NULL,
`attendedSabM` int(11) NOT NULL,
`attendedSabE` int(11) NOT NULL,
`ChurchScore` double(10,2) GENERATED ALWAYS AS ((((((`attendedWed` + `attendedFri`) + `attendedSabM`) + `attendedSabE`) * 100) / 60)) STORED
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is the error
Operation failed: There was an error while applying the SQL script to
the database. Executing: ALTER TABLE citizenshipgroup3.church
CHANGE COLUMN ChurchScore ChurchScore DOUBLE(10,2) NULL DEFAULT
attendedWed ;
ERROR 1064: You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'attendedWed' at line 2 SQL Statement: ALTER TABLE
citizenshipgroup3.church CHANGE COLUMN ChurchScore
ChurchScore DOUBLE(10,2) NULL DEFAULT attendedWed
Your syntax is fine. The problem is that MySQL does not support generated columns until 5.7. You are presumably using an earlier version.
Probably the simplest solution is to use a view for the calculation.
The error appears at the time of ALTER TABLE, when you are trying to modify the column ChurchScore setting the DEFAULT value to an expression involving another Column attendedWed.
Also, your error message seems to be originating from MariaDB, not MySQL.
From Mariadb Documentation:
From MariaDB 10.2.1 you can use most functions in DEFAULT. Expressions
should have parentheses around them. If you use a non deterministic
function in DEFAULT then all inserts to the table will be replicated
in row mode. You can even refer to earlier columns in the DEFAULT
expression:
CREATE TABLE t1 (a int DEFAULT (1+1), b int DEFAULT (a+1));
CREATE TABLE t2 (a bigint primary key DEFAULT UUID_SHORT());
So you need to ensure couple of things:
Upgrade your MariaDB version to 10.2.1 and above. Preferably, upgrade to current latest version (it is 10.3+ right now).
You need to use parentheses around the expression specified in the DEFAULT clause.
So the ALTER TABLE statement would look like:
ALTER TABLE `citizenshipgroup3`.`church`
CHANGE COLUMN `ChurchScore` `ChurchScore` DOUBLE(10,2) NULL
DEFAULT (attendedWed) ;
I ran into a new problem today, when I was dealing with a mysql query that works on 10.1.19-MariaDB localhost, but not on MySQL 5.7.21-0ubuntu0.16.04.1-log:
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE position (position)
) ENGINE=InnoDB CHARSET=utf8
In MySQL 5.6 it works without any hickups (fiddle), however, in MySQL 5.7.21 (fiddle) it throws:
1064 - 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 'position (position)) ENGINE=InnoDB CHARSET=utf8' at line 1
I figured out a solution by replacing UNIQUE position (position) with UNIQUE (position).
But I am wondering, what the underlying problem is, why it works with the other db system, and I am not sure if my solution is correct.
MySQL doesn't allow you to create an index with the same name of the column
The following code works for MySQL (fiddle)
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE idx_position (position)
) ENGINE=InnoDB CHARSET=utf8
That's why it is so important to wrap field and table names in backticks in MySQL (and their derivatives):
Try this (same query and index names, only added backticks):
CREATE TABLE `testing` (
`pageid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`position` SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (`pageid`),
UNIQUE `position` (`position`)
) ENGINE=InnoDB CHARSET=utf8;
I am using MySQL version 5.7.18 and MySQL Workbench is targeting version 5.6.20. However, when I try and create a table using the table generator it produces the following sql statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () VIRTUAL,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`));
But this produces the following error message:
Error 1064: 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 ') STORED',
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))' at line 2
SQL Statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () STORED,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))
Why would this be happening and how would I go about fixing this?
I have no idea what an empty expression means for a computed column. Nor do I understand a VIRTUAL column being a primary key. I would use:
CREATE TABLE sys.`annotations` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`annotation` LONGTEXT NOT NULL
);
I am getting a SQL query syntax error while creating a table. Here is the SQL query:
CREATE TABLE ACCOUNT (
ACCNO NUMBER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BA L NUMBER(8,2) NOT NULL,
CREATION-DT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
Here is the error:
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 'NUMBER(5) NOT NULL.
What is wrong with my syntax?
Using SQLfiddle you can see this is not valid.
You have multiple problems with your syntax; invalid data types, invalid column names, etc. You can encapsulate the invalid names in backticks but then you will have to remember to encapsulate them later.
Instead of Number you probably meant numeric but i would suggest trying Integer instead.
Or just try this:
CREATE TABLE ACCOUNT (
ACCNO INTEGER(5) NOT NULL,
NAME VARCHAR(20) NOT NULL,
BAL INTEGER(8) NOT NULL,
CREATIONDT DATE NOT NULL,
PRIMARY KEY ( ACCNO )
);
NUMBER is not a valid datatype in MySQL, you should choose the one from the list (maybe you've meant NUMERIC).
You should also escape some identifiers (in your case, column names BA L, which includes a space within it, and CREATION-DT, which includes a dash) with backticks ` in MySQL:
CREATE TABLE `ACCOUNT` (
`ACCNO` NUMERIC(5) NOT NULL,
`NAME` VARCHAR(20) NOT NULL,
`BA L` NUMERIC(8,2) NOT NULL,
`CREATION-DT` DATE NOT NULL,
PRIMARY KEY ( `ACCNO` )
);
SQLFiddle
I am just starting with SQL syntax, and am trying to create a table.
Here is my error:
#1064 - 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 'CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName), ) ENGINE = INNODB' at line 7
And here is my SQL:
CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` TEXT NOT NULL,
`firstName` TEXT NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id)
CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName),
) ENGINE = INNODB;
I tried this in NodeDB (which I am developing in), and then PHPMyAdmin.
Fix the comma and make the names varchar():
CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) NOT NULL,
`firstName` varchar(255) NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id),
CONSTRAINT uc_people_2nd UNIQUE (lastName, firstName)
) ENGINE = INNODB;
This works on SQL Fiddle.
Note that you don't have to give a unique constraint a name. You can also drop the constraint keyword, so the following works just fine:
UNIQUE (lastName, firstName)
EDIT:
The text data type is described here on the page with other "large-objects". These are special types that are arbitrarily long (think megabytes). They have limits when used in indexes. In particular, they need a length prefix. So, you cannot declare that a text column is unique. Only that they are unique in the first N characters (up to about 1000).
For names, that is way overkill. MySQL supports string types of various sorts. The most useful is varchar(). These are appropriate for a name field. They can be used with indexes easily. And MySQL supports a plethora of functions on them.
In other words, if you do not know what text is, you do not need it. Learn about and use varchar() and char() (or nvarchar() and nchar() if you need national character set support). Forget about text. One day if you need it, you'll rediscover it.