I'm am trying to use mysqldump to export a database which needs to be imported using a different database name. Looking at the SQL generated by mysqldump, it appears that triggers are the only object names which are fully-qualified with the source database name thus foiling my needs. Is there anyway to direct mysqldump to not fully-qualify any object names including triggers?
I had the same problem and I found the solution. I was using MySQL Workbench to design my database and I've created some triggers there. All of them used the syntax CREATE TRIGGER trigger_name except for one: CREATE TRIGGER dbname.trigger_name (it was just my mistake). Mysqldump output included all triggers in the same way: only one had database name.
Mysqldump uses your original CREATE TRIGGER instructions which you can see via SHOW CREATE TRIGGER. If you have a trigger defined with a database name, simply replace it (drop and create) with a one without dbname.
Most probably you add the database name when you create the trigger. Try updating your trigger without the database name in it.
Not an ideal solution but pumping the output through the following has gotten rid of the database name on the triggers for me.
mysqldump ... opts ... | sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/'
Related
Is it possible and what are best practices for generating schema-qualified object names in a Liquibase change log .sql file?
For example, I want the command liquibase generateChangeLog to generate a .sql file where the objects are schema qualified (CREATE TABLE [schema_name].[table_name]...). What I get is something like CREATE TABLE [table_name]... without the schema qualification.
I have tried the schemas and defaultSchema properties and their command-line equivalent w/no success. I have also tried setting the default schema for the liquibase login w/no success.
P.S. - this is for an MSSQL database.
Have you looked into using --includeSchema or --includeCatalog optional arguments with liquibaseGenerateChangeLog command?
Here's the documentation that mentions these optional arguments:
I'm a beginner in PostgreSQL. I wonder why the \l command in psql shows databases template0 and template1.
I searched the web but unfortunately didn't find the right resources. But I did find that after removing both (template0 & template1) we can't create new databases any more.
As the names indicate, those are template databases for creating new databases.
template1 is the one used by default. You can alter / add / remove objects there to affect every newly created DB. CREATE DATABASE basically makes a copy of it on the file level (very fast) to create a new instance.
template0 starts out being the same and should never be changed - to provide a virgin template with original settings.
Their role is described in detail in the chapter "Template Databases" in the manual.
But you can use any database of the same cluster as template with the TEMPLATE keyword - as long as there are no open connections to it. This is useful to populate new databases quickly. See:
How to clone a test database from a production one in one single action?
Shell script to execute pgsql commands in files
Truncating all tables in a Postgres database
I have made changes to my model.py in Django and now I want to syncronize these changes. It's fine to delete the tables and re-create them. However, nothing seems to work. I am using sqlite3:
syncdb: only works first time, not with changes
"python manage.py sql my_site", followed by syncdb: I thought this would 'redo' it all, but the table still only has the old columns (or so I assume as I get an error when I try to access the table using my model).
Then I figure that I can access the database directly and delete the tables that way. However, I don't know how to get "in" to the DB where I can execute commands. Typing sqlite3 from the command prompt is not recognized. I also tried "python manage.py sql my_site", but I again get the message that sqlite3 is not recognized.
Suggestions?
First you have to install the command line tool for sqlite. On Ubuntu/Debian, you can simply do
sudo apt-get install sqlite3
On windows, you can download it from here: http://www.sqlite.org/download.html. Look for the one that looks like sqlite-shell-win32-xxx.zip.
Use it like this:
> sqlite3 /path/to/your/database
;show some help
.help
; list all databases
.databases
; clear the contents of a table
DELETE FROM <tablename>;
See also the command line reference: http://www.sqlite.org/sqlite.html
and the sqlite SQL reference: http://www.sqlite.org/lang.html.
Using the "ALTER TABLE" sql command, you can also add columns without deleting the entire contents of the table. To do this, compare the output of .schema in sqlite3, and the output of manage.py sql my_site to find out which columns you need to add.
An example:
ALTER TABLE "buildreport_series" ADD COLUMN "parameters" text
Use Django's built in database management tool:
python manage.py dbshell
And issue the required sql commands. The sql command will only print to stdout what the required sql is to create the current tables (as defined by the current models).
Is it possible to programmatically find the database context from a tsql script? ie the context that changes when you add a USE . I ask because I am not using a USE, and would like to find the database name the script is running on.
select db_name()
I have approximately 100 SQL views that are a variation of this:
select * from RTC.dbo.MyTable
...now I find I need to change the name of the RTC table to something else. Rather than edit one view at a time, is there a way to script out all their drop/create statements to a text file so that I can do a global replacement?
In SSMS right click the database, go to Tasks and select there 'Generate Scripts...'. Select 'Views', select the views you want exported, export.
I'd use PowerShell. If you're not using SQL 2008 Client Tools, install them. Then get the PowerShell client, add the registered snapins (plenty of information out there on how to do that), and then use the directory structure to get to the folder representing your Views.
Then script them using something like:
Get-ChildItems | % {$_.Script()}
Use ScriptOptions to tell it to use an Alter script.
And replace "RTC." with the new database name... and run them using sqlcmd.
PowerShell actually becomes a really nice deployment option too.