How to use insert and update with no primary key - php

I have the following Mysql-table:
+-------+--------+------------+----+-------+
| Name | Number | Department | id | JobID |
+-------+--------+------------+----+-------+
| Sven | 2204 | Marketing | 10 | 111 |
| Peter | 2304 | IT | 20 | 222 |
| Bjorn | 4409 | IT | 30 | 333 |
+-------+--------+------------+----+-------+
I get the three columns: Name, Number, Department from a system where I don't have the id and need to perform something in my php script.
Now I would like to performa an insert if there is a new record. If there is an existing record I would like to perform an update, if something changed like Name, Number or Department.
For example, if Number changes it should compare Name and Department and then change number. If Departmend changes it should compare Name and Number and then change Department and so on.
The problem is, that I can not use insert...on duplicate key, because I don't get the primary key.
If I use Replace Into it deletes me also the entry for JobID. Is there a solution how to perform a sql that it will insert and also update if there is now entry? Or something that can do the trick?
Thanks for your help!

Related

How to identify which table to delete a record when data coming from two tables?

I have two tables where some same kind of information kept. One table has approved information and other one contains pending(waiting for approval) data. I fetch data from both table and display in a same view. So user will see data from both the tables. User can delete those records. But when deleting I've a trouble with finding out which table I should delete.
Assume, table1(Approved info), table2(Pending info)
table1
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 100 |
11 | test2 | N/A | 100 |
12 | test3 | N/A | 101 |
13 | test4 | N/A | 200 |
table2
id | name | description | creator |
-----------------------------------
10 | test1 | N/A | 105 |
11 | test2 | N/A | 103 |
12 | test3 | N/A | 106 |
13 | test4 | N/A | 202 |
table1 has a record with id of 10; and table2 has a record with id of 10 in that table. Id is the primary key of both tables. Both record will show to user. Let's say user wants to delete the record related to id 12 came from table2. So I want to delete that record from table2. But how can I figure out which table to delete that record. Because I can't use id to figure out the table. I have tried using some kind of data attribute attached with
data coming from table2 to differentiate them. But anyone can change them by inspecting it. So what is the proper way for solve this issue?
On any case, on any system, makes sense to have two to tables with same columns. That should be one of the firsts rules of database design. What's more, you discovered yourself how hard is to maintain a design like that. I see this on legacy systems developed with zero love to the code. In the future this will turn into a snowball. You should change it as soon as possible.
status column
The status of and entity or resource, is classic requirement, usually implemented with one little column which called : status, flag, mode, etc. In your case, it could have these values (#BhaumikPandhi comment):
pending/approved/rejected
id | name | description | creator | status |
--------------------------------------------
10 | test1 | N/A | 100 | pending|
If you are worried to the database optimization, you could use a tinyint with these equivalence in your documentation:
1 = pending
2 = approved
3 = rejected
status table
You could keep your first table called record
id | name | description | creator |
And create another one called record_status with 2 columns, in which record_id is a FK of record table
record_id | status |
Anyway, the status column is the most easy a classic approach to your requirement.

How to fetch items based on inserted order in DynamoDB..?

I have message table in dynamodb with the following fields:
Primary partition key => conversation_id
Primary sort key => id
other attributes => message,date_time,sender_id
+------------------+----------+----------------------+------------------------+----------+
| conversation_id | id | message | date_time | sender_id|
+----------------------------------------------------+------------------------+----------+
| p1OS9E | S04Ln | Hi.. how are you..? | 2016-11-30 06:58:11 pm | 11 |
| p1OS9E | JZkSo | Work finished..? | 2016-11-30 06:58:13 pm | 11 |
| p1OS9E | EN9N4 | I am fine. | 2016-11-30 06:58:12 pm | 12 |
| | | | | |
| a0zgOO | jmDdm | In online..? | 2016-12-40 08:43:12 pm | 51 |
| a0zgOO | mAEdY | Yes.. say.. | 2016-12-40 08:43:14 pm | 34 |
| a0zgOO | aWKrp | Come to play.. | 2016-12-40 08:43:12 pm | 51 |
+------------------+----------+----------------------+------------------------+----------+
While query items based on partition key, the result returned as unordered(not in inserted order).
The sort key id, AttributeType is `string', a random generated code to make the Primary key as unique.
First i used date_time as sort key, but if within same conversation_id with same date_time a message will enter. So the data loss may occur.
How to get my items with the order the message get inserted(based on time)..?
Note: I am using PHP (Codeigniter MVC).
There are many ways you can achieve this, the following are the two approaches that I generally use:
1) Add indexes: You need to add LSI(local secondary index) to your table with range key as DateTime.
Now you can query you LSI by passing you hash key and for DateTime you can use >= or >= operators to get in sorted order.
2) Sorting at application level: I personally use this solution as we don't have to pay additional amount for index, Get all data based on Hash then sort manually at application level by DateTime
There is one more answer of mine with a similar issue.
Hope that helps

MySQL assign a unique value from another table

I have a problem that I can't figure out, I'm not experienced enough (or it can't be done!) I've trawled Google for the answer with no luck.
I have a system where I need to assign an ID to each row, with the ID from another table. The catch is that the ID must be unique for each row created in this batch.
Basically I'm selling links on my Tumblr accounts, I need to assign a Tumblr account to each link that a customer purchases but I want to assign all possible Tumblr accounts so that duplicates are kept to the minimum possible.
The URLs - each link that a customer buys is stored in this table (urls_anchors):
+----------+--------------------+------------+-----------+------+
| clientID | URL | Anchor | tumblrID  | paid |
+----------+--------------------+------------+-----------+------+
| 1234 | http://example.com | Click here | 67 | Yes |
| 1234 | http://example.com | Click here | 66 | Yes |
| 1234 | http://example.com | Click here | 65 | Yes |
| 1234 | http://example.com | Click here | 64 | Yes |
+----------+--------------------+------------+-----------+------+
All of the Tumblr accounts available for allocation are stored in this table (tumblrs):
+----------+-------------------+------------+
| tumblrID | tumblrURL | spacesLeft |
+----------+-------------------+------------+
| 64 | http://tumblr.com | 9 |
| 65 | http://tumblr.com | 9 |
| 66 | http://tumblr.com | 9 |
| 67 | http://tumblr.com | 9 |
+----------+-------------------+------------+
My best attempt at this has been the following query:
INSERT INTO `urls_anchors` (`clientID`, `URL`,`Anchor`, `tumblrID`, `paid`) VALUES ('$clientID','$url','$line', (SELECT #rank:=#rank+1 AS tumblrID FROM tumblrs WHERE #rank < 68 LIMIT 1), 'No')
Which works but keeps adding incrementally indefinitely, when there are only X number of Tumblrs to assign. I need the query to loop back around when it reaches the last row of Tumblrs and run through the list again.
Also i'm using this in a PHP script, I'm not sure if that's of any significance.
Any help would be MASSIVELY appreciated!
Thanks for looking :)
You can use a SELECT query as the source of data to insert.
INSERT INTO urls_anchors (`clientID`, `URL`,`Anchor`, `tumblrID`, `paid`)
SELECT '$clientID','$url','$line', tumblrID, 'No'
FROM tumblrs
LIMIT $number_of_rows
DEMO
This will assign $number_of_rows different tumblrID values to the rows.
If you need to assign more tumbler IDs than are available, you'll need to do this in a loop, subtracting the number of rows inserted from $number_of_rows each time. You can use mysqli_affected_rows() to find out how many rows were inserted each time.

how to maintain a continuous id number row count in mysql

i have an id coloumn which is integer and auto incremented type.
The problem is when ever i delete a row the continuity of the number breaks.
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Behance | 2 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
FOR EXAMPLE if i delete the row with id=2, then i output in id will be
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 3 |
| flipkart | 4 |
+----------------------+----+
but i want it to be like :
+----------------------+----+
| name | id |
+----------------------+----+
| mashable | 1 |
| Techcrunch | 2 |
| flipkart | 3 |
+----------------------+----+
How to do it ??
To directly answer your question, here's how you fix those gaps in sequential numeric fields: Fixing gaps in mysql table row id after we delete some of them
But let's be careful here for a moment.
Let's assume id is your primary key. ID's are usually the point of reference to an object, because auto-generated ID's are unique. Call it a convention.
That means that If ANY part of your code depends on the id column, your application will break.
If you NEED to do this, then use some other field as main reference. Perhaps an unique name field or something similar.
If ID is NOT your primary key, then you probably should've chosen another name for it to begin with. Anyway, in this case, the chances of you breaking anything are much smaller.
Notice that I said smaller, but not zero. We don't know your application, so it's possible that your code uses id for something important, and that'll mean trouble for you.

How to construct table with dynamic input fields?

I have a PHP script where you can (as admin) select how many input-fields there will be in a question form. Some of the fields are non optional, but som are (as many as you like).
The table in MySQL for collecting the answers looks like this:
id | userid | fname | ename | seat | optional
If the admin want it to be two optional input-fields then the result of one filled form would take three tows in the table:
| 5 | 3 | Peter | Pan | 4 | |
| | 3 | | | | opt.value1 |
| | 3 | | | | opt.value2 |
Is this really the best way to store this in? How would you solve it?
And also, how can I make shure that the userid is unique for the user? I can't use the auto-increment key value thing in MySQL because the same value is on three rows...
The way i learned it you have to use multiple tables. Like this:
Table1:
id | userid | fname | ename | seat
Table2:
userid | optional
Table2.userid is a reference to Table1.userid
Then the fields that has to be filed can be put into the first table and all the optional in the second.
If i follow your example your database should look like this:
Table1:
id | userid | fname | ename | seat
5 | 3 | Peter | Pan | 4
Table2:
userid | optional
3 | opt.value1
3 | opt.value2
By the way, why do you have both id and userid in Table1?
Best practice would be to store "id" and "optional" values in a separate table.
Then pull the information you want from it for each "id".

Categories