Difference between revisions of "MySQL"

From Useful Things
Jump to: navigation, search
(After update)
Line 18: Line 18:
 
= Triggers =
 
= Triggers =
  
== After update ==
+
== Before ==
 +
* OLD table name does not exist here
 
<syntaxhighlight lang="mysql">
 
<syntaxhighlight lang="mysql">
create trigger {trigger_name} after update on {table_name}
+
create trigger {trigger_name} before [update|insert|delete] on {table_name}
 +
for each row begin
 +
insert into {new_table} (col1, col2, col3) values (OLD.col1, OLD.col2, OLD.col3)
 +
end
 +
</syntaxhighlight>
 +
 
 +
== After ==
 +
* OLD and NEW table names can be used to access the old value and the new value respectively
 +
<syntaxhighlight lang="mysql">
 +
create trigger {trigger_name} after [update|insert|delete] on {table_name}
 
for each row begin
 
for each row begin
 
insert into {new_table} (col1, col2, col3) values (OLD.col1, NEW.col2, OLD.col3)
 
insert into {new_table} (col1, col2, col3) values (OLD.col1, NEW.col2, OLD.col3)
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:30, 26 June 2014

User management

Adding a user

grant all privileges on {database_name}.* to {database_user}@'%' identified by 'their_password';

Revoking privileges

revoke usage on {databse_name}.* from {database_user}@'%';

Removing a user

drop user {database_user}@'%';

Triggers

Before

  • OLD table name does not exist here
create trigger {trigger_name} before [update|insert|delete] on {table_name}
	for each row begin
		insert into {new_table} (col1, col2, col3) values (OLD.col1, OLD.col2, OLD.col3)
	end

After

  • OLD and NEW table names can be used to access the old value and the new value respectively
create trigger {trigger_name} after [update|insert|delete] on {table_name}
	for each row begin
		insert into {new_table} (col1, col2, col3) values (OLD.col1, NEW.col2, OLD.col3)
	end