I'm using the following query to replace old link with a new one:
UPDATE my_table SET file = 'link' WHERE my_table.file ='old_link';
In my tests I can't duplicate that and I'm not sure what's wrong with that query, but apparently sometimes it leaves the old entry and inserts a new one instead of updating!
mysql ver:
5.6.12-56 Percona Server, table type: innodb
The query looks fine to me. UPDATE should never create new rows, only modify existing rows. The problem is probably in another part of the code.
Although an UPDATE won't fire an insert in its own, there could exist triggers in the database that would fire an INSERT whenever a record gets updated.
Here are some links that you should check:
CREATE TRIGGER Syntax
Trigger Syntax and Examples
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 it possible to use any sort of logic in MySQL without using any procedures? My web hosting does not let me create any procedures so I'm looking for a workaround.
The type of thing I want to do is only add an item to a table if it doesn't already exist. Or add a column to a table if it's not already there. There are some operations that can be done such as CREATE TABLE IF NOT EXISTS and so on, but some operations I require do not have such luxuries :(
I realised late on that my lovely procs won't work and so I tried writing IF/ELSE logic as top-level queries, but for MySQL, IF ELSE blocks only seem to work inside functions/procs and not at the global scope.
Any workarounds greatfully received - I've already asked the hosting to grant me privileges to create procedures but no reply as yet...
I suppose you don't have access to the INFORMATION_SCHEMA either. You can possibly find solutions but it would be better, in my oninion, to:
Change your hosting provider. Seriously. Pay more - if needed - for a MySQL instance that you can configure to your needs. You only have a crippled DBMS if you are not allowed to create procedures and functions.
Posible workarounds for the specific task: You want to add a column if it doesn't exist.
1) Just ALTER TABLE and add the column. If it already exists, you'll get an error. You can catch that error, in your application.
2) (If you have no access to the INFORMATION_SCHEMA) maintain a version of the schema, for your database.
The best solution that I can think of would be to use an additional language with SQL. For example, you can run a query for a specific record, and based on the response that you get, you can conditionally run an INSERT statement.
For inserting a table if it doesn't exist, try using the SHOW TABLES statement and testing whether or not a name exists in the result set.
MySQL supports INSERT IGNORE. and INSERT ... ON DUPLICATE KEY UPDATE.
The following will insert a new row, but only if there is no existing row with id=10. (This assumes that id is defined as a unique or primary key).
INSERT IGNORE INTO my_table (id, col1, col2) values (10, "abc", "def");
The following will insert a new row, but if there is an existing row with id=10 (again, assuming id is unique or primary), the existing row will be updated to hold the new values, instead of inserting a new row.
INSERT INTO my_table (id, col1, col2) values (10, "abc", "def")
ON DUPLICATE KEY UPDATE col1=VALUES(col1), col2=VALUES(col2)
Also, CREATE TABLE supports the IF NOT EXISTS modifier. So you can do something like:
CREATE TABLE IF NOT EXISTS my_table ...
There are many other similar options and modifiers available in MySQL. Check the docs for more.
Originally I created a big script to create or update the database schema, to make it easier to deploy database changes from my local machine to the server.
My script was doing a lot of "if table 'abc' exists and it doesn't have a FK constraint called 'blah'" then create an FK constraint called 'blah' on table 'abc'... and so on.
I now realise it's not actually necessary to check whether a table has a certain column or constraint etc, because I can just maintain a schema-versioning system, and query the DB schema-version when my app starts, or when I navigate to a certain page.
e.g. let's say I want to add a new column to a table. It works like this:
Add a new migration script to the app code, containing the SQL required to add the column to the existing table
Increment the app's schema-version by 1
On app startup, the app queries the DB for the DB's schema-version
If DB schema-version < app schema-version, execute the SQL migration scripts between the two schema-versions, and then update the DB schema-version to be the same as the app
e.g. if the DB's schema-version is 5 and the app version is 8, the app will apply migration scripts 5-6, 6-7 and 7-8 to the DB. These can just be run without having to check anything on the DB side.
The app is therefore solely responsible for updating the DB schema and there's no need for me to ever have to execute schema change scripts on the local or remote DB.
I think it's a better system than the one I was trying to implement for my question.
Is there any way to create triggers on different databases? my requirement is like:-
database: a1.db consist table: t1
database:a2.db consist table: t2
now i have to use trigger on t1 (whenever any delete and update operation) happens on t1 a value has to be inserted into t2.
waiting for your feedback...
I can only speak for MySQL, but you should be able to do something like:
CREATE TRIGGER ad_t1 AFTER DELETE ON `a1.db`.t1
FOR EACH ROW
INSERT INTO `a2.db`.t2 VALUES (...)
What are the other databases you are using besides mysql? If Oracle is one of them, then you can create dblinks from Oracle to the other databases, and your trigger (running on Oracle) could use those dblinks to update the tables in the other databases.
You can refer to this link for info on creating dblinks in Oracle:
http://download.oracle.com/docs/cd/B12037_01/server.101/b10759/statements_5005.htm
Also, see this link (How to create a DB link between two oracle instances) for another answer on stackoverflow.
Looks like you need the MySQL equivalent of link servers (MSSQL) or dblink (Oracle). There is something called the FEDERATE storage engine:
Check here
Can I use a trigger in MySQL to create a new database when a value is inserted into another database.
There is definitely a design problem in why you want to do this. (Create a database in trigger). As it turns out this is not allowed. This is a list of what all is allowed to be placed in a trigger.