Specify a default of 225 spaces for a varchar column - mysql

I am trying to save a 15 by 15 letters board for a word game as a 225 characters string in MySQL 5.6 database by using the following table:
create table games (
gid integer primary key auto_increment,
player1 integer references users(uid) on delete cascade,
player2 integer references users(uid) on delete cascade,
stamp1 integer not null default 0,
stamp2 integer not null default 0,
letters1 varchar(7) not null,
letters2 varchar(7) not null,
letters varchar(116) not null,
board varchar(225) not null default space(225)
);
Unfortunately, this returns an 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 'space(225))'
I am just trying to initialize the game board with 15 x 15 spaces - and would like to modify that board with each move later.
Could you please recommend me a better way for doing that?

You cannot use a function as a default value:
Can I use a function for a default value in MySql?
so you can just define your default values as a string:
board varchar(225) not null default ".................up to 225...."
or you can use a trigger.

Related

Error creating table: You have an error in your SQL syntax;

I'm trying to make some tables in my database using this.
but I get this error:
Error creating table: 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 'UNSIGNED AUTO_INCREMENT, ip VARCHAR(65) NOT NULL, attempts INT(11) ' at line 2
on this:
$dbprefix = 'duh_';
$prefixloginAttempts = $dbprefix . 'loginAttempts';
$table = array();
$table[] = "CREATE TABLE IF NOT EXISTS $prefixloginAttempts (
id INT(11) NOT NULL UNSIGNED AUTO_INCREMENT,
ip VARCHAR(65) NOT NULL,
attempts INT(11) NOT NULL,
lastLogin DATETIME NOT NULL,
username VARCHAR(65) NOT NULL,
mod_timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE KEY 'id_UNIQUE' ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
foreach ($table as $sql) {
$query = $conn->query($sql);
}
Someone that can see where the fail is? I cant..
The error messages provided by MySQL contain the part of the query where the parsing stopped. The error is usually either on the first word from the part of the query listed in the ... near "..." message or just before it.
On this query, the problem is on the UNSIGNED keyword. It is part of the data type and it must stay right after with INT(11):
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
Read about the syntax of the CREATE TABLE statement.
There are also errors on the definitions of indexes. The column names are enclosed in apostrophes and this makes them strings and not object names.
The object names can be left unquoted or, if they are also SQL keywords or MySQL reserved words then they must be enclosed in backticks (`):
PRIMARY KEY (id),
UNIQUE KEY id_UNIQUE (id)
or
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`)
Read when to use single quotes, double quotes or backticks in MySQL.

Error at "unsigned" when creating table in MySQL?

I can't tell what is wrong with this query.
create table roles
(
id int unsigned not null auto_increment
,name varchar(32) not null
,phone varchar(256) not null
,primary key (id)
)
engine=innodb
default charset=utf8;
When I try to run it, I get:
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 'unsigned not null auto_increment ,role_id int unsigned ' at
line 3
Using MySQL 5.7
Thanks for the advice, tadman
Turns out I had a tab between "int unsigned" instead of a space. Replaced the tab with a space and it worked just fine.
That was driving me crazy for almost 2 hours.

SQL error #1064 on CREATE TABLE

I am importing a database from MySQL 4.0.27-standard into a new webhost with Server version: 5.5.48-37.8 - Percona Server (GPL), Release 37.8, Revision 727, using PHPMySQL.
I am getting the following 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 '(14) NOT NULL '', PRIMARY KEY (site_id) ) ENGINE=MyISAM' at
line 14
Here is the CREATE TABLE query:
CREATE TABLE Brewing (
site_id int(5) NOT NULL auto_increment,
site_url varchar(255) NOT NULL default '',
site_name varchar(255) NOT NULL default '',
site_comment varchar(255) default NULL,
site_rating int(2) NOT NULL default '0',
site_entrydate varchar(25) NOT NULL default '',
site_lasttouched timestamp(14) NOT NULL,
PRIMARY KEY (site_id)
) ENGINE=MyISAM;
Remove the size of timestamp. It already has a default for setting up timestamp. So remove (14).
Plus added bonus, add ON UPDATE CURRENT_TIMESTAMP for auto update of timestamp.
The length argument for timestamp represents fractional seconds (see the documentation). The allowed lengths for fractional seconds are 0 to 6. 14 is too long.
I would advise you to just remove the length altogether.

MySQL ERROR 1064 (42000) when creating a table

I've looked at previous posts but I am struggling to figure out where i am going wrong.
I am trying to create a table using this code:
mysql> CREATE TABLE Engine
-> (
-> ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
-> Name VARCHAR(255) NOT NULL,
-> Displacement VARCHAR(10) NOT NULL,
-> Code VARCHAR(10) NOT NULL,
-> PowerOutput VARCHAR(10),
-> MadeAt VARCHAR(255) NOT NULL,
-> PRIMARY KEY (ID),
-> FOREIGN KEY (MadeAt)
-> );
However I am repeatedly 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 'UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
Name VARCHAR(255) NOT NULL,
Displace' at line 3
Any help would be much appreciated.
Try to change
ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE
into:
ID INT NOT NULL AUTO_INCREMENT
UNASSIGNED should be UNSIGNED, but it is not necessary to define this, because AUTO_INCREMENT will start at 1 by default when not defined otherwise. UNIQUE is redundant as well, because the ID will increment and the numbers will be unique anyway.
In most cases VARCHAR is not recommended to use as PRIMARY KEY.

This database.sql not working correctly?

I'm trying to get this to work but I don't understand how SQL works outside of the basics. Can you find the error in this?
CREATE TABLE lil_urls (
id varchar(255) NOT NULL default '',
url text,
date timestamp(14) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
I'm currently getting this error in PhPMyAdmin:
#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 '(14) NOT NULL,
PRIMARY KEY (id)' at line 4
Thanks for your help!
Correct syntax is
CREATE TABLE lil_urls (
id varchar(255) NOT NULL default '',
url text,
date timestamp NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
TYPE is no longer used and you need to use as ENGINE
And no need to specify length for timestamp