My tables are :
orders(orderID,orderDesc)
orderscoding(orderscodingID,orderID,codeA)
I have around 230 records for both tables.
I want to alter the (orderID) auto-increment from
1,2,3,4,5 ..... to 100001,10002,10003,10004,10005
and reflect it on the related table , is this possible?
So basically this will not work, as it is for new insert (but I want to modify existing records):
ALTER TABLE orders AUTO_INCREMENT=100001;
If you want to update existing orderIDs, you can do like this
UPDATE orders SET orderID = orderID + 10000
All existing orderID will be incremented by 10000 with this query. So, 1 will become 10001, 2 will be 10002 and so on...
Do the same for orderscoding table to keep it synced with orders table (do the same if any more tables are synced to orderID.)
UPDATE orderscoding SET orderID = orderID + 10000
Related
I wander around can I transfer the data gathered in memory table to actual one by sql query only.
The two tables have the same structure and pk is products_id(int, AI)
The problem is the criteria is completely different than the pk. The products are identified by 2 columns - barcode and company.
So ignoring the pk in whole, I need to update the data if in actual table there is a row with the same barcode and company, and insert new record if there is none.
Tried this:
INSERT INTO products (products_sku, ...)
SELECT products_sku... FROM temp_products
WHERE (temp_products.products_barcode = products.products_barcode) AND (temp_products.products_comp = products.products_comp)
But i dont have access in the select to products table so to make the filtering
I think you need to add a unique key on products_barcode and products_comp:
ALTER TABLE products ADD UNIQUE KEY (products_barcode, products_comp);
Once you have it you can perform insert-or-update in one statement:
INSERT INTO products (/* all columns except the id */)
SELECT /* all columns except the id */
FROM products_sku
ON DUPLICATE KEY UPDATE some_field = VALUES(some_field), ...
/* list all columns except the id / barcode / comp */;
So when it meets a duplicate barcode/comp pair it will fall into the ON DUPLICATE KEY UPDATE and won't insert. Read more how it works: https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html
I have to update as well as insert data in MySQL in a single query. I have two tables: one is paytransactionfailure and second is walletbalance in which I have to first get data from paytransactionfailure table and and update the status of this table and insert data in walletbalance table. Is this possible in a one query.
My first table name:
paytransactionfailure
...........................................................
id fromid toid amount refund_status user_register
...........................................................
1 5 6 20 0 1
walletbalance
...............................................
id user_id balance transaction_type
...............................................
1 5 100 credit
I want this if SELECT * from paytransactionfailure WHERE refund_status='0' and user_register='1' and fromid='5' query exists then I have to update refund_status=1 in paytransactionfailure table and at the same time I want to insert data in walletbalance table for fromid user like this
want this output
...............................................
id user_id balance transaction_type
...............................................
1 5 100 credit
2 5 20 credit
for this I have used below query but I have get success in updating record but I don't get success in inserting record in walletbalance.
Is this possible or I am going in a wrong direction?
UPDATE paytransactionfailure
SET `refund_status` =1
WHERE EXISTS (SELECT * from paytransaction WHERE refund_status='0' and `user_register`='1')
What you are looking for is INSERT ... ON DUPLICATE KEY UPDATE (to insert new rows or update one that exists), or REPLACE (to insert new rows and overwrite data of any that already exist).
In both cases, the statements rely on UNIQUE keys in the table, which your walletbalance table is missing, and the statements can only be performed on the same table.
You might want to look into using stored procedures to carry out these operations - or redesign the process entirely, because IMO the schema in question just wasn't designed for what you are trying to do with it.
I have a table A which has a auto increment serial number field SLNO. When i insert values in table it will increment automatically like 1,2,3,4... etc. But when i delete a row from the table the order get break. ie, if delete row with serial number 2 then the serial number field will 1,3,4. But I want to maintain a continuous order like 1,2,3 even delete rows. Is there any way to maintain this order, like using trigger or somthing
A primary auto-increment key is only for uniquely identifying a record. Just leave it be.
Don't misuse the primary key as indicator of your record order. If you need specific order of your records then use an extra column for that. For instance a timestamp column.
If you need a specific order of your records use a timestamp column with a default value of current_timestamp. That way it will be inserted automatically.
ALTER TABLE your_table
ADD column inserted_timestamp TIMESTAMP default current_timestamp;
SQLFiddle demo
You should leave it as it is.
However, if you do really need, you can "recalculate" the primary key, the index:
set #pk:=0;
update
your_table
set pk=#pk:=#pk+1
order by pk;
add a column that will speicfy that is deleted
example:
1 - deleted already
0 - not deleted
and add where deleted = 0 in your select query
primary key column 2 column3 ..... deleted
1 1
2 0
3 0
4 1
5 0
Storing an number of a record would make deletes inefficient. Instead you can rely on existing SLNO indexes you already have, that should be enough for all use cases that come up to my mind.
If you SELECT whatever ORDER BY SLNO LIMIT ... OFFSET k, then returned rows have IDs k, k+1, k+2, ...
If you want to get an id of a record knowing its SLNO:
SELECT COUNT(SLNO) FROM A WHERE SLNO <= thatnumber
If you want to get thatnumber'th record:
SELECT * FROM A ORDER BY SLNO LIMIT 1 OFFSET thatnumber
You can do by alter the table and delete primary key then again create primary key.
But why you need this. If you have use this key as foreign key in other table. Then you lost all the data.
For example:
Row Name
1 John
2 May
3 Marry
4 Tom
5 Peter
Suppose I delete row 2 and row 3, is it possible to update Tom and Peter to row id 2 and 3 respectively and the next insert row to be row id 4?
yes, but you need to recreate Row:
ALTER TABLE `users` DROP `Row`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `Row` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
No, because think of the problems that this could create. Imagine if you are managing a store inventory and each item has an internal barcode, based on an ID in a database table. You decide never to stock one item again and take it out of the database, and then every ID for every item with an ID greater than the removed item gets decremented by 1... all those barcodes on all those items become useless.
ID numbers in databases aren't generally meant to be used or referenced by people. They are meant to be a static index of a certain item in a database which allows the computer to pull up a given record quickly and reliably. When creating your ID field of your table, make sure you make the datatype large enough to account for these lost IDs when they do occur.
This is just a suggestion. I don't say this is the best solution. Just consider.
You execute your delete query.
DELETE FROM table_name WHERE Row IN (2,3);
Once deleted you make a select query request with PHP and get the data set to an array.
SELECT Row, Name from table_name ORDER BY Row ASC;
Then make a UPDATE execution using a loop.
$index = 1;
foreach($dataset as $item)
{
// Your update query
$sql = "UPDATE table_name SET Row=$index where Name='" . $item['Name'] . "'";
$index++;
}
Before you insert next query you have to get the max value of Row and set +1 value as the Row of the insert query.
This is just an idea. Not the complete code.
I have a table that stores orders.
id (auto increment) an oid (not auto incremented) and then the order details.
some orders can have multiple entries, this is why i have an ID and an Oid(order id)
so if you order 3 xwidgets and 4Zwidgets they go under the same OID
I am using the following code to assign the next OID number and am wondering if this is the best way:
$maxquery = mysql_query("SELECT MAX(oid) FROM ordr") or die(mysql_error());
$maxresult = mysql_fetch_array($maxquery);
$maxid = $maxresult['MAX(oid)'];
$oid = $maxid + 1; -- this gives me a new OID for the next entry since i can not auto increment this column
You probably want to use auto-increment columns. This will let you insert the data, and MySQL will pick an index for you. You'll need to denormalize your data to do this though.
You'll want two tables: orders and order_items, where:
orders
------
order_id
[any other columns]
order_items
-----------
item_id
order_id
[any other columns]
Insert the order into the orders table with a NULL order_id and record the order_id that MySQL sets. Now insert the order_item using the order_id you have, and let it auto-generate the item_id.
This way, MySQL will generate the numbers, so you don't need to worry about two orders trying to use the same order ID. Also, any order information won't be duplicated for each item in the order.