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.
Related
I have an error in my SQL syntax, can anyone help me please beacause I really don't see the error...
Here is the request SQL :
ALTER TABLE t_personne Change email_personne to mail_pers ;
MySQL 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 'to mail_pers' at line 1
please help
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):
ALTER TABLE table_name
DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax:
SQL Server / MS Access:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
If you are trying to rename the column, Then
Query
ALTER TABLE t_personne
CHANGE COLUMN email_personne mail_pers VARCHAR(255) NOT NULL;
Documentation
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);
CREATE TRIGGER Tr1
AFTER DELETE ON Table1
REFERENCING
OLD TABLE AS OldTable,
NEW TABLE AS NewTable
FOR EACH STATEMENT
Produces the following 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 'REFERENCING
OLD TABLE AS OldTable,
NEW TABLE AS NewTable
Why is this?
If you want to view old table, then you should create BEFORE DELETE trigger -
CREATE TRIGGER Tr1
BEFORE DELETE
ON Table1
FOR EACH ROW
BEGIN
...
SET #old_count = NULL;
SELECT COUNT(*) INTO #old_count FROM Table1;
...
END
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.
On phpMyAdmin, when I create this table the SQL is executed correctly but when I add all the sql code
I get an error saying
"#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 'CREATE TABLEbuying(CustomerIDint(10) unsigned NOT NULL,PurchaseI' at line 14`"
Why is that and how can I fix it ?
You have to use a semicolon ; after each CREATE TABLE sometable ( ) statement.
CREATE TABLE `table1` (
...
);
CREATE TABLE `table2` (
...
);
If you are running multiple SQL statements, they need to be ended with a ; (semi-colon)