Which DB to connect to for higher level application managing other database - sql

I have an application that will be creating and dropping postgres databases. The application itself has its own sql server database. Kind of a bizarre architecture but it's not by choice.
I'm a little confused on how I should connect to the postgres server to execute these create table and drop table commands. Normally in an app.config or web.config, the connection string would specify the database. In this case, I just want to specify the server.
Can queries be run directly to a postgres server, without a particular database?
Should I use the postgres database that was created by the server? I tried this... select * from pg_database and then drop database DBNAME with a result from the first query, and it gave an error saying the database does not exist.
Or I could create an empty database to connect to and submit the queries to it, despite it not being used for anything.

Can queries be run directly to a postgres server, without a particular database?
No. PostgreSQL requires that you connect to a specific database.
It's possible that restriction could actually be relaxed eventually, so you could do things that only work on the shared catalogs from a connection to no particular database. It'd require changes to how authentication works and all sorts of things, though, and I don't think having an "admin database" like the usually-empty postgres database is really a problem.
Should I use the postgres database that was created by the server?
Generally, yes. It's possible to DROP the postgres database, but you should usually just leave it alone and use it as an admin database.

You could connect to the postgres database and then run drop database <DBNAME> from there, yes. Another option would be, say, template1. (I would avoid template0 since that's essentially the root template from which template1 was created, and you could always recreate template1 quickly from template0 if something happened to it, assuming you haven't modified template1 but not template0.)
I usually connect to postgres, myself, for server-level commands.
I ran DROP DATABASE droptest; via psql after creating an empty database and seeing it returned from a pg_database query, so that definitely works in general.
Perhaps it was somehow deleted via some other process in the interim between when you queried things and when you did the DROP....
Another option would be to shell out to the command line tool dropdb instead. This is a wrapper around drop database and is what I generally use both for manual and automated instances of database drops.

Related

Creating a Database in SQL+?

In SQL+, I first connect to the server I have been given;
CONNECT/
Connected.
However, when trying to create a Database I get the following:
CREATE DATABASE Project3;
CREATE DATABASE failed
database already mounted
I've also tried - STARTUP NOMOUNT but only states I have insufficient privileges.
Is there something i'm doing wrong here?
In case this is Oracle you are talking about (Oracles default CLI is called SQL*Plus), this means that a database has already been configured on the server. Oracle only uses a single database per server instance. Inside those databases are schema's, and that is where your database objects will be stored.
See the below quote from: http://docs.oracle.com/cd/B28359_01/server.111/b28301/install.htm#ADMQS002
After you create a database, either during installation or as a standalone
operation, you do not need to create another. Each Oracle instance works
with a single database only. Rather than requiring that you to create
multiple databases to accommodate different applications, Oracle Database
uses a single database, and accommodates multiple applications by enabling
you to separate data into different schemas within the single database.

connecting to remote oracle database in SQL

I need to do some data migration between two oracle databases that in different servers. I've thought of some ways to do it like writing a jdbc program but i think the best way is to do it in SQL itself. I can also copy the entire table over to the database I am migrating to but these tables are big and doesnt seem like a "elegant" solution.
Is it possible to open a connection to one DB in SQL developer then connect to the other one using SQL and writing update/insert functions on tables as if they were both in the same connection?
I have read some examples on creating linked tables but none seem to be oracle specific or tell me how to open the external connection by supplying it the server hostname/port/SID/user credentials.
thanks for the help!
If you create a Database Link, you can just select a from different database by querying TABLENAME#dblink.
You can create such a link using the CREATE DATABASE LINK statement.
It depends if its a one time thing or a normal process and if you need to do ETL (Extract, Transform and Load) or not, but ill help you out based on what you explained.
From what i can gather from your explanation, what you attempt to accomplish is to copy a couple of tables from one db to another, if they can reach one another then its really simple, you could just create a DBLINK (http://www.dba-oracle.com/t_how_create_database_link.htm) and then do a SELECT AS INSERT from either side using the DBLINK for one of the tables and the local table as the receiver or sender. Its pretty straight forward.
But if its a one time thing i would just move the table with expdp and impdp since that will be a lot faster and a lot less strain on the DB.
If its something you need to maintain and keep updated, why not just add the DBLINK and use that on both sides, this will be dependent on network performance though.
If this is a bit out of you depth or you cant create dblinks due to restrictions, SQL Developer has had a database copy option for a while and you can go as far a copying individual tables, but its very heavy on the system where its being run (http://deepak-sharma.net/2014/01/12/copy-database-objects-between-two-databases-in-oracle-using-sql-developer/).

Ensure that a SQL query is READ-only

What would be the best way to ensure that a SQL query won't alter the data of a database?
In my scenario, you don't have access to the database layer and can only do this logic on the application layer.
Would you recommend using a gem, a ruby custom script?
You can manage the permissions of the users so that they have access for reading the database but they don't have access to alter the database (i.e. not able to insert, update and delete). If you are using mysql, for instance, you can easily do this in phpmyadmin or equivalent tool.
Update based on your change. Even if you only have access through the application you are still connected to the database as a user who has or does not have privileges to update, delete, insert or select and as such the only way to ensure no such queries are executed is to alter that user's permissions.
A simple but far from foolproof method is to employ a blacklist of words that cannot be in the query, such as insert, update, etc.
Alternatively, you could use a parser on the sql query that will provide you with the necessary information to derive whether or not to allow the query.
I would take option 1 only as a last resort or if your checking needs are relatively simple.
On the database layer, make sure that the user the Rails app is accessing the database as only has the access that you desire, perhaps only SELECT.
Sequel has support for read only slave databases with a writable master database. Read-only slaves handle SELECT queries, other queries are done by the master database.
Maybe you can just setup master database as nil?
Another approach could be using hooks (before_save) to prevent writing to the database.

Add Indexes to columns in remote table - Oracle

Am querying a remote database using DBLink. Now am wondering to speed up the query, how can i add indexes to few columns in the remote table.
Would appreciate if anyone can provide any recommendations around the same.
You could use DBMS_JOB or DBMS_SCHEDULER packages on the remote database to schedule a job, executing DDL.
But consider this, if Oracle throws an exception for DDL over databse links, there must be a good reason for it, right? You don't want anyone messing with your schema remotely over a database link. So instead, talk to the remote DBA and try to figure out solutions with him/her.
it can't be done over the dblink (even if your dblink is using the owning schema) you will see
ORA-02021: DDL operations are not allowed on a remote database
You could create a Materialized View in the remote database based in your query, add your prefered indexes to it, and then, if you need it, create a synonym for that materialized view.
John,
A good place to start would be the following Oracle documentation on "Tuning Distributed Queries".
http://download.oracle.com/docs/cd/B28359_01/server.111/b28310/ds_appdev004.htm
you could create the indexes in the remote database and build up your query in a view form (in the remote database of course).
that way the remote database will complete the query using all the methods he got (like indexes) and bring you back only the wanted resultes.

Clone entire database with a SP

I'm trying to find out if this is possible, but so far I haven't found out any good solutions. What I would like to achieve is write a stored procedure that can clone a database but without the stored data. That means all tables, views, constraints, keys and indexes should be included but without any data. Can it be done?
Sure - your stored proc would have to read the system catalog views to find out what objects are in the database, determine their potential dependencies, and then create a single or a collection of SQL scripts which re-create the database, and execute those.
It's possible - not very nice and easy to do. Especially the dependencies between objects might cause more headaches than first meets the eye....
You could also:
use something like SQL Server Management Studio (if you're on SQL Server - you didn't specify) and create the scripts manually, and just re-execute them on a separate server
use a "diff" tool like Redgate SQL Compare to compare two servers and have the second one brought up to date
I've successfully used the Microsoft SQL Server Database Publishing Wizard for this purpose. It's pretty straightforward, no coding needed. Here's a sample call:
sqlpubwiz script -d DatabaseName -S ServerName -schemaonly C:\Projects2\Junk\ DatabaseName.sql
I believe the default is to create both data and schema, but you can use the schemaonly parameter.
Download it here
In SQL Server you can roll through the system tables (sys.tables, sys.columns, etc.) and construct things one at a time. It's going to be very manual and error prone at the beginning, but it should become systematic pretty quickly.
Another way to do it is to write something in .Net using SMO. Check out this link:
http://www.sqlteam.com/article/scripting-database-objects-using-smo-updated