Is it necessary to create tables each time you connect the derby database? - sql

I created a schema and few tables manually using the Eclipse database development perspective. But whenever I try to access the table from code, I get "Schema does not exist" error. However, if I create the tables within my program just before using them, all go well. Do I have to create the tables each time I connect to database? Since, I am testing my code, I have to restart the project multiple times.

Three common reasons for "table does not exist" when you think you've already created the tables:
You are connecting to a different database than you think you were connecting to, and since you specified "create=true" on the Connection URL, Derby quietly created a new empty database for you.
You are using the "in-memory" configuration of Derby, which means that when the database is closed (or your application exits), all the contents of the database disappear.
You are connecting to the database as a different user, and you aren't issuing the SET SCHEMA statement, so you are using the default schema name, which is based on your user name, and so the two schemas are different and have completely different tables, so the table you created doesn't seem to exist when you use the other schema.

Related

New database in SQL Server is getting created with tables from another database on the same server

I have SQL Server installed on my machine with a few databases in it. Now I am creating a new database on the same server: Databases -> right click -> New database.
When I open the database it is getting created with tables from another database on the same server! Did any one see this behavior/problem before? Any possible solution?
It is completely normal. New databases are based on system model database which is template for them.
Model database
When a CREATE DATABASE statement is issued, the first part of the database is created by copying in the contents of the model database
Is used as the template for all databases created on the instance of SQL Server. Modifications made to the model database, such as database size, collation, recovery model, and other database options, are applied to any databases created afterward.
Read also about other system databases and their usage model/msdb/tempdb/model/resource
New databases are created as a copy of the model system database.
By default, this database is empty, but there is nothing stopping a user with the proper permissions from adding tables or data to the that database. Indeed, if you want to control how new databases are created such as default compatibility levels, default languages, default function, data types, etc. then you can do so by creating them in the model database.

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

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.

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.

How to copy tables from one db to another? (Sql azure)

Situation:
I have 2 azure database on the same server. All I need is to copy all tables(with data) from one db to another.
How can I make this with query help? I wasn't able to found any answer for it.
I don't think you can do it with query. Check out Import and Export Data program that follows sql management studio. I think it's called DTSWizard.exe. Anyway, it can copy stuff from pretty much any source.
You can use the CREATE DATABASE AS COPY OF command, like this:
CREATE DATABASE destination_database_name AS COPY OF
[source_server_name.]source_database_name
Same-Server Copying
When you copy a database to make a new database on the same SQL Database server, the same logins can be used on both databases. The security principal you use to copy the database becomes the database owner (DBO) on the new database when it is created. After the copy is complete, the destination database becomes a fully functional, independent database. The logins, users, and permissions of the destination can be managed independently of the source database.
Cross-Server Copying
You can also copy a database between two different SQL Database servers that are in the same sub-region or data center. Because the new database is created on a different SQL Database server, it is associated with a different master database. All users in the new database maintain the permissions that they had in the source database. The security principal you use to copy the database becomes DBO on the new database when it is created and is assigned a new security identifier (SID).
For more info, see this link: http://msdn.microsoft.com/en-us/library/ff951624.aspx

Creating temp table on logon in SQL Server

Is it possible to have SQL Server create a temp table inside a particular database upon a user connecting to the database in such a way that the connecting user is the only one with access to the contents in this table (or even better, the connecting user is the only one that can even see the table)?
I tried using a logon trigger (including using a 'with execute as caller' clause) but although this creates the temp table, the connecting user can never see it/select from it.
All of this has to run inside SQL Server and require no user interaction at all...
Basically, this is the scenario I want to support:
user connects
a temp table is created inside a particular DB inside SQL (by SQL, kicked off by establishing of the connection)
some specific information is populated inside the table
for the duration of the connection; the user has (Read) access to the contents in this table; the information in this table is used by a sub-system inside a particular database
user disconnects
the temp table and all its contents is dropped by SQL
Thanks
First thoughts:
modify your client code to create the table on connection? Then it can be done only when needed not all the time
use a common, persisted table with a SessionID based on a GUID? This will provide some audit + troubleshooting information too
use table value parameters to send data on demand rather than have any server-side caching
And what I'd probably do:
create the table when it's populated when I need it. The user can connect to the database for a variety of reasons (I assume). So "connection" should be decoupled from "CREATE TABLE".
Using temp tables for this would not be the right approach if your data access is properly designed to open a connection-do an operation/query-close the connection. The moment you closed the connection, the temp table would be destroyed. It would be better to use a view or stored procedure to filter the information to which the user should have access. The structure of that view will depend greatly on how users connect to the database. Do users connect to the database using their own personal windows authentication account or do they connect indirectly through another account like many web servers do?
IMO, the better approach is the second bullet point of gbn's answer: a common persisted table with an indicator as to the session or user.