How to give the long name as a field of the table? - mysql

How can I give the long text as a table field name in mysql?
Here is what I tried:
CREATE TABLE IF NOT EXISTS surveyForm_8(
surveyForm_8_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(surveyForm_8_id),
survey_form_id VARCHAR(255),
submitted_by VARCHAR(15),
submitted_on TIMESTAMP,
'How_to_change_the_way_of_road?' VARCHAR(255)
)
But I got this error:
#1059 error

Try this one, you should use the ` symbol for column names
CREATE TABLE IF NOT EXISTS surveyForm_8(surveyForm_8_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(surveyForm_8_id), survey_form_id varchar(255) ,submitted_by varchar(15),
submitted_on timestamp, `How_to_change_the_way_of_road?` varchar(255));

Please see http://dev.mysql.com/doc/refman/5.5/en/identifiers.html for valid table and field names.
Basically, double quotes only work in ANSI_QUOTES mode. The default is to use `backticks` to quote. Also, the maximum length of table / field names is 64 characters.

Related

MySQL Incorrect syntax near 'MODIFY'

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;

Phpmyadmin sql command isn't working, what is wrong?

I tried to setup a table in my database:
CREATE TABLE products(
id INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
info TEXT,
price DOUBLE(10)
);
and got the error #1064, but I think there is no error in my SQL syntax.
This is due to Wrong Datatype specification while creating Table.
You can use FLOAT(10) if required, if you want to use Double, you have to mention the precision value as DOUBLE(10,0).

Syntax error on adding multiple indexes on Table Creation mysql

I am trying to run a CREATE TABLE script which has multiple INDEXES.
CREATE TABLE IF NOT EXISTS Equipment (
EquipmentID BIGINT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
Type VARCHAR(255) NOT NULL,
Make VARCHAR(255),
Model VARCHAR(255),
Description VARCHAR(255),
OperationNotes TEXT,
Damaged BOOLEAN DEFAULT 0,
PRIMARY KEY (EquipmentID),
INDEX ('EquipmentID'),
INDEX('Type'),
INDEX('Model'),
INDEX('Description')
INDEX('Damaged')
);
However I get a syntax error:
"(" is not valid at this position for this server version
On line:
INDEX ('EquipmentID'),
Single quote (') denote string literals. When referring to object names (such as columns), you shouldn't use single quotes. Remove them, and you should be OK. Also, note that a primary key implicitly creates an index, so you don't need to explicitly create an index on EquipmentID:
CREATE TABLE IF NOT EXISTS Equipment (
EquipmentID BIGINT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
Type VARCHAR(255) NOT NULL,
Make VARCHAR(255),
Model VARCHAR(255),
Description VARCHAR(255),
OperationNotes TEXT,
Damaged BOOLEAN DEFAULT 0,
PRIMARY KEY (EquipmentID),
INDEX (Type),
INDEX (Model),
INDEX (Description),
INDEX (Damaged)
);

MySQL Hash Value as Column Name

I'm getting a syntax error when creating a table with the result of PHP's md5(microtime()) as the column name.
In particular, the error is getting thrown at the part with ** surrounding it:
CREATE TABLE form_data_38 (
id INT SIGNED auto_increment NOT NULL,
rltd_pri_key INT SIGNED NULL,
0accc77c084cc74a51dee479f8d095e3 TEXT(65535) NOT NULL,
**092e60b78f7804e86ea9a6e83701a929 TEXT(65535) NOT NULL**,
6734131796201537410e4d43635bf1b3 TEXT(65535) NULL,
PRIMARY KEY (id)
) TYPE=InnoDB;
What is confusing me is why it's being thrown at that spot and not the other 2 hash values prior to it. I appended 'a' to the column names and it created the table no problem. I've searched MySQL naming rules and so far I haven't come up with anything. It just says all alphanumeric characters plus '_' and '$' are okay to use, which should be fine in this instance.
What am I missing?
Put your table and field names in backquotes: `09e4_fieldName`, at least for those that can create such problems:
CREATE TABLE form_data_38
( id INT SIGNED auto_increment NOT NULL
, rltd_pri_key INT SIGNED NULL
, `0accc77c084cc74a51dee479f8d095e3` TEXT(65535) NOT NULL
, `092e60b78f7804e86ea9a6e83701a929` TEXT(65535) NOT NULL
, `6734131796201537410e4d43635bf1b3` TEXT(65535) NULL
, PRIMARY KEY (id)
)
ENGINE = InnoDB ; --- ENGINE, not TYPE

What's wrong with my MySQL code here in varchar(max) line?

create table snippet(
id int not null auto_increment,
primary key(id),
idlanguage int not null,
foreign key(idlanguage) references language(id),
iduser int not null,
foreign key(iduser) references user(id),
title varchar(200) not null,
content varchar(max) not null,
rating int,
creationdate datetime
);
I'm getting an error at line 9 near 'max)' according to PHPMyAdmin.
VARCHAR(MAX) is an MS SQL Server extension to the SQL language -- it does not exist in mysql. Put a number there and you will be golden.
I don't think that "max" is a valid value for the maximum number of character that can be put in your content column : you should specify a numerical value.
But note that varchar has a limited maximum length (see the varchar page in the MYSQL's manual for the details) -- which means it might not be the best data-type for a "content" column.
A possibibly better solution might be to use one of the TEXT data-type :
...
content TEXT not null,
...
TEXT columns can contain strings that are a lot longer than varchar ; for more informations, see 10.4.3. The BLOB and TEXT Types.