Adding a table to MySQL keeps throwing up errors - mysql

Following a tutorial for web development. Here's the SQL code
CREATE TABLE 'users_relationships' (
'users_relationship_id' INT(10) NOT NULL,
'from_user_id' INT(10) NOT NULL,
'to_user_id' INT(10) unsigned NOT NULL,
'users_relationship_type' VARCHAR(10) NOT NULL,
'users_relationship_timestamp' DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('users_relationship_id'),
INDEX 'from_user_id' ('from_user_id'),
INDEX 'to_user_id' ('to_user_id'),
INDEX 'from_user_id_to_user_id' ('from_user_id', 'to_user_id'),
INDEX 'from_user_id_to_user_id_users_relationship_type' ('from_user_id', 'to_user_id', 'users_relationship_type'));
Whenever I run it i get the following 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 ''users_relationships' (
'users_relationship_id' INT(10) NOT NULL,
'from_' at line 1
I tried multiple changes but it keeps giving me the same error. The code is directly from the book. Any ideas?

You don't need backticks or single quotes. Just strain on the body. Single quotes however are downright syntax errors waiting to happen.
CREATE TABLE users_relationships (
users_relationship_id INT(10) NOT NULL,
from_user_id INT(10) NOT NULL,
to_user_id INT(10) unsigned NOT NULL,
users_relationship_type VARCHAR(10) NOT NULL,
users_relationship_timestamp DATETIME not null,
PRIMARY KEY (users_relationship_id),
INDEX from_user_id(from_user_id),
INDEX to_user_id (to_user_id),
INDEX from_user_id_to_user_id (from_user_id, to_user_id),
INDEX from_user_id_to_user_id_users_relationship_type (from_user_id, to_user_id, users_relationship_type)
);
Backticks are cute. But needed only when the the names would otherwise violate reserved words. Though they are generated by tools for create table dumps, why bother with them unless you really hate your little left pinky. Just my opinion.

If you want to use MySQL quotes, you need to use `, or else MySQL will mess up and think there is some string being created instead of table columns (typically for column names, you don't use ', or else MySQL will think a string is being used for data selection)
CREATE TABLE `users_relationships` (
`users_relationship_id` INT(10) NOT NULL,
`from_user_id` INT(10) NOT NULL,
`to_user_id` INT(10) unsigned NOT NULL,
`users_relationship_type` VARCHAR(10) NOT NULL,
`users_relationship_timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`users_relationship_id`),
INDEX `from_user_id` (`from_user_id`),
INDEX `to_user_id` (`to_user_id`),
INDEX `from_user_id_to_user_id` (`from_user_id`, `to_user_id`),
INDEX `from_user_id_to_user_id_users_relationship_type` (`from_user_id`, `to_user_id`, `users_relationship_type`));
Here's the working SQLFiddle here

first u should replace all your single quotes with backticks.
CREATE TABLE `users_relationships` (
`users_relationship_id` INT(10) NOT NULL,
`from_user_id` INT(10) NOT NULL,
`to_user_id` INT(10) unsigned NOT NULL,
`users_relationship_type` VARCHAR(10) NOT NULL,
`users_relationship_timestamp` DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`users_relationship_id`),
INDEX `from_user_id` (`from_user_id`),
INDEX `to_user_id` (`to_user_id`),
INDEX `from_user_id_to_user_id` (`from_user_id`, `to_user_id`),
INDEX `from_user_id_to_user_id_users_relationship_type` (`from_user_id`, `to_user_id`, `users_relationship_type`));
second u cannot have DATETIME DEFAULT CURRENT_TIMESTAMP
or DATETIME DEFAULT ''
or TIMESTAMP DEFAULT CURRENT_TIMESTAMP

Related

I can't find the syntax error in mysql command

CREATE TABLE `üyeler` (
`id` bigint NOT NULL AUTO_INCREMENT,
`ad` tinytext NOT NULL,
`soyad` tinytext NOT NULL,
`KN` tinytext(11) NOT NULL UNIQUE,
`Eposta` tinytext(50) NOT NULL UNIQUE,
`Telefon` tinytext(11) UNIQUE,
`şifre` tinytext NOT NULL,
`favori` bigint NOT NULL,
`yetki` BOOLEAN NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
);
i use phpmyadmin to create the table. I tried everythin but i can't create the table it says theres a syntax error in it
using varchar instead of tinytext worked!

Mysql automated date field, syntax errors

I'm trying to create a mysql table which contains two fields that are automatically given the current date when data is first put into them
CREATE TABLE IF NOT EXISTS `database`.`characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATE NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)
this is not working, i get invalid default value error, which is progress
Everything else i've tried throws error 1064, sql syntax
In place of current timestamp I have tried...
GET_DATE()
GET_DATE
GETDATE
CURDATE
CURDATE()
CURRENT_DATE
CURRENT_DATE()
All of these are throwing the same syntax error, there is no error if i remove the default value.
I am almost entirely clueless about database code, this is a real newbie project, any help would be appreciated
Use datetime instead of date.
CREATE TABLE IF NOT EXISTS `characters` (
`character_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`ckey` VARCHAR(30) NOT NULL,
`character_name` VARCHAR(26) NOT NULL,
`birth` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`character_id`),
UNIQUE INDEX `character_id_UNIQUE` (`character_id` ASC) VISIBLE)

MySQL - SQLite - Converting is spitting out SET error

I am trying to convert the following sql into sqlite via sqlite3.exe but it keeps giving me this error:
Error: near line 1: near "SET": syntax error
I'm not entirely sure what this means or why. Here is my sql script:
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO',
time_zone = '+00:00';
CREATE TABLE `px` (
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `px`
ADD PRIMARY KEY (`id`);
ALTER TABLE `px`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
I'm sure i'm overlooking a simple syntax error. Any help would be much appreciated!
You can 't just run a MySQL script in SQLite. These are two different databases, whose syntax differ.
In SQLite, a relatively close syntax to the MySQL script would be:
CREATE TABLE `px` (
`x` integer NOT NULL,
`y` integer NOT NULL,
`colour` varchar(30) NOT NULL,
`time` varchar(30) NOT NULL,
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
);

what is my syntax error here in mySQL query?

CREATE TABLE IF NOT EXISTS `creditors` (
`id` int(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int(10) NOT NULL,
`credit_amount` double(100) NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL,
UNIQUE KEY `idUnique` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1
MySQL said: Documentation
#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 ') NOT NULL,
start_date date NOT NULL,
due_date date NOT NULL, ' at line 6
What is my syntax error in this SQL query?
If you set a Column to primary key, it consists only Unique value. Then why did you try to made the same to UNIQUE KEY?
And also Delete the RANGE for Double.
SQLFiddle Example
CREATE TABLE `creditors` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` varchar(50) NOT NULL,
`route` varchar(255) NOT NULL,
`mobile` int NOT NULL,
`credit_amount` double NOT NULL,
`start_date` date NOT NULL,
`due_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
The error is with this statement:
`credit_amount` double(100) NOT NULL
Give precision after 100 in double data type.
Solution:
`credit_amount` double(100,0) NOT NULL
Instead of 0 after 100 give number of digits after decimal point.
If you want to use double you must specify both M and D where
M is number digits in total, of which D digits may be after the decimal point, i.e. double(10,0). Otherwise use float which which allows syntax like float(10).
Also, unless you really benefit from this, do not use latin1 charset, but rather UTF-8

Create Table Syntax Error

I received a MySQL data dump and am trying to insert the data into a set of temporary tables. The creation statement for the first table is shown below. When I run this I receive 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 ''temp_books'( 'ID'int( 11 ) NOT NULL AUTO_INCREMENT, 'start'varchar( 20 ) ' at line 1". I've checked the documentation for MySQL syntax, and I don't see that the problem is.
CREATE TABLE 'temp_books' (
'ID' int(11) NOT NULL AUTO_INCREMENT,
'start' varchar(20) NOT NULL,
'customer_id' int(11) NOT NULL DEFAULT '0',
'total_num' int(11) NOT NULL,
'amount' double(5,2) NOT NULL DEFAULT '0.00',
'changed' timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('ID'),
UNIQUE KEY 'start' ('start')
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
You shouldn't put single-quotes on your identifiers. If you're going to quote them use the "back tick" character (“`”). You can also use double-quotes but you have to specify that mode:
SET sql_mode='ANSI_QUOTES';
http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
I've ALWAYS had issues with CREATE TABLE. Not sure why. Takes some trial-and-error.
Try this:
CREATE TABLE temp_books (
ID int(11) NOT NULL AUTO_INCREMENT,
start varchar(20) NOT NULL,
customer_id int(11) NOT NULL DEFAULT '0',
total_num int(11) NOT NULL,
amount double(5,2) NOT NULL DEFAULT '0.00',
changed timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (ID),
UNIQUE KEY start (start)
) ENGINE=MyISAM AUTO_INCREMENT=4853 DEFAULT CHARSET=latin1;
I had to delete the quote marks, as well as the default for the changed field, as well as the default charset. Hopefully that won't affect the data.
Here's another way of writing it that might work for some: (left away most of the columns for brevity)
create table temp_books
(
id int not null,
start varchar(255) null,
constraint six_cb_datasource_pk
primary key (id)
);