How to set specific UNIQUE column in MySQL DML Statements - php

i'm facing an issue when i'm trying to insert records in a table with similar columns. So basically my table is structured like this ::
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | MUL | NULL | |
+-------+-------------+------+-----+---------+----------------+
Now my PDO statement is this::
INSERT INTO supplement_brand (name) VALUES (:name)
So basically what i want is, that each name field will have a unique value. I already have INDEX set on the name field. Now i wanted to remove the primary key, and the auto-increment attributes from the id field. But that would entail bad database design practices.
Right now upon insertion the id field is being auto-incremented automatically, and many duplicate values are getting inserted in the name field:
Here is a sample of whats happening ::
+-----+-----------------------------+
| id | name |
+-----+-----------------------------+
| 1 | 2 to 1 Protein Bar |
| 7 | 2 to 1 Protein Bar |
| 8 | 2 to 1 Protein Bar |
| 28 | 2 to 1 Protein Bar |
| 93 | 2 to 1 Protein Bar |
| 98 | 2 to 1 Protein Bar |
| 230 | 2 to 1 Protein Bar |
| 231 | 2 to 1 Protein Bar |
| 232 | 2 to 1 Protein Bar |
| 2 | 360CUT |
| 3 | 360CUT |
| 4 | 360CUT |
| 5 | 360CUT |
| 6 | 360CUT |
| 9 | 4 Dimension Nutrition |
| 10 | 4 Dimension Nutrition |
| 11 | 4 Dimension Nutrition |
| 12 | 4 Dimension Nutrition |
| 13 | 4 Dimension Nutrition |
| 14 | 4 Dimension Nutrition |
| 15 | 4 Dimension Nutrition |
| 16 | 4 Dimension Nutrition |
| 17 | 4 Dimension Nutrition |
| 18 | 4 Dimension Nutrition |
| 19 | 4 Dimension Nutrition |
| 20 | 4 Dimension Nutrition |
| 21 | 4 Dimension Nutrition |
| 22 | 4 Dimension Nutrition |
| 23 | 4 Dimension Nutrition |
| 24 | 4 Dimension Nutrition |
| 25 | 4 Dimension Nutrition |
+-----+-----------------------------+
How can i prevent the insertion of duplicate values in the name field. Please provide any sort of help, i'm literally stuck right now. Thanks in advance

INDEX will not guarantee uniqueness. There is UNIQUE key created for such tasks.

ALTER TABLE `supplement_brand` ADD UNIQUE (`name`);
This should ensure that the name field requires a unique value.
If you add another column ie brand and you want the name to be unique, but only if they share the same brand also you can do the following:
ALTER TABLE `supplement_brand` ADD UNIQUE (`name` ,`brand`);

You have two option, first you can use UNIQUE for that field "name" statement above Or you can use UPSERT statements: follow this link for complete description: http://mechanics.flite.com/blog/2013/09/30/how-to-do-an-upsert-in-mysql/

Related

Do we need to create separate tables for diff types of recharges such as(mobilerech,dthrech,elecbill, waterbill, etc) or can done in 1 table

I am working on a project for recharge and bill payments. I am confused about whether to use a single table for all type of recharges like mobile recharge, dtn recharge, electricity bill, water bill, card recharges, etc, which is difficult or do I create separate tables for each type of recharge and work on them.
Table has colums
recharge_id PRIMARY KEY,
recharge_amount ,
recharge_status,
recharge_time,
user_id,
payment_id
The data has to be added into the table when there is any recharge process with status and other details.
Although, you didn't show anything you tried, i think this is a viable question.
A possible approach would be to create one table for your type and one for your recharges
Something like the following should work
create a table recharge_type like
+----+------------------+--------+
| id | name | active |
+----+------------------+--------+
| 1 | Mobile recharge | 1 |
| 2 | Dtn recharge | 1 |
| 3 | electricity bill | 1 |
| 4 | water bill | 1 |
| 5 | card recharge | 1 |
+----+------------------+--------+
and your table recharge
+----+------------------+---------+------------+--------+--------+------------+
| id | recharge_type_id | user_id | payment_id | amount | status | time |
+----+------------------+---------+------------+--------+--------+------------+
| 1 | 1 | 1 | 1 | 2.00 | 1 | 2019-03-05 |
| 2 | 2 | 1 | 2 | 3.00 | 3 | 2019-03-05 |
| 3 | 2 | 1 | 2 | 4.00 | 4 | 2019-03-05 |
+----+------------------+---------+------------+--------+--------+------------+
With this type of construction you are pretty flexible for nearly any approach.
If you want to understand why this is a good approach, you should read
some articles about first normal form. You can find an article
here on Wikipedia.

Split in comma-separated string and show in MySQL and PHP

I have a small database:
+-----------+-----------+------------------------+
| Name | Number | Hobby |
+-----------+-----------+------------------------+
| Alex | 2, 3 | Game, Shopping |
+-----------+------------------------------------+
It's mean Number 2 is Game and Number 3 is Shopping.
How can I show above data like this table
+-----------+-----------+
| 2 | Game |
+-----------+-----------+
| 3 | Shopping |
+-----------+------------
Your database is not normalized. You need a third table that will be what's usually called a join table.
The people table. The primary key is id
+-----------+-----------+
| Id | Name |
+-----------+-----------+
| 1 | Alex |
| 2 | Thor |
| 3 | Iron Man |
| 4 | Dr Stange |
| 5 | Thanos |
+-----------+------------
The hobbies Table
+-----------+-----------+
| Id | Name |
+-----------+-----------+
| 1 | Game |
| 2 | Shopping |
| 3 | Fighting |
+-----------+-----------+
Join table called (for example) people_hobbies
+-----------+-----------+
| person_id | hobby_id |
+-----------+-----------+
| 1 | 1 |
| 1 | 2 |
+-----------+-----------+
This people_hobbies table will use person_id and hobby_id to create a multi field primary key. This will ensure that you will not be able to add the same combination twice... which should not even make sense.
person_id is a foreign key that references the id from the people table.
hobby_id is a foreign key that references the id from the hobbies table.
Having foreign keys will let you avoid having a key in the people_hobbies table that do not exist in both the people and the hobbies table.
The example in the table below shows that the person id 1 has two hobbies (1 and 2). For a human, that translates to Alex's hobbies are Game and Shopping.
The above structure will let you manage your DB the way most people do.
Just keep a few things in mind:
You cannot add anything in people_hobbies before they exist in both people and hobbies tables
You must have the CASCADE UPDATE and CASCADE DELETE to the foreign key definitions so that when you delete a person or a hobby from your tables, it will remove the relationship from the people_hobbies table.
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT * FROM bad_schema;
+------+--------+----------------+
| name | number | hobby |
+------+--------+----------------+
| Alex | 2, 3 | Game, Shopping |
+------+--------+----------------+
CREATE TABLE better_schema AS
SELECT DISTINCT name
, SUBSTRING_INDEX(SUBSTRING_INDEX(number,',',i+1),',',-1) + 0 number
, TRIM(SUBSTRING_INDEX(SUBSTRING_INDEX(hobby,',',i+1),',',-1)) hobby
FROM bad_schema
, ints;
SELECT * FROM better_schema;
+------+--------+----------+
| name | number | hobby |
+------+--------+----------+
| Alex | 2 | Game |
| Alex | 3 | Shopping |
+------+--------+----------+

Updating table with unique column

A table contains the following data, is using INNODB, has a UNIQUE constraint on position/fk, and doesn't allow NULL for position.
+----+----------+-----+
| id | position | fk |
+----+----------+-----+
| 1 | 1 | 123 |
| 2 | 2 | 123 |
| 3 | 3 | 123 |
| 4 | 4 | 123 |
| 5 | 5 | 123 |
| 6 | 6 | 123 |
| 7 | 7 | 123 |
| 8 | 8 | 123 |
| 9 | 9 | 123 |
| 10 | 10 | 123 |
+----+----------+-----+
PHP receives a request to update the table to the following. The format of the request can be provided how ever is most convenient such as [2,1,4,3,6,5,8,7,10,9] or [{"id":1, "position":2}, ... ], etc.
+----+----------+-----+
| id | position | fk |
+----+----------+-----+
| 1 | 2 | 123 |
| 2 | 1 | 123 |
| 3 | 4 | 123 |
| 4 | 3 | 123 |
| 5 | 6 | 123 |
| 6 | 5 | 123 |
| 7 | 8 | 123 |
| 8 | 7 | 123 |
| 9 | 10 | 123 |
| 10 | 9 | 123 |
+----+----------+-----+
I've confirmed that SET unique_checks=0; will not allow unique checks to be temporarily disabled, and don't wish to actually remove the unique index, update the table, and reapply the unique index.
How can this table be updated?
If there is no simple means to do so, I thought of a couple of options, but don't like them:
Allowing NULL in position. Is there a way to temporarily allow NULL similar to how SET FOREIGN_KEY_CHECKS=0; can disable foreign keys?
First delete all the records and then reinsert them. This might result in performance issues as there are indexes on the table which will need to be recreated.
All I can think is that you need to first change all the positions to some other values that aren't in the range of new position values you ultimately need to set, but are still unique within the rows.
An easy way to do this, assuming your position column is a signed integer, is to set all the positions to their opposite (negative) value. They'll remain unique, but they won't be in the set of the new values.
You can do this in a transaction along with your subsequent updates, so no other concurrent transaction will ever see the negative values.
BEGIN;
UPDATE MyTable SET position = -position;
UPDATE MyTable SET position = 2 WHERE id = 1;
...etc...
COMMIT;
This is a hack. The sign bit of the integer is being used for a purpose other than showing negative numbers.

Assigned multiple users to a 'task'

Okay so I'm creating a task manager for my company. A user can assign assign a task to multiple other users. So I've though of 2 ways of implementing this.
This is my tasks table for option one (at least the columns that are important in this discussion ):
----------------------------------------------
| id | assigned_to | assigned_from |
---------------------------------------------
| 1 | 1,3,6 | 4 |
--------------------------------------------
| 2 | 1,4 | 2 |
---------------------------------------------
So here I pretty much just comma separate each user_id that is assigned to this particular task
Option 2:
----------------------------------------------------------
| id | task_id | assigned_to | assigned_from |
------------------------------------------------------------
| 1 | 335901 | 1 | 4 |
-----------------------------------------------------------
| 2 | 335901 | 3 | 4 |
-----------------------------------------------------------
| 3 | 335901 | 6 | 4 |
-----------------------------------------------------------
| 4 | 564520 | 1 | 2 |
-----------------------------------------------------------
| 4 | 564520 | 4 | 2 |
-----------------------------------------------------------
So as you can see here instead of putting the assiged_to is's here I just create a task id which is a random number and then I can groupBy 'task_id'. This is currently they way I have built it but for some reason it feels like it might screw me over in the future (not that option one doesn't give me the same feeling). So my question is which way do you guys recommend or is there maybe a different better way that I could be doing this?
Option 2 ist the better solution since you can acutally work with the table. You may e.g. create another table Tasks with
Task_id | Task_name | Budget | ...
Or a table with user-IDs for assigned_to and assigned_from. All these tables can be joined together if you use 2nd Option.
btw it is the correct normalization form
You can use Option 2 and normalize further if tasks are always assigned by/from the same person.
Tasks table:
task_id | assigned_from
1 | 4
2 | 2
The Assignees table then doesn't need to have the assigned_from since it's always the same for that task_id:
id | task_id | assigned_to
1 | 1 | 1
2 | 1 | 3
3 | 1 | 6
4 | 2 | 1
5 | 2 | 4

Junction tables in MySQL

(Sorry, my english isn't very good)
Hi, I am trying to learn how to work with junction tables in MySQL and I can't figure how to do something. I know the basics of MySQL but I have never worked with "JOIN".
In this test project, I would like to be able to show on a page the app of a given category (you click on "Games", only the apps that are in the "Games" category will be displayed on the page). I would like to know what the SQL request should look like.
Second question, let's say that an App could fit 2 different categories, how can I manage to give that app 2 different Category_ID in my database ?
Here is what my Database looks like at the moment :
Table name: APPS
+------------+-------------------+
| App_ID (pk)| App_Name |
+------------+-------------------+
| 1 | Weather Network |
| 2 | Is it sunny 2.0 |
| 3 | The Weather App |
| 4 | Zelda |
| 5 | Megaman |
| 6 | Doom 3 |
+------------+-------------------+
Table name : CATEGORY
+-----------------+-----------------+
| Category_ID (pk)| Category_Name |
+-----------------+-----------------+
| 1 | Games |
| 2 | Weather |
+-----------------+-----------------+
Table name : JUNCTION_APP_CATEGORY
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+
For your first question, the answer is
SELECT a.*, c.*
FROM APPS a, CATEGORY c, JUNCTION_APP_CATEGORY ac
WHERE a.App_ID=ac.APP_ID
AND c.Category_ID=ac.Category_ID
AND ac.Category_ID=<category_id for category "Games">
For your second question, you can use both APP_ID and Categor_ID as the primary key of table JUNCTION_APP_CATEGORY(note NOT TWO pks, but use the two columns together as ONE pk). So that you can put data like this:
+----------------+--------------------+
| APP_ID (pk) | Category_ID (pk) |
+----------------+--------------------+
| 1 | 1 | <-- APP_ID=1 belongs to both cat 1 & 2
| 1 | 2 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 1 |
| 6 | 1 |
+----------------+--------------------+

Categories