How to change the Primary Key of a table in MySQL? - mysql

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

Related

how to change primary key column to auto increment when its a foreign key of different table in mysql

Can not alter table because animal id is foreign key in different table
ALTER TABLE 'animal' CHANGE 'animal_id' 'animal_id' INT(11) NOT NULL AUTO_INCREMENT;
Because I can't see your full code, however, I think you can try this:
ALTER TABLE 'animal' MODIFY 'animal_id INT(11) AUTO_INCREMENT;
I think you could do something similar to this response: How to update primary key. When you update the PK, I think you could do something like this response: Alter a MySQL column to be AUTO_INCREMENT.
But since this response is not best practice, if you don't need the data in the table, you could drop and then recreate the table.

Can we make existing column as primary key?

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

Add another primary key to table in mysql

I want to add another primary key to a table in mysql.
I use phpmyadmin to communicate with mysql server.
When I click the primary icon for the desired field it gives me this error:
#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
Edited:
here's the query:
ALTER TABLE `files` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `file_type` )
How can I do it?
As the name "primary" key says, there may be only one of that (ref: Highlander).
What you might want to try is a UNIQUE KEY, that acts just like a primary for most purpouses. Auto_increment doesn't seem to fulfill any purpouse if used a second time - what'ts the point of two fields carrying exactly the same information?
I believe in your case, what you need is a composite key. I do not know your table structure, but here is a general example taken from here,
CREATE TABLE track(
album CHAR(10),
disk INTEGER,
posn INTEGER,
song VARCHAR(255),
PRIMARY KEY (album, disk, posn)
)
In this case, there is a combination of 3 columns which avoid the duplicate records as you require. Please let me know if I have any mistakes in understanding your scenario.
The error message says it, I think:
the auto_increment column must be key.
So use this query first:
ALTER TABLE 'files' CHANGE 'id' 'id' INT( 10 ) UNSIGNED NOT NULL;
this will remove the auto_increment.
Also, I recommend the Uniqe key as suggested by other answer. I believe there should always (almost) be an Id column in each table.
We can Give Primary Key only once for a table. You can prefer UNIQUE KEY to prevent duplicate records
ALTER TABLE Persons
ADD UNIQUE (P_Id)
You can mark all the fields you want as primary keys, including the existing one. The system internally will drop the existing one and will set all you marked.

How to drop a primary key? I get incorrect table definition

I am trying to run this query:
ALTER TABLE table DROP PRIMARY KEY, ADD PRIMARY KEY( `CUSTNO` , `DEPTNO` , `PRODNO` , `DT` );
I get
Incorrect table definition; there can be only one auto column and it must be defined as a key
You have to alter your pk column so that it hasn't auto_increment modifier anymore.
you'll have to do this in 3 (or 4) steps:
remove the "auto-incremet" attribute from your current primary key
drop your primary key
set the new primary key
(reset "auto_incremet" to your orl primary-key-column)
EDIT: maybe setting a new primary isn't what you realy want to do. please take a look at unique indexes - i think thats waht you want to set on your other columns to make sure they don't occur more than once.
First and foremost, if I'm correct you're defining a compound key? This is usually bad practice. It's better to have an extra ID column and to add a separate constraint to check you have a unique combination. And as codymanix suggested, you need to first alter the column to not have auto_increment anymore and then drop it.

How do I rename a primary key column in MySQL?

How do I rename a primary key column in MySQL?
it's no different than altering any other column --
ALTER TABLE `pkey` CHANGE `keyfield` `keyfield2` INT( 11 ) NOT NULL AUTO_INCREMENT
this changes the column keyfield in table pkey to be called keyfield2 -- you have to supply the definition afterwards, as usual.
Leave off the PRIMARY KEY part of the alter statement. The primary key will be updated automatically.
Maybe you have a foreign key constraint in place. You can disable those by SET foreign_key_constraints=0 but you have to remember to update the database afterwards.
Possible a bad practice work around. But you could export your entire db to an sql text file. Find and replace the PK you want to rename, and then restore the database over sql.
If you are working with InnoDB then I think you cannot rename primary keys, at least you can't if they are referenced by foreign keys. You need to dump the database, rename the columns and referencing keys in the dump file, then reload the database.
If others tables have a foreign key on your table, you cannot directly rename the column using alter table, it will throw the following error: [HY000][1025] Error on rename of xxx to yyy (errno: 150)
You must :
drop foreign keys from others tables pointing to the primary key you want to rename
rename the primary key
add the foreign column to other tables
When renaming a table in Intellij, it generates you the code do drop and add the foreign key.