I have one column name Token and I'm generating random numbers and saving them in token but sometimes it saves duplicate tokens so I want to make it unique.
I want to know will it affect existing records.
If you try to add a unique constraint (or primary key constraint) to a column that contains non-unique values, the alter statement will just fail. You need to first update the column so all values are unique (or remove duplicates), and then alter the table.
ALTER table Student add primary key (studentID)
Use the Alter command to edit table's DDL and then add a primary key to it by specifying the column.
If the primary key already exist, then first you will have to drop it before defining another PK by -
ALTER table STUDENT drop CONSTRAINT <constraint_name>
Try doing this
ALTER table_namePersons ADD UNIQUE (Token);
After doing this if you'll try to insert a duplicate key you will have an error and catching it you can generate another token
Related
We need to add a foreign key constraint on the basis of another column. As you can see in the screenshot below, we have columns refer_id and reference_type. We need to add a constraint on the column refer_id based on the value contained in column reference_type. We will always have two values of data type ENUM: Manual or User.
If reference_type has a value of Manual, we need to add a foreign key to the refer_id column on table reference_detail whose primary key column is id. If reference_type has a value of User, then we need to add a foreign key to the refer_id column on table users whose primary key is id.
Please help me how to add constraints on the basis of these conditions.
Please see table structure in below screenshot.
You can't add conditional foreign key(At least not in MySQL). A foreign key on the table can only reference a single primary column, and even if you can(in some RDBMS) it is considered a bad practice.
What you can do is that, you can create two child tables which references each parent table reference_detail(ENUM:Manual) and users(ENUM:Users).
It will be more clearer than your approach and easier to query. This is what I can make out of the database structure you have described.
Hope this helps.
I want to add complex unique key to existing table. Key contains from 4 fields (user_id, game_id, date, time).
But table have non unique rows.
I understand that I can remove all duplicate dates and after that add complex key.
Maybe exist another solution without searching all duplicate data. (like add unique ignore etc).
UPD
I searched, how can remove duplicate mysql rows - i think it's good solution.
Remove duplicates using only a MySQL query?
You can do as yAnTar advised
ALTER TABLE TABLE_NAME ADD Id INT AUTO_INCREMENT PRIMARY KEY
OR
You can add a constraint
ALTER TABLE TABLE_NAME ADD CONSTRAINT constr_ID UNIQUE (user_id, game_id, date, time)
But I think to not lose your existing data, you can add an indentity column and then make a composite key.
The proper syntax would be - ALTER TABLE Table_Name ADD UNIQUE (column_name)
Example
ALTER TABLE 0_value_addition_setup ADD UNIQUE (`value_code`)
I had to solve a similar problem. I inherited a large source table from MS Access with nearly 15000 records that did not have a primary key, which I had to normalize and make CakePHP compatible. One convention of CakePHP is that every table has a the primary key, that it is first column and that it is called 'id'. The following simple statement did the trick for me under MySQL 5.5:
ALTER TABLE `database_name`.`table_name`
ADD COLUMN `id` INT NOT NULL AUTO_INCREMENT FIRST,
ADD PRIMARY KEY (`id`);
This added a new column 'id' of type integer in front of the existing data ("FIRST" keyword). The AUTO_INCREMENT keyword increments the ids starting with 1. Now every dataset has a unique numerical id. (Without the AUTO_INCREMENT statement all rows are populated with id = 0).
Set Multiple Unique key into table
ALTER TABLE table_name
ADD CONSTRAINT UC_table_name UNIQUE (field1,field2);
I am providing my solution with the assumption on your business logic. Basically in my design I will allow the table to store only one record for a user-game combination. So I will add a composite key to the table.
PRIMARY KEY (`user_id`,`game_id`)
Either create an auto-increment id or a UNIQUE id and add it to the natural key you are talking about with the 4 fields. this will make every row in the table unique...
For MySQL:
ALTER TABLE MyTable ADD MyId INT AUTO_INCREMENT PRIMARY KEY;
If yourColumnName has some values doesn't unique, and now you wanna add an unique index for it. Try this:
CREATE UNIQUE INDEX [IDX_Name] ON yourTableName (yourColumnName) WHERE [id]>1963 --1963 is max(id)-1
Now, try to insert some values are exists for test.
I'd like to know how to make paired columns unique key?
I'm working on MySQL workbench, and right now I've set two columns (follow,following) to be UQ, I'm assuming this is Unique Key?
So when I tried to insert rows, I tried to insert
follow following
3 5
3 6
But follow is unique key. But i'd like to only do unique key for a pair only, not individual numbers.
Thanks
If i'm not mistaken you have only set a unique key for each column. Maybe you want compound column to be UNIQUE, try
ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (follow, following)
if you run the ALTER statement, the example data above is valid but if you try to insert another pair of 3, 5, it is guaranteed to fail.
If you created the table with follow as the PRIMARY key, then the RDBMS will enforce uniqueness based on that column alone.
As an alternate to JW's suggestion, you can change the PRIMARY KEY:
ALTER TABLE tableName DROP PRIMARY KEY;
ALTER TABLE tableName ADD CONSTRAINT tb_UQ PRIMARY KEY (follow, following);
Just wondering if this can be enforced by the database or not...
I have a table that has a foreign key, and another column that needs to be unique across rows where the foreign key is the same. Duplicate entries are allowed as long as the foreign keys are different.
Is there a way to do this? I can't seem to figure out a way to set a unique constraint that is based on some condition rather than applied to the entire table.
You can create UNIQUE constraint on both columns too.
ALTER TABLE myTableName
ADD CONSTRAINT tb_UQ UNIQUE (FKColumn, OtherColumn)
UPDATE 1
SQLFiddle Demo
You can add a unique constraint on two columns in MySQL:
alter table add unique index table(fk, othercolumn)
I have a column that's a foreign key. Adding rows to the column is fine as long as it the row I'm adding has data that is present in the parent table. Although, some rows don't have an entry that belongs in the parent table. I'd like to keep the column with a foreign key but even if it doesn't have a parent, the column should still store it.
Are there any ways to do this within MySQL?
Regards
A foreign key constraint enforces that the value exists in the referenced table. If you try to insert a value that doesn't exist in the referenced table then it will fail.
You have two options:
Store NULL instead of the ID that doesn't exist.
Don't use a foreign key constraint.