Navicat rename field in model and sync to server without drop - mysql

I created a model from a database schema's tables. It's working correctly, but when I rename a column in a table and after that I sync to the server. Navicat recognize the changes and show the SQL queries which needed to be executed. The queries are the following:
disable foreign key checks
drop indexes
create column with new name
drop old column
recreate previously dropped indexes with the currently created column
enable foreign key checks
How can I force Navicat to not drop columns, just rename them?
Thanks in advance, kukko.

Have a look at object editors in dbForge Studio for MySQL. There is possibility to rename the field with dependent object modifications.
Here it is another simple way to rename the column:
select column in Database Explorer
press F2 (Rename command) and enter new name
choose Rename or Refactor (with dependent objects modifications)

By simply executing the statement to rename the column:
ALTER TABLE your_tablename CHANGE column_name new_name datatype other_options_like_nullable;
For example:
ALTER TABLE myTable CHANGE badname newname INT NOT NULL DEFAULT 0;

Related

Is there any way to import database table schema only

I have two databases. Now I'm trying to import all table schema only from first database to second database. I already exported all table structure from first database using phpmyadmin. But I can't import the table structures in the second database. phpmyadmin says
#1050 - Table 'XXXXXX' already exists
How can I import only the table structure correctly?
Note: Both databases had same table and all table had same structure. I have changed some table structures in the first database that I can't remember right now. Now I need to merge both table structure only. Also both database contains different data set and I can't afford any data loss from both databases.
Before executing any command I would recommend taking full database backup, otherwise you may lost a few days of sleep.
Using ALTER command
Use ALTER command to modify table structure. Here's sql that adds age not nullable age field to users table.
ALTER TABLE users ADD age int(11) not null
Re-creating table
I wouldn't recommend this method because you'll have data loss. Drop old table then create with new schema.
DROP TABLE mytable;
CREATE TABLE mytable (
...
);
Or if you want to keep data you can:
Duplicate or rename table to different name.
Create a new table with new schema.
Copy data from old table: INSERT INTO newtable SELECT * FROM oldtable
Renaming tables might cause relationship issues. I would recommend using ALTER command as much as possible. You can also take a look at scheme migration(aka: code first migration, database migration).
The main Issue is merging the tables. To identify the differences between the two tables I use a small software called SQL Power Architect.

How do I change the data type for all columns in MySQL?

I want to change the datatype for all columns in my table mysql.
For instance varchar to double.
alter table myTable alter column vColumn int;
This will work as long as:
-all of the data will fit inside an int
-all of the data can be converted to int (i.e. a value of "car" will fail)
-there are no indexes that include vColumn. If there are indexes, you will need to include a drop and create for them to get back to where you were.
Changing of column types is possible with SQL command ALTER TABLE MODIFY COLUMN (it does not work in every DBMS, however).
Usually you have to remove data anyway, so another option would be to DROP TABLE (=remove it entirely) and create anew with desired columns (with CREATE TABLE). You could also remove just the single column (ALTER TABLE DROP COLUMN) and add a new one (ALTER TABLE NEW COLUMN) with the new definition.
Of course changing a column is so simple only as long as this column is not used in any constraints or keys
For syntax of the above commands see MySQL docs

Insert into table with auto-increment field

I have two tables called HRData and HRDataHistory. HRDataHistory has the same structure as HRData except the first column is an autoincrement field and the last column is a DateTime field.
HRData has a trigger:
CREATE TRIGGER [HR].[HRData_History]
ON [HR].[HRData]
AFTER INSERT, UPDATE
AS
INSERT INTO HR.HRDataHistory
SELECT *, GETDATE()
FROM inserted
;
GO
This is working on an existing development machine. I am trying to mirror this relationship on my local sql server instance so that I can test some changes. Using SSMS I used 'Script Table as Create To...' and created the structure of each table and index on my local sql server instance. However when I do this for the trigger I get the following error:
An explicit value for the identity column in table 'HR.HRDataHistory' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I know the preferred method would be to specify the columns, but I want to mirror production which does not currently do that and further I want to understand why it is working in production but not on my test database.
You're getting this error because you're trying to insert data into an IDENTITY column, which auto-populates itself whenever you insert another row in that table.
Off the top of my head, you can do something like below (although I believe there are more elegant solutions and I do not guarantee that this is a safe solution, nor have I tried something like this and I recommend testing on a TEST database before trying in production/LIVE):
add another column to HRDataHistory table which does not have identity set on it (because you cannot remove identity form a colum once set), but must have the same datatype as the current ID (IDENTITY) column
use a UPDATE query to move all of your ID's from your IDENTITY column to your new column:
UPDATE HRDataHistory
SET new_column = ID
Drop the IDENTITY column (but this might have grave implications if you have any FK set on it and possibly other objects that use it):
ALTER TABLE HRDataHistory
DROP COLUMN ID
Rename the "new_column" to the name of your previous IDENTITY column:
EXEC sp_RENAME 'HRDataHistory.new_column' , 'ID', 'COLUMN'
At this point I believe you can use your trigger to "copy" the newly inserted data from the HRData table into the HRDataHistory, since the column names should match and there is no more conflict due to IDENTITY.
Again, this might (not guaranteed) work so I recommend you first check on a TEST environment.

How to get a column creation script workbench?

I upload my sql database on workbench to add properly a new relation between two tables. But I only need The ALTER TABLE .. ADD .. AFTER script not the CREATE TABLE because it's already created with inserted values.
So is there any solution to get the script only to create the column with its foreign keys ?
Thank you in advance.
Not sure when this function was introduced, but with workbench 5.2.33 you can obtaing the ALTER statment by selecting the table you want to alter, and right-click and select ALTER
After this you can define the Foreing Keys
And when you press "Apply", you can see and copy the script
Once you have the script, click "cancel" and don't apply the changes.

How can I rename all the indexes in MySQL

I have a MySQL database where the names of indexes are shared between tables. Which is fine in MySQL, because index names only have to be unique within a table, and not within a database. But I have to export this database to a system that requires index name be globally unique.
Is there some command or script I can run to assign unique names to the indexes? I don't care if they are randomly generated.
As of the latest version of MySQL, there is no way to rename indexes. Your only option would be to DROP the index then CREATE a new one or just CREATE a new one.
I think it's worth highlighting what #zerkms alluded to in a comment above - you can rename an index (since MySQL 5.7):
alter table mytable RENAME INDEX old_index_name TO new_index_name
There is no RENAME or ALTER index command on MySQL.
You need to either DROP the index or CREATE a new one.
MySQL supports no syntax in ALTER TABLE to rename an index (or key, which is a synonym).
You can ALTER TABLE DROP KEY and ALTER TABLE ADD KEY.
There is no ALTER INDEX command in MySQL. You can only DROP INDEX and then CREATE INDEX with the new name.