I am building a cash register for my work place. I am having a trouble with Mysql.
I have db with three tables.
orders (where it stores : list_id, order_id (FK from order_line table),Id (FK for product id from product table), qty, total).
order_line (Where it stores : order_id, order_date, order_time )
products (id,product_name,product_desc,price)
What i want to do is to add the order in database. Insert into order_line a date and time and order_id(auto increment)
Also insert the items into orders table where it can retrieve order_id from order_line table so i can have same order_num for each item in orders table.
so far, this is what i tried;
INSERT INTO order_line
VALUES('','04/25/2015','11.52.06');
INSERT INTO orders
VALUES ('','orders_ibfk_2','orders_ibfk_1','2',30.00);
and recieved an error: Cannot add or update a child row: a foreign key constraint fails (shoelacestore.orders, CONSTRAINT orders_ibfk_1 FOREIGN KEY (id) REFERENCES products (id))
Any help will be appreciated. Thank You
I think your id column data types are not integer. Make it integer if it is ok.
when you execute following query
INSERT INTO order_line
VALUES(0,'04/25/2015','11.52.06');
You will get a value for order_id. Just assume it is 344
And assume in your products table you have a product with id 98
Then you can use follwing query to insert into
INSERT INTO orders
VALUES (0,344,98,'2',30.00);
i assume all ids are integers
Guys i found the solution.
INSERT INTO T1 (col1,col2) VALUES (val1,val2);
SET #last_id_in_T1 = LAST_INSERT_ID();
INSERT INTO T2 (col1,col2) VALUES (#last_id_in_T1,val2);
so basicly, T1 is the table for order_line which creates an auto increment order_id. SET #last_id_in_T1 = LAST_INSERT_ID(); copies the last order_id and inputs into orders table as i wished.
I hope this will help others as well.
Related
I have a databasw that has 2 columns.
First is category id, the second is group id.
Both are primary.
And i need to add to the table to the same category id some rows with group id 1 and 2.
I mean all the categories need to have groups 1,2,3
INSERT INTO `ps_category_group` (`id_category`, `id_group`)
SELECT `id_category`, 2 FROM (
SELECT DISTINCT `id_category`,`id_group` From `ps_category_group`) as x;
gives me
1062 - Duplicate entry '2-2' for key 'PRIMARY'
You are selecting values from the same table for inserting into the table. Then you are asking why you are getting primary key violations? Of course you are; that is exactly what your query is doing.
Based on the description, you seem to want:
INSERT INTO `ps_category_group` (`id_category`, `id_group`)
SELECT `id_category`, 2
FROM `ps_category_group`
ON DUPLICATE KEY UPDATE id_category = VALUES(id_category); -- This does nothing if the value already exists in the table
I need to create 4tables(Products,Customers,Orders,Order Items). Products hold name and price. Customers hold name. Orders hold customer, date and set of order items. Order items hold order, product and quantity. All tables should have auto-increment primary key – id.
After creating the table I need to execute different scripts and I cannot understand why when I run this script:
INSERT INTO Orders VALUES (1,'2015-02-13 13:47:04'), (2,'2015-02-14 22:03:44'), (3,'2015-02-18 09:22:01'), (4,'2015-02-11 20:17:18');
I am getting this error:
Column name or number of supplied values does not match table definition.
I create the table in this way:
Create table Orders
(
OrdersID int not null,
Customer varchar(50),
date date,
Set_Of_Order_Items varchar(50),
primary key(OrdersID)
)
Any suggestions why I am receiving this error?
If you want insert only some columns you must explicitally indicate these columns
INSERT INTO Orders (OrdersID , date)
VALUES (1,'2015-02-13 13:47:04'),
(2,'2015-02-14 22:03:44'),
(3,'2015-02-18 09:22:01'),
(4,'2015-02-11 20:17:18');
I have these three tables
loans,
customers,
loans_customers
loans has one record
insert into loans values(1234, "st", '2015-01-11', 2000);
customers has one record
insert into customers values(2276, "Anay", "Student");
I created another table loans_customers
create table loans_customers(
loans_customers_id decimal(10),
primary key(loans_customers_id),
loan_id decimal,
customer_id decimal,
constraint FK_loans foreign key(loan_id) references loans(loan_id),
constraint FK_customers foreign key(customer_id) references customers(customer_id));
Now I'm trying to insert values
insert into loans_customers values(3376, ?, ?);
In the above syntax how to I give values in the place of ?,? so that it should have many to many relationship perfectly.
Thank you,
First, when using insert, you should always list the columns. Then, you justinclude the ids:
insert into loans_customers(loans_customers_id, loan_id, customer_id)
values(3376, 1234, 2276);
If you are using a prepared query with parameters, then you can supply the values using parameters.
I have the following table in mariadb:
-id = key which shall autoincrement
-price
-amount
-name
-order_id
order_id can appear twice in the table but the combination of name and order_id should be unique. I now have a combination of name and order_id.
What I want to do is to add a record if the combination of name and order_id is not in the table.
If it's in then I want to change/get the amount value.
Is there a nice query to accomplish this?
Regards
I like using on duplicate key update for this. You need to start by creating a unique index on name and order_id:
create unique index ix_table_orderid_name on table(order_id, name);
Then the insert looks like:
insert into table(price, amount, name, order_id)
values (#price, #amount, #name, #order_id)
on duplicate key update amount = values(amount), price = values(price);
This replaces the values in the table with the new values. You can also increment them. Your question is unclear on the exact operation.
I have an InnoDB table that lists customer orders - this table records the customer's email address, contact information for the order, and the order id. This table uses innodb and contains roughly 220 million rows. What I do not have, is a table that lists the users.
Is it possible, perhaps using foreign keys, for me to create and populate a table that will contain a customer_id and e-mail address from the data that is in my orders table?
This is the query that I have so far:
INSERT INTO customers (email, last_order)
SELECT (email_address, order_date) FROM orders
ON DUPLICATE KEY
UPDATE last_order = MAX(order_date, last_order);
I believe this query will build the database, but it won't put the customer id into the row in the orders table, and I am not sure if it is the best approach to start with. Any suggestions?
You can achieve this it in 2 steps:
1st, fill in the customers table:
INSERT INTO customers (email, last_order ) SELECT email_address,
max(order_date) as last_order FROM orders GROUP by email_address;
2nd, update the orders table with customers' ids (assuming you have a field called customer_id in the orders table)
UPDATE orders o SET customer_id =
(SELECT id FROM customers WHERE email = o.email_address);
Finally, if you want to keep your customers table consistent, you can add an AFTER INSERT TRIGGER to the orders table to keep your customers table up-to-date.