MySQL: Resetting AUTO_INCREMENT - mysql

I am trying to reset the auto increment value in one of my tables based on the number of rows currently in it. Here is the code I have so far.
SET #numrows = 0;
SELECT COUNT(*) total, #numrows := COUNT(*) + 1 numrows FROM maj_user ;
ALTER TABLE `maj_user` AUTO_INCREMENT = #numrows ;
This works great if I execute it in MySQL Workbench. However, I need to save this as an SQL file and execute it as part of a database import script. If I do this, I get this:
ERROR 1064 (42000) at line 39: 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 '#numrows' at line 1
Line 39 is the ALTER TABLE statement. Any ideas?

Can you change your syntax to skip actually setting #numrows? I'm not sure what the problem is but a workaround seems to be something like:
ALTER TABLE `maj_user` AUTO_INCREMENT = (SELECT COUNT(*) + 1 from maj_user);

Related

Mysql statement syntax error with truncate and alter

This is probably very simple but why cant i run this script? Gat a syntax error appear. If i run these separate it works, why?
TRUNCATE TABLE table1;
ALTER TABLE table1 AUTO_INCREMENT = 25;
error
Failed to execute SQL : SQL TRUNCATE TABLE table1; ALTER TABLE table1 AUTO_INCREMENT = 25; failed : 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 'ALTER TABLE table1 AUTO_INCREMENT = 25' at line 1
it will work only if you execute it one after another
i.e.
TRUNCATE TABLE table1;
and then
ALTER TABLE table1 AUTO_INCREMENT = 25;
don't execute together.
if you execute it together first query executed, but second will raise an error.

Making a backup copy of SQL table throws me this error: "#1064 - You have an error in your SQL syntax; "

I'm trying to make a backup of my table in MySql but I get 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 'table `zbackup_oc_t_city` from `oc_t_city` LIMIT 0, 30' at line 1
This is the code that I'm using to backup
SELECT * INTO TABLE `zbackup_oc_t_city` FROM `oc_t_city`
Here is my oc_t_city table:
Here is zbackup_oc_t_city
I have tried it on numerous tables and it keeps throwing me the same error... any ideas?
Thanks
If you want to create your backup table and do the backup in just one statement use
CREATE TABLE `zbackup_oc_t_city` SELECT * FROM `oc_t_city`;
CREATE TABLE ... SELECT Syntax
You can create one table from another by adding a SELECT statement at
the end of the CREATE TABLE statement:
CREATE TABLE new_tbl [AS] SELECT * FROM orig_tbl;
With MySQL you can't use SELECT ... INTO to select into a new table:
SELECT ... INTO Syntax
The SELECT ... INTO form of SELECT enables a
query result to be stored in variables or written to a file:
SELECT ... INTO var_list selects column values and stores them into
variables.
SELECT ... INTO OUTFILE writes the selected rows to a file. Column and
line terminators can be specified to produce a specific output format.
SELECT ... INTO DUMPFILE writes a single row to a file without any
formatting.
I do remember having similar troubles while working with SQL myself. One cause of error I found was the use of citation marks... try removing the citation marks like this:
SELECT * INTO zbackup_oc_t_city FROM oc_t_city;
I'm not sure this fixes your problem (but I can't see anything else wrong with your query). I hope it does though. :)

delete query for mysql using c

can anyone tell me the correct query to delete values from mysql db table,in my case the table name and id are accepted from the user and the row is deleted based on id.This is my query
sprintf(Query,"DELETE FROM ('%s') where id = (%d)",tb1,idt1) ;
/*table name is in form of string and id is int */
mysql_query(conn,Query);
You should remove parentheses around the table name:
sprintf(Query,"DELETE FROM '%s' where id = (%d)",tb1,idt1) ;
MySQL considers queries like this syntax errors:
delete from (mytable) where id=2;
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 '(mytable) where id=2' at line 1
(I'll assume that you know everything about SQL injection attacks, and that neither tb1 nor idt1 are constructed from user input in any shape or form).

mysql insert - select syntax error

This is my query -
$q = "CREATE TEMPORARY TABLE tmp SELECT * from category c WHERE category_id IN (SELECT category_id FROM category_to_store WHERE store_id = '".(int)$from_store_id."');
ALTER TABLE tmp drop category_id;
INSERT INTO category SELECT 0,tmp.* FROM tmp;
SET #last_id_in_category := LAST_INSERT_ID();
select #last_id_in_category;
DROP TABLE tmp;"
mysql_query($q);
I am getting this error on execution
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 'ALTER TABLE tmp drop category_id; INSERT INTO category SELECT 0'
at line 2 Error No: 1064
But when I run the query in database directly then I am not getting any error.
Please help me !
From the php docu on `mysql_query'
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database ...
So basically split your queries to multiple calls of `mysql_query' each with a single query and you should be fine.

MySQL foreign key problem

I'm getting that error running on MySQL 5.5.8
Mysql2::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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?
I suspect the problem is there is no FROM clause in your select statement.
Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?