I have 2 tables as : 'spen_recipe' and 'spen_recipe_type' as shown below
I want to insert the values into spen_recipe and spen_recipe_type simultaneously based on the pk and fk constraints.
Please suggest me the query.
This is what i have tried:
insert into spen_recipe
(`user_id`,`recipe_name`,`recipe_title`,`recipe_type`,`cooking_time`,`preparation_time`,`serving_to`,`recipe_desc`,`recipe_photo`,`created_at`,`modified_at`,`published_at`)
values
('2','recipe2','title2',(select `recipe_type_id` from spen_recipe_type),'20','30','4','desc1','image1','2016-11-05 11:21:43','2016-11-05 11:22:43','2016-11-05 11:21:43');
But need suggestion on how to proceed after select recipe_type_id from spen_recipe_type, cause both the table has null values in the columns, both are empty.
Should spen_recipe_type or spen_recipe be first inserted?
Related
I want to insert if in the table is not same row with values, but if there is row with the same values I want to only update one that will add +1 to the current value. I have current code, but it doesn't seem to update values in row that exists.
INSERT INTO raport(id, wykonawca, tytul, czas_trwania, powtorzenia)
VALUES('','$wykonawca2','$tytul2','$czas_trwania2', '$powtorzenia2')
ON DUPLICATE KEY UPDATE wykonawca='$wykonawca2', tytul='$tytul2', czas_trwania='$czas_trwania2',
powtorzenia='$powtorzenia2'+1
Ensure your table has a column declared as UNIQUE or PRIMARY KEY and is not an Auto-increment column
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row occurs. For example, if column a is declared as UNIQUE and contains the value 1, the following two statements have similar effect:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE t1 SET c=c+1 WHERE a=1;
(The effects are not identical for an InnoDB table where a is an auto-increment column. With an auto-increment column, an INSERT statement increases the auto-increment value but UPDATE does not.)
MySQL Reference
EDIT :
Your id column is empty you need to pass a value
The actual answer to your initial question as to why the rows do not update is because you are not passing a value for the PRIMARY KEY - this is your AUTO-INCREMENT id column.
Every time you pass :
VALUES('','$wykonawca2','$tytul2','$czas_trwania2', '$powtorzenia2')
this means that your id column is blank, so there is no duplicate just a new row. If you want to have an ON DUPLICATE KEY UPDATE you'll need to pass in the id.
For some more info about ways to handle multiple indexes and DUPLICATE UPDATE check this question out: MySQL behavior of ON DUPLICATE KEY UPDATE for multiple UNIQUE fields
Also, you should read this and action it as soon as possible, you shouldn't be passing variables straight into sql - it's hugely outdated and very unsafe:
How can prepared statements protect from SQL injection attacks?
Good day guys, I have a grade/score table in MySql that students record will be inserted into using php. I want to avoid a student having a score repeated for a term/period. What I mean is that a student cant have two(2) grades/scores for a subject(mathematics) in a term(periodOne) table. How do I accomplish this in MySql or php? here is how my table looks:
table periodOne (
id int AUTO_INCREMENT,
studentId int,
subjectId int,
score
)
Let me know if you need extra information. Thanks!!!!!!
you have to add a unique contraint in mysql like this : ALTER TABLE periodOne ADD CONSTRAINT uc_check UNIQUE(studentId, subjectId). You will also have to check with PHP that there is no existing row before to do your INSERT
You can declare the attribute as "Unique" by using UNIQUE CONSTRAINT for which you don't want duplicate value.
If your score are dependent on some other table also then you can use Composite Primary Key.
Two Table
First table CUST_ENTRY
cust_id(primary key),cust_name,cust_add
Second table CUST_PRICE
cust_price_id,cust_id(foreign key),cust_price1,cust_date1,cust_price2,cust_date2
I have insert first table completely but second table data cust_price2 and cust_date2 is insert blank but that not possible.
Please help me.
i hava a table with varchar primary key, that is a foreing key for some other tables.
Something like:
ID-------------NAME
1011001020-----product 1
1011001022-----product 2
1011001025-----product 3
Then, i have this array
array(
'1011001020',
'1011001022',
'1011001025',
'x',
'y'
)
This array will be used to insert values in another table with FK, so if any value is not an ID on the first table, the INSERT query will brake.
How do i find 'x' and 'y' before any attempt to insert. I would like to avoid selecting all ids from table one and make a PHP comparison, since there are a lot of records. I would rather a MySQL approach
I would suggest the following procedure:
Create the table and insert all the values into the table.
Remove the values that do not match.
Add the foreign key constraint.
The second and third steps can be done easily in SQL:
delete from temporarytable
where tt.otherid not in (select ot.otherid from othertable ot);
alter table temporarytable
add constraint fk_othertable foreign key (otherid) references othertable(otherid);
You can load the data however you like. For speed, I would recommend load data infile.
give this a shot. Tested on server, products is the new table and inserted only rows found in the array from items table.
INSERT INTO `products` ( `name` )
SELECT `name` FROM `items` WHERE `vendor_id` IN ( 1,2,3,4,.... )
I'm building a config table with two columns, config_name and config_value. I insert multiple rows in one statement:
INSERT INTO ".$dbPrefix."config (config_name,config_value) VALUES
('domain','$domain'),
('forest_root','$fr_if'),
('userGroup','$userGroup'),
('adminGroup','$adminGroup');
The config_name column is a primary key. How would I change this statement to automatically update the config_value if the config_name already exists?
You could try this syntax:
INSERT INTO table (field) VALUES (value) ON DUPLICATE KEY UPDATE field=value
Docs can be found here.
You're trying to do an upsert, and I think this may help:
http://database-programmer.blogspot.com/2009/06/approaches-to-upsert.html