How do I rename a primary key column in MySQL? - 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.

Related

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

Alter table does not add the foreign key restraint

I've exported a database from Access to a MySQL server (local)
While doing this it did not export the foreign keys I had assigned in the database with them. While inconvenient I thought it didn't matter since I could add them manually. However when I use the following command:
alter table betalingsstatus
add foreign key (bedrijf_id)
references bedrijven(bedrijf_id)
It says it successfully did add the constraint. However when I insert something into the table and I add a number that isn't in the table bedrijven it still inserts it. I had the same problem with other foreign keys I have in the database.
Put ENGINE=INNODB; after the creation - altering of those tables that you want to enforce the foreign key constraints.
Example:
CREATE TABLE mytbl
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(10) NOT NULL
) ENGINE=INNODB;

How to change the Primary Key of a table in 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

MySQL FK syntax: insert column called practice into table cred_insurances that is FK to table practices

I need to insert a column called "practice" into table "cred_insurances" that is a FK referencing table "practices" PK "id"
You will need to ensure that your MySQL table is using the InnoDB engine, by running the following from the mysql prompt.
show create table cred_insurances
the output will include (towards the bottom) the text ENGINE=.... If it is not InnodDB, then you will first need to convert it using the following SQL. You may need to do this to the parent table as well.
ALTER TABLE cred_insurances ENGINE=InnoDB
Then you can add a column and a foreign key constraint with the following command:
ALTER TABLE cred_insurances
ADD practice INT,
ADD CONSTRAINT fk_practice
FOREIGN KEY (practice) REFERENCES practices (ID)
If you are having difficulties with errors whilst adding a foreign key, try the following command to get more detailed information on the error.
SHOW ENGINE INNODB STATUS

#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)

So I am trying to add a primary key to one of the tables in my database. Right now it has a primary key like this:
PRIMARY KEY (user_id, round_number)
Where user_id is a foreign key.
I am trying to change it to this:
PRIMARY KEY (user_id, round_number, created_at)
I am doing this in phpmyadmin by clicking on the primary key icon in the table structure view.
This is the error I get:
#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)
It is a MySQL database with InnoDB table engine.
There is probably another table with a foreign key referencing the primary key you are trying to change.
To find out which table caused the error you can run SHOW ENGINE INNODB STATUS and then look at the LATEST FOREIGN KEY ERROR section.
As was said you need to remove the FKs before. On Mysql do it like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
For those who are getting to this question via google... this error can also happen if you try to rename a field that is acting as a foreign key.
To bypass this in PHPMyAdmin or with MySQL, first remove the foreign key constraint before renaming the attribute.
(For PHPMyAdmin users: To remove FK constrains in PHPMyAdmin, select the attribute then click "relation view" next to "print view" in the toolbar below the table structure)
If you are trying to delete a column which is a FOREIGN KEY, you must find the correct name which is not the column name. Eg: If I am trying to delete the server field in the Alarms table which is a foreign key to the servers table.
SHOW CREATE TABLE alarm;
Look for the CONSTRAINT `server_id_refs_id_34554433` FORIEGN KEY (`server_id`) REFERENCES `server` (`id`) line.
ALTER TABLE `alarm` DROP FOREIGN KEY `server_id_refs_id_34554433`;
ALTER TABLE `alarm` DROP `server_id`
This will delete the foreign key server from the Alarms table.
I had this problem, it is for foreign-key
Click on the Relation View (like the image below) then find name of the field you are going to remove it, and under the Foreign key constraint (INNODB) column, just put the select to nothing! Means no foreign-key
Hope that works!
If you are adding a foreign key and faced this error, it could be the value in the child table is not present in the parent table.
Let's say for the column to which the foreign key has to be added has all values set to 0 and the value is not available in the table you are referencing it.
You can set some value which is present in the parent table and then adding foreign key worked for me.