How should I insert numbers in a SET column? I know that for ENUM it's recommended to not store numbers in ENUM column, and numbers should be insert with ''.
Is it the same for a SET column? Should be numbers inserted with ''? Like '1','2' or is it fine to insert it like: 1, 2 ?
You insert using SET values names and enclose them in single quotes and separate them with commas.
Minimalistic example from here:
CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
INSERT INTO myset (col) VALUES ('a,d,d,s');
First of all, don't use MySQL's SET and ENUM column types.
Second, your set definition really shouldn't contain numbers. 'One' is OK, '1' will be extremely confusing.
Third, if you insert numerals like 1, 2, etc then it will actually insert the n-th member of the set instead of that number, whereas if you insert '1' it will insert that member. The difference comes up when you have a set with the following members in that order: 'foo', 'bar', '1', '2' - if you insert '1' it would be the same as inserting a 3, but if you insert a 1 it'd be the same as 'foo'.
Bottom line: don't use the SET and ENUM column types.
Number should insert like 1 ,2 ,3.
This consider as character if you put into single quote like '1','2'.
Related
I have a table with something like 100 columns and now I would like to insert a row with some columns equal to some values and default values for all other columns. Is it possible?
Something like:
insert into tablename (col1, col22, col33) values (1, 2, 3)
The columns not listed in the column list will be assigned to their default values.
You can also do:
insert into tablename values (1, default, 3, default, 5, ..., 100)
But nothing I'd do with 100 columns. Too easy to make mistakes.
i try to make column in my database table , with set data type to store in it data in this fomat " 10,2,44" i made SET column like this SET('A' , 'B' , 'C')
but when i try to insert data in it i got this response
Error Code: 1265. Data truncated for column 'setcol' at row 1
and this is my query
INSERT INTO `voting`.`questionnaires` (`name`, `cat_id`, `init_date`, `end_date`, `setcol`) VALUES ('sad', '2', '2008-02-02', '2008-02-02', 'A, B');
should i use specific format ? and does set accept repetition ?
To set your desired value you would store a 3 (without quotes) instead of 'A,B'. That is because values in a SET field are stored numerically in bitwise fashion. So each value in a set definition corresponds to on/off bits in a binary number.
Therefore you statement would be:
INSERT INTO `voting`.`questionnaires` (`name`, `cat_id`, `init_date`, `end_date`, `setcol`) VALUES ('sad', '2', '2008-02-02', '2008-02-02', 3);
For example, from the MySQL Documentation:
For a column specified as SET('a','b','c','d'), the members have the
following decimal and binary values.
So since A has a value of 1 and B has a value of 2, add them together and you get 3. Or to set C and D you would store 12, etc.
For this reason, it's ill-advised to have numbers as members of a SET field (as you hinted you might) since storing '5' has a completely different meaning than storing 5.
create table ints (val1 int not null defalut 10,val2 int not null dafult 20);
insert into ints values();
Output is 10,20..OK
But if i explicity pass value to one column then insert will not work
insert into ints values(99,) ;
INSERT FAILS
Please help...
if you want to insert into all columns it's not need to specify columns name but you have to specify columns if not to want insert into all columns
insert into ints(val1) values(99)
You need to specify what columns the values you do give apply to:
insert into ints (val1) values(99)
you should specify the column that you are inserting the value into
insert into ints(val1) values(99);
Read this documentation for insert syntax
UPDATE:
Since you want the default for var1, var2 and var3, you just have to specify the value for var4
insert into ints(var4) values(99);
or
insert into ints(var1, var2, var3, var4) values(10, 20, "DEFAULT", 99)
Its as simple as, specify the column names for which you want to insert values in the same order
INSERT INTO `configuration` VALUES ('', 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
I run this command in phpMyAdmin, it shows
#1366 - Incorrect integer value: '' for column 'configuration_id' at row 1
configuration_id is an auto increment field beginnning of 1
Instead of this INSERT INTO configuration VALUES ('', 'News Box Character Count',
Pass the value as NULL for auto_increment or integer column or you can simply not to include that column in sql query.
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', ...
This is because, mysql is running in the strict mode.
You can either use NULL for all the integer columns when there is nothing to enter them or turn off the MySql Strict mode.
For an autoincrement in MySQL, either insert NULL or insert nothing at all:
Easiest & Cleanest: using NULL
INSERT INTO `configuration` VALUES (NULL, 'News Box Character Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);
More work: name every column except the autoincrement one
INSERT INTO `configuration` (every,column,except,the,first) VALUES ('News Box Charac`ter Count', 'NEWS_BOX_CHAR_COUNT', '200', 'Set the number of characters (bytes) that you want to display in the news preview box.', 19, 99, NULL, '2004-09-07 12:00:00', NULL, NULL);`
It seems pretty obvious to me? You gave an integer column and you are explicitly inserting a string (although it's empty). If the column is set to auto increment, remove the first value ('') from your values array and you should be fine. Also, maybe you wanto to specify the columns you are inserting values for, like:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
For the AUTO_INCREMENT field you can set NULL; it will generate new value automatically.
For example -
INSERT INTO table_name VALUES(NULL, 'value', ...)...
If the column is auto-increment then you can simple omit it from the INSERT statement.
Replace the '' value with NULL ('' isn't a valid integer). See here.
You should always use column names when inserting. So just don't have to insert into the column configuration_id and your sql statement doesn't break when changing the columns.
Use something like this: INSERT INTO table (column1, column2) VALUES (value1, value2);
I have a table of values, with one of the columns being of type SET.
If it currently has the members ('a', 'b', 'c', 'd'), how do I add 'e' to the possible values?
I realize that using a SET type is a little strange, and I'm unclear why one would use it instead of a foreign key to another table with the values of the set, but I didn't design the database in question, and can't change it that much.
Thanks for your help!
UPDATE: I want to update the SET type for all rows, not just one, if that helps.
To add an element to an existing SET use the CONCAT() function to add the new element to your comma separated list. To work with decimal values, we can use the bitwise OR operator |.
UPDATE set_test SET myset = CONCAT(myset,",Travel")
WHERE rowid = 3;
or
UPDATE set_test SET myset = myset | 1 WHERE rowid = 3;
You can also use the CONCAT_WS() function, which handles list separators for us:
UPDATE set_test SET myset = CONCAT_WS(',',myset,'Dancing')
WHERE rowid = 6;
You want to add 'e' to the allowable values in that set field, or it's already there and you want to add 'e' to the current value of the set field in that table?
If it's not already an allowed value, then you'll have to do an ALTER TABLE and redefine the field:
ALTER TABLE set_test CHANGE myset myset SET('a','b','c','d','e');
(yes, 'myset' is put in there twice, it's a "currentname newname" sort of thing.)
Otherwise just do an UPDATE TABLE and concat the field with 'e' as the previous answer says.