How to prevent insert in distinct 2 columns combination in mysql [duplicate] - mysql

This question already has an answer here:
MySQL unique index by multiple fields
(1 answer)
Closed 6 years ago.
I have a mysql table that looks like this:
id | FirstName | lastName | age
--------------------------------
But when I inserted some data my table that looks like this.
id | FirstName | lastName | age
--------------------------------
01 | praneeth | madusanka| 22
02 | praneeth | praneeth | 23
03 | madusanka | praneeth | 25
04 | praneeth | madusanka| 33
05 | damith | asanka | 43
06 | damith | danushka | 22
07 | damith | asanka | 33
08 |asanka | damith | 44
But I wont to prevent id=04 data. Because id=01 and id=04 is same data. How I create insert query for preventing above data, for inserting my table.

Try to create composite key (FirstName + Lastname)
If a primary key consists of two or more columns it is called a
composite primary key.

Add a unique key with columns firstname and lastname. This would prevent duplicate combinations.

Try this:
ALTER TABLE table_name ADD UNIQUE unique_index(FirstName, lastName);

Related

Column with comma delimited list of primary keys of same table, how to manage such mysql table?

I have one Users table which has user_id as its primary key. For some functionality purpose (it typically makes group of users), it has a grouped_with field, which has list of user_ids, comma separated. These user_ids are primary keys of same table. I have kept it as a comma separated list. Are there any alternatives to this? Are any other tables needed to make relationship? If so, how?
Or in the same table, how it would be managed? Is there any solution to optimization? I'm using a relational database.
Thanks in advance.
I might be over-simplifying your requirement, but this is one solution (although a simple one):
t_users
user_id | other details
--------+--------------
78 |
90 |
18 |
22 |
45 |
t_groups
group_id | other details (there might be none)
---------+--------------
1 |
2 |
3 |
t_group_users (shows which users belong to each group)
group_id | user_id
------------------
1 | 18
1 | 22
1 | 78
1 | 90
(after user_id 45 joins the group)
t_group_users
group_id | user_id
------------------
1 | 18
1 | 22
1 | 78
1 | 90
1 | 45

Update table data using foreign key

I have two tables structure like below
Table1
Serial | Src | Albumid(primarykey)
________|__________________|________
1 | /root/wewe.jpg | 20
2 | /root/wewe.jpg | 21
3 | /root/wewe.jpg | 21
4 | /root/wewe.jpg | 23
5 | /root/wewe.jpg | 18
Table2
Albumid | Albumname | AlbumCover //albumid is secondary key ref. to first table
________|__________________|________
20 | AAA | null
21 | bbb | null
31 | vcc | null
42 | ddd | null
18 | eee | null
I followed this POST two update my Albumcover in Table2 using Serial no. of first table..
create proc AddCover #Serial int
as
Begin
update Table1 set albumcover='somthing' where table1.serial = #Serial
end
Can i do like this using foregin key constraint??
You'll need to do the update on Table2. To tell it to have a condition based on values from table1, check this post for examples:
MySQL - UPDATE query based on SELECT Query

How to update one table in MySQL after insertion in another table

I have two table first is "employee_details" second is "attendance_rule"
employee_details
e_code | e_name | e_type
01 | Ram | Regular
02 | Shyam | Contract
03 | Mohan | Regular
attendance_rule
e_code | e_type | casual_leaves
01 | Regular | 7
02 | Contract | 6
03 | Regular | 7
I have manually insert values in "attendance_rule".
Now the problem is that when the attendance rule changes in the organisation. The user has to manually update the casual_leave coloumn for all the employees.
What I want is that when an employee details are added in employee_details table , the entry in the attendance_rule table should be automatically made having e_code and e_type same as e_code and e_type of employee_details table and casual_leaves equals to the casual_leaves of the same type of employee.
I think that a trigger should be used here.
but I dont know how to use trigger for this condition i.e. for the entry of casual leaves.
Please help me...how should I do that? Is there a way to do it other that trigger?
It sounds like you don't need a separate entry in attendance_rule for every employee, just for every employee type, so you don't need a trigger at all.
Create employee_details like you suggested:
e_code | e_name | e_type
01 | Ram | Regular
02 | Shyam | Contract
03 | Mohan | Regular
But create attendance_rule with only the rules for each type:
e_type | casual_leaves
Regular | 7
Contract | 6
If you need to find the number of casual leaves for a particular employee, join the two tables:
select
e.e_code,
e.e_name,
e.e_type,
a.casual_leaves
from employee_details e
inner join attendance_rule a on e.e_type = a.e_type
to get
e_code | e_name | e_type | casual_leaves
01 | Ram | Regular | 7
02 | Shyam | Contract | 6
03 | Mohan | Regular | 7
And if you want to make it as easy to use as a table, you can create a view:
create view emp_all_details as
select
e.e_code,
e.e_name,
e.e_type,
a.casual_leaves
from employee_details e
inner join attendance_rule a on e.e_type = a.e_type
Now you can simply say: select * from emp_all_details to get:
e_code | e_name | e_type | casual_leaves
01 | Ram | Regular | 7
02 | Shyam | Contract | 6
03 | Mohan | Regular | 7

Query to retrieve values of similar primary keys

I have a table with two columns which looks like this:
+-----------+--------+
| Mobile_id | Person |
+-----------+--------+
| ME_02_05 | John |
| ME-02 05 | Barry1 |
| 02-05 | John |
| ME 03-02 | James |
+-----------+--------+
I want to retrieve all persons whose Mobile_id contains integers (02 05). It can be seen that there are different combinations of 02 05 in the table such as ME_02_05, ME-02 05 and 02-05.
Can anyone please tell me how to do this?
SELECT Person FROM Persons WHERE Mobile_Id LIKE "%02%05"
should work.
SELECT Person FROM t1 WHERE Mobile_id REGEXP '02[-_ ]05'
This selects each Person entry that has 02 followed by dash, underscore, or space followed by 05.

Erasing duplicate records from MySQL

Due to a bug in my javascript click handling, multiple Location objects are posted to a JSON array that is sent to the server. I think I know how to fix that bug, but I'd also like to implement a server side database duplicate erase function. However, I'm not sure how to write this query.
The only affected table is laid out as
+----+------------+--------+
| ID | locationID | linkID |
+----+------------+--------+
| 64 | 13 | 14 |
| 65 | 14 | 13 |
| 66 | 14 | 15 |
| 67 | 15 | 14 |
| 68 | 15 | 16 |
| 69 | 16 | 17 |
| 70 | 16 | 14 |
| 71 | 17 | 16 |
| 72 | 17 | 16 |
| 73 | 17 | 16 |
| 74 | 17 | 16 |
| 75 | 17 | 16 |
| 76 | 17 | 16 |
| 77 | 17 | 16 |
+----+------------+--------+
As you can see, I have multiple pairs of (17, 16), while 14 has two pairs of (14, 13) and (14, 15). How can I delete all but one record of any duplicate entries?
Don't implement post factum correction logic, put a unique index on the fields that need to be unique, that way the database will stop dupe inserts before it's too late.
If you're using MySQL 5.1 or higher you can remove dupes and create a unique index in 1 command:
ALTER IGNORE TABLE 'YOURTABLE'
ADD UNIQUE INDEX somefancynamefortheindex (locationID, linkID)
You can create a temporary table where you can store the distinct records and then truncate the original table and insert data from temp table.
CREATE TEMPORARY TABLE temp_table (locationId INT,linkID INT)
INSERT INTO temp_table (locationId,linkId) SELECT DISTINCT locationId,linkId FROM table1;
DELETE from table1;
INSERT INTO table1 (locationId,linkId) SELECT * FROM temp_table ;
delete from tbl
using tbl,tbl t2
where tbl.locationID=t2.locationID
and tbl.linkID=t2.linkID
and tbl.ID>t2.ID
I am assuming you don't mean for the clean up, but for the new check? Put a unique index on if possible, if you don't have control of the DB do an upsert and check for nulls instead of an insert.