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.
Related
I have to design a database in such a manner so that when ever i have to add or delete any column from the table i should not have to use alter command . Please guide me if it is possible to design a database like that
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;
While Altering a table in mysql to Add a new Column I am getting Fatal Error Occurred.. I ve seen the relevant answers for this question where I found an Answer like :--
Make a new table with the same structure.
Add the column to the new table.
Insert the data from the old table into the new table.
Rename the old table to old.bak
Rename the new table to the old table.
If all went well, delete the old.bak.
But my original table contains some triggers , indexes, etc.
My question is
"Can I write my Alter Script in any diff. way to overcome this fatal Error" ?
My concern is related to MYSQL, but any other RDBMS related answers also fine...
This is MySQL specific: You can use a combination of [SHOW CREATE TABLE tabname][1] and [SHOW TRIGGERS WHERE Table = 'tabname'][2] to regenerate the table and triggers. You probably don't want the triggers firing when you are copying the rows. Also, if the table is of a significant size or you have a high enough rate of change, you probably want to prevent writes to it during the copy.
Sequence of steps:
Prevent writes to table (optional)
Create new table with SHOW CREATE TABLE output.
Apply schema changes.
Copy data from old table to new table.
Apply triggers from SHOW TRIGGERS output.
Swap old and new tables.
Hope this helps.
Is there a way in SQL server to generate a script to re-create all the foreign key constraints and relationships? Basically I need to delete all relationships and then need to re-script them against the database.
Thanks in advance
For MS SQL Server here you have a full script with details and comments. And also you can use the SMSS to generate a create script of a table with all its necesary details. But this will do the work for only one table, if you need to do it for all tables and there are a lot, it could be a pain right there.
For MySQL, in case you needed it check this link
In SQL Server Management Studio, you can select see your tables. You can walk through 1 by 1. Press right click on the table -> Script Table As -> DROP And CREATE TO.
With this the Studio will generate the drop constraints and alter table add constraints in a new query window.
In mysql I can create a trigger, and then show information about it like this:
mysql> show triggers like 'fooTrigger';
This command gives output that looks an awful lot like a select statement, with a row showing the matching trigger. Is it possible to update a column on the row it shows me?
For example, one column is named Statement, and it defines what happens when the trigger is activated. Is it possible to change the Statement field for fooTrigger so the trigger does something different? Or do I need to drop and re-create the trigger?
As of MySQL 5.5, you must drop and re-create the trigger.
It is not possible to update the Statement column without dropping the trigger.
Documentation: CREATE TRIGGER DROP TRIGGER
You may also access information about triggers using the INFORMATION_SCHEMA tables:
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS
But, as these tables are actually views, you can't use UPDATE on them.
It's just a more convenient way to access and manipulate the information than using SHOW TRIGGERS.
Documentation: INFORMATION_SCHEMA.TRIGGERS
You may require, on Windows OS, Connector/Net Visual Studio Integration to view and edit existing database object. Limitation is that you can only edit trigger body within For each row ... loop.
Otherwise only option one has is drop and re create the trigger.
But make sure before dropping a trigger that the table associated with a trigger is locked and
and unlocked after trigger is re-created.
As #Jocelyn mentioned you can't alter the trigger. But if you're using MySql Workbench it will allow you to alter the trigger. Just right click on your table name and click Alter table option from there you can pick Trigger option and alter it. Although you cannot perform it from query.
Table Name --> Alter Table --> Triggers.