How work UNIQUE + INSERT IGNORE? - mysql

I have this table :
CREATE TABLE `recent_adds` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`trackid` INT(11) UNSIGNED NOT NULL,
`user` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
And I'd like to insert data only when the pair trackd/user is not already inserted in the table.
I know there is a sort of UNIQUE + INSERT IGNORE for this kind of problems, but in fact I don't really understand how it works.
If I do this command :
ALTER TABLE recent_adds
ADD UNIQUE INDEX unique_name (`trackid`, `user`);
where I see that these fields are UNIQUE? On the mysql code I don't see it
Also, after this, can I remove the id field?

You need to add a unique index, then use insert ignore instead of a normal insert. As for the id field, that's up to you, but I would keep it:
ALTER TABLE recent_adds
ADD UNIQUE KEY recent_adds_unique_idx (trackid,user);
INSERT IGNORE INTO recent_adds (id,trackid,user)
VALUES(NULL,...,...);

Related

SQL/DDL - Needing a unique column from time to time

this is my current table
-- Table structure for table `test`
CREATE TABLE `test` (
`id` int(8) NOT NULL,
`emp_number` int(8) NOT NULL,
`some_value_1` varchar(255) NOT NULL,
`some_value_2` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- Indexes for table `test`
ALTER TABLE `test`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `emp_number` (`emp_number`);
Because of the unique column "emp_number", no rows with an existing value of emp_number cant be inserted.
Is there a way to ignore the unique column and insert the row if i really want to?
I wont like to change the tables structure (dynamically).
Why i defined the emp_number as unique
We got customers who would like to have only unique emp_numbers, but we got also customers now, who got the same emp_number for multiple employees. I do know that this is bull****. I dont want to alter the table of existing ones.
Thank you in advance, Louis.

How to use mysql join to deal with duplicate insertion when there is no primary key - MySQL

I have a table which looks like following
CREATE TABLE `groups` (
`id_rec` INT(11) NOT NULL DEFAULT '0',
`id_group` INT(11) NOT NULL DEFAULT '0',
UNIQUE INDEX `unikum` (`id_rec`, `id_group`),
INDEX `idxgroup` (`id_group`))
there is no primary key like id of the table on which i can use insert on duplicate key clause. Now i am trying to insert multiple rows with signle MySQL query in groups table, but i don't want insert duplicates. Now, the solution i came up with is creating and inserting in another temporary table and then use join on group table and temporary table in order to find duplicates or non-duplicates(new) records depending on the join that i should use.
The temporary table looks like following
CREATE TEMPORARY TABLE IF NOT EXISTS $tempTable
(id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
group_id INT(11) NOT NULL,
id_rec INT(11) NOT NULL)
Now at this point i am not sure which join should i use. Any help would be grealty appreciated.
You don't need a primary key for ON DUPLICATE KEY UPDATE to work. The unique index you have is perfectly fine.
You can alternatively use INSERT IGNORE:
INSERT IGNORE INTO `groups` VALUES (1, 1);
If the pair (1, 1) already exists in your table then the above statement will be simply ignored.
Demo here

Need clarification on mysql syntax

CREATE TABLE `table1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`)
)
-
CREATE TABLE `table2` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
`email` varchar(128) DEFAULT NULL UNIQUE,
)
Questions:
When created, is there any difference between table1 and table2?
(I assume no, but better to ask than live in blissful ignorance;)
if (yes) what is it?; if (no) why do we have different syntax?
in larger tables show create table tbl_name displays UNIQUE KEY email_2 (email) or some other _num. Why? What does tlb_name_num mean and what is it used for?
What syntax is preferable and why?
show create table tbnm always displays sql in table1 form, even if table is created by table2 sql syntax. Why?
UNIQUE KEY (email) also works. but it's transformed to UNIQUE KEY email (email) on show create table. Why does it work, why is it transformed, etc?
There will be no difference between the two tables.
there are different syntaxes to declare everything first and then add primary key, unique ... Or if some people like to do it immeadiately they also have the possibility
No idea, edit this if found
they both do the same thing, use what you prefer.
No idea, edit this if found
No idea, edit this if found

MySQL Difficult Question on auto_increment

I can't seem to figure out a MYSQL command or set of commands that will allow me to create an 'id' field, add auto_increment values, and then set it as the primary key.
The reason I want to do this is that I have an existing table with millions of rows and I want to assign a unique ID to each row which would become the primary key and have it auto_increment.
Is there a way to do this with SQL syntax or do I need to write a script?
Just figured it out in case anyone else needs this in the future:
alter table test add column id int(9) unsigned auto_increment PRIMARY KEY;
This should be relatively simple:
ALTER TABLE `myTable` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY
May be you can create a new table like this:
CREATE TABLE new_table (id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id), KEY(b))
ENGINE=InnoDB SELECT b,c FROM original;

I need to auto_increment a field in MySQL that is not primary key

Right now, I have a table whose primary key is an auto_increment field. However, I need to set the primary key as username, date (to ensure that there cannot be a duplicate username with a date).
I need the auto_increment field, however, in order to make changes to row information (adding and deleting).
What is normally done with this situation?
Thanks!
Just set a unique index on composite of (username, date).
ALTER TABLE `table` ADD UNIQUE INDEX `name` (`username`, `date`);
Alternatively, you can try to
ALTER TABLE `table` DROP PRIMARY KEY, ADD PRIMARY KEY(`username`,`date`);
and I think in the latter case you need those columns to be declared NOT NULL.
I know this is old question, here is how i solved the problem -
ALTER TABLE `student_info` ADD `sn` INT(3) UNIQUE NOT NULL AUTO_INCREMENT FIRST
Use something like:
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
user VARCHAR(32) NOT NULL,
thedate DATE NOT NULL,
UNIQUE(user,thedate)
);
If you already have the table, and just want to add a unique constraint on
user+thedate, run
ALTER TABLE users ADD UNIQUE KEY user_date_idx (user, thedate);
Change your current primary key to be a unique key instead:
ALTER TABLE table DROP PRIMARY KEY, ADD UNIQUE KEY(username,date);
The auto_increment will function normally after that without any problems. You should also place a unique key on the auto_increment field as well, to use for your row handling:
ALTER TABLE table ADD UNIQUE KEY(id);