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.
Related
I have a MySQL query that is meant to update two columns by increments of 1, or create them if they do not exist.
My current table has four columns: id, username, playtime, and deathtime.
As it stands now, this is the query I use:
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1
AND `deathtime`=`deathtime`+1
(The username is dynamic in application, but one is provided for the example).
My issue, however, is that on a duplicate key it doesn't increment the values, rather it sets both values to 0. Through numerous tests of tweaking the query, I have found that it is the AND clause that causes the query to "reset" the values. For instance, this query will increment the playtime column correctly:
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1
And if you swap "playtime" with "deathtime", then it increments the other column correctly. Why is this?
Use a comma to delimit statements here.
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1, `deathtime`=`deathtime`+1
The example can be seen in the documentation here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
Try this.. a comma, instead of AND
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1 ,
`deathtime`=`deathtime`+1
I need to make something like this:
INSERT `stats` (`id`,`clicks`) VALUES
(1, clicks+7),
(2, clicks+3),
....
ON DUPLICATE KEY UPDATE `clicks`=VALUES(clicks)
in other words, when table has no record with pk id - it inserted and clicks gets 7 (or 3). When record with PK is present, then old value of click should be increased by 7 (or 3). As you can see, increment value for each row is different. Current query always overwrites old value. Please help to modify this query.
The VALUES have to be literal values, not references to columns:
INSERT INTO `stats` (`id`,`clicks`) VALUES
(1, 7),
(2, 3)
ON DUPLICATE KEY UPDATE `clicks`=`clicks` + VALUES(`clicks`)
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'.
I have to insert big amount of records in a table. It is not quite normalized, so most of the fields are repeated.
I know the proper command is:
INSERT INTO table_name (field1, field2, ..., field_n)
VALUES (value1, value2, ..., value_n),
...
(value1, value2, ..., value_n)
But I wonder whether it is possible to keep some of the values fixed and just indicate the different ones.
Let's say instead of
INSERT INTO table_name (shop, month, sale)
VALUES (1, 2, 23),
(1, 2, 28),
(1, 2, 29),
(1, 2, 30)
Having something like
INSERT INTO table_name (shop, month, sale)
VALUES (1, 2, 23), ... 28 / 29 / 30
If it is not possible I would create a procedure with a loop, feeding a string, etc. It would not be a big issue, but my point is to know if INSERT INTO has any particularity that allows doing this without procedures.
You can try something like the following:
INSERT INTO table_name (shop, month, sale)
SELECT * FROM
(SELECT 1 as shop, 2 as month) as sm,
(SELECT 23 as sale UNION ALL SELECT 28 UNION ALL SELECT 30) as sales;
You can use the default constraint which will add the default value when you do not specify that in the insert into statement. If you specify a value that value will be added.
Just set the default value for your column in your table
ALTER TABLE tblname ALTER columnName SET DEFAULT 'value'
Refer http://www.w3schools.com/sql/sql_default.asp
You can use a temporary table to insert the different values and then use insert ... select. I don't know if it will be a big saving for you:
CREATE TEMPORARY TABLE sale_temp (sale int);
INSERT sale_temp (sale) VALUES (23), (28), (29), (30);
INSERT INTO table_name (shop, month, sale)
SELECT 1, 2, sale
FROM sale_temp;
DROP TABLE sale_temp;
I have a table with 2 column say C1 and C2. i need to insert 10 rows where always Columns 2 will be same.
INSERT INTO Table
(C1, C2)
VALUES
(100, 'X'),
(101, 'X'),
(102, 'X'),
(103, 'X'),
(104, 'X'),
(105, 'X'),
(106, 'X');
Is there any other way like below,
INSERT INTO Table
(C1)
VALUES
(100,102,103,104,105,106)
and value of C2 should be X for the inserted rows
Thanks in advance,
You will have to create a table
CREATE TABLE `tblA` (
`C1` INT(10) NULL,
`C2` VARCHAR(50) NULL DEFAULT 'X'
);
Then when you do INSERT INTO Table (C1) VALUES (100,102,103,104,105,106); the column C2 will have 'X' as a value.
There are, but not really very advantageous, one way or the other. If what you're trying to do is to write the constant value only once, I'd try to keep it simple and go for
SET #X := 'X'
or
SELECT #X := expression FROM ...
followed by
INSERT INTO Table
(C1, C2)
VALUES
(100, #X), (102, #X), ...;
You can apply an insert into from a select statement VALUES is hard-coded answers... insert into from select is query based insert. In the sample below, I am using #MySQL variables to create a variable "#num" and start it at 99. Then you just have to substitute per the sample as it describes... Any table that has at least the number of records you want to add. So, in my case, I am only concerned with any table that has AT LEAST 10 records. If you want to add 1000, and have a table with AT LEAST that many records, use that. The LIMIT obviously limits how many records you return from it. Since you are not really using any columns from the table, it doesn't matter what that table is. So, now the #num := #num +1 will keep increasing for every record processed, thus going to 100, 101, 102, etc for the "c1" column, and the 'X' will be constanct for the "c2" column and you are done.
INSERT INTO Table
(C1, C2)
select
#num := #num +1 as c1,
'X' as c2
from
AnyTableThatHasAtLeastNumberOfRecordsYouWant,
( select #num := 99 ) sqlvars
limit
10