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

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.

Related

Is it ok to use VARCHAR (5000) field in MYSQL for given scenario?

I am developing a classified website using ASP.NET and DB is MYSQL.
I have a header table for store common details of ads.
So here is my header table's database schema.
CREATE TABLE `test`.`header` (
`header_id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR (500) NOT NULL,
`description` VARCHAR (5000) NOT NULL,
`is_published` TINYINT (1) NOT NULL DEFAULT TRUE,
//etc..
PRIMARY KEY (`header_id`)
) ENGINE = INNODB CHARSET = latin1 COLLATE = latin1_swedish_ci ;
So I am using varchar(500) for title and varchar(5000) for description. So is it OK to use varchar 5000? Reason why I am asking this is some people are saying long varchar fields are converted to Text field inside MYSQL ( I dont know about this). How much is this long? Also some people are saying there is a limitation in row size. So is varchar(5000) field will lead to any performance issue?
Yes I can use Text field but remember I want a limitation for the description. otherwise users will copy paste a novel to description field. :)
What is your suggestion? Another data type or anything....
Thank you very much.
Assuming that 5000 characters is your limitation, then VARCHAR(5000) is perfectly reasonable.
Take a look at this question if you are curious about the differences between VARCHAR and TEXT: MySQL: Large VARCHAR vs. TEXT?.

MySQL Create Table with Unique Constraints

I am just starting with SQL syntax, and am trying to create a table.
Here is my 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 'CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName), ) ENGINE = INNODB' at line 7
And here is my SQL:
CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` TEXT NOT NULL,
`firstName` TEXT NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id)
CONSTRAINT uc_people_2nd UNIQUE (lastName,firstName),
) ENGINE = INNODB;
I tried this in NodeDB (which I am developing in), and then PHPMyAdmin.
Fix the comma and make the names varchar():
CREATE TABLE `people` (
`_id` INT NOT NULL AUTO_INCREMENT,
`lastName` varchar(255) NOT NULL,
`firstName` varchar(255) NOT NULL,
`JSON` TEXT NOT NULL,
PRIMARY KEY(_id),
CONSTRAINT uc_people_2nd UNIQUE (lastName, firstName)
) ENGINE = INNODB;
This works on SQL Fiddle.
Note that you don't have to give a unique constraint a name. You can also drop the constraint keyword, so the following works just fine:
UNIQUE (lastName, firstName)
EDIT:
The text data type is described here on the page with other "large-objects". These are special types that are arbitrarily long (think megabytes). They have limits when used in indexes. In particular, they need a length prefix. So, you cannot declare that a text column is unique. Only that they are unique in the first N characters (up to about 1000).
For names, that is way overkill. MySQL supports string types of various sorts. The most useful is varchar(). These are appropriate for a name field. They can be used with indexes easily. And MySQL supports a plethora of functions on them.
In other words, if you do not know what text is, you do not need it. Learn about and use varchar() and char() (or nvarchar() and nchar() if you need national character set support). Forget about text. One day if you need it, you'll rediscover it.

What is difference between char and varchar

CREATE TABLE IF NOT EXISTS `test` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`country` varchar(5) NOT NULL,
`state` char(5) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
I tried following query to insert data
INSERT INTO `test`.`test` (`id` ,`country` ,`state`)
VALUES (NULL , 'south-india', 'Gujarat');
When I execute above query It will shows following warning
Warning: #1265 Data truncated for column 'country' at row 1
Warning: #1265 Data truncated for column 'state' at row 1
I found Reference that VARCHAR is variable-length.CHAR is fixed length.
Then what you mean by
VARCHAR is variable-length.
CHAR is fixed length.
VARCHAR(5) will use at most 5 characters of storage, while CHAR(5) will always use exactly 5.
For a field holding a person's name, for example, you'd want to use a VARCHAR, because while on average someone's name is usually short, you still want to cope with the few people with very long names, without having to have that space wasted for the majority of your database rows.
As you said varchar is variable-length and char is fixed. But the main difference is the byte it uses.
Example.
column: username
type: char(10)
if you have data on column username which is 'test', it will use 10 bytes. and it will have space.
'test______'
Hence the varchar column will only uses the byte you use. for 'test' it will only use 4 bytes. and your data will be
'test'
THanks.
As you mentioned VARCHAR is variable-length. CHAR is fixed length.
when you say
Varchar(5) and if the data you store in it is of length 1, The
remaining 4 byte memory space will be used by others. example: "t"
on the other hand
Char(5) and if the data you store in it is of length 1, The remaining
4 byte memory space cant be used. The 4 byte will end up not used by
any other data. example: "t____" here ____ is the unused space.

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

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.

Mysql Warning Question?

I inserted some info into my mysql database and I got the following error listed below. What does this mean and how can I fix it?
1 row(s) inserted.
Inserted row id: 1
Warning: #1265 Data truncated for column 'summary' at row 1
Here is my Mysql tables structure below.
CREATE TABLE mem_articles (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
member_id INT UNSIGNED NOT NULL,
title VARCHAR(255) NOT NULL,
summary VARCHAR(255) DEFAULT NULL,
content LONGTEXT NOT NULL,
date_created DATETIME NOT NULL,
date_updated DATETIME DEFAULT NULL,
PRIMARY KEY (id)
);
I think it means that the amount of characters you attempted to insert into the summary column exceeded 255, perhaps you should alter it to be TEXT instead of VARCHAR(255).
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html
It means that the data were "truncated", which in MySQL terminology means either it was truncated, or it was changed into something totally different if it was incompatible with the type.
This behaviour sucks; if you don't want it, use
SET SQL_MODE='TRADITIONAL'
Then it will behave like a sensible database (unfortunately this will probably break your entire code base if it's an existing application)
I would suggest setting the type to "longtext" or something larger.