Unexpected output Dbdelta WordPress - sql

I have looked at many other questions on here relating to this issue but none so far have been able to resolve mine.
As per the codex on dbDelta. SQL statements must:
You must put each field on its own line in your SQL statement.
You must have two spaces between the words PRIMARY KEY and the
definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you
must include at least one KEY.
You must not use any apostrophes or backticks around field names.
Field types must be all lowercase.
SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
The error message I'm getting is:
WordPress database 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 '(
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NUL' at line 1]
But to my eyes my SQL statement conforms to dbDelta's requirements. NB the variables $table_name and $charset_collate are defined earlier in the function.
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
statistics longblob NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";

How about this :
$sql = "CREATE TABLE " . $table_name. " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
statistics longblob NOT NULL,
PRIMARY KEY (id)
) " . $charset_collate . ";";

Related

H2 refuses to create auto_increment for Postgres emulated database

I created an in memory H2 database with JDBC URL
jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
The H2 web console refuses to let me do an auto_increment. I've seen serial for Postgres, but that doesn't work either.
At it's simplest, it hates:
create table test(id bigint auto_increment);
Syntax error in SQL statement "create table test(id bigint [*]auto_increment)"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create table test(id bigint auto_increment) [42001-214] 42001/42001 (Help)
Why do I care:
My code base was failing with NULL not allowed for column "REV". I'm using JPA/Hibernate + Liquibase. In order to try the suggestions at
Hibernate Envers + Liquibase: NULL not allowed for column "REV"
I'm trying to add an auto_increment to my Liquibase changelog file.
You can use the SQL Standard's generation clause GENERATED ALWAYS AS IDENTITY. For example:
create table test (
id bigint generated always as identity,
name varchar(10)
);
See PostgreSQL Example.
It works the same way in H2. For example:
create table test(id bigint generated always as identity, name varchar(10));
insert into test (name) values ('Chicago') ;
select * from test;
Result:
ID NAME
-- -------
1 Chicago

Create named Column default in CREATE TABLE statement in ANSI SQL

I want to create a named default value in an ANSI compliant fashion, if possible, in a CREATE TABLE statement
If I try to add the CONSTRAINT as I would normally write it in an ALTER TABLE statement, it fails (at least in SQL SERVER, though I emphasise I am hoping to find an ANSI complaint statement as I would prefer it to work over a variety of Ado.NET DbConnections).
Example:
CREATE TABLE [dbo].[MyExample]
(
Id int NOT NULL IDENTITY (1, 1),
Name varchar(512) NOT NULL,
IsActive bit NOT NULL,
CONSTRAINT PK_MyExample PRIMARY KEY CLUSTERED (Id),
CONSTRAINT DF_MyExample_IsActive DEFAULT (1) FOR [IsActive]
)
Error:
Incorrect syntax near 'for'.
In terms of the SQL-92 Standard -- which is both ISO (I = International) and ANSI (A + American), by the way -- DEFAULT is not a constraint that may be given a name. In SQL-92 the DEFAULT can only be defined inline with the column definition and must be between the data type and the NOT NULL (if used) e.g.
CREATE TABLE T (c INTEGER DEFAULT 1 NOT NULL UNIQUE);
Note you have much non-Standard syntax in your small example:
square brackets as quoted identifiers (should be double quotes)
non-compliant data type (e.g. incorrect bit null behaviour)
abbreviated data types (e.g. int rather than INTEGER)
IDENTITY
CLUSTERED
Is it not ANSI compliant?
CREATE TABLE [dbo].[MyExample]
(
Id int NOT NULL IDENTITY (1, 1),
Name varchar(512) NOT NULL,
IsActive bit NOT NULL CONSTRAINT DF_MyExample_IsActive DEFAULT (1),
CONSTRAINT PK_MyExample PRIMARY KEY CLUSTERED (Id)
)

sql: making a table structure for injections

I want to take the values from this site for the country table in my database.
The problem is that they don't provide the table structure, so I have to create one, but I cannot get it right - my phpMyAdmin keeps displaying an error when I want to inject the data into the table I created below:
#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 'NUMERIC, alpha3, name, officialName) VALUES ('004','AFG','Afghanistan','Afghan' at line 1
--
-- Table structure for table `countrytable`
--
CREATE TABLE IF NOT EXISTS `countrytable` (
`NUMERIC` int(11) NOT NULL,
`alpha3` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`officialName` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I think my table structure is incorrect. How can I fix it? Thanks!
Try all varchar fields to get the data in since all fields are in quotes in the string you have.
NUMERIC is reserved word in mysql
add in back-tick or quote it -> http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
`alpha3` should be a varchar(3) (or larger), not an int(11).

What can cause legit MySql INSERT INTO command to fail? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I can't figure out what's causing my INSERT INTO's to fail to certain table in MySql. I can manage them to other tables. The table looks like:
CREATE TABLE IF NOT EXISTS `Match` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`match_no` int(11) NOT NULL,
`season` int(11) NOT NULL,
`hometeam` int(11) NOT NULL,
`awayteam` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `match_no` (`match_no`),
KEY `season` (`season`),
KEY `hometeam` (`hometeam`),
KEY `awayteam` (`awayteam`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And the command is
INSERT INTO Match (`match_no`, `season`, `hometeam`, `awaytem`) VALUES (1, 1, 2, 3)
All I get is:
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 'Match (match_no, season, hometeam, awaytem) VALUES (1, 1, 2, 3)' at line 1
I have checked the manual and half-a-dozen examples from the web and whatnought and tried all sorts of changes to the syntax in case there is some MySql specific oddity, but nothing seems to work.
Change awaytem to awayteam and see how it goes and use `Match` as the table: match is a reserved word.
Match is a reserved word in MySQL.
Here goes the list of MySQL reserved words
Enclose Match in back ticks as:
INSERT INTO `Match` .........
Also as Pax pointed out you've misspelt a column name.
Match is a reversed word
so,
INSERT INTO `Match`
note the same backticks you used for the fieldnames
these are not for decoration

Why sql-script isn't executed?

CREATE TABLE PERMISSIONS(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255) NOT NULL, UNIQUE(ID)
)
CREATE TABLE ROLES(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255)
)
I want to run this in MySql. When I try to execute separately each create-query everything works fine but they don't work together. I thought that separator was missed and tried to put semicolon after each query but MySql says that I have syntax mistake near ";" . Where is the mistake?
using the queries in the mysql console with a semi-colon after the each statement works. maybe you use an api (like php's mysql_query) which only supports one query at the time.
It's a semi-colon.
What is the equivalent of 'go' in MySQL?
I don't have a MySql instance running here and it's by no means my cup of tea but I believe you're supposed to separate your queries with ;.
CREATE TABLE PERMISSIONS(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255) NOT NULL, UNIQUE(ID)
) ;
CREATE TABLE ROLES(
ID BIGINT NOT NULL PRIMARY KEY,
NAME VARCHAR(255)
)