I am getting the following error message when trying to run some sql code in my 5.5.35-MariaDB. Please can someone help me understand what is wrong?
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 'CREATE TABLE IF NOT EXISTS action_recorder ( id int(11) NOT NULL AUTO_I' at line 1
My code is below:
CREATE TABLE IF NOT EXISTS `action_recorder` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`module` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`success` char(1) COLLATE utf8_unicode_ci DEFAULT NULL,
`date_added` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `idx_action_recorder_module` (`module`),
KEY `idx_action_recorder_user_id` (`user_id`),
KEY `idx_action_recorder_identifier` (`identifier`),
KEY `idx_action_recorder_date_added` (`date_added`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=23 ;
Please can someone help me resolve this? Thanks!
You're missing the closing parenthesis. The one at the end is only for the key idx_action_recorder_date_added, but you need another one to close the whole table definition.
Also, I think you shouldn't use those normal single quotes. They are for strings. Use backticks or omit them altogether.
Since the "near..." mentions 'CREATE...', the error is either the CREATE or what immediately precedes it. I vote for the latter -- Look at what have right before it.
Related
Continuation from this question:
PDO query updating a datetime column not in query
A column in my table called lastLoginDate was being automatically updated even though my prepared statement did not include said column.
Apparently, when I created the new column, a trigger was set.
Upon using the command SHOW CREATE TABLE table_name, I returned the following results:
CREATE TABLE `users_edi` (
`username` varchar(30) NOT NULL DEFAULT '',
`fullname` varchar(50) DEFAULT NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`ipaddress` varchar(30) DEFAULT NULL,
`email` varchar(150) DEFAULT NULL,
`entrydate` datetime DEFAULT NULL,
`division` varchar(35) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`userid` varchar(32) DEFAULT NULL,
`timestamp` int(11) unsigned NOT NULL,
`job_title` varchar(30) DEFAULT NULL,
`dept` varchar(50) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`lastLoginDate` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, // <-- here
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
The table is years old. I just created the column and somehow, a trigger was set to it (I guess).
Regardless, I tried to remove it using the following command:
ALTER TABLE `users_edi`
`lastLoginDate` datetime DEFAULT NULL
But I only get the following error:
[Err] 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 '`lastLoginDate` datetime DEFAULT NULL' at line 4
How do I remove this trigger using the ALTER TABLE command or any other command?
ALTER TABLE users_edi MODIFY COLUMN lastLoginDate DATETIME DEFAULT NULL;
You might like to read this page on ALTER TABLE: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
Exported a MySQL database on one of my servers and am trying to import it on my new server. All done through phpMyAdmin.
MySQL says
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 'CREATE TABLE IF NOT EXISTS jobs ( id mediumint(9) NOT
NULL, title varc' at line 12
That area of code is
CREATE TABLE IF NOT EXISTS `jobs` (
`id` mediumint(9) NOT NULL,
`title` varchar(200) DEFAULT NULL,
`descshort` varchar(400) DEFAULT NULL,
`descr` varchar(7000) DEFAULT NULL,
`postdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
One thing I might note is that I exported this from phpMyAdmin version 4.4.4 and MySQL 5.6, to my new server which is running phpMyAdmin 4.5.02 and MySQL 5.6
Any idea what might be causing this?
EDIT:
The table created before it successfully is
CREATE TABLE IF NOT EXISTS `horses` (
`id` mediumint(9) NOT NULL,
`bgcolor` varchar(7) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`bgimgurl` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`htop` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`hbottom` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`ptext` varchar(250) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`linktext` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`linktarget` varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`sord` int(11) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
I am trying to add check constraint in my table that prevents adding more data into a table if the sum of rows shop_id is greater than 3. I have written the following code and its just not working. Kindly check this and guide me.
ALTER TABLE kinect_temp_data
ADD CONSTRAINT my_const CHECK (sum(distinct(shop_id))<3)
The above query runs successful,but it does not create any effect and i can still able to add more rows, and when i query this, it display that no check constraint was added.
SHOW CREATE TABLE kinect_temp_data
Output
CREATE TABLE `kinect_temp_data` (
`cart_number` int(11) NOT NULL AUTO_INCREMENT,
`product_id` varchar(50) NOT NULL,
`shop_id` varchar(50) NOT NULL,
`product_name` varchar(50) NOT NULL,
`item_number` varchar(50) NOT NULL,
`image1_path` varchar(50) NOT NULL,
`image2_path` varchar(50) NOT NULL,
`image3_path` varchar(50) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`cart_number`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1
Kindly check this and guide me what i am doing wring here.
Thanks.
MySQL don't support check constraints -- they are ignored.
But you can use BEFORE INSERT and BEFORE UPDATE triggers to realize such functionality.
There is good explanation
I am using the redbeanPHP ORM and mysql. I have the following table:
CREATE TABLE `mast` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`note` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`geolocation` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`location` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`zip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`state` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`app` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` (`app`,`geolocation`)
) ENGINE=InnoDB AUTO_INCREMENT=98 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I want to insert records into the 'mast' table providing they are unique with respect to both of the 2 fields listed above. In other words if either 'geolocation' or 'app' is a duplicate, I don't want to insert the associated record.
I am using following php code to insert the records using rebean:
$resultBean= R::dispense('mast');
$resultBean ->import($resultsarray);
try {
$id = R::store($resultBean); // TRY TO INSERT INTO MAST
} catch (Exception $exc) {
}
The insert is occurring except I notice that duplicate records on at least the 'app' field are being inserted. I am getting a normal looking record, while the duplicate has all zero or null values except for the 'app' field which has a duplicate entry.
I don't want the duplicate entries in the table at all. How can I prevent them from being inserted?
if either 'geolocation' or 'app' is a duplicate, I
don't want to insert the associated record.
UNIQUE KEY `UQ_84a93b55f688c94f73092dba1b9590c41a92cbf5` (`app`,`geolocation`)
Only prevents the pair app and geolocation from being duplicated.
If you want to prevent either one individually being duplicated, add separate indexes for each i.e.
CREATE UNIQUE INDEX UQ_app ON mast (app);
CREATE UNIQUE INDEX UQ_geolocation ON mast (geolocation);
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 9 years ago.
Please help me!
I imported a MySQL file with this code:
delimiter $$
CREATE TABLE "login" (
"IdUser" int(11) NOT NULL AUTO_INCREMENT,
"username" varchar(45) CHARACTER SET latin1 NOT NULL,
"pass" varchar(45) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY ("IdUser")
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8$$
CREATE TABLE "photos" (
"IdPhoto" int(11) NOT NULL AUTO_INCREMENT,
"title" varchar(100) CHARACTER SET latin1 NOT NULL,
"IdUser" int(11) NOT NULL,
PRIMARY KEY ("IdPhoto")
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8$$
....and I get the following error:
MySQL said:
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 'delimiter $$ CREATE TABLE "login" (
"IdUser" int(11) NOT NULL AUTO_INCREME' at line 1
Update:
Problem solved. Thank you for responding!
CREATE TABLE `login` (
`IdUser` int(11) NOT NULL auto_increment,
`username` varchar(45) NOT NULL,
`pass` varchar(45) NOT NULL,
PRIMARY KEY (`IdUser`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
CREATE TABLE `photos` (
`IdPhoto` int(11) NOT NULL auto_increment,
`title` varchar(100) NOT NULL,
`IdUser` int(11) NOT NULL,
PRIMARY KEY (`IdPhoto`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
The delimiter command is not a server instruction; rather, it is a client-specific instruction recognised by certain client programmes such as the MySQL command line tool. It has the effect of changing the character that the client programme recognises as delimiting the statements which are to be sent to the server.
In phpMyAdmin, the statement delimiter can be changed in the Delimiter text box before clicking Go.