I have a database with 3 columns:
id, name, somethingelse
This table has no index set and i am getting "No index defined!" in phpmyadmin
id is a 7digit alphanumeric value, unique to each row.
I want to set Drugid to be the primarykey/index (i dont know the difference if there is one)
Please explain in detail as i am new to this.
Thank you.
Either run in SQL:
ALTER TABLE tableName
ADD PRIMARY KEY (id) ---or Drugid, whichever you want it to be PK
or use the PHPMyAdmin interface (Table Structure)
ALTER TABLE your_table
ADD PRIMARY KEY (Drugid);
If you want to do it with phpmyadmin interface:
Select the table -> Go to structure tab -> On the row corresponding to the column you want, click on the icon with a key
alter table table_name
add constraint myprimarykey primary key(column);
reference :
http://www.w3schools.com/sql/sql_primarykey.asp
Go to localhost/phpmyadmin and press enter key. Now select:
database --> table_name --->Structure --->Action ---> Primary -->click on Primary
Related
I have assigned unique key in two field username and email. I have execute this query.
ALTER TABLE goipmonl_users DROP INDEX username;
DROP INDEX username ON goipmonl_users
It's show an error. So how can I remove unique key from selected field.
#1091 - Can't DROP 'username'; check that column/key exists.
I have username, email columns in my table.
Please find the screen shot to delete unique index from table using phpMyAdmin
1- Select the desired database,
2-then select the desired table,
3-click on the Structure tab, select the "Relation View" option at the top of the table,
4- and finally the Indexes option at the bottom of the page.
You can make use of the below command to find out the list of indexes of your table. From that, get the name of your unique index.
SHOW INDEX FROM tbl_name
Then use the below one to drop that index
ALTER TABLE tbl_name DROP INDEX unique_constraint_name;
ALTER TABLE [table name] DROP INDEX [unique key constraint name];
Please double check your unique key constraint name, use this command to check:
select distinct CONSTRAINT_NAME
from information_schema.TABLE_CONSTRAINTS
where table_name = [tablename] and constraint_type = 'UNIQUE';
Well you can simply do something like this:
```
ALTER TABLE goipmonl_users DROP INDEX goipmonl_users_username_unique;
```
That is you prefix the table name with an underscore followed by the table colunm name to which the constraint is on followed again by underscore finally the index/constraint name which is UNIQUE
hope it will helps someone else that might bump into this issue again
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 need to remove a unique key from my mysql table. How can remove that using mysql query.
I tried this but it is not working
alter table tbl_quiz_attempt_master drop unique key;
Please help me
Thanks
All keys are named, you should use something like this -
ALTER TABLE tbl_quiz_attempt_master
DROP INDEX index_name;
To drop primary key use this one -
ALTER TABLE tbl_quiz_attempt_master
DROP INDEX `PRIMARY`;
ALTER TABLE Syntax.
First you need to know the exact name of the INDEX (Unique key in this case) to delete or update it.
INDEX names are usually same as column names. In case of more than one INDEX applied on a column, MySQL automatically suffixes numbering to the column names to create unique INDEX names. For example if 2 indexes are applied on a column named customer_id
The first index will be named as customer_id itself.
The second index will be names as customer_id_2 and so on.
To know the name of the index you want to delete or update
SHOW INDEX FROM <table_name>
as suggested by #Amr
To delete an index
ALTER TABLE <table_name> DROP INDEX <index_name>;
ALTER TABLE mytable DROP INDEX key_Name;
Here is how to get index_name which is mentioned in Devart's answer or key_name which is mentioned in Uday Sawant's answer:
SHOW INDEX FROM table_name;
This will show all the indexes for the given table_name. And you can pick the name of the index or the unique key you want to remove.
There are two method two remove index in mysql.
First method is GUI. In this method you have to open GUI interface of MYSQL and then go to that database and then go to that particular table in which you want to remove index.
After that click on the structure option, Then you can see table structure and below you can see table indexes. You can remove indexes by clicking on drop option
Second method by
ALTER TABLE student_login_credentials DROP INDEX created_at;
here student_login_credentials is table name and created_at is column name
Unique key is actually an index.
http://codeghar.wordpress.com/2008/03/28/drop-unique-constraint-in-mysql/
To remove a unique key from a column, you have to run the below query:
ALTER TABLE your_table_name
DROP INDEX tableName_columnName_keyName;
Where tableName should be your name of the table followed by an underscore then columnName should be the name of the column which you want to remove from the unique key constraint followed by an underscore and at last keyName should be the name of the key i.e unique in your case.
To add a unique key use :
alter table your_table add UNIQUE(target_column_name);
To remove a unique key use:
alter table your_table drop INDEX target_column_name;
I don't want to remove the Primary Key column and it's data. I just want to change it from column A to column B. What is the command syntax for that?
ALTER TABLE Example DROP PRIMARY KEY;
ALTER TABLE Example ADD PRIMARY KEY ('columnB');
It depends on your SQL Server, the following URL has all the information you will need.
http://www.w3schools.com/sql/sql_primarykey.asp
How can I drop the "Unique Key Constraint" on a column of a MySQL table using phpMyAdmin?
A unique constraint is also an index.
First use SHOW INDEX FROM tbl_name to find out the name of the index. The name of the index is stored in the column called key_name in the results of that query.
Then you can use DROP INDEX:
DROP INDEX index_name ON tbl_name
or the ALTER TABLE syntax:
ALTER TABLE tbl_name DROP INDEX index_name
You can DROP a unique constraint from a table using phpMyAdmin as requested as shown in the table below. A unique constraint has been placed on the Wingspan field. The name of the constraint is the same as the field name, in this instance.
The indexes capable of placing a unique key constraint on a table are PRIMARY and UNIQUE indexes.
To remove the unique key constraint on a column but keep the index, you could remove and recreate the index with type INDEX.
Note that it is a good idea for all tables to have an index marked PRIMARY.
To add UNIQUE constraint using phpmyadmin, go to the structure of that table and find below and click that,
To remove the UNIQUE constraint, same way, go to the structure and scroll down till Indexes Tab and find below and click drop,
Hope this works.
Enjoy ;)
If you want to remove unique constraints from MySQL database table, use alter table with drop index.
Example:
CREATE TABLE unique_constraints (
unid INT,
activity_name VARCHAR(100),
CONSTRAINT activty_uqniue UNIQUE (activity_name),
PRIMARY KEY (unid)
);
ALTER TABLE unique_constraints
DROP INDEX activty_uqniue;
Where activty_uqniue is UNIQUE constraint for activity_name column.
For WAMP 3.0 :
Click Structure
Below Add 1 Column you will see '- Indexes'
Click -Indexes and drop whichever index you want.
The constraint could be removed with syntax:
ALTER TABLE
As of MySQL 8.0.19, ALTER TABLE permits more general (and SQL standard) syntax for dropping and altering existing constraints of any type, where the constraint type is determined from the constraint name: ALTER TABLE tbl_name DROP CONSTRAINT symbol;
Example:
CREATE TABLE tab(id INT, CONSTRAINT unq_tab_id UNIQUE(id));
-- checking constraint name if autogenerated
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'tab';
-- dropping constraint
ALTER TABLE tab DROP CONSTRAINT unq_tab_id;
db<>fiddle demo
This might help:
Inside your sql terminal
FIRST STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME}
SECOND STEP:
SHOW INDEX FROM {YOUR_TABLE_NAME} WHERE Column_name='ACTUAL_COLUMN_NAME_YOU_GOT_FROM_FIRST_STEP_OUTPUT'
THIRD STEP:
ORIGINAL_KEY_NAME_VALUE = SECOND_STEP_RESPONSE["Key_name"]
FOURTH STEP:
ALTER TABLE {YOUR_TABLE_NAME} DROP INDEX ${ORIGINAL_KEY_NAME_VALUE}
while dropping unique key we use index
ALTER TABLE tbl
DROP INDEX unique_address;
my table name is buyers which has a unique constraint column emp_id now iam going to drop the emp_id
step 1: exec sp_helpindex buyers, see the image file
step 2: copy the index address
step3: alter table buyers drop constraint [UQ__buyers__1299A860D9793F2E]
alter table buyers
drop column emp_id
note:
Blockquote
instead of buyers change it to your table name :)
Blockquote
thats all column name emp_id with constraints is dropped!