someone know what is bad?
ALTER TABLE "stats"
MODIFY "id" int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
Incorrect syntax near 'MODIFY'.
idk what is wrong, someone can help?
Instead of using double quotes use backticks, also an Auto_increment must be PRIMARY KEY
ALTER TABLE `stats`
MODIFY `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, AUTO_INCREMENT=2;
COMMIT;
short answer: instead of " (double quotes ) use ` (backticks)
Long answer :
Backticks are used in MySQL to select columns and tables from your MySQL source. In the example below, we are calling to the table titled Album and the column Title. Using backticks we are signifying that those are the column and table names.
ALTER TABLE `stats`
MODIFY `id` int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
or, The backticks for column names may not be necessary though.
ALTER TABLE stats
MODIFY id int NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;
Related
I want to create a table in MySQL with the name "123 Product", with numbers at the beginning and a space in between. I can do this using phpMyAdmin but I want to make this table using PHP. But, unfortunately, I am unable to do this.
You can simply try this: enclose table name with backticks (`)
CREATE TABLE `123 Product`
(
`product_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product_name` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB;
I am trying to add multiple columns to an existing table in phpMyAdmin, but I keep getting the same error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax ...
I am writing:
ALTER TABLE `WeatherCenter`
ADD COLUMN
BarometricPressure SMALLINT NOT NULL,
CloudType VARCHAR(70) NOT NULL,
WhenLikelyToRain VARCHAR(30) NOT NULL;
I have referred to past posts on StackOverflow, and I am following the experts' recommendation, so why am I getting an error?
ALTER TABLE table_name
ADD COLUMN column_name datatype
correct syntax
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
check syntax
You need to specify multiple ADD COLUMN
ALTER TABLE `WeatherCenter`
ADD COLUMN BarometricPressure SMALLINT NOT NULL,
ADD COLUMN CloudType VARCHAR(70) NOT NULL,
ADD COLUMN WhenLikelyToRain VARCHAR(30) NOT NULL;
You can alter a table and add multiple columns in one statement by doing it like this.
alter table WeatherCenter add column (BarometricPressure SMALLINT NOT NULL, CloudType VARCHAR(70) NOT NULL, WhenLikelyToRain VARCHAR(30) NOT NULL);
This will help you:
alter table A add first_name varchar(10),last_name varchar(10);
As you're adding columns to an existing table I don't think you're meant to declare NOT NULL in the statement. Also, you don't need to use ADD COLUMN, you can just use ADD.
ALTER TABLE WeatherCentre
ADD BarometricPressure SMALLINT,
ADD CloudType VARCHAR(70),
ADD WhenLikelyToRain VARCHAR(30);
This is from Official MySQL Documentation
ALTER TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
| ADD [COLUMN] col_name column_definition
[FIRST | AFTER col_name]
| ADD [COLUMN] (col_name column_definition,...)
Possible duplicate of alter table add MULTIPLE columns AFTER column1
alter table table_name add (product varchar(20) not null, price int(10))
this is also working fine
I want to create a table in MySQL with the name "123 Product", with numbers at the beginning and a space in between. I can do this using phpMyAdmin but I want to make this table using PHP. But, unfortunately, I am unable to do this.
You can simply try this: enclose table name with backticks (`)
CREATE TABLE `123 Product`
(
`product_id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`product_name` VARCHAR(255) NOT NULL
)
ENGINE = InnoDB;
I am having 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 ''tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'd' at line 1
From this statement:
CREATE TABLE 'tablename'(
'id'MEDIUMINT NOT NULL AUTO_INCREMENT,
'content'TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
); ENGINE=MyISAM;
Why?
You need the backtick instead of the single quote ('). The backtick is this character:
`
Better yet - don't bother with either:
CREATE TABLE tablename (
id MEDIUMINT ...
Important: also see the comments below from tadman; they round out this answer nicely by explaining the backticks and pointing out another syntax issue.
You are using incorrect notation.
In your create table statement, you are using the single quote ( ').
You can't use that here for table names and column names.
Alternatives would be the tick mark (`). Or just completely remove all notation altogether.
Here is your code, fully functional:
CREATE TABLE tablename (
`id` MEDIUMINT NOT NULL,
`content`TEXT NOT NULL,
`date_added` DATETIME NOT NULL,
`user` VARCHAR (16) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE (`id`)
);
You are missing a space between the columnname and the type of the first two columns. Also, you have a ; to many at the end
CREATE TABLE 'tablename'(
'id' MEDIUMINT NOT NULL AUTO_INCREMENT,
'content' TEXT NOT NULL,
'date_added' DATETIME NOT NULL,
'user' VARCHAR (16) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE ('id')
) ENGINE=MyISAM;
I've defined a table like
CREATE TABLE `mytable` (
`identifier` varchar(45) NOT NULL,
`f1` char(1) NOT NULL,
KEY `identifier` (`identifier`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
and then added primary key and index as
ALTER TABLE `mytable` ADD PRIMARY KEY ( `identifier` )
ALTER TABLE `mytable` ADD INDEX ( `identifier` )
In the identifier field my table is populated with values like (about 800,000 records)
USER01-TESTXXY-CAD-10172
USER01-TESTXXY-CAD-1020
USER01-TESTXXY-CAD-10245
USER02-TEST-003-SUBA
USER02-TEST-002-SUBB
I've discovered that queries where the identifier ends with a number aren't matched:
SELECT *
FROM identifier
WHERE identifier = 'USER01-TESTXXY-CAD-10245';
but queries matching an identifier which ends with letters are matched successfully
SELECT *
FROM identifier
WHERE identifier = 'USER02-TEST-003-SUBA';
My queries are exact, I don't need to compare with LIKE because my users provide me exact strings. Besides varchar(45) is more than enough space for my identifiers.
What I did wrong? What could be the reason or solution?
I think you have made a typo in both the queries. Your table name should be mytable instead of identifier.
SELECT *
FROM mytable
WHERE identifier = 'USER01-TESTXXY-CAD-10245';
Is it a typo ?
Is there another table in your database that is called as identifier?