How can I ensure a read only user is created for every new database in SQL server 2005? - sql-server-2005

We create multiple databases in sql server 2005. I would like to make sure that every new database that is created has a specific read only account when it gets created. I know there is a way to write code to do this, but is there a way we can set up a database template of some sort so every time a new database is created the account is automatically added from SQL server side rather than the code side?

If you mean one read-only user account for all databases on a server, you can use this hack:
Create a new SQL Server login
Create a new Database user in model database with db_datareader privilegues.
All new databases use the model db as template, so the user will be available in all databases.
But keep the security issues in mind. ;-)

Related

If a SQL Server Database is read only can I grant a single user write access?

I have a SQL Server database that we are trying to make read-only for everyone except one user. I already went to properties>options>Database read-only = true and set the entire DB to read-only. Now we are finding that one program updates this DB and will not work correctly without being able to do so. Is there a way to leave the whole DB read-only and just grant write to this one single "user"? SQL server 2008 using SSMS 2017.
No, a database that is read-only cannot be written to by any user. You're best solution would be to give read only permissions to all users except this app.

Copying a small database in SQL server

I have a small SQL server 2014 database that simply has just 3 tables and some information in them for a school group project and am wondering what the easiest way would be to have a group member be able to access the database rather then manually copying down all the information from each row and column. We wouldnt be changing any information in the database. Thanks.
I would create a database role. After you create the role add each person that needs access to that role then grant that role read access to each table. This way if a person in that group no longer needs access you can just remove them from the role and vise versa.
Since this is a school project I am assuming that the SQL Server database is a local DB and not on a shared server. You could perform a backup and restore where you back up the database on your machine and send them the back up files and they can use them to perform a restore to create the database on wherever.

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.

Why change user type to SQL user with login is disabled in SSMS?

I backed up a database from a SQL Server 2008 and restored it to my local machine using SQL Server 2012, now I'm trying to login to the server with the copied database user account and I wasn't able to do so.
After googling the issue I found that I have to change the user type from SQL User Without Login to SQL User with Login but the drop-down list is disabled as you can see in the picture below, how can I fix this and is this is the best way of doing what I need to accomplish or do I need to add this user to the server level?
I remember running into this before when doing backup / restore across servers. Basically it comes down to how SQL Server works. There's SQL Server users & there's database users (SQL Server users who are database users are represented via mappings). They are however not the same thing.
A SQL Server user belongs to the SQL Server, a database user ONLY belongs to the associated database. What happens when you have a database user, but not a SQL Server user? You can't login to SQL Server non-obviously.
Thereby what I do is after moving the database, I add the user I need to login as to SQL Server users using SSMS, remove the old database user (it's got dependencies associated w it that prevent mapping to it) & lastly make a new user on the database by mapping my SQL user to the database w appropriate permissions.
This approach is by no means elegant, but it works 100% of the time w no code needed, & you should consider a more permanent system if you have automated backup / restores happening. For the one off, this is how I've always done it.

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