I am creating several logbooks on the same siteāall with the same structure, so when I need to add a new logbook, I'd like to be able to do so with a simple function or something (since the table-structure will be exactly the same.)
DELIMITER //
CREATE FUNCTION GenerateNewLogbook(LogbookTitle varchar(15))
BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTitle (
time timestamp NOT NULL DEFAULT NOW(),
username varchar(25) DEFAULT NULL,
message text,
crossout tinyint(1) NOT NULL DEFAULT '0',
post_id mediumint(9) NOT NULL AUTO_INCREMENT,
file_id text,
PRIMARY KEY (post_id)
) ENGINE=InnoDb AUTO_INCREMENT=0 DEFAULT CHARACTER SET=latin1;
END//
DELIMITER ;
The error I'm getting is:
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 'BEGIN
DROP TABLE IF EXISTS LogbookTitle;
CREATE TABLE LogBookTit' at line 2
I've tried many things: Replacing the ";" with "//", creating a PROCEDURE instead of a FUNCTION, adding "#" before LogBookTitle, removing BEGIN/END, etc. and have spent quite a while searching for examples to no avail.
What's wrong with the above code?
Related
I have written an sql script with the following contents :
CREATE DATABASE IF NOT EXISTS stock_trading;
USE stock_trading;
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name,passwrd),
)ENGINE=InnoDB;
and every time I try to run it in the SQL command prompt it keeps giving away the error as:
ERROR 1064 (42000) at line 5 in file: 'db_script.sql': 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 ')ENGINE=InnoDB' at line 5
the script appears to be correct but I don't know why it keeps giving this error.
Additional information:
Operating System : Arch Linux
Database : MariaDB
You have a stray comma at the end of your table definition:
PRIMARY KEY (user_name,passwrd),
^^^ remove this
Your full table definition:
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name, passwrd)
) ENGINE=InnoDB;
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)
I am trying to alter a table and set a default value for a nullable column. But i get the following error.
Here is the command:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1 ;
Here is the error:
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 '(11) NULL DEFAULT 1' at line 2
SQL Statement:
ALTER TABLE `questionboard`.`questions`
CHANGE COLUMN `status` `status` (11) NULL DEFAULT 1
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'question' already exists
What am i doing wrong?
You forgot the data type. Did you mean
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` INT(11) NULL DEFAULT 1 ;
^^^
Your query should be this:
ALTER TABLE `questiontboard`.`questions`
CHANGE COLUMN `status` `status` int(11) NULL DEFAULT 1 ;
^^ here add int as you want the datatype
You are missing datatype of field in the query.
I got the same error when altering a table. I did the exact same thing you did (minus the code typo).
I got the error when altering a column from a SMALLINT to a varchar(n). It gives the "1050 Table already exists..." error. The error was confusing. Of course the table exists, that's why I'm trying to alter it!
In the end, I found out that the problem was that my new varchar(2) was not big enough to hold all the original smallint data. I had one row that had a 4 digit number, so varchar(2) wouldn't work. I changed it to use varchar(4), and it worked.
ALTER TABLE omiccom_wp.myTable
CHANGE COLUMN myColumn myColumn VARCHAR(2) NOT NULL DEFAULT '0' ;
At the site Sea Quail Database Diagram Tool which is great for creating database diagrams I just created a MySQL database schema diagram - very basic one to get a feel of it.
I created the script to run from PHPMyadmin to add this all to a database created there to see if importing would work OK, but I ran into this 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 '--------------------------------------------------------------------------------' at line 2
Here is a snippet of the SQL data that was generated for me.
-- Create Table: name_fields
--------------------------------------------------------------------------------
CREATE TABLE name_fields
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_api
--------------------------------------------------------------------------------
CREATE TABLE name_api
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_users
--------------------------------------------------------------------------------
CREATE TABLE name_users
(
`ID` BIGINT NULL
,`user_login` VARCHAR(250) NULL
,`user_name` VARCHAR(250) NULL
,`user_password` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_options
--------------------------------------------------------------------------------
CREATE TABLE name_options
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
-- Create Table: name_ideal
Not sure what is wrong with the syntax here. Is there an easy way to debug this in MySQL?
Yes you have syntax error
First of all the
-- Create Table: name_fields
-------------------------------------------------------------------------------
Should be in one line you need to check this
something like
-- Create Table: name_fields ------------------------------------------------------
2nd thing
CREATE TABLE name_fields
(
`` VARCHAR(250) NULL
)
ENGINE=INNODB
Where is the column name ? you have just specified ``. The same is for name_api table too, correct these and it should work.
Noob Alert.
Hi. I'm trying to input this event in mySQL through phpMyAdmin. I'm not sure if the table can be named using YEAR() and MONTHNAME() functions. Even before i could tackle that, this #1064 error has sprouted:
#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 'END' at line 14
Could someone please point out what i'm doing wrong?
Also is it possible to name tables the way I'm trying to?
CREATE EVENT newmonthlytable
ON SCHEDULE EVERY '1' MONTH
STARTS '2012-09-01 00:00:00'
DO
BEGIN
CREATE TABLE IF NOT EXISTS `YEAR()-MONTHNAME()-0-msgs`
(`msgid` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`sender` int(11) NOT NULL,
`reciever` int(11) NOT NULL,
`sendername` varchar(64) NOT NULL,
`msg` varchar(512) NOT NULL,
`tstamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP)
ENGINE = MyISAM, CHARACTER SET = utf8, COLLATE = utf8_bin
END;
There is syntax error, to fix it add ';' at the end of CREATE TABLE statement.
You can name tables as you want, but do you really need it? It is better to think about the db-design, and do not create tables from routines.